-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add migration to associative items array keys (#3400)
* [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
Showing
4 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.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,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; | ||
} | ||
} | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/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,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], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ateItemsIndexedKeysToAssociativeRector/MigrateItemsIndexedKeysToAssociativeRectorTest.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,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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...s/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/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\Rector\v12\v3\tca\MigrateItemsIndexedKeysToAssociativeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(MigrateItemsIndexedKeysToAssociativeRector::class); | ||
}; |