From daa9a6367e05ba46b8cc2f3025d52225d3239c19 Mon Sep 17 00:00:00 2001 From: Nikita Hovratov Date: Thu, 22 Jun 2023 16:38:36 +0200 Subject: [PATCH] [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 --- ...ateItemsIndexedKeysToAssociativeRector.php | 109 ++++++++++++++++++ .../Fixture/fixture.php.inc | 87 ++++++++++++++ ...temsIndexedKeysToAssociativeRectorTest.php | 32 +++++ .../config/configured_rule.php | 11 ++ 4 files changed, 239 insertions(+) create mode 100644 src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.php create mode 100644 tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/MigrateItemsIndexedKeysToAssociativeRectorTest.php create mode 100644 tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/config/configured_rule.php diff --git a/src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.php b/src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.php new file mode 100644 index 000000000..38fe006a8 --- /dev/null +++ b/src/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector.php @@ -0,0 +1,109 @@ + [ + '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; + } + } + } + } +} diff --git a/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/Fixture/fixture.php.inc b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/Fixture/fixture.php.inc new file mode 100644 index 000000000..28dd60a4e --- /dev/null +++ b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/Fixture/fixture.php.inc @@ -0,0 +1,87 @@ + [], + '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], + ], + ], + ], + ], +]; + +?> +----- + [], + '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], + ], + ], + ], + ], +]; + +?> diff --git a/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/MigrateItemsIndexedKeysToAssociativeRectorTest.php b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/MigrateItemsIndexedKeysToAssociativeRectorTest.php new file mode 100644 index 000000000..346f94891 --- /dev/null +++ b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/MigrateItemsIndexedKeysToAssociativeRectorTest.php @@ -0,0 +1,32 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/config/configured_rule.php b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/config/configured_rule.php new file mode 100644 index 000000000..2186a9dec --- /dev/null +++ b/tests/Rector/v12/v3/tca/MigrateItemsIndexedKeysToAssociativeRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../../config/config_test.php'); + $rectorConfig->rule(MigrateItemsIndexedKeysToAssociativeRector::class); +};