Skip to content

Commit

Permalink
[FEATURE] Add RemoveTypeHintViewInterfaceRector (#4222)
Browse files Browse the repository at this point in the history
Resolves: #3715
  • Loading branch information
simonschaufi committed Apr 10, 2024
1 parent 4ef49c9 commit 5989417
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/v11/typo3-115.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,4 +44,5 @@
$rectorConfig->rule(HandleCObjRendererATagParamsMethodRector::class);
$rectorConfig->rule(SubstituteBackendTemplateViewWithModuleTemplateRector::class);
$rectorConfig->rule(SubstituteGetIconFactoryAndGetPageRendererFromModuleTemplateRector::class);
$rectorConfig->rule(RemoveTypeHintViewInterfaceRector::class);
};
126 changes: 126 additions & 0 deletions rules/TYPO311/v5/RemoveTypeHintViewInterfaceRector.php
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);
}
}
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) {}
}
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 {}
}
?>
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) {}
}
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';
}
}
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);
};

0 comments on commit 5989417

Please sign in to comment.