Updated Rector to commit e6894a9d1b96dc52d2e2b6e7ab3972204320d3a4

e6894a9d1b [Generics] Add MakeClassMethodGenericRector (#948)
This commit is contained in:
Tomas Votruba 2021-10-02 14:06:29 +00:00
parent 28c6a546ff
commit e8f35ec2b6
8 changed files with 222 additions and 19 deletions

View File

@ -0,0 +1,152 @@
<?php
declare (strict_types=1);
namespace Rector\Generics\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Generics\ValueObject\GenericClassMethodParam;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20211002\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Generics\Rector\ClassMethod\GenericClassMethodParamRector\GenericClassMethodParamRectorTest
*/
final class GenericClassMethodParamRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string
*/
public const GENERIC_CLASS_METHOD_PARAMS = 'generic_class_method_params';
/**
* @var GenericClassMethodParam[]
*/
private $genericClassMethodParams = [];
/**
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
/**
* @var \Rector\Naming\Naming\PropertyNaming
*/
private $propertyNaming;
public function __construct(\Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\Naming\Naming\PropertyNaming $propertyNaming)
{
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->propertyNaming = $propertyNaming;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\ClassMethod::class];
}
/**
* @param ClassMethod $node
* @return \PhpParser\Node\Stmt\ClassMethod|null
*/
public function refactor(\PhpParser\Node $node)
{
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
if (!$scope instanceof \PHPStan\Analyser\Scope) {
return null;
}
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
return null;
}
foreach ($this->genericClassMethodParams as $genericClassMethodParam) {
if (!$classReflection->implementsInterface($genericClassMethodParam->getClassType())) {
continue;
}
if (!$this->isName($node, $genericClassMethodParam->getMethodName())) {
continue;
}
// we have a match :)
if (!$node->isPublic()) {
$this->visibilityManipulator->makePublic($node);
}
$this->refactorParam($node, $genericClassMethodParam);
}
return $node;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Make class methods generic based on implemented interface', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class SomeClass implements SomeInterface
{
private method getParams(SomeSpecificType $someParam)
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass implements SomeInterface
{
/**
* @param SomeSpecificType $someParam
*/
public method getParams(ParamInterface $someParam)
{
}
}
CODE_SAMPLE
, [self::GENERIC_CLASS_METHOD_PARAMS => [new \Rector\Generics\ValueObject\GenericClassMethodParam('SomeInterface', 'getParams', 0, 'ParamInterface')]])]);
}
/**
* @param array<string, GenericClassMethodParam[]> $configuration
*/
public function configure(array $configuration) : void
{
$makeClassMethodGenerics = $configuration[self::GENERIC_CLASS_METHOD_PARAMS] ?? [];
\RectorPrefix20211002\Webmozart\Assert\Assert::allIsAOf($makeClassMethodGenerics, \Rector\Generics\ValueObject\GenericClassMethodParam::class);
$this->genericClassMethodParams = $makeClassMethodGenerics;
}
private function refactorParam(\PhpParser\Node\Stmt\ClassMethod $classMethod, \Rector\Generics\ValueObject\GenericClassMethodParam $genericClassMethodParam) : void
{
$genericParam = $classMethod->params[$genericClassMethodParam->getParamPosition()] ?? null;
if (!$genericParam instanceof \PhpParser\Node\Param) {
$paramName = $this->propertyNaming->fqnToVariableName(new \PHPStan\Type\ObjectType($genericClassMethodParam->getParamGenericType()));
$param = new \PhpParser\Node\Param(new \PhpParser\Node\Expr\Variable($paramName));
$param->type = new \PhpParser\Node\Name\FullyQualified($genericClassMethodParam->getParamGenericType());
$classMethod->params[$genericClassMethodParam->getParamPosition()] = $param;
// 2. has a parameter?
} else {
$oldParamClassName = null;
// change type to generic
if ($genericParam->type !== null) {
$oldParamClassName = $this->getName($genericParam->type);
if ($oldParamClassName === $genericClassMethodParam->getParamGenericType()) {
// if the param type is correct, skip it
return;
}
}
// change type to generic
$genericParam->type = new \PhpParser\Node\Name\FullyQualified($genericClassMethodParam->getParamGenericType());
// update phpdoc
if ($oldParamClassName === null) {
return;
}
$this->refactorPhpDocInfo($classMethod, $genericParam, $oldParamClassName);
}
}
private function refactorPhpDocInfo(\PhpParser\Node\Stmt\ClassMethod $classMethod, \PhpParser\Node\Param $param, string $oldParamClassName) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
// add @param doc
$paramName = $this->getName($param);
$this->phpDocTypeChanger->changeParamType($phpDocInfo, new \PHPStan\Type\ObjectType($oldParamClassName), $param, $paramName);
}
}

View File

