Skip to content

Commit

Permalink
[FEATURE] Add MigratePluginContentElementAndPluginSubtypesTCARector
Browse files Browse the repository at this point in the history
Closes: #4323
  • Loading branch information
simonschaufi committed Nov 20, 2024
1 parent 8e17ac9 commit 443901a
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/v13/typo3-134.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\TYPO313\v4\MigratePluginContentElementAndPluginSubtypesRector;
use Ssch\TYPO3Rector\TYPO313\v4\MigratePluginContentElementAndPluginSubtypesTCARector;
use Ssch\TYPO3Rector\TYPO313\v4\RemoveTcaSubTypesExcludeListTCARector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesRector::class);
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesTCARector::class);
$rectorConfig->rule(RemoveTcaSubTypesExcludeListTCARector::class);
};
35 changes: 33 additions & 2 deletions docs/all_rectors_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 151 Rules Overview
# 153 Rules Overview

<br>

Expand All @@ -14,7 +14,7 @@

- [TYPO312](#typo312) (54)

- [TYPO313](#typo313) (17)
- [TYPO313](#typo313) (19)

- [TypeDeclaration](#typedeclaration) (1)

Expand Down Expand Up @@ -3226,6 +3226,24 @@ Migrate plugin content element and plugin subtypes (list_type)

<br>

### MigratePluginContentElementAndPluginSubtypesTCARector

Migrate plugin content element and plugin subtypes (list_type) TCA

- class: [`Ssch\TYPO3Rector\TYPO313\v4\MigratePluginContentElementAndPluginSubtypesTCARector`](../rules/TYPO313/v4/MigratePluginContentElementAndPluginSubtypesTCARector.php)

```diff
-$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist'][$pluginSignature] = 'pi_flexform';
+ExtensionManagementUtility::addToAllTCAtypes(
+ 'tt_content',
+ '--div--;Configuration,pi_flexform,',
+ $pluginSignature,
+ 'after:subheader',
+);
```

<br>

### MigrateViewHelperRenderStaticRector

Migrate static ViewHelpers to object-based ViewHelpers
Expand Down Expand Up @@ -3269,6 +3287,19 @@ Unset the value in the config mmHasUidField

<br>

### RemoveTcaSubTypesExcludeListTCARector

Remove subtypes_excludelist from list type

- class: [`Ssch\TYPO3Rector\TYPO313\v4\RemoveTcaSubTypesExcludeListTCARector`](../rules/TYPO313/v4/RemoveTcaSubTypesExcludeListTCARector.php)

```diff
-$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_excludelist']['my_plugin'] = 'layout,select_key,pages';
+-
```

<br>

### StrictTypesPersistenceManagerRector

Strict types for PersistenceManager
Expand Down
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 rules/TYPO313/v4/RemoveTcaSubTypesExcludeListTCARector.php
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;
}
}
89 changes: 89 additions & 0 deletions src/Rector/AbstractArrayDimFetchTcaRector.php
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;
}
}
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';

?>
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';
}
}
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);
};
Loading

0 comments on commit 443901a

Please sign in to comment.