Skip to content

Commit

Permalink
[FEATURE] Add migration to associative items array keys (#3400)
Browse files Browse the repository at this point in the history
* [FEATURE] Add migration to associative items array keys

Fixes: #3399

* [FEATURE] Add migration to associative items array keys

Fixes: #3399

* [FEATURE] Add migration to associative items array keys

Fixes: #3399

* [FEATURE] Add migration to associative items array keys

Fixes: #3399
  • Loading branch information
nhovratov authored Jun 22, 2023
1 parent 17b3e58 commit daa9a63
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v12\v3\tca;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\String_;
use Ssch\TYPO3Rector\Helper\TcaHelperTrait;
use Ssch\TYPO3Rector\Rector\Tca\AbstractTcaRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.3/Feature-99739-AssociativeArrayKeysForTCAItems.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v3\tca\MigrateItemsIndexedKeysToAssociativeRector\MigrateItemsIndexedKeysToAssociativeRectorTest
*/
final class MigrateItemsIndexedKeysToAssociativeRector extends AbstractTcaRector
{
use TcaHelperTrait;

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrates indexed item array keys to associative for type select, radio and check', [new CodeSample(
<<<'CODE_SAMPLE'
'columns' => [
'aColumn' => [
'config' => [
'type' => 'select',
'renderType' => 'selectCheckBox',
'items' => [
['My label', 0, 'my-icon', 'group1', 'My Description'],
['My label 1', 1, 'my-icon', 'group1', 'My Description'],
['My label 2', 2, 'my-icon', 'group1', 'My Description'],
],
],
],
],
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
'columns' => [
'aColumn' => [
'config' => [
'type' => 'select',
'renderType' => 'selectCheckBox',
'items' => [
['label' => 'My label', 'value' => 0, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
['label' => 'My label 1', 'value' => 1, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
['label' => 'My label 2', 'value' => 2, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
]
],
],
],
CODE_SAMPLE
)]);
}

protected function refactorColumn(Expr $columnName, Expr $columnTca): void
{
$configArray = $this->extractSubArrayByKey($columnTca, self::CONFIG);
if (!$configArray instanceof Array_) {
return;
}

if (
!$this->isConfigType($configArray, 'select')
&& !$this->isConfigType($configArray, 'radio')
&& !$this->isConfigType($configArray, 'check')
) {
return;
}


$exprArrayItemToChange = $this->extractArrayItemByKey($configArray, 'items');
if (!$exprArrayItemToChange instanceof ArrayItem) {
return;
}
foreach ($exprArrayItemToChange->value->items as $exprArrayItem) {
if (array_key_exists(0, $exprArrayItem->value->items)) {
$exprArrayItem->value->items[0]->key = new String_('label');
$this->hasAstBeenChanged = true;
}
if (!$this->isConfigType($configArray, 'check') && array_key_exists(1, $exprArrayItem->value->items)) {
$exprArrayItem->value->items[1]->key = new String_('value');
$this->hasAstBeenChanged = true;
}
if ($this->isConfigType($configArray, 'select')) {
if (array_key_exists(2, $exprArrayItem->value->items)) {
$exprArrayItem->value->items[2]->key = new String_('icon');
$this->hasAstBeenChanged = true;
}
if (array_key_exists(3, $exprArrayItem->value->items)) {
$exprArrayItem->value->items[3]->key = new String_('group');
$this->hasAstBeenChanged = true;
}
if (array_key_exists(4, $exprArrayItem->value->items)) {
$exprArrayItem->value->items[4]->key = new String_('description');
$this->hasAstBeenChanged = true;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\tca\MigrateItemsIndexedKeysToAssociativeRector\Fixture;

return [
'ctrl' => [],
'columns' => [
'aColumn' => [
'config' => [
'type' => 'select',
'renderType' => 'selectCheckBox',
'items' => [
['My label', 0, 'my-icon', 'group1', 'My Description'],
['My label 1', 1, 'my-icon', 'group1', 'My Description'],
['My label 2', 2, 'my-icon', 'group1', 'My Description'],
['label' => 'My label 3', 'value' => 3, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
],
],
],
'bColumn' => [
'config' => [
'type' => 'radio',
'items' => [
['My label', 0],
['My label 1', 1],
['My label 2', 2],
['label' => 'My label 3', 'value' => 3],
],
],
],
'cColumn' => [
'config' => [
'type' => 'check',
'items' => [
[0 => 'My label', 'invertStateDisplay' => 1],
['label' => 'My label 2', 'invertStateDisplay' => 1],
],
],
],
],
];

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateItemsIndexedKeysToAssociativeRector\Fixture;

return [
'ctrl' => [],
'columns' => [
'aColumn' => [
'config' => [
'type' => 'select',
'renderType' => 'selectCheckBox',
'items' => [
['label' => 'My label', 'value' => 0, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
['label' => 'My label 1', 'value' => 1, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
['label' => 'My label 2', 'value' => 2, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
['label' => 'My label 3', 'value' => 3, 'icon' => 'my-icon', 'group' => 'group1', 'description' => 'My Description'],
],
],
],
'bColumn' => [
'config' => [
'type' => 'radio',
'items' => [
['label' => 'My label', 'value' => 0],
['label' => 'My label 1', 'value' => 1],
['label' => 'My label 2', 'value' => 2],
['label' => 'My label 3', 'value' => 3],
],
],
],
'cColumn' => [
'config' => [
'type' => 'check',
'items' => [
['label' => 'My label', 'invertStateDisplay' => 1],
['label' => 'My label 2', 'invertStateDisplay' => 1],
],
],
],
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v12\v3\tca\MigrateItemsIndexedKeysToAssociativeRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigrateItemsIndexedKeysToAssociativeRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->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\Rector\v12\v3\tca\MigrateItemsIndexedKeysToAssociativeRector;

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

0 comments on commit daa9a63

Please sign in to comment.