Updated Rector to commit eb76f4562c

eb76f4562c [8.2] Add a RemoveAllowDynamicPropertyAttribute rule (#1392)
This commit is contained in:
Tomas Votruba 2021-12-10 01:09:37 +00:00
parent 8c4aedacef
commit facf40a900
8 changed files with 162 additions and 23 deletions

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
@ -14,19 +15,25 @@ use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpAttribute\Printer\PhpAttributeGroupFactory;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20211210\Webmozart\Assert\Assert;
/**
* @changelog https://wiki.php.net/rfc/deprecate_dynamic_properties
*
* @see \Rector\Tests\Transform\Rector\Class_\AddAllowDynamicPropertiesAttributeRector\AddAllowDynamicPropertiesAttributeRectorTest
*/
final class AddAllowDynamicPropertiesAttributeRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
final class AddAllowDynamicPropertiesAttributeRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface, \Rector\VersionBonding\Contract\MinPhpVersionInterface
{
/**
* @var string
*/
private const ATTRIBUTE = 'AllowDynamicProperties';
public const TRANSFORM_ON_NAMESPACES = 'transform_on_namespaces';
/**
* @var array<array-key, string>
*/
private $transformOnNamespaces = [];
/**
* @readonly
* @var \Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer
@ -56,18 +63,22 @@ final class AddAllowDynamicPropertiesAttributeRector extends \Rector\Core\Rector
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add the `AllowDynamicProperties` attribute to all classes', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add the `AllowDynamicProperties` attribute to all classes', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
namespace Example\Domain;
class SomeObject {
public string $someProperty = 'hello world';
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
namespace Example\Domain;
#[AllowDynamicProperties]
class SomeObject {
public string $someProperty = 'hello world';
}
CODE_SAMPLE
)]);
, [\Rector\Transform\Rector\Class_\AddAllowDynamicPropertiesAttributeRector::TRANSFORM_ON_NAMESPACES => ['Example\\*']])]);
}
/**
* @return array<class-string<Node>>
@ -76,6 +87,13 @@ CODE_SAMPLE
{
return [\PhpParser\Node\Stmt\Class_::class];
}
public function configure(array $configuration) : void
{
$transformOnNamespaces = $configuration[self::TRANSFORM_ON_NAMESPACES] ?? $configuration;
\RectorPrefix20211210\Webmozart\Assert\Assert::isArray($transformOnNamespaces);
\RectorPrefix20211210\Webmozart\Assert\Assert::allString($transformOnNamespaces);
$this->transformOnNamespaces = $transformOnNamespaces;
}
/**
* @param Class_ $node
*/
@ -117,6 +135,14 @@ CODE_SAMPLE
}
private function shouldSkip(\PhpParser\Node\Stmt\Class_ $class) : bool
{
if (\count($this->transformOnNamespaces) !== 0) {
$className = (string) $this->nodeNameResolver->getName($class);
foreach ($this->transformOnNamespaces as $transformOnNamespace) {
if (!$this->nodeNameResolver->isStringName($className, $transformOnNamespace)) {
return \true;
}
}
}
if ($this->isDescendantOfStdclass($class)) {
return \true;
}

View File

@ -0,0 +1,111 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20211210\Webmozart\Assert\Assert;
/**
* @changelog https://wiki.php.net/rfc/deprecate_dynamic_properties
*
* @see \Rector\Tests\Transform\Rector\Class_\RemoveAllowDynamicPropertiesAttributeRector\RemoveAllowDynamicPropertiesAttributeRectorTest
*/
final class RemoveAllowDynamicPropertiesAttributeRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface
{
/**
* @var string
*/
private const ATTRIBUTE = 'AllowDynamicProperties';
public const TRANSFORM_ON_NAMESPACES = 'transform_on_namespaces';
/**
* @var array<array-key, string>
*/
private $transformOnNamespaces = [];
/**
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
*/
private $phpAttributeAnalyzer;
public function __construct(\Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer $phpAttributeAnalyzer)
{
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove the `AllowDynamicProperties` attribute from all classes', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
namespace Example\Domain;
#[AllowDynamicProperties]
class SomeObject {
public string $someProperty = 'hello world';
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
namespace Example\Domain;
class SomeObject {
public string $someProperty = 'hello world';
}
CODE_SAMPLE
, [\Rector\Transform\Rector\Class_\RemoveAllowDynamicPropertiesAttributeRector::TRANSFORM_ON_NAMESPACES => ['Example\\*']])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Class_::class];
}
public function configure(array $configuration) : void
{
$transformOnNamespaces = $configuration[self::TRANSFORM_ON_NAMESPACES] ?? $configuration;
\RectorPrefix20211210\Webmozart\Assert\Assert::isArray($transformOnNamespaces);
\RectorPrefix20211210\Webmozart\Assert\Assert::allString($transformOnNamespaces);
$this->transformOnNamespaces = $transformOnNamespaces;
}
/**
* @param Class_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($this->shouldRemove($node)) {
return $this->removeAllowDynamicPropertiesAttribute($node);
}
return null;
}
private function removeAllowDynamicPropertiesAttribute(\PhpParser\Node\Stmt\Class_ $class) : \PhpParser\Node\Stmt\Class_
{
$newAttrGroups = [];
foreach ($class->attrGroups as $attrGroup) {
$newAttrs = [];
foreach ($attrGroup->attrs as $attribute) {
if (!$this->nodeNameResolver->isName($attribute, self::ATTRIBUTE)) {
$newAttrs[] = $attribute;
}
}
$attrGroup->attrs = $newAttrs;
if (\count($attrGroup->attrs) !== 0) {
$newAttrGroups[] = $attrGroup;
}
}
$class->attrGroups = $newAttrGroups;
return $class;
}
private function shouldRemove(\PhpParser\Node\Stmt\Class_ $class) : bool
{
if (\count($this->transformOnNamespaces) !== 0) {
$className = (string) $this->nodeNameResolver->getName($class);
foreach ($this->transformOnNamespaces as $transformOnNamespace) {
if (!$this->nodeNameResolver->isStringName($className, $transformOnNamespace)) {
return \false;
}
}
}
return $this->phpAttributeAnalyzer->hasPhpAttribute($class, self::ATTRIBUTE);
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'fa31841d4da7d9fd155acc7310873c799cd0d560';
public const PACKAGE_VERSION = 'eb76f4562c573ac067c46ab32d4429696b3ab66a';
/**
* @var string
*/
public const RELEASE_DATE = '2021-12-10 00:10:20';
public const RELEASE_DATE = '2021-12-10 01:56:36';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211210\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 ComposerAutoloaderInit7f42b5b23ef292103c12f48b6062b53c::getLoader();
return ComposerAutoloaderInit83a74bf56cfcabb04172fc7da306ef8f::getLoader();

View File

@ -2952,6 +2952,7 @@ return array(
'Rector\\Transform\\Rector\\Class_\\ChangeSingletonToServiceRector' => $baseDir . '/rules/Transform/Rector/Class_/ChangeSingletonToServiceRector.php',
'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => $baseDir . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php',
'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => $baseDir . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php',
'Rector\\Transform\\Rector\\Class_\\RemoveAllowDynamicPropertiesAttributeRector' => $baseDir . '/rules/Transform/Rector/Class_/RemoveAllowDynamicPropertiesAttributeRector.php',
'Rector\\Transform\\Rector\\FuncCall\\ArgumentFuncCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',

View File

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

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c
class ComposerStaticInit83a74bf56cfcabb04172fc7da306ef8f
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -3342,6 +3342,7 @@ class ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c
'Rector\\Transform\\Rector\\Class_\\ChangeSingletonToServiceRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/ChangeSingletonToServiceRector.php',
'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php',
'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php',
'Rector\\Transform\\Rector\\Class_\\RemoveAllowDynamicPropertiesAttributeRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/RemoveAllowDynamicPropertiesAttributeRector.php',
'Rector\\Transform\\Rector\\FuncCall\\ArgumentFuncCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',
@ -3794,9 +3795,9 @@ class ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7f42b5b23ef292103c12f48b6062b53c::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit83a74bf56cfcabb04172fc7da306ef8f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit83a74bf56cfcabb04172fc7da306ef8f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit83a74bf56cfcabb04172fc7da306ef8f::$classMap;
}, null, ClassLoader::class);
}

View File

@ -12,8 +12,8 @@ if (!class_exists('GenerateChangelogCommand', false) && !interface_exists('Gener
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211210\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit7f42b5b23ef292103c12f48b6062b53c', false) && !interface_exists('ComposerAutoloaderInit7f42b5b23ef292103c12f48b6062b53c', false) && !trait_exists('ComposerAutoloaderInit7f42b5b23ef292103c12f48b6062b53c', false)) {
spl_autoload_call('RectorPrefix20211210\ComposerAutoloaderInit7f42b5b23ef292103c12f48b6062b53c');
if (!class_exists('ComposerAutoloaderInit83a74bf56cfcabb04172fc7da306ef8f', false) && !interface_exists('ComposerAutoloaderInit83a74bf56cfcabb04172fc7da306ef8f', false) && !trait_exists('ComposerAutoloaderInit83a74bf56cfcabb04172fc7da306ef8f', false)) {
spl_autoload_call('RectorPrefix20211210\ComposerAutoloaderInit83a74bf56cfcabb04172fc7da306ef8f');
}
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('RectorPrefix20211210\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -81,9 +81,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211210\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire7f42b5b23ef292103c12f48b6062b53c')) {
function composerRequire7f42b5b23ef292103c12f48b6062b53c() {
return \RectorPrefix20211210\composerRequire7f42b5b23ef292103c12f48b6062b53c(...func_get_args());
if (!function_exists('composerRequire83a74bf56cfcabb04172fc7da306ef8f')) {
function composerRequire83a74bf56cfcabb04172fc7da306ef8f() {
return \RectorPrefix20211210\composerRequire83a74bf56cfcabb04172fc7da306ef8f(...func_get_args());
}
}
if (!function_exists('scanPath')) {