mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-16 13:00:23 +01:00
Updated Rector to commit 706170a2bee1e6df93e0b9789c97f7af36eb9c73
706170a2be
feature: replace MyCLabs Enum constructor call (#6588)
This commit is contained in:
parent
348456e6b0
commit
331a8ca05e
@ -11,8 +11,9 @@ use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
|
||||
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
|
||||
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
|
||||
use Rector\Php81\Rector\MethodCall\SpatieEnumMethodCallToEnumConstRector;
|
||||
use Rector\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector;
|
||||
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([ReturnNeverTypeRector::class, MyCLabsClassToEnumRector::class, MyCLabsMethodCallToEnumConstRector::class, ReadOnlyPropertyRector::class, SpatieEnumClassToEnumRector::class, SpatieEnumMethodCallToEnumConstRector::class, NewInInitializerRector::class, NullToStrictStringFuncCallArgRector::class, FirstClassCallableRector::class]);
|
||||
$rectorConfig->rules([ReturnNeverTypeRector::class, MyCLabsClassToEnumRector::class, MyCLabsMethodCallToEnumConstRector::class, MyCLabsConstructorCallToEnumFromRector::class, ReadOnlyPropertyRector::class, SpatieEnumClassToEnumRector::class, SpatieEnumMethodCallToEnumConstRector::class, NewInInitializerRector::class, NullToStrictStringFuncCallArgRector::class, FirstClassCallableRector::class]);
|
||||
};
|
||||
|
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Php81\Rector\New_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Enum\ObjectReference;
|
||||
use Rector\PHPStan\ScopeFetcher;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\ValueObject\MethodName;
|
||||
use Rector\ValueObject\PhpVersionFeature;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\MyCLabsConstructorCallToEnumFromRectorTest
|
||||
*/
|
||||
final class MyCLabsConstructorCallToEnumFromRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
private ReflectionProvider $reflectionProvider;
|
||||
private const MY_C_LABS_CLASS = 'MyCLabs\\Enum\\Enum';
|
||||
private const DEFAULT_ENUM_CONSTRUCTOR = 'from';
|
||||
public function __construct(ReflectionProvider $reflectionProvider)
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [New_::class];
|
||||
}
|
||||
/**
|
||||
* @param New_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
return $this->refactorConstructorCallToStaticFromCall($node);
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::ENUM;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Refactor MyCLabs Enum using constructor for instantiation', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
$enum = new Enum($args);
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
$enum = Enum::from($args);
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
private function refactorConstructorCallToStaticFromCall(New_ $node) : ?StaticCall
|
||||
{
|
||||
if (!$this->isObjectType($node->class, new ObjectType(self::MY_C_LABS_CLASS))) {
|
||||
return null;
|
||||
}
|
||||
$classname = $this->getName($node->class);
|
||||
if (\in_array($classname, [ObjectReference::SELF, ObjectReference::STATIC], \true)) {
|
||||
$classname = ($nullsafeVariable1 = ScopeFetcher::fetch($node)->getClassReflection()) ? $nullsafeVariable1->getName() : null;
|
||||
}
|
||||
if ($classname === null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isMyCLabsConstructor($node, $classname)) {
|
||||
return null;
|
||||
}
|
||||
return new StaticCall(new Name\FullyQualified($classname), self::DEFAULT_ENUM_CONSTRUCTOR, $node->args);
|
||||
}
|
||||
private function isMyCLabsConstructor(New_ $node, string $classname) : bool
|
||||
{
|
||||
$classReflection = $this->reflectionProvider->getClass($classname);
|
||||
if (!$classReflection->hasMethod(MethodName::CONSTRUCT)) {
|
||||
return \true;
|
||||
}
|
||||
return $classReflection->getMethod(MethodName::CONSTRUCT, ScopeFetcher::fetch($node))->getDeclaringClass()->getName() === self::MY_C_LABS_CLASS;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'fe8e48de8cc6c962430abab1b1766c05dd12309e';
|
||||
public const PACKAGE_VERSION = '706170a2bee1e6df93e0b9789c97f7af36eb9c73';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2025-01-10 18:33:46';
|
||||
public const RELEASE_DATE = '2025-01-11 08:41:26';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -2012,6 +2012,7 @@ return array(
|
||||
'Rector\\Php81\\Rector\\FuncCall\\NullToStrictStringFuncCallArgRector' => $baseDir . '/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php',
|
||||
'Rector\\Php81\\Rector\\MethodCall\\MyCLabsMethodCallToEnumConstRector' => $baseDir . '/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php',
|
||||
'Rector\\Php81\\Rector\\MethodCall\\SpatieEnumMethodCallToEnumConstRector' => $baseDir . '/rules/Php81/Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.php',
|
||||
'Rector\\Php81\\Rector\\New_\\MyCLabsConstructorCallToEnumFromRector' => $baseDir . '/rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php',
|
||||
'Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector' => $baseDir . '/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php',
|
||||
'Rector\\Php82\\Rector\\Class_\\ReadOnlyClassRector' => $baseDir . '/rules/Php82/Rector/Class_/ReadOnlyClassRector.php',
|
||||
'Rector\\Php82\\Rector\\Encapsed\\VariableInStringInterpolationFixerRector' => $baseDir . '/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php',
|
||||
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
@ -2231,6 +2231,7 @@ class ComposerStaticInit4349f48e2d711e343a1bf0101d0ae855
|
||||
'Rector\\Php81\\Rector\\FuncCall\\NullToStrictStringFuncCallArgRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php',
|
||||
'Rector\\Php81\\Rector\\MethodCall\\MyCLabsMethodCallToEnumConstRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php',
|
||||
'Rector\\Php81\\Rector\\MethodCall\\SpatieEnumMethodCallToEnumConstRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.php',
|
||||
'Rector\\Php81\\Rector\\New_\\MyCLabsConstructorCallToEnumFromRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php',
|
||||
'Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php',
|
||||
'Rector\\Php82\\Rector\\Class_\\ReadOnlyClassRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/Class_/ReadOnlyClassRector.php',
|
||||
'Rector\\Php82\\Rector\\Encapsed\\VariableInStringInterpolationFixerRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user