Updated Rector to commit 3cf3e1fc25

3cf3e1fc25 [TypeDeclaration] Add ParamTypeByParentCallTypeRector (#519)
This commit is contained in:
Tomas Votruba 2021-07-28 19:43:49 +00:00
parent 8e55ca0a2c
commit 8b8c4dc016
11 changed files with 242 additions and 21 deletions

View File

@ -20,4 +20,5 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector::class);
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector::class);
// $services->set(AddParamTypeFromCallersRector::class);
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector::class);
};

View File

@ -13,7 +13,6 @@ use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\NodeManipulator\NullsafeManipulator;
use Rector\Core\Rector\AbstractRector;

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Rector\Transform\ValueObject\MethodCallToAnotherMethodCallWithArguments;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

View File

@ -0,0 +1,75 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\NodeAnalyzer;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\NodeNameResolver\NodeNameResolver;
final class ParentParamMatcher
{
/**
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @var \Rector\Core\PhpParser\AstResolver
*/
private $astResolver;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\AstResolver $astResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->astResolver = $astResolver;
}
public function matchParentParam(\PhpParser\Node\Expr\StaticCall $parentStaticCall, \PhpParser\Node\Param $param, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node\Param
{
$methodName = $this->nodeNameResolver->getName($parentStaticCall->name);
if ($methodName === null) {
return null;
}
// match current param to parent call position
$parentStaticCallArgPosition = $this->matchParentStaticCallArgPosition($parentStaticCall, $param);
if ($parentStaticCallArgPosition === null) {
return null;
}
return $this->resolveParentMethodParam($scope, $methodName, $parentStaticCallArgPosition);
}
/**
* @return int|null
*/
private function matchParentStaticCallArgPosition(\PhpParser\Node\Expr\StaticCall $parentStaticCall, \PhpParser\Node\Param $param)
{
$paramName = $this->nodeNameResolver->getName($param);
foreach ($parentStaticCall->args as $argPosition => $arg) {
if (!$arg->value instanceof \PhpParser\Node\Expr\Variable) {
continue;
}
if (!$this->nodeNameResolver->isName($arg->value, $paramName)) {
continue;
}
return $argPosition;
}
return null;
}
private function resolveParentMethodParam(\PHPStan\Analyser\Scope $scope, string $methodName, int $paramPosition) : ?\PhpParser\Node\Param
{
/** @var ClassReflection $classReflection */
$classReflection = $scope->getClassReflection();
foreach ($classReflection->getParents() as $parnetClassReflection) {
if (!$parnetClassReflection->hasMethod($methodName)) {
continue;
}
$parentClassMethod = $this->astResolver->resolveClassMethod($parnetClassReflection->getName(), $methodName);
if (!$parentClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
continue;
}
return $parentClassMethod->params[$paramPosition] ?? null;
}
return null;
}
}

View File

