diff --git a/config/v12/flexform-120.php b/config/v12/flexform-120.php index 9bca4ca0d..77517fd50 100644 --- a/config/v12/flexform-120.php +++ b/config/v12/flexform-120.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Ssch\TYPO3Rector\Rector\v12\v0\flexform\MigrateEmailFlagToEmailTypeFlexFormRector; use Ssch\TYPO3Rector\Rector\v12\v0\flexform\MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector; use Ssch\TYPO3Rector\Rector\v12\v0\flexform\MigrateInternalTypeFolderToTypeFolderFlexFormRector; use Ssch\TYPO3Rector\Rector\v12\v0\flexform\MigrateNullFlagFlexFormRector; @@ -12,6 +13,7 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->import(__DIR__ . '/../config.php'); + $rectorConfig->rule(MigrateEmailFlagToEmailTypeFlexFormRector::class); $rectorConfig->rule(MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector::class); $rectorConfig->rule(MigrateInternalTypeFolderToTypeFolderFlexFormRector::class); $rectorConfig->rule(MigrateNullFlagFlexFormRector::class); diff --git a/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php new file mode 100644 index 000000000..5aa10ce09 --- /dev/null +++ b/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php @@ -0,0 +1,139 @@ + $elements */ + $elements = $xpath->query('//config'); + + if ($elements->count() === 0) { + return false; + } + + foreach ($elements as $element) { + $this->refactorColumn($domDocument, $element); + } + + return $this->domDocumentHasBeenChanged; + } + + /** + * @codeCoverageIgnore + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Migrate email flag to email type', [new CodeSample( + <<<'CODE_SAMPLE' + + + aTitle + array + + + + + input + trim,email + 255 + + + + + +CODE_SAMPLE + , + <<<'CODE_SAMPLE' + + + aTitle + array + + + + + email + + + + + +CODE_SAMPLE + )]); + } + + private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + { + if (! $configElement instanceof DOMElement) { + return; + } + + if (! $this->isConfigType($configElement, 'input')) { + return; + } + + if (! $this->hasKey($configElement, 'eval')) { + return; + } + + $evalDomElement = $this->extractDomElementByKey($configElement, 'eval'); + if (! $evalDomElement instanceof DOMElement) { + return; + } + + $evalListValue = $evalDomElement->nodeValue; + if (! is_string($evalListValue)) { + return; + } + + if (! StringUtility::inList($evalListValue, 'email')) { + return; + } + + // Set the TCA type to "email" + $this->changeTcaType($domDocument, $configElement, 'email'); + + $this->removeChildElementFromDomElementByKey($configElement, 'max'); + + $evalList = ArrayUtility::trimExplode(',', $evalListValue, true); + + // Remove "null" and "trim" from $evalList + $evalList = array_filter($evalList, static fn (string $eval) => $eval !== 'email' && $eval !== 'trim'); + + if ($evalList !== []) { + // Write back filtered 'eval' + $evalDomElement->nodeValue = ''; + $evalDomElement->appendChild($domDocument->createTextNode(implode(',', $evalList))); + } elseif ($evalDomElement->parentNode instanceof DOMElement) { + // 'eval' is empty, remove whole configuration + $evalDomElement->parentNode->removeChild($evalDomElement); + } + + $this->domDocumentHasBeenChanged = true; + } +} diff --git a/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/Fixture/fixture.xml.inc b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/Fixture/fixture.xml.inc new file mode 100644 index 000000000..669f129f9 --- /dev/null +++ b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/Fixture/fixture.xml.inc @@ -0,0 +1,73 @@ + + + + + sheetTitle + array + + + + input + email,trim,unique + 1 + + + + + input + trim,unique + + + + + text + email,trim,unique + + + + + email + + + + + + + +----- + + + + + sheetTitle + array + + + + email + unique + 1 + + + + + input + trim,unique + + + + + text + email,trim,unique + + + + + email + + + + + + + diff --git a/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/MigrateEmailFlagToEmailTypeFlexFormRectorTest.php b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/MigrateEmailFlagToEmailTypeFlexFormRectorTest.php new file mode 100644 index 000000000..fbf986782 --- /dev/null +++ b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/MigrateEmailFlagToEmailTypeFlexFormRectorTest.php @@ -0,0 +1,32 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.xml.inc'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/config/configured_rule.php b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/config/configured_rule.php new file mode 100644 index 000000000..fe194f865 --- /dev/null +++ b/tests/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../../config/config_test.php'); + $rectorConfig->rule(MigrateEmailFlagToEmailTypeFlexFormRector::class); +};