Updated Rector to commit 0d3e54357f

0d3e54357f extract VendorLocationDetector (#462)
This commit is contained in:
Tomas Votruba 2021-07-19 20:57:26 +00:00
parent ac469e5fc2
commit cad500c817
15 changed files with 132 additions and 63 deletions

21
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,21 @@
## How to Contribute
Contributions here are more than welcomed! You can contribute to [rector-src](https://github.com/rectorphp/rector-src) repository.
There 3 rules will highly increase changes to get your PR merged:
- **1 feature per pull-request**
- **new features need tests**
- CI must pass... you can mimic it locally by running
```bash
composer complete-check
```
- Do you need to fix coding standards?
```bash
composer fix-cs
```
We would be happy to accept PRs that follow these guidelines.

View File

@ -23,6 +23,14 @@ final class AddedFileWithContent implements \Rector\FileSystemRector\Contract\Ad
throw new \Rector\Core\Exception\ShouldNotHappenException('File path and content are the same, probably a bug');
}
}
public function getRealPath() : string
{
$realpath = \realpath($this->filePath);
if ($realpath === \false) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
return $realpath;
}
public function getFilePath() : string
{
return $this->filePath;

View File

@ -9,10 +9,9 @@ use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -30,10 +29,15 @@ final class UnSpreadOperatorRector extends \Rector\Core\Rector\AbstractRector
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(\Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector $spreadVariablesCollector, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver)
/**
* @var \Rector\CodingStyle\Reflection\VendorLocationDetector
*/
private $vendorLocationDetector;
public function __construct(\Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector $spreadVariablesCollector, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver, \Rector\CodingStyle\Reflection\VendorLocationDetector $vendorLocationDetector)
{
$this->spreadVariablesCollector = $spreadVariablesCollector;
$this->reflectionResolver = $reflectionResolver;
$this->vendorLocationDetector = $vendorLocationDetector;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
@ -102,7 +106,7 @@ CODE_SAMPLE
return null;
}
// skip those in vendor
if ($this->skipForVendor($methodReflection)) {
if ($this->vendorLocationDetector->detectFunctionLikeReflection($methodReflection)) {
return null;
}
$spreadParameterReflections = $this->spreadVariablesCollector->resolveFromMethodReflection($methodReflection);
@ -146,25 +150,6 @@ CODE_SAMPLE
}
return $variadicArgs;
}
/**
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $functionLikeReflection
*/
private function skipForVendor($functionLikeReflection) : bool
{
if ($functionLikeReflection instanceof \PHPStan\Reflection\Php\PhpFunctionReflection) {
$fileName = $functionLikeReflection->getFileName();
} elseif ($functionLikeReflection instanceof \PHPStan\Reflection\MethodReflection) {
$declaringClassReflection = $functionLikeReflection->getDeclaringClass();
$fileName = $declaringClassReflection->getFileName();
} else {
return \false;
}
// probably internal
if ($fileName === \false) {
return \true;
}
return \strpos($fileName, '/vendor/') !== \false;
}
private function removeLaterArguments(\PhpParser\Node\Expr\MethodCall $methodCall, int $argumentPosition) : void
{
$argCount = \count($methodCall->args);

View File

@ -0,0 +1,52 @@
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Reflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use PHPStan\Reflection\ReflectionWithFilename;
use RectorPrefix20210719\Symplify\SmartFileSystem\Normalizer\PathNormalizer;
final class VendorLocationDetector
{
/**
* @var \Symplify\SmartFileSystem\Normalizer\PathNormalizer
*/
private $pathNormalizer;
public function __construct(\RectorPrefix20210719\Symplify\SmartFileSystem\Normalizer\PathNormalizer $pathNormalizer)
{
$this->pathNormalizer = $pathNormalizer;
}
/**
* @param \PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
*/
public function detectFunctionLikeReflection($reflection) : bool
{
$fileName = $this->resolveReflectionFileName($reflection);
// probably internal
if ($fileName === \false) {
return \false;
}
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName);
return \strpos($normalizedFileName, '/vendor/') !== \false;
}
/**
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\FunctionReflection $reflection
* @return string|bool
*/
private function resolveReflectionFileName($reflection)
{
if ($reflection instanceof \PHPStan\Reflection\ReflectionWithFilename) {
return $reflection->getFileName();
}
if ($reflection instanceof \PHPStan\Reflection\Php\PhpFunctionReflection) {
return $reflection->getFileName();
}
if ($reflection instanceof \PHPStan\Reflection\MethodReflection) {
$declaringClassReflection = $reflection->getDeclaringClass();
return $declaringClassReflection->getFileName();
}
return \false;
}
}

