-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
MigrateToEmailTypeRector.php
114 lines (95 loc) · 3.12 KB
/
MigrateToEmailTypeRector.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
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO312\v0;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\String_;
use Ssch\TYPO3Rector\Helper\ArrayUtility;
use Ssch\TYPO3Rector\Helper\StringUtility;
use Ssch\TYPO3Rector\Rector\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.0/Feature-97013-NewTCATypeEmail.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateToEmailTypeRector\MigrateToEmailTypeRectorTest
*/
final class MigrateToEmailTypeRector extends AbstractTcaRector
{
/**
* @var string
*/
private const EMAIL = 'email';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrates existing input TCA with eval email to new TCA type email', [new CodeSample(
<<<'CODE_SAMPLE'
return [
'columns' => [
'email_field' => [
'label' => 'Email',
'config' => [
'type' => 'input',
'eval' => 'trim,email',
'max' => 255,
],
],
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return [
'columns' => [
'email_field' => [
'label' => 'Email',
'config' => [
'type' => 'email',
],
],
],
];
CODE_SAMPLE
)]);
}
protected function refactorColumn(Expr $columnName, Expr $columnTca): void
{
$configArrayItem = $this->extractArrayItemByKey($columnTca, self::CONFIG);
if (! $configArrayItem instanceof ArrayItem) {
return;
}
$configArray = $configArrayItem->value;
if (! $configArray instanceof Array_) {
return;
}
if (! $this->isConfigType($configArray, 'input')) {
return;
}
if (! $this->hasKey($configArray, 'eval')) {
return;
}
$evalArrayItem = $this->extractArrayItemByKey($configArray, 'eval');
if (! $evalArrayItem instanceof ArrayItem) {
return;
}
$evalListValue = $this->valueResolver->getValue($evalArrayItem->value);
if (! is_string($evalListValue)) {
return;
}
if (! StringUtility::inList($evalListValue, self::EMAIL)) {
return;
}
$this->removeArrayItemFromArrayByKey($configArray, 'max');
$evalList = ArrayUtility::trimExplode(',', $evalListValue, true);
// Remove "email" and "trim" from $evalList
$evalList = array_filter($evalList, static fn (string $eval) => $eval !== self::EMAIL && $eval !== 'trim');
if ($evalList !== []) {
// Write back filtered 'eval'
$evalArrayItem->value = new String_(implode(',', $evalList));
} else {
$this->removeArrayItemFromArrayByKey($configArray, 'eval');
}
$this->changeTcaType($configArray, self::EMAIL);
$this->hasAstBeenChanged = true;
}
}