-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
SimplifyCheckboxItemsTCARector.php
162 lines (145 loc) · 4.88 KB
/
SimplifyCheckboxItemsTCARector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO311\v5;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Scalar\Int_;
use Ssch\TYPO3Rector\Rector\AbstractTcaRector;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://review.typo3.org/c/Packages/TYPO3.CMS/+/72056
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v5\SimplifyCheckboxItemsTCARector\SimplifyCheckboxItemsTCARectorTest
*/
final class SimplifyCheckboxItemsTCARector extends AbstractTcaRector implements DocumentedRuleInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Simplify checkbox items TCA', [new CodeSample(
<<<'CODE_SAMPLE'
return [
'columns' => [
'enabled' => [
'label' => 'enabled',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'default' => 1,
'items' => [
[
0 => '',
1 => '',
],
],
],
],
'hidden' => [
'label' => 'hidden',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'default' => 0,
'items' => [
[
0 => '',
1 => '',
'invertStateDisplay' => true,
],
],
],
],
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return [
'columns' => [
'enabled' => [
'label' => 'enabled',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'default' => 1,
],
],
'hidden' => [
'label' => 'hidden',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'default' => 0,
'items' => [
[
0 => '',
'invertStateDisplay' => true,
],
],
],
],
],
];
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, 'check')) {
return;
}
if (! $this->configIsOfRenderType($configArray, 'checkboxToggle')) {
return;
}
if (! $this->hasKey($configArray, 'items')) {
return;
}
$itemsArrayItem = $this->extractArrayItemByKey($configArray, 'items');
if (! $itemsArrayItem instanceof ArrayItem) {
return;
}
/** @var Array_ $itemsArray */
$itemsArray = $itemsArrayItem->value;
$firstArrayItem = $itemsArray->items[0] ?? null;
if (! $firstArrayItem instanceof ArrayItem) {
return;
}
/** @var Array_ $firstItemsArray */
$firstItemsArray = $firstArrayItem->value;
// Check if 'invertStateDisplay' is set
if ($this->hasKey($firstItemsArray, 'invertStateDisplay')) {
// Remove array key 1
$this->removeArrayItemFromArrayByKey($firstItemsArray, 1);
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 Int_($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, '')) {
$this->removeArrayItemFromArrayByKey($firstItemsArray, 1);
return;
}
$labelKeyArrayItem = $this->extractArrayItemByKey($firstItemsArray, 'label');
if ($labelKeyArrayItem instanceof ArrayItem) {
// Skip migrated items which have a "label" key that comes from MigrateItemsIndexedKeysToAssociativeRector.
return;
}
// Remove the whole items array
$this->removeArrayItemFromArrayByKey($configArray, 'items');
}
}