-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add MigratePluginContentElementAndPluginSubtypesTCARector
Closes: #4323
- Loading branch information
1 parent
8e17ac9
commit 443901a
Showing
12 changed files
with
405 additions
and
3 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
71 changes: 71 additions & 0 deletions
71
rules/TYPO313/v4/MigratePluginContentElementAndPluginSubtypesTCARector.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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\TYPO313\v4; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use Ssch\TYPO3Rector\Rector\AbstractArrayDimFetchTcaRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.4/Deprecation-105076-PluginContentElementAndPluginSubTypes.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesTCARector\MigratePluginContentElementAndPluginSubtypesTCARectorTest | ||
*/ | ||
final class MigratePluginContentElementAndPluginSubtypesTCARector extends AbstractArrayDimFetchTcaRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Migrate plugin content element and plugin subtypes (list_type) TCA', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist'][$pluginSignature] = 'pi_flexform'; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
ExtensionManagementUtility::addToAllTCAtypes( | ||
'tt_content', | ||
'--div--;Configuration,pi_flexform,', | ||
$pluginSignature, | ||
'after:subheader', | ||
); | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
/** | ||
* @param Assign $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$pluginSignature = $node->var; | ||
if (! $pluginSignature instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if (! $pluginSignature->dim instanceof String_ && ! $pluginSignature->dim instanceof Variable) { | ||
return null; | ||
} | ||
|
||
$rootLine = ['TCA', 'tt_content', 'types', 'list', 'subtypes_addlist']; | ||
$result = $this->isInRootLine($pluginSignature, $rootLine); | ||
if (! $result) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createStaticCall( | ||
'TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility', | ||
'addToAllTCAtypes', | ||
$this->nodeFactory->createArgs([ | ||
new String_('tt_content'), | ||
new String_('--div--;Configuration,pi_flexform,'), | ||
$pluginSignature->dim, | ||
new String_('after:subheader'), | ||
]) | ||
); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
rules/TYPO313/v4/RemoveTcaSubTypesExcludeListTCARector.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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\TYPO313\v4; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\NodeTraverser; | ||
use Ssch\TYPO3Rector\Rector\AbstractArrayDimFetchTcaRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.4/Deprecation-105076-PluginContentElementAndPluginSubTypes.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v13\v4\RemoveTcaSubTypesExcludeListTCARector\RemoveTcaSubTypesExcludeListTCARectorTest | ||
*/ | ||
final class RemoveTcaSubTypesExcludeListTCARector extends AbstractArrayDimFetchTcaRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove subtypes_excludelist from list type', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_excludelist']['my_plugin'] = 'layout,select_key,pages'; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
- | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Expression::class]; | ||
} | ||
|
||
/** | ||
* @param Expression $node | ||
*/ | ||
public function refactor(Node $node): ?int | ||
{ | ||
$assignment = $node->expr; | ||
if (! $assignment instanceof Assign) { | ||
return null; | ||
} | ||
|
||
$pluginSignature = $assignment->var; | ||
if (! $pluginSignature instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if (! $pluginSignature->dim instanceof String_ && ! $pluginSignature->dim instanceof Variable) { | ||
return null; | ||
} | ||
|
||
$rootLine = ['TCA', 'tt_content', 'types', 'list', 'subtypes_excludelist']; | ||
$result = $this->isInRootLine($pluginSignature, $rootLine); | ||
if (! $result) { | ||
return null; | ||
} | ||
|
||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\Rector\AbstractRector; | ||
|
||
/** | ||
* Base rector that detects Assignments containing TCA definitions and allows to refactor them | ||
*/ | ||
abstract class AbstractArrayDimFetchTcaRector extends AbstractRector | ||
{ | ||
protected ValueResolver $valueResolver; | ||
|
||
public function __construct(ValueResolver $valueResolver) | ||
{ | ||
$this->valueResolver = $valueResolver; | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Assign::class]; | ||
} | ||
|
||
/** | ||
* @param string[] $rootLine | ||
*/ | ||
protected function isInRootLine(ArrayDimFetch $arrayDimFetch, array &$rootLine): bool | ||
{ | ||
$isInRootLine = false; | ||
$this->traverseNodesWithCallable($arrayDimFetch->var, function (Node $node) use ( | ||
&$isInRootLine, | ||
&$rootLine | ||
): ?ArrayDimFetch { | ||
if (! $node instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if (! $node->dim instanceof String_) { | ||
return null; | ||
} | ||
|
||
$lastArrayElement = end($rootLine); | ||
if ($this->valueResolver->isValue($node->dim, $lastArrayElement)) { | ||
array_pop($rootLine); | ||
$isInRootLine = true; | ||
} else { | ||
$isInRootLine = false; | ||
} | ||
|
||
return $node; | ||
}); | ||
return $isInRootLine; | ||
} | ||
|
||
protected function findParent(ArrayDimFetch $arrayDimFetch, string $key): ?ArrayDimFetch | ||
{ | ||
$foundNode = null; | ||
$this->traverseNodesWithCallable($arrayDimFetch->var, function (Node $node) use ( | ||
&$foundNode, | ||
$key | ||
): ?ArrayDimFetch { | ||
if (! $node instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if (! $node->dim instanceof String_) { | ||
return null; | ||
} | ||
|
||
if (! $this->valueResolver->isValue($node->dim, $key)) { | ||
return null; | ||
} | ||
|
||
$foundNode = $node; | ||
return $node; | ||
}); | ||
return $foundNode; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ctor/v13/v4/MigratePluginContentElementAndPluginSubtypesTCARector/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,29 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesTCARector\Fixture; | ||
|
||
$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist']['my_plugin'] = 'pi_flexform'; | ||
|
||
$pluginSignature = 'tx_rector_pi1'; | ||
$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist'][$pluginSignature] = 'pi_flexform'; | ||
|
||
$GLOBALS['TCA']['tt_content']['types']['list']['leave_me_alone']['my_plugin'] = 'pi_flexform'; | ||
$GLOBALS['TCA']['pages']['types']['list']['leave_me_alone']['my_plugin'] = 'pi_flexform'; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesTCARector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
|
||
ExtensionManagementUtility::addToAllTCAtypes('tt_content', '--div--;Configuration,pi_flexform,', 'my_plugin', 'after:subheader'); | ||
|
||
$pluginSignature = 'tx_rector_pi1'; | ||
ExtensionManagementUtility::addToAllTCAtypes('tt_content', '--div--;Configuration,pi_flexform,', $pluginSignature, 'after:subheader'); | ||
|
||
$GLOBALS['TCA']['tt_content']['types']['list']['leave_me_alone']['my_plugin'] = 'pi_flexform'; | ||
$GLOBALS['TCA']['pages']['types']['list']['leave_me_alone']['my_plugin'] = 'pi_flexform'; | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...tAndPluginSubtypesTCARector/MigratePluginContentElementAndPluginSubtypesTCARectorTest.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesTCARector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MigratePluginContentElementAndPluginSubtypesTCARectorTest 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
...r/v13/v4/MigratePluginContentElementAndPluginSubtypesTCARector/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\TYPO313\v4\MigratePluginContentElementAndPluginSubtypesTCARector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesTCARector::class); | ||
}; |
Oops, something went wrong.