Updated Rector to commit 0df2351b89

0df2351b89 [PHP 8.0] Add property support to ConstantListClassToEnumRector (#2422)
This commit is contained in:
Tomas Votruba 2022-06-03 08:03:32 +00:00
parent 14da17860d
commit 3f0eebf57d
9 changed files with 113 additions and 40 deletions

View File

@ -6,12 +6,14 @@ namespace Rector\Php80\NodeAnalyzer;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Php80\ValueObject\ClassNameAndTagValueNode;
/**
* Detects enum-like params, e.g.
* Direction::*
@ -27,7 +29,7 @@ final class EnumParamAnalyzer
{
$this->reflectionProvider = $reflectionProvider;
}
public function matchParameterClassName(\PHPStan\Reflection\ParameterReflection $parameterReflection, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : ?string
public function matchParameterClassName(\PHPStan\Reflection\ParameterReflection $parameterReflection, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : ?\Rector\Php80\ValueObject\ClassNameAndTagValueNode
{
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($parameterReflection->getName());
if (!$paramTagValueNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode) {
@ -40,15 +42,31 @@ final class EnumParamAnalyzer
if (!$this->reflectionProvider->hasClass($className)) {
return null;
}
return $className;
return new \Rector\Php80\ValueObject\ClassNameAndTagValueNode($className, $paramTagValueNode);
}
public function matchReturnClassName(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : ?string
public function matchReturnClassName(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : ?\Rector\Php80\ValueObject\ClassNameAndTagValueNode
{
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
if (!$returnTagValueNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode) {
return null;
}
return $this->resolveClassFromConstType($returnTagValueNode->type);
$className = $this->resolveClassFromConstType($returnTagValueNode->type);
if (!\is_string($className)) {
return null;
}
return new \Rector\Php80\ValueObject\ClassNameAndTagValueNode($className, $returnTagValueNode);
}
public function matchPropertyClassName(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : ?\Rector\Php80\ValueObject\ClassNameAndTagValueNode
{
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (!$varTagValueNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode) {
return null;
}
$className = $this->resolveClassFromConstType($varTagValueNode->type);
if (!\is_string($className)) {
return null;
}
return new \Rector\Php80\ValueObject\ClassNameAndTagValueNode($className, $varTagValueNode);
}
private function resolveClassFromConstType(\PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode) : ?string
{

View File

@ -8,8 +8,7 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@ -18,6 +17,7 @@ use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Php80\NodeAnalyzer\EnumConstListClassDetector;
use Rector\Php80\NodeAnalyzer\EnumParamAnalyzer;
use Rector\Php80\ValueObject\ClassNameAndTagValueNode;
use Rector\Php81\NodeFactory\EnumFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -84,10 +84,10 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Class_::class, \PhpParser\Node\Stmt\ClassMethod::class];
return [\PhpParser\Node\Stmt\Class_::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Property::class];
}
/**
* @param Class_|ClassMethod $node
* @param Class_|ClassMethod|Property $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
@ -97,7 +97,10 @@ CODE_SAMPLE
}
return $this->enumFactory->createFromClass($node);
}
return $this->refactorClassMethod($node);
if ($node instanceof \PhpParser\Node\Stmt\ClassMethod) {
return $this->refactorClassMethod($node);
}
return $this->refactorProperty($node);
}
private function refactorClassMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod) : ?\PhpParser\Node\Stmt\ClassMethod
{
@ -136,8 +139,8 @@ CODE_SAMPLE
$hasNodeChanged = \false;
$parametersAcceptor = \PHPStan\Reflection\ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$enumLikeClass = $this->enumParamAnalyzer->matchParameterClassName($parameterReflection, $phpDocInfo);
if ($enumLikeClass === null) {
$classNameAndTagValueNode = $this->enumParamAnalyzer->matchParameterClassName($parameterReflection, $phpDocInfo);
if (!$classNameAndTagValueNode instanceof \Rector\Php80\ValueObject\ClassNameAndTagValueNode) {
continue;
}
$param = $this->getParamByName($classMethod, $parameterReflection->getName());
@ -145,24 +148,34 @@ CODE_SAMPLE
continue;
}
// change and remove
$param->type = new \PhpParser\Node\Name\FullyQualified($enumLikeClass);
$param->type = new \PhpParser\Node\Name\FullyQualified($classNameAndTagValueNode->getEnumClass());
$hasNodeChanged = \true;
/** @var ParamTagValueNode $paramTagValueNode */
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($parameterReflection->getName());
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $paramTagValueNode);
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $classNameAndTagValueNode->getTagValueNode());
}
return $hasNodeChanged;
}
private function refactorReturn(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo, \PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
{
$returnType = $this->enumParamAnalyzer->matchReturnClassName($phpDocInfo);
if ($returnType === null) {
$classNameAndTagValueNode = $this->enumParamAnalyzer->matchReturnClassName($phpDocInfo);
if (!$classNameAndTagValueNode instanceof \Rector\Php80\ValueObject\ClassNameAndTagValueNode) {
return \false;
}
$classMethod->returnType = new \PhpParser\Node\Name\FullyQualified($returnType);
/** @var ReturnTagValueNode $returnTagValueNode */
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $returnTagValueNode);
$classMethod->returnType = new \PhpParser\Node\Name\FullyQualified($classNameAndTagValueNode->getEnumClass());
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $classNameAndTagValueNode->getTagValueNode());
return \true;
}
private function refactorProperty(\PhpParser\Node\Stmt\Property $property) : ?\PhpParser\Node\Stmt\Property
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
if (!$phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo) {
return null;
}
$classNameAndTagValueNode = $this->enumParamAnalyzer->matchPropertyClassName($phpDocInfo);
if (!$classNameAndTagValueNode instanceof \Rector\Php80\ValueObject\ClassNameAndTagValueNode) {
return null;
}
$property->type = new \PhpParser\Node\Name\FullyQualified($classNameAndTagValueNode->getEnumClass());
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $classNameAndTagValueNode->getTagValueNode());
return $property;
}
}