@ -0,0 +1,47 @@
<?php
declare (strict_types=1);
namespace Rector\Generics\ValueObject;
final class GenericClassMethodParam
{
/**
* @var string
*/
private $classType;
/**
* @var string
*/
private $methodName;
/**
* @var int
*/
private $paramPosition;
/**
* @var string
*/
private $paramGenericType;
public function __construct(string $classType, string $methodName, int $paramPosition, string $paramGenericType)
{
$this->classType = $classType;
$this->methodName = $methodName;
$this->paramPosition = $paramPosition;
$this->paramGenericType = $paramGenericType;
}
public function getClassType() : string
{
return $this->classType;
}
public function getMethodName() : string
{
return $this->methodName;
}
public function getParamPosition() : int
{
return $this->paramPosition;
}
public function getParamGenericType() : string
{
return $this->paramGenericType;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '20b5c94b22b20eda4f6adefd02ce2ff11738e0de';
public const PACKAGE_VERSION = 'e6894a9d1b96dc52d2e2b6e7ab3972204320d3a4';
/**
* @var string
*/
public const RELEASE_DATE = '2021-10-02 16:55:26';
public const RELEASE_DATE = '2021-10-02 13:53:47';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211002\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 ComposerAutoloaderInit2d624c5cf9ad5366d38c1a5834e53fe2::getLoader();
return ComposerAutoloaderInitcea735196a58412c721700164887f2c9::getLoader();

View File

@ -2247,6 +2247,8 @@ return array(
'Rector\\FileSystemRector\\ValueObjectFactory\\AddedFileWithNodesFactory' => $baseDir . '/packages/FileSystemRector/ValueObjectFactory/AddedFileWithNodesFactory.php',
'Rector\\FileSystemRector\\ValueObject\\AddedFileWithContent' => $baseDir . '/packages/FileSystemRector/ValueObject/AddedFileWithContent.php',
'Rector\\FileSystemRector\\ValueObject\\AddedFileWithNodes' => $baseDir . '/packages/FileSystemRector/ValueObject/AddedFileWithNodes.php',
'Rector\\Generics\\Rector\\ClassMethod\\GenericClassMethodParamRector' => $baseDir . '/rules/Generics/Rector/ClassMethod/GenericClassMethodParamRector.php',
'Rector\\Generics\\ValueObject\\GenericClassMethodParam' => $baseDir . '/rules/Generics/ValueObject/GenericClassMethodParam.php',
'Rector\\Laravel\\NodeFactory\\AppAssignFactory' => $vendorDir . '/rector/rector-laravel/src/NodeFactory/AppAssignFactory.php',
'Rector\\Laravel\\NodeFactory\\ModelFactoryNodeFactory' => $vendorDir . '/rector/rector-laravel/src/NodeFactory/ModelFactoryNodeFactory.php',
'Rector\\Laravel\\NodeFactory\\RouterRegisterNodeAnalyzer' => $vendorDir . '/rector/rector-laravel/src/NodeFactory/RouterRegisterNodeAnalyzer.php',

View File

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

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2
class ComposerStaticInitcea735196a58412c721700164887f2c9
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -2607,6 +2607,8 @@ class ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2
'Rector\\FileSystemRector\\ValueObjectFactory\\AddedFileWithNodesFactory' => __DIR__ . '/../..' . '/packages/FileSystemRector/ValueObjectFactory/AddedFileWithNodesFactory.php',
'Rector\\FileSystemRector\\ValueObject\\AddedFileWithContent' => __DIR__ . '/../..' . '/packages/FileSystemRector/ValueObject/AddedFileWithContent.php',
'Rector\\FileSystemRector\\ValueObject\\AddedFileWithNodes' => __DIR__ . '/../..' . '/packages/FileSystemRector/ValueObject/AddedFileWithNodes.php',
'Rector\\Generics\\Rector\\ClassMethod\\GenericClassMethodParamRector' => __DIR__ . '/../..' . '/rules/Generics/Rector/ClassMethod/GenericClassMethodParamRector.php',
'Rector\\Generics\\ValueObject\\GenericClassMethodParam' => __DIR__ . '/../..' . '/rules/Generics/ValueObject/GenericClassMethodParam.php',
'Rector\\Laravel\\NodeFactory\\AppAssignFactory' => __DIR__ . '/..' . '/rector/rector-laravel/src/NodeFactory/AppAssignFactory.php',
'Rector\\Laravel\\NodeFactory\\ModelFactoryNodeFactory' => __DIR__ . '/..' . '/rector/rector-laravel/src/NodeFactory/ModelFactoryNodeFactory.php',
'Rector\\Laravel\\NodeFactory\\RouterRegisterNodeAnalyzer' => __DIR__ . '/..' . '/rector/rector-laravel/src/NodeFactory/RouterRegisterNodeAnalyzer.php',
@ -3884,9 +3886,9 @@ class ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2d624c5cf9ad5366d38c1a5834e53fe2::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitcea735196a58412c721700164887f2c9::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitcea735196a58412c721700164887f2c9::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitcea735196a58412c721700164887f2c9::$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('RectorPrefix20211002\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit2d624c5cf9ad5366d38c1a5834e53fe2', false) && !interface_exists('ComposerAutoloaderInit2d624c5cf9ad5366d38c1a5834e53fe2', false) && !trait_exists('ComposerAutoloaderInit2d624c5cf9ad5366d38c1a5834e53fe2', false)) {
spl_autoload_call('RectorPrefix20211002\ComposerAutoloaderInit2d624c5cf9ad5366d38c1a5834e53fe2');
if (!class_exists('ComposerAutoloaderInitcea735196a58412c721700164887f2c9', false) && !interface_exists('ComposerAutoloaderInitcea735196a58412c721700164887f2c9', false) && !trait_exists('ComposerAutoloaderInitcea735196a58412c721700164887f2c9', false)) {
spl_autoload_call('RectorPrefix20211002\ComposerAutoloaderInitcea735196a58412c721700164887f2c9');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20211002\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -3306,9 +3306,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211002\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire2d624c5cf9ad5366d38c1a5834e53fe2')) {
function composerRequire2d624c5cf9ad5366d38c1a5834e53fe2() {
return \RectorPrefix20211002\composerRequire2d624c5cf9ad5366d38c1a5834e53fe2(...func_get_args());
if (!function_exists('composerRequirecea735196a58412c721700164887f2c9')) {
function composerRequirecea735196a58412c721700164887f2c9() {
return \RectorPrefix20211002\composerRequirecea735196a58412c721700164887f2c9(...func_get_args());
}
}
if (!function_exists('parseArgs')) {