diff --git a/config/set/php81.php b/config/set/php81.php index e7c136b51d5..0d521b68465 100644 --- a/config/set/php81.php +++ b/config/set/php81.php @@ -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]); }; diff --git a/rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php b/rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php new file mode 100644 index 00000000000..4e53151a959 --- /dev/null +++ b/rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php @@ -0,0 +1,87 @@ +reflectionProvider = $reflectionProvider; + } + /** + * @return array> + */ + 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; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 88548f6b7f9..57076eb2b5a 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */ diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 374b147c855..7d4f58e085a 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -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', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 09f67ab8643..e0acc23a8a2 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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',