View File

@ -0,0 +1,40 @@
<?php
declare (strict_types=1);
namespace Rector\Php80\ValueObject;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
final class ClassNameAndTagValueNode
{
/**
* @readonly
* @var string
*/
private $enumClass;
/**
* @readonly
* @var \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode
*/
private $tagValueNode;
/**
* @param \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode $tagValueNode
*/
public function __construct(string $enumClass, $tagValueNode)
{
$this->enumClass = $enumClass;
$this->tagValueNode = $tagValueNode;
}
public function getEnumClass() : string
{
return $this->enumClass;
}
/**
* @return \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode
*/
public function getTagValueNode()
{
return $this->tagValueNode;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '7722a57d8076577fcd7f8e2b60632fa0f5a38890';
public const PACKAGE_VERSION = '0df2351b89c65ee2ac61528a63624da15d210f06';
/**
* @var string
*/
public const RELEASE_DATE = '2022-06-03 09:15:40';
public const RELEASE_DATE = '2022-06-03 09:56:38';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b::getLoader();
return ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77::getLoader();

View File

@ -2619,6 +2619,7 @@ return array(
'Rector\\Php80\\ValueObjectFactory\\StrStartsWithFactory' => $baseDir . '/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php',
'Rector\\Php80\\ValueObject\\AnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/AnnotationToAttribute.php',
'Rector\\Php80\\ValueObject\\ArrayDimFetchAndConstFetch' => $baseDir . '/rules/Php80/ValueObject/ArrayDimFetchAndConstFetch.php',
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => $baseDir . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
'Rector\\Php80\\ValueObject\\CondAndExpr' => $baseDir . '/rules/Php80/ValueObject/CondAndExpr.php',
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
'Rector\\Php80\\ValueObject\\PropertyPromotionCandidate' => $baseDir . '/rules/Php80/ValueObject/PropertyPromotionCandidate.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b
class ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirec6018f13cbf5847f436ab1b49e57382b($fileIdentifier, $file);
composerRequirea6ff48621c9ea268c95f35fbe0101c77($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b
* @param string $file
* @return void
*/
function composerRequirec6018f13cbf5847f436ab1b49e57382b($fileIdentifier, $file)
function composerRequirea6ff48621c9ea268c95f35fbe0101c77($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b
class ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2946,6 +2946,7 @@ class ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b
'Rector\\Php80\\ValueObjectFactory\\StrStartsWithFactory' => __DIR__ . '/../..' . '/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php',
'Rector\\Php80\\ValueObject\\AnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/AnnotationToAttribute.php',
'Rector\\Php80\\ValueObject\\ArrayDimFetchAndConstFetch' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/ArrayDimFetchAndConstFetch.php',
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
'Rector\\Php80\\ValueObject\\CondAndExpr' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/CondAndExpr.php',
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
'Rector\\Php80\\ValueObject\\PropertyPromotionCandidate' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/PropertyPromotionCandidate.php',
@ -3791,9 +3792,9 @@ class ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc6018f13cbf5847f436ab1b49e57382b::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInita6ff48621c9ea268c95f35fbe0101c77::$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('RectorPrefix20220603\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b', false) && !interface_exists('ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b', false) && !trait_exists('ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b', false)) {
spl_autoload_call('RectorPrefix20220603\ComposerAutoloaderInitc6018f13cbf5847f436ab1b49e57382b');
if (!class_exists('ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77', false) && !interface_exists('ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77', false) && !trait_exists('ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77', false)) {
spl_autoload_call('RectorPrefix20220603\ComposerAutoloaderInita6ff48621c9ea268c95f35fbe0101c77');
}
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('RectorPrefix20220603\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220603\print_node(...func_get_args());
}
}
if (!function_exists('composerRequirec6018f13cbf5847f436ab1b49e57382b')) {
function composerRequirec6018f13cbf5847f436ab1b49e57382b() {
return \RectorPrefix20220603\composerRequirec6018f13cbf5847f436ab1b49e57382b(...func_get_args());
if (!function_exists('composerRequirea6ff48621c9ea268c95f35fbe0101c77')) {
function composerRequirea6ff48621c9ea268c95f35fbe0101c77() {
return \RectorPrefix20220603\composerRequirea6ff48621c9ea268c95f35fbe0101c77(...func_get_args());
}
}
if (!function_exists('scanPath')) {