Updated Rector to commit 21c4d378a5a054aecff8d114d34a25771a519ebe

21c4d378a5 [PHP 8.0] Add way to adjust specifics attribute arguments (#2834)
This commit is contained in:
Tomas Votruba 2022-08-24 11:51:48 +00:00
parent acdbc7be1b
commit 12fa9feb17
12 changed files with 116 additions and 45 deletions

View File

@ -129,4 +129,9 @@ final class AttributeKey
* @var string
*/
public const COMMENT_CLOSURE_RETURN_MIRRORED = 'comment_closure_return_mirrored';
/**
* To pass PHP 8.0 attribute FQN names
* @var string
*/
public const PHP_ATTRIBUTE_NAME = 'php_attribute_name';
}

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Use_;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
@ -82,6 +83,8 @@ final class PhpAttributeGroupFactory
$args = $this->createArgsFromItems($values, $annotationToAttribute->getAttributeClass());
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
$attributeName = $this->attributeNameFactory->create($annotationToAttribute, $doctrineAnnotationTagValueNode, $uses);
// keep FQN in the attribute, so it can be easily detected later
$attributeName->setAttribute(AttributeKey::PHP_ATTRIBUTE_NAME, $annotationToAttribute->getAttributeClass());
$attribute = new Attribute($attributeName, $args);
return new AttributeGroup([$attribute]);
}

View File

@ -0,0 +1,34 @@
<?php
declare (strict_types=1);
namespace Rector\Php80\AttributeDecorator;
use PhpParser\Node\Attribute;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use Rector\Php80\Contract\AttributeDecoratorInterface;
/**
* The string is replaced by specific value
*/
final class JMSAccessTypeAttributeDecorator implements AttributeDecoratorInterface
{
public function getAttributeName() : string
{
return 'JMS\\Serializer\\Annotation\\AccessType';
}
public function decorate(Attribute $attribute) : void
{
$args = $attribute->args;
if (\count($args) !== 1) {
return;
}
$currentArg = $args[0];
if ($currentArg->name !== null) {
return;
}
if (!$currentArg->value instanceof String_) {
return;
}
$currentArg->name = new Identifier('type');
}
}

View File

@ -0,0 +1,20 @@
<?php
declare (strict_types=1);
namespace Rector\Php80\AttributeDecorator;
use PhpParser\Node\Attribute;
use Rector\Php80\Contract\AttributeDecoratorInterface;
final class SensioParamConverterAttributeDecorator implements AttributeDecoratorInterface
{
public function getAttributeName() : string
{
return 'Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter';
}
public function decorate(Attribute $attribute) : void
{
// make first named arg silent, @see https://github.com/rectorphp/rector/issues/7352
$firstArg = $attribute->args[0];
$firstArg->name = null;
}
}

View File

@ -0,0 +1,11 @@
<?php
declare (strict_types=1);
namespace Rector\Php80\Contract;
use PhpParser\Node\Attribute;
interface AttributeDecoratorInterface
{
public function getAttributeName() : string;
public function decorate(Attribute $attribute) : void;
}

View File

