Skip to content

Commit

Permalink
[BUGFIX] Add numbered key if missing for SimplifyCheckboxItemsTCARector
Browse files Browse the repository at this point in the history
Resolves: #4298
  • Loading branch information
simonschaufi committed Aug 29, 2024
1 parent 2236b9f commit 88cc8ea
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rules/TYPO311/v5/SimplifyCheckboxItemsTCARector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\LNumber;
use Ssch\TYPO3Rector\Rector\AbstractTcaRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -129,6 +130,18 @@ protected function refactorColumn(Expr $columnName, Expr $columnTca): void
return;
}

$itemsCount = count($firstItemsArray->items);
if ($itemsCount > 1) {
// we have an item without any keys (['label', 'value'])
foreach ($firstItemsArray->items as $i => $arrayItem) {
if ($arrayItem instanceof ArrayItem) {
// add a numbered key
$arrayItem->key = new LNumber($i);
$this->hasAstBeenChanged = true;
}
}
}

// Check if array key 0 has a label
$zeroKeyArrayItem = $this->extractArrayItemByKey($firstItemsArray, 0);
if ($zeroKeyArrayItem instanceof ArrayItem && ! $this->valueResolver->isValue($zeroKeyArrayItem->value, '')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\SimplifyCheckboxItemsTCARector\Fixture;

return [
'ctrl' => [],
'columns' => [
'col_name' => [
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'items' => [
[
'LLL:EXT:some_extension/Resources/Private/Language/locallang_db.xlf:some_label',
'',
],
],
],
],
],
];
?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v5\SimplifyCheckboxItemsTCARector\Fixture;

return [
'ctrl' => [],
'columns' => [
'col_name' => [
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'items' => [
[
0 => 'LLL:EXT:some_extension/Resources/Private/Language/locallang_db.xlf:some_label',
],
],
],
],
],
];
?>

0 comments on commit 88cc8ea

Please sign in to comment.