@ -0,0 +1,143 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector\ParamTypeByParentCallTypeRectorTest
*/
final class ParamTypeByParentCallTypeRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var \Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher
*/
private $parentParamMatcher;
public function __construct(\Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher $parentParamMatcher)
{
$this->parentParamMatcher = $parentParamMatcher;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change param type based on parent param type', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeControl
{
public function __construct(string $name)
{
}
}
class VideoControl extends SomeControl
{
public function __construct($name)
{
parent::__construct($name);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeControl
{
public function __construct(string $name)
{
}
}
class VideoControl extends SomeControl
{
public function __construct(string $name)
{
parent::__construct($name);
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($this->shouldSkip($node)) {
return null;
}
$parentStaticCall = $this->findParentStaticCall($node);
if (!$parentStaticCall instanceof \PhpParser\Node\Expr\StaticCall) {
return null;
}
/** @var Scope $scope */
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
$hasChanged = \false;
foreach ($node->params as $param) {
// already has type, skip
if ($param->type !== null) {
continue;
}
$parentParam = $this->parentParamMatcher->matchParentParam($parentStaticCall, $param, $scope);
if (!$parentParam instanceof \PhpParser\Node\Param) {
continue;
}
if ($parentParam->type === null) {
continue;
}
// mimic type
$paramType = $parentParam->type;
// original attributes have to removed to avoid tokens crashing from origin positions
$paramType->setAttributes([]);
$param->type = $paramType;
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
private function findParentStaticCall(\PhpParser\Node\Stmt\ClassMethod $classMethod) : ?\PhpParser\Node\Expr\StaticCall
{
$classMethodName = $this->getName($classMethod);
/** @var StaticCall[] $staticCalls */
$staticCalls = $this->betterNodeFinder->findInstanceOf($classMethod, \PhpParser\Node\Expr\StaticCall::class);
foreach ($staticCalls as $staticCall) {
if (!$this->isName($staticCall->class, 'parent')) {
continue;
}
if (!$this->isName($staticCall->name, $classMethodName)) {
continue;
}
return $staticCall;
}
return null;
}
private function shouldSkip(\PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
{
if ($classMethod->params === []) {
return \true;
}
$scope = $classMethod->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
if (!$scope instanceof \PHPStan\Analyser\Scope) {
return \true;
}
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
return \true;
}
return !$classReflection->isClass();
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'a467f764ec34703a3dab0bd861775f16656b8ac8';
public const PACKAGE_VERSION = '3cf3e1fc2559b5c1bd53b107aed3ed9f645edf09';
/**
* @var string
*/
public const RELEASE_DATE = '2021-07-28 18:42:54';
public const RELEASE_DATE = '2021-07-28 19:34:42';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210728\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 ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39::getLoader();
return ComposerAutoloaderInit74210497f277e2d89843d04f204b168f::getLoader();

View File

@ -3123,6 +3123,7 @@ return array(
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParentParamMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ParentParamMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
@ -3140,6 +3141,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39
class ComposerAutoloaderInit74210497f277e2d89843d04f204b168f
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit74210497f277e2d89843d04f204b168f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit74210497f277e2d89843d04f204b168f', '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\ComposerStaticInit5f5984635b44005fd2612b74457aaa39::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit74210497f277e2d89843d04f204b168f::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit5f5984635b44005fd2612b74457aaa39::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit74210497f277e2d89843d04f204b168f::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire5f5984635b44005fd2612b74457aaa39($fileIdentifier, $file);
composerRequire74210497f277e2d89843d04f204b168f($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire5f5984635b44005fd2612b74457aaa39($fileIdentifier, $file)
function composerRequire74210497f277e2d89843d04f204b168f($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit5f5984635b44005fd2612b74457aaa39
class ComposerStaticInit74210497f277e2d89843d04f204b168f
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3478,6 +3478,7 @@ class ComposerStaticInit5f5984635b44005fd2612b74457aaa39
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParentParamMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ParentParamMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
@ -3495,6 +3496,7 @@ class ComposerStaticInit5f5984635b44005fd2612b74457aaa39
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
@ -3849,9 +3851,9 @@ class ComposerStaticInit5f5984635b44005fd2612b74457aaa39
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit5f5984635b44005fd2612b74457aaa39::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5f5984635b44005fd2612b74457aaa39::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5f5984635b44005fd2612b74457aaa39::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit74210497f277e2d89843d04f204b168f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit74210497f277e2d89843d04f204b168f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit74210497f277e2d89843d04f204b168f::$classMap;
}, null, ClassLoader::class);
}

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('RectorPrefix20210728\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39', false) && !interface_exists('ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39', false) && !trait_exists('ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39', false)) {
spl_autoload_call('RectorPrefix20210728\ComposerAutoloaderInit5f5984635b44005fd2612b74457aaa39');
if (!class_exists('ComposerAutoloaderInit74210497f277e2d89843d04f204b168f', false) && !interface_exists('ComposerAutoloaderInit74210497f277e2d89843d04f204b168f', false) && !trait_exists('ComposerAutoloaderInit74210497f277e2d89843d04f204b168f', false)) {
spl_autoload_call('RectorPrefix20210728\ComposerAutoloaderInit74210497f277e2d89843d04f204b168f');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210728\Doctrine\Inflector\Inflector');
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210728\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire5f5984635b44005fd2612b74457aaa39')) {
function composerRequire5f5984635b44005fd2612b74457aaa39() {
return \RectorPrefix20210728\composerRequire5f5984635b44005fd2612b74457aaa39(...func_get_args());
if (!function_exists('composerRequire74210497f277e2d89843d04f204b168f')) {
function composerRequire74210497f277e2d89843d04f204b168f() {
return \RectorPrefix20210728\composerRequire74210497f277e2d89843d04f204b168f(...func_get_args());
}
}
if (!function_exists('parseArgs')) {