-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add RemoveTypeHintViewInterfaceRector (#4222)
Resolves: #3715
- Loading branch information
1 parent
4ef49c9
commit 5989417
Showing
7 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\TYPO311\v5; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode; | ||
use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.5/Deprecation-95222-ExtbaseViewInterface.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector\RemoveTypeHintViewInterfaceRectorTest | ||
*/ | ||
final class RemoveTypeHintViewInterfaceRector extends AbstractRector | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private DocBlockUpdater $docBlockUpdater; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private PhpDocInfoFactory $phpDocInfoFactory; | ||
|
||
public function __construct(DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory) | ||
{ | ||
$this->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<class-string<Node>> | ||
*/ | ||
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/already_migrated.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; | ||
|
||
class MyActionController extends ActionController | ||
{ | ||
/** | ||
* @param ViewInterface $view | ||
*/ | ||
protected function initializeView($view) {} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; | ||
|
||
class MyActionController extends ActionController | ||
{ | ||
protected function initializeView(ViewInterface $view) {} | ||
|
||
protected function differentMethod(ViewInterface $view): void {} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; | ||
|
||
class MyActionController extends ActionController | ||
{ | ||
/** | ||
* @param ViewInterface $view | ||
*/ | ||
protected function initializeView($view) {} | ||
|
||
protected function differentMethod(ViewInterface $view): void {} | ||
} | ||
?> |
11 changes: 11 additions & 0 deletions
11
tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/Fixture/standalone_view.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
use TYPO3\CMS\Fluid\View\StandaloneView; | ||
|
||
class MyActionController extends ActionController | ||
{ | ||
protected function initializeView(StandaloneView $view) {} | ||
} |
32 changes: 32 additions & 0 deletions
32
...Rector/v11/v5/RemoveTypeHintViewInterfaceRector/RemoveTypeHintViewInterfaceRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\RemoveTypeHintViewInterfaceRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveTypeHintViewInterfaceRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rector/v11/v5/RemoveTypeHintViewInterfaceRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Ssch\TYPO3Rector\TYPO311\v5\RemoveTypeHintViewInterfaceRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../config/config.php'); | ||
$rectorConfig->rule(RemoveTypeHintViewInterfaceRector::class); | ||
}; |