Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add MigratePluginContentElementAndPluginSubtypesSwapArgsRector #4451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/v13/typo3-134.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand All @@ -12,4 +13,5 @@
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesRector::class);
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesTCARector::class);
$rectorConfig->rule(RemoveTcaSubTypesExcludeListTCARector::class);
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesSwapArgsRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO313\v4;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
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/13.4/Deprecation-105076-PluginContentElementAndPluginSubTypes.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesSwapArgsRector\MigratePluginContentElementAndPluginSubtypesSwapArgsRectorTest
*/
final class MigratePluginContentElementAndPluginSubtypesSwapArgsRector extends AbstractRector
{
/**
* @readonly
*/
private ValueResolver $valueResolver;

public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Swap arguments for ExtensionManagementUtility::addPiFlexFormValue', [new CodeSample(
<<<'CODE_SAMPLE'
ExtensionManagementUtility::addPiFlexFormValue(
$pluginSignature,
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);
CODE_SAMPLE
)]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

$argsCount = count($node->args);
switch ($argsCount) {
case 2:
$this->swapArgs($node);
return $node;
case 3:
if ($node->args[0]->value instanceof String_) {
$firstArgumentValue = $this->valueResolver->getValue($node->args[0]);

// check if already migrated
if ($firstArgumentValue === '*') {
return null;
}
}

if ($node->args[2]->value instanceof String_) {
$thirdArgumentValue = $this->valueResolver->getValue($node->args[2]);
if ($thirdArgumentValue === 'list' || $thirdArgumentValue === '*') {
$this->swapArgs($node);
return $node;
}
}
}

return null;
}

private function shouldSkip(StaticCall $staticCall): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$staticCall,
new ObjectType('TYPO3\CMS\Core\Utility\ExtensionManagementUtility')
)) {
return true;
}

return ! $this->isName($staticCall->name, 'addPiFlexFormValue');
}

private function swapArgs(StaticCall $node): void
{
// put first argument on third place
$firstArgument = $node->args[0];

$node->args[0] = new Arg(new String_('*'));
$node->args[2] = $firstArgument;
}
}
4 changes: 4 additions & 0 deletions stubs/TYPO3/CMS/Core/Utility/ExtensionManagementUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ public static function addUserTSConfig(string $content): void
public static function addPlugin($itemArray, string $type = 'list_type', ?string $extensionKey = null): void
{
}

public static function addPiFlexFormValue(string $piKeyToMatch, string $value, string $CTypeToMatch = 'list'): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesSwapArgsRector\Fixture;

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

$pluginSignature = 'tx_rector_pi1';

// already migrated
ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);

ExtensionManagementUtility::addPiFlexFormValue(
$pluginSignature,
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
);

ExtensionManagementUtility::addPiFlexFormValue(
'tx_rector_pi1',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
);

ExtensionManagementUtility::addPiFlexFormValue(
$pluginSignature,
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'list',
);

ExtensionManagementUtility::addPiFlexFormValue(
'tx_rector_pi1',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'list',
);

ExtensionManagementUtility::addPiFlexFormValue(
$pluginSignature,
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'*',
);

ExtensionManagementUtility::addPiFlexFormValue(
'tx_rector_pi1',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'*',
);

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v4\MigratePluginContentElementAndPluginSubtypesSwapArgsRector\Fixture;

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

$pluginSignature = 'tx_rector_pi1';

// already migrated
ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'tx_rector_pi1',
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'tx_rector_pi1',
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
$pluginSignature,
);

ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:examples/Configuration/Flexforms/HtmlParser.xml',
'tx_rector_pi1',
);

?>
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\MigratePluginContentElementAndPluginSubtypesSwapArgsRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigratePluginContentElementAndPluginSubtypesSwapArgsRectorTest 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\MigratePluginContentElementAndPluginSubtypesSwapArgsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php');
$rectorConfig->rule(MigratePluginContentElementAndPluginSubtypesSwapArgsRector::class);
};