View File

@ -7,6 +7,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class ParentClassMethodTypeOverrideGuard
@ -15,9 +16,14 @@ final class ParentClassMethodTypeOverrideGuard
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver)
/**
* @var \Rector\CodingStyle\Reflection\VendorLocationDetector
*/
private $vendorLocationDetector;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\CodingStyle\Reflection\VendorLocationDetector $vendorLocationDetector)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->vendorLocationDetector = $vendorLocationDetector;
}
public function hasParentMethodOutsideVendor(\PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
{
@ -37,12 +43,7 @@ final class ParentClassMethodTypeOverrideGuard
if (!$ancestorClassReflection->hasMethod($methodName)) {
continue;
}
$fileName = $ancestorClassReflection->getFileName();
// PHP internal class
if ($fileName === \false) {
continue;
}
if (\strpos($fileName, '/vendor/') !== \false) {
if ($this->vendorLocationDetector->detectFunctionLikeReflection($ancestorClassReflection)) {
return \true;
}
}

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -39,11 +40,16 @@ final class OptionalParametersAfterRequiredRector extends \Rector\Core\Rector\Ab
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(\Rector\Php80\NodeResolver\RequireOptionalParamResolver $requireOptionalParamResolver, \Rector\Php80\NodeResolver\ArgumentSorter $argumentSorter, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver)
/**
* @var \Rector\CodingStyle\Reflection\VendorLocationDetector
*/
private $vendorLocationDetector;
public function __construct(\Rector\Php80\NodeResolver\RequireOptionalParamResolver $requireOptionalParamResolver, \Rector\Php80\NodeResolver\ArgumentSorter $argumentSorter, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver, \Rector\CodingStyle\Reflection\VendorLocationDetector $vendorLocationDetector)
{
$this->requireOptionalParamResolver = $requireOptionalParamResolver;
$this->argumentSorter = $argumentSorter;
$this->reflectionResolver = $reflectionResolver;
$this->vendorLocationDetector = $vendorLocationDetector;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
@ -149,13 +155,7 @@ CODE_SAMPLE
*/
private function resolveExpectedArgParamOrderIfDifferent(\PHPStan\Reflection\MethodReflection $methodReflection, array $argsOrParams) : ?array
{
$classReflection = $methodReflection->getDeclaringClass();
$fileName = $classReflection->getFileName();
// probably internal class
if ($fileName === \false) {
return null;
}
if (\strpos($fileName, '/vendor/') !== \false) {
if ($this->vendorLocationDetector->detectFunctionLikeReflection($methodReflection)) {
return null;
}
$parametersAcceptor = \PHPStan\Reflection\ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '7f8881391936078680274762fbce286ed7a0f566';
public const PACKAGE_VERSION = '0d3e54357f39cef2be8581d78596be1a9e252313';
/**
* @var string
*/
public const RELEASE_DATE = '2021-07-19 11:16:02';
public const RELEASE_DATE = '2021-07-19 22:46:06';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210719\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a::getLoader();
return ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1::getLoader();

View File

@ -1747,6 +1747,7 @@ return array(
'Rector\\CodingStyle\\Rector\\Ternary\\TernaryConditionVariableAssignmentRector' => $baseDir . '/rules/CodingStyle/Rector/Ternary/TernaryConditionVariableAssignmentRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\RemoveUnusedAliasRector' => $baseDir . '/rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\SeparateMultiUseImportsRector' => $baseDir . '/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php',
'Rector\\CodingStyle\\Reflection\\VendorLocationDetector' => $baseDir . '/rules/CodingStyle/Reflection/VendorLocationDetector.php',
'Rector\\CodingStyle\\TypeAnalyzer\\IterableTypeAnalyzer' => $baseDir . '/rules/CodingStyle/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\CodingStyle\\ValueObject\\ConcatExpressionJoinData' => $baseDir . '/rules/CodingStyle/ValueObject/ConcatExpressionJoinData.php',
'Rector\\CodingStyle\\ValueObject\\ConcatStringAndPlaceholders' => $baseDir . '/rules/CodingStyle/ValueObject/ConcatStringAndPlaceholders.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a
class ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire4d0d8f6ad35cf522c9414ca49e3c876a($fileIdentifier, $file);
composerRequirec20212dfc6acb5c64740c4e317fc05f1($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire4d0d8f6ad35cf522c9414ca49e3c876a($fileIdentifier, $file)
function composerRequirec20212dfc6acb5c64740c4e317fc05f1($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a
class ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -2102,6 +2102,7 @@ class ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a
'Rector\\CodingStyle\\Rector\\Ternary\\TernaryConditionVariableAssignmentRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Ternary/TernaryConditionVariableAssignmentRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\RemoveUnusedAliasRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\SeparateMultiUseImportsRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php',
'Rector\\CodingStyle\\Reflection\\VendorLocationDetector' => __DIR__ . '/../..' . '/rules/CodingStyle/Reflection/VendorLocationDetector.php',
'Rector\\CodingStyle\\TypeAnalyzer\\IterableTypeAnalyzer' => __DIR__ . '/../..' . '/rules/CodingStyle/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\CodingStyle\\ValueObject\\ConcatExpressionJoinData' => __DIR__ . '/../..' . '/rules/CodingStyle/ValueObject/ConcatExpressionJoinData.php',
'Rector\\CodingStyle\\ValueObject\\ConcatStringAndPlaceholders' => __DIR__ . '/../..' . '/rules/CodingStyle/ValueObject/ConcatStringAndPlaceholders.php',
@ -3839,9 +3840,9 @@ class ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4d0d8f6ad35cf522c9414ca49e3c876a::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc20212dfc6acb5c64740c4e317fc05f1::$classMap;
}, null, ClassLoader::class);
}

View File

@ -1782,12 +1782,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/symfony\/console.git",
"reference": "a8106e7f386e11944242a624a7a6f6af0cc61e16"
"reference": "4f90ccd5ef32a0c1bd5f7b75cf27f94d12bfab04"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/symfony\/console\/zipball\/a8106e7f386e11944242a624a7a6f6af0cc61e16",
"reference": "a8106e7f386e11944242a624a7a6f6af0cc61e16",
"url": "https:\/\/api.github.com\/repos\/symfony\/console\/zipball\/4f90ccd5ef32a0c1bd5f7b75cf27f94d12bfab04",
"reference": "4f90ccd5ef32a0c1bd5f7b75cf27f94d12bfab04",
"shasum": ""
},
"require": {
@ -1825,7 +1825,7 @@
"symfony\/lock": "",
"symfony\/process": ""
},
"time": "2021-07-18T16:30:56+00:00",
"time": "2021-07-19T09:57:30+00:00",
"default-branch": true,
"type": "library",
"installation-source": "dist",

File diff suppressed because one or more lines are too long

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20210719\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a', false) && !interface_exists('ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a', false) && !trait_exists('ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a', false)) {
spl_autoload_call('RectorPrefix20210719\ComposerAutoloaderInit4d0d8f6ad35cf522c9414ca49e3c876a');
if (!class_exists('ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1', false) && !interface_exists('ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1', false) && !trait_exists('ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1', false)) {
spl_autoload_call('RectorPrefix20210719\ComposerAutoloaderInitc20212dfc6acb5c64740c4e317fc05f1');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210719\Doctrine\Inflector\Inflector');
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210719\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire4d0d8f6ad35cf522c9414ca49e3c876a')) {
function composerRequire4d0d8f6ad35cf522c9414ca49e3c876a() {
return \RectorPrefix20210719\composerRequire4d0d8f6ad35cf522c9414ca49e3c876a(...func_get_args());
if (!function_exists('composerRequirec20212dfc6acb5c64740c4e317fc05f1')) {
function composerRequirec20212dfc6acb5c64740c4e317fc05f1() {
return \RectorPrefix20210719\composerRequirec20212dfc6acb5c64740c4e317fc05f1(...func_get_args());
}
}
if (!function_exists('parseArgs')) {

View File

@ -844,7 +844,7 @@ class Application implements \RectorPrefix20210719\Symfony\Contracts\Service\Res
$helper->setInput($input);
}
}
if ($command instanceof \RectorPrefix20210719\Symfony\Component\Console\Command\SignalableCommandInterface) {
if ($command instanceof \RectorPrefix20210719\Symfony\Component\Console\Command\SignalableCommandInterface && ($this->signalsToDispatchEvent || $command->getSubscribedSignals())) {
if (!$this->signalRegistry) {
throw new \RectorPrefix20210719\Symfony\Component\Console\Exception\RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}