diff --git a/config/v11/typo3-115.php b/config/v11/typo3-115.php index 02a9d3da6..82e1a14b3 100644 --- a/config/v11/typo3-115.php +++ b/config/v11/typo3-115.php @@ -9,6 +9,7 @@ use Ssch\TYPO3Rector\TYPO311\v5\FlexFormToolsArrayValueByPathRector; use Ssch\TYPO3Rector\TYPO311\v5\HandleCObjRendererATagParamsMethodRector; use Ssch\TYPO3Rector\TYPO311\v5\RemoveDefaultInternalTypeDBRector; +use Ssch\TYPO3Rector\TYPO311\v5\RemoveTypeHintViewInterfaceRector; use Ssch\TYPO3Rector\TYPO311\v5\ReplaceTSFEATagParamsCallOnGlobalsRector; use Ssch\TYPO3Rector\TYPO311\v5\SubstituteBackendTemplateViewWithModuleTemplateRector; use Ssch\TYPO3Rector\TYPO311\v5\SubstituteGetIconFactoryAndGetPageRendererFromModuleTemplateRector; @@ -43,4 +44,5 @@ $rectorConfig->rule(HandleCObjRendererATagParamsMethodRector::class); $rectorConfig->rule(SubstituteBackendTemplateViewWithModuleTemplateRector::class); $rectorConfig->rule(SubstituteGetIconFactoryAndGetPageRendererFromModuleTemplateRector::class); + $rectorConfig->rule(RemoveTypeHintViewInterfaceRector::class); }; diff --git a/rules/TYPO311/v5/RemoveTypeHintViewInterfaceRector.php b/rules/TYPO311/v5/RemoveTypeHintViewInterfaceRector.php new file mode 100644 index 000000000..73d484ef0 --- /dev/null +++ b/rules/TYPO311/v5/RemoveTypeHintViewInterfaceRector.php @@ -0,0 +1,126 @@ +docBlockUpdater = $docBlockUpdater; + $this->phpDocInfoFactory = $phpDocInfoFactory; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Remove', [new CodeSample( + <<<'CODE_SAMPLE' +protected function initializeView(ViewInterface $view) +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +protected function initializeView($view) +CODE_SAMPLE + )]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + + /** + * @param ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + if ($this->shouldSkip($node)) { + return null; + } + + $param = $node->getParams()[0]; + $param->type = null; + + $this->decorateParamDocType($param, $node); + + return $node; + } + + private function shouldSkip(ClassMethod $classMethod): bool + { + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( + $classMethod, + new ObjectType('TYPO3\CMS\Extbase\Mvc\Controller\ActionController') + )) { + return true; + } + + if (! $this->isName($classMethod->name, 'initializeView')) { + return true; + } + + $params = $classMethod->getParams(); + if ($params === []) { + return true; + } + + /** @var Param $firstParam */ + $firstParam = $params[0]; + if ($firstParam->type === null) { + return true; + } + + $type = $firstParam->type; + return $type instanceof FullyQualified && ! $this->isName($type, 'TYPO3\CMS\Extbase\Mvc\View\ViewInterface'); + } + + /** + * @see \Rector\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector::decorateParamDocType + */ + private function decorateParamDocType(Param $param, ClassMethod $classMethod): void + { + $paramName = '$' . $this->getName($param); + + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); + $phpDocInfo->addTagValueNode( + new ParamTagValueNode( + new FullyQualifiedIdentifierTypeNode('TYPO3\CMS\Extbase\Mvc\View\ViewInterface'), + \false, + $paramName, + '' + ) + ); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); + } +} diff --git a/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/already_migrated.php.inc b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/already_migrated.php.inc new file mode 100644 index 000000000..c8a039ad5 --- /dev/null +++ b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/already_migrated.php.inc @@ -0,0 +1,14 @@ + +----- + diff --git a/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/standalone_view.php.inc b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/standalone_view.php.inc new file mode 100644 index 000000000..100f61e51 --- /dev/null +++ b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/standalone_view.php.inc @@ -0,0 +1,11 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/config/configured_rule.php b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/config/configured_rule.php new file mode 100644 index 000000000..a83cfaecb --- /dev/null +++ b/tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../config/config.php'); + $rectorConfig->rule(RemoveTypeHintViewInterfaceRector::class); +};