@ -3,46 +3,38 @@
declare (strict_types=1);
namespace Rector\Php80\NodeManipulator;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\Contract\AttributeDecoratorInterface;
final class AttributeGroupNamedArgumentManipulator
{
/**
* @param AttributeGroup[] $attributeGroups
* @return AttributeGroup[]
* @var AttributeDecoratorInterface[]
* @readonly
*/
public function processSpecialClassTypes(array $attributeGroups) : array
private $attributeDecorators;
/**
* @param AttributeDecoratorInterface[] $attributeDecorators
*/
public function __construct(array $attributeDecorators)
{
foreach ($attributeGroups as $attributeGroup) {
$attrs = $attributeGroup->attrs;
foreach ($attrs as $attr) {
$attrName = \ltrim($attr->name->toString(), '\\');
$this->processReplaceAttr($attr, $attrName);
}
}
return $attributeGroups;
$this->attributeDecorators = $attributeDecorators;
}
/**
* Special case for JMS Access type, where string is replaced by specific value
* @param AttributeGroup[] $attributeGroups
*/
private function processReplaceAttr(Attribute $attribute, string $attrName) : void
public function decorate(array $attributeGroups) : void
{
if (!\in_array($attrName, ['JMS\\Serializer\\Annotation\\AccessType', 'JMS\\AccessType'], \true)) {
return;
foreach ($attributeGroups as $attributeGroup) {
foreach ($attributeGroup->attrs as $attr) {
$phpAttributeName = $attr->name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
foreach ($this->attributeDecorators as $attributeDecorator) {
if ($attributeDecorator->getAttributeName() !== $phpAttributeName) {
continue;
}
$attributeDecorator->decorate($attr);
}
}
}
$args = $attribute->args;
if (\count($args) !== 1) {
return;
}
$currentArg = $args[0];
if ($currentArg->name !== null) {
return;
}
if (!$currentArg->value instanceof String_) {
return;
}
$currentArg->name = new Identifier('type');
}
}

View File

@ -130,7 +130,7 @@ CODE_SAMPLE
if ($attributeGroups === []) {
return null;
}
$attributeGroups = $this->attributeGroupNamedArgumentManipulator->processSpecialClassTypes($attributeGroups);
$this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
$node->attrGroups = \array_merge($node->attrGroups, $attributeGroups);
return $node;
}

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '041f41fd4e1d58ffb45d10e2348e62014ce9c0da';
public const PACKAGE_VERSION = '21c4d378a5a054aecff8d114d34a25771a519ebe';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-08-24 10:28:36';
public const RELEASE_DATE = '2022-08-24 11:47:29';
/**
* @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 ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38::getLoader();
return ComposerAutoloaderInit0bf5a8d2faf2a4360c9c5747c0a3dc46::getLoader();

View File

@ -2350,6 +2350,9 @@ return array(
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => $baseDir . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
'Rector\\Php74\\Tokenizer\\FollowedByCurlyBracketAnalyzer' => $baseDir . '/rules/Php74/Tokenizer/FollowedByCurlyBracketAnalyzer.php',
'Rector\\Php74\\TypeAnalyzer\\ObjectTypeAnalyzer' => $baseDir . '/rules/Php74/TypeAnalyzer/ObjectTypeAnalyzer.php',
'Rector\\Php80\\AttributeDecorator\\JMSAccessTypeAttributeDecorator' => $baseDir . '/rules/Php80/AttributeDecorator/JMSAccessTypeAttributeDecorator.php',
'Rector\\Php80\\AttributeDecorator\\SensioParamConverterAttributeDecorator' => $baseDir . '/rules/Php80/AttributeDecorator/SensioParamConverterAttributeDecorator.php',
'Rector\\Php80\\Contract\\AttributeDecoratorInterface' => $baseDir . '/rules/Php80/Contract/AttributeDecoratorInterface.php',
'Rector\\Php80\\Contract\\StrStartWithMatchAndRefactorInterface' => $baseDir . '/rules/Php80/Contract/StrStartWithMatchAndRefactorInterface.php',
'Rector\\Php80\\Contract\\ValueObject\\AnnotationToAttributeInterface' => $baseDir . '/rules/Php80/Contract/ValueObject/AnnotationToAttributeInterface.php',
'Rector\\Php80\\Enum\\MatchKind' => $baseDir . '/rules/Php80/Enum/MatchKind.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38
class ComposerAutoloaderInit0bf5a8d2faf2a4360c9c5747c0a3dc46
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit0bf5a8d2faf2a4360c9c5747c0a3dc46', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit0bf5a8d2faf2a4360c9c5747c0a3dc46', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire45f173ce5933808d50e80f4aa12b8e38($fileIdentifier, $file);
composerRequire0bf5a8d2faf2a4360c9c5747c0a3dc46($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit45f173ce5933808d50e80f4aa12b8e38
* @param string $file
* @return void
*/
function composerRequire45f173ce5933808d50e80f4aa12b8e38($fileIdentifier, $file)
function composerRequire0bf5a8d2faf2a4360c9c5747c0a3dc46($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 ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38
class ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2647,6 +2647,9 @@ class ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
'Rector\\Php74\\Tokenizer\\FollowedByCurlyBracketAnalyzer' => __DIR__ . '/../..' . '/rules/Php74/Tokenizer/FollowedByCurlyBracketAnalyzer.php',
'Rector\\Php74\\TypeAnalyzer\\ObjectTypeAnalyzer' => __DIR__ . '/../..' . '/rules/Php74/TypeAnalyzer/ObjectTypeAnalyzer.php',
'Rector\\Php80\\AttributeDecorator\\JMSAccessTypeAttributeDecorator' => __DIR__ . '/../..' . '/rules/Php80/AttributeDecorator/JMSAccessTypeAttributeDecorator.php',
'Rector\\Php80\\AttributeDecorator\\SensioParamConverterAttributeDecorator' => __DIR__ . '/../..' . '/rules/Php80/AttributeDecorator/SensioParamConverterAttributeDecorator.php',
'Rector\\Php80\\Contract\\AttributeDecoratorInterface' => __DIR__ . '/../..' . '/rules/Php80/Contract/AttributeDecoratorInterface.php',
'Rector\\Php80\\Contract\\StrStartWithMatchAndRefactorInterface' => __DIR__ . '/../..' . '/rules/Php80/Contract/StrStartWithMatchAndRefactorInterface.php',
'Rector\\Php80\\Contract\\ValueObject\\AnnotationToAttributeInterface' => __DIR__ . '/../..' . '/rules/Php80/Contract/ValueObject/AnnotationToAttributeInterface.php',
'Rector\\Php80\\Enum\\MatchKind' => __DIR__ . '/../..' . '/rules/Php80/Enum/MatchKind.php',
@ -3253,9 +3256,9 @@ class ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit45f173ce5933808d50e80f4aa12b8e38::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0bf5a8d2faf2a4360c9c5747c0a3dc46::$classMap;
}, null, ClassLoader::class);
}