diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/Fixture/call_method.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/Fixture/call_method.php.inc new file mode 100644 index 00000000000..44478f276ce --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/Fixture/call_method.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/KnownMagicClassMethodTypeRectorTest.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/KnownMagicClassMethodTypeRectorTest.php new file mode 100644 index 00000000000..f6a8b8adcdc --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/KnownMagicClassMethodTypeRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/config/configured_rule.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/config/configured_rule.php new file mode 100644 index 00000000000..bc7d6210ac2 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([KnownMagicClassMethodTypeRector::class]); diff --git a/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php new file mode 100644 index 00000000000..d8c4ebfd5f9 --- /dev/null +++ b/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php @@ -0,0 +1,92 @@ +> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + $hasChanged = false; + + foreach ($node->getMethods() as $classMethod) { + if (! $classMethod->isMagic()) { + continue; + } + + if ($this->isName($classMethod, MethodName::CALL)) { + $firstParam = $classMethod->getParams()[0]; + if (! $firstParam->type instanceof Node) { + $firstParam->type = new Identifier('string'); + $hasChanged = true; + } + + $secondParam = $classMethod->getParams()[1]; + if (! $secondParam->type instanceof Node) { + $secondParam->type = new Name('array'); + $hasChanged = true; + } + } + } + + if ($hasChanged) { + return $node; + } + + return null; + } +} diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index 88c97dfdd15..6b98029ae28 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -23,6 +23,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstReturnsRector; use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanStrictReturnsRector; +use Rector\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictReturnsRector; use Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector; use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector; @@ -115,6 +116,9 @@ final class TypeDeclarationLevel ReturnTypeFromStrictTypedCallRector::class, ChildDoctrineRepositoryClassTypeRector::class, + // php native types + KnownMagicClassMethodTypeRector::class, + // param AddMethodCallBasedStrictParamTypeRector::class, ParamTypeByParentCallTypeRector::class,