Updated Rector to commit 4c782243fdb6b03474faa0076800048cb3ef0de9

4c782243fd Remove ChangeReadOnlyPropertyWithDefaultValueToConstantRector as overly detailed checks, better use PHPStan and refactor individually (#4028)
This commit is contained in:
Tomas Votruba 2023-05-30 13:09:27 +00:00
parent 010d5d2c6f
commit df2ad70dd3
11 changed files with 13 additions and 313 deletions

View File

@ -8,13 +8,7 @@ use Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector;
use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(FinalizeClassesWithoutChildrenRector::class);
$rectorConfig->rule(ChangeGlobalVariablesToPropertiesRector::class);
$rectorConfig->rule(ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class);
$rectorConfig->rule(PrivatizeLocalGetterToPropertyRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(PrivatizeFinalClassMethodRector::class);
$rectorConfig->rules([FinalizeClassesWithoutChildrenRector::class, ChangeGlobalVariablesToPropertiesRector::class, PrivatizeLocalGetterToPropertyRector::class, PrivatizeFinalClassPropertyRector::class, PrivatizeFinalClassMethodRector::class]);
};

View File

@ -1,31 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Privatization\Naming;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\NodeNameResolver\NodeNameResolver;
use RectorPrefix202305\Symfony\Component\String\UnicodeString;
final class ConstantNaming
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
}
public function createFromProperty(PropertyProperty $propertyProperty) : string
{
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($propertyProperty);
return $this->createUnderscoreUppercaseString($propertyName);
}
private function createUnderscoreUppercaseString(string $propertyName) : string
{
$propertyNameUnicodeString = new UnicodeString($propertyName);
return $propertyNameUnicodeString->snake()->upper()->toString();
}
}

View File

@ -1,37 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Privatization\NodeFactory;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\Naming\ConstantNaming;
final class ClassConstantFactory
{
/**
* @readonly
* @var \Rector\Privatization\Naming\ConstantNaming
*/
private $constantNaming;
public function __construct(ConstantNaming $constantNaming)
{
$this->constantNaming = $constantNaming;
}
public function createFromProperty(Property $property) : ClassConst
{
$propertyProperty = $property->props[0];
$constantName = $this->constantNaming->createFromProperty($propertyProperty);
/** @var Expr $defaultValue */
$defaultValue = $propertyProperty->default;
$const = new Const_($constantName, $defaultValue);
$classConst = new ClassConst([$const]);
$classConst->flags = $property->flags & ~Class_::MODIFIER_STATIC;
$classConst->setAttribute(AttributeKey::PHP_DOC_INFO, $property->getAttribute(AttributeKey::PHP_DOC_INFO));
$classConst->setAttribute(AttributeKey::COMMENTS, $property->getAttribute(AttributeKey::COMMENTS));
return $classConst;
}
}

View File

@ -1,69 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Privatization\NodeReplacer;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\Privatization\Naming\ConstantNaming;
final class PropertyFetchWithConstFetchReplacer
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
/**
* @readonly
* @var \Rector\Privatization\Naming\ConstantNaming
*/
private $constantNaming;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\NodeFactory
*/
private $nodeFactory;
public function __construct(NodeNameResolver $nodeNameResolver, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, PropertyFetchAnalyzer $propertyFetchAnalyzer, ConstantNaming $constantNaming, NodeFactory $nodeFactory)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->constantNaming = $constantNaming;
$this->nodeFactory = $nodeFactory;
}
public function replace(Class_ $class, Property $property) : void
{
$propertyProperty = $property->props[0];
$propertyName = $this->nodeNameResolver->getName($property);
$constantName = $this->constantNaming->createFromProperty($propertyProperty);
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class, function (Node $node) use($propertyName, $constantName) : ?ClassConstFetch {
if (!$this->propertyFetchAnalyzer->isLocalPropertyFetch($node)) {
return null;
}
/** @var PropertyFetch|StaticPropertyFetch $node */
if (!$this->nodeNameResolver->isName($node->name, $propertyName)) {
return null;
}
// replace with constant fetch
return $this->nodeFactory->createSelfFetchConstant($constantName);
});
}
}

View File

@ -1,136 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Privatization\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Privatization\NodeFactory\ClassConstantFactory;
use Rector\Privatization\NodeReplacer\PropertyFetchWithConstFetchReplacer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector\ChangeReadOnlyPropertyWithDefaultValueToConstantRectorTest
*/
final class ChangeReadOnlyPropertyWithDefaultValueToConstantRector extends AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\PropertyManipulator
*/
private $propertyManipulator;
/**
* @readonly
* @var \Rector\Privatization\NodeFactory\ClassConstantFactory
*/
private $classConstantFactory;
/**
* @readonly
* @var \Rector\Privatization\NodeReplacer\PropertyFetchWithConstFetchReplacer
*/
private $propertyFetchWithConstFetchReplacer;
public function __construct(PropertyManipulator $propertyManipulator, ClassConstantFactory $classConstantFactory, PropertyFetchWithConstFetchReplacer $propertyFetchWithConstFetchReplacer)
{
$this->propertyManipulator = $propertyManipulator;
$this->classConstantFactory = $classConstantFactory;
$this->propertyFetchWithConstFetchReplacer = $propertyFetchWithConstFetchReplacer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change property with read only status with default value to constant', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var string[]
*/
private $magicMethods = [
'__toString',
'__wakeup',
];
public function run()
{
foreach ($this->magicMethods as $magicMethod) {
echo $magicMethod;
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var string[]
*/
private const MAGIC_METHODS = [
'__toString',
'__wakeup',
];
public function run()
{
foreach (self::MAGIC_METHODS as $magicMethod) {
echo $magicMethod;
}
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Property::class];
}
/**
* @param Property $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
/** @var PropertyProperty $onlyProperty */
$onlyProperty = $node->props[0];
// we need default value
if (!$onlyProperty->default instanceof Expr) {
return null;
}
if (!$node->isPrivate()) {
return null;
}
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (!$class instanceof Class_) {
return null;
}
// is property read only?
if ($this->propertyManipulator->isPropertyChangeable($class, $node, $scope)) {
return null;
}
$this->propertyFetchWithConstFetchReplacer->replace($class, $node);
return $this->classConstantFactory->createFromProperty($node);
}
private function shouldSkip(Property $property) : bool
{
if (\count($property->props) !== 1) {
return \true;
}
$classLike = $this->betterNodeFinder->findParentType($property, Class_::class);
if (!$classLike instanceof Class_) {
return \true;
}
if ($property->attrGroups !== []) {
return \true;
}
return $this->isObjectType($classLike, new ObjectType('PHP_CodeSniffer\\Sniffs\\Sniff'));
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '22e97d1c418d0772cf449ed888522ca060829ddf';
public const PACKAGE_VERSION = '4c782243fdb6b03474faa0076800048cb3ef0de9';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-30 14:01:00';
public const RELEASE_DATE = '2023-05-30 13:04:28';
/**
* @var int
*/

View File

@ -212,19 +212,6 @@ final class PropertyManipulator
}
return \false;
}
public function isPropertyChangeable(Class_ $class, Property $property, Scope $scope) : bool
{
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $property);
foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch, $scope)) {
return \true;
}
if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
return \true;
}
}
return \false;
}
public function resolveExistingClassPropertyNameByType(Class_ $class, ObjectType $objectType) : ?string
{
foreach ($class->getProperties() as $property) {

2
vendor/autoload.php vendored
View File

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

View File

@ -2365,15 +2365,11 @@ return array(
'Rector\\PostRector\\Rector\\UseAddingPostRector' => $baseDir . '/packages/PostRector/Rector/UseAddingPostRector.php',
'Rector\\PostRector\\ValueObject\\PropertyMetadata' => $baseDir . '/packages/PostRector/ValueObject/PropertyMetadata.php',
'Rector\\Privatization\\Guard\\ParentPropertyLookupGuard' => $baseDir . '/rules/Privatization/Guard/ParentPropertyLookupGuard.php',
'Rector\\Privatization\\Naming\\ConstantNaming' => $baseDir . '/rules/Privatization/Naming/ConstantNaming.php',
'Rector\\Privatization\\NodeFactory\\ClassConstantFactory' => $baseDir . '/rules/Privatization/NodeFactory/ClassConstantFactory.php',
'Rector\\Privatization\\NodeManipulator\\VisibilityManipulator' => $baseDir . '/rules/Privatization/NodeManipulator/VisibilityManipulator.php',
'Rector\\Privatization\\NodeReplacer\\PropertyFetchWithConstFetchReplacer' => $baseDir . '/rules/Privatization/NodeReplacer/PropertyFetchWithConstFetchReplacer.php',
'Rector\\Privatization\\Rector\\ClassMethod\\PrivatizeFinalClassMethodRector' => $baseDir . '/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php',
'Rector\\Privatization\\Rector\\Class_\\ChangeGlobalVariablesToPropertiesRector' => $baseDir . '/rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php',
'Rector\\Privatization\\Rector\\Class_\\FinalizeClassesWithoutChildrenRector' => $baseDir . '/rules/Privatization/Rector/Class_/FinalizeClassesWithoutChildrenRector.php',
'Rector\\Privatization\\Rector\\MethodCall\\PrivatizeLocalGetterToPropertyRector' => $baseDir . '/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php',
'Rector\\Privatization\\Rector\\Property\\ChangeReadOnlyPropertyWithDefaultValueToConstantRector' => $baseDir . '/rules/Privatization/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector.php',
'Rector\\Privatization\\Rector\\Property\\PrivatizeFinalClassPropertyRector' => $baseDir . '/rules/Privatization/Rector/Property/PrivatizeFinalClassPropertyRector.php',
'Rector\\Privatization\\TypeManipulator\\TypeNormalizer' => $baseDir . '/rules/Privatization/TypeManipulator/TypeNormalizer.php',
'Rector\\Privatization\\VisibilityGuard\\ClassMethodVisibilityGuard' => $baseDir . '/rules/Privatization/VisibilityGuard/ClassMethodVisibilityGuard.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit18ca20d17b299a5129994679ad8528a1
class ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit18ca20d17b299a5129994679ad8528a1
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit18ca20d17b299a5129994679ad8528a1', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit18ca20d17b299a5129994679ad8528a1', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit18ca20d17b299a5129994679ad8528a1::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit034991f4b22807fc1159cd21567fcc93::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit18ca20d17b299a5129994679ad8528a1::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$files;
$requireFile = \Closure::bind(static function ($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 ComposerStaticInit18ca20d17b299a5129994679ad8528a1
class ComposerStaticInit034991f4b22807fc1159cd21567fcc93
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2607,15 +2607,11 @@ class ComposerStaticInit18ca20d17b299a5129994679ad8528a1
'Rector\\PostRector\\Rector\\UseAddingPostRector' => __DIR__ . '/../..' . '/packages/PostRector/Rector/UseAddingPostRector.php',
'Rector\\PostRector\\ValueObject\\PropertyMetadata' => __DIR__ . '/../..' . '/packages/PostRector/ValueObject/PropertyMetadata.php',
'Rector\\Privatization\\Guard\\ParentPropertyLookupGuard' => __DIR__ . '/../..' . '/rules/Privatization/Guard/ParentPropertyLookupGuard.php',
'Rector\\Privatization\\Naming\\ConstantNaming' => __DIR__ . '/../..' . '/rules/Privatization/Naming/ConstantNaming.php',
'Rector\\Privatization\\NodeFactory\\ClassConstantFactory' => __DIR__ . '/../..' . '/rules/Privatization/NodeFactory/ClassConstantFactory.php',
'Rector\\Privatization\\NodeManipulator\\VisibilityManipulator' => __DIR__ . '/../..' . '/rules/Privatization/NodeManipulator/VisibilityManipulator.php',
'Rector\\Privatization\\NodeReplacer\\PropertyFetchWithConstFetchReplacer' => __DIR__ . '/../..' . '/rules/Privatization/NodeReplacer/PropertyFetchWithConstFetchReplacer.php',
'Rector\\Privatization\\Rector\\ClassMethod\\PrivatizeFinalClassMethodRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php',
'Rector\\Privatization\\Rector\\Class_\\ChangeGlobalVariablesToPropertiesRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php',
'Rector\\Privatization\\Rector\\Class_\\FinalizeClassesWithoutChildrenRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/Class_/FinalizeClassesWithoutChildrenRector.php',
'Rector\\Privatization\\Rector\\MethodCall\\PrivatizeLocalGetterToPropertyRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php',
'Rector\\Privatization\\Rector\\Property\\ChangeReadOnlyPropertyWithDefaultValueToConstantRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector.php',
'Rector\\Privatization\\Rector\\Property\\PrivatizeFinalClassPropertyRector' => __DIR__ . '/../..' . '/rules/Privatization/Rector/Property/PrivatizeFinalClassPropertyRector.php',
'Rector\\Privatization\\TypeManipulator\\TypeNormalizer' => __DIR__ . '/../..' . '/rules/Privatization/TypeManipulator/TypeNormalizer.php',
'Rector\\Privatization\\VisibilityGuard\\ClassMethodVisibilityGuard' => __DIR__ . '/../..' . '/rules/Privatization/VisibilityGuard/ClassMethodVisibilityGuard.php',
@ -3085,9 +3081,9 @@ class ComposerStaticInit18ca20d17b299a5129994679ad8528a1
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit18ca20d17b299a5129994679ad8528a1::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit18ca20d17b299a5129994679ad8528a1::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit18ca20d17b299a5129994679ad8528a1::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$classMap;
}, null, ClassLoader::class);
}