From f3f8d2a0f10d2b921f37ab3807975af6708a0552 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Tue, 9 Jan 2024 11:51:04 +0100 Subject: [PATCH] [TASK] Introduce AbstractFlexFormRector (#3994) --- .../Rector/v7/v6/RenderTypeFlexFormRector.php | 175 +++++++++-------- .../FlexForm/AbstractFlexFormRector.php | 39 ++++ ...rateEmailFlagToEmailTypeFlexFormRector.php | 32 +-- ...ntAndDouble2ToTypeNumberFlexFormRector.php | 34 +--- ...alTypeFolderToTypeFolderFlexFormRector.php | 30 +-- .../MigrateNullFlagFlexFormRector.php | 33 +--- ...edPasswordToPasswordTypeFlexFormRector.php | 34 +--- ...peColorpickerToTypeColorFlexFormRector.php | 32 +-- .../MigrateRequiredFlagFlexFormRector.php | 32 +-- ...IndexedArrayKeysForFlexFormItemsRector.php | 32 +-- .../Fixture/flexforms.xml.inc | 183 +++++++++++++++++- .../Fixture/flexforms_no_change.xml.inc | 2 +- .../Fixture/sitemap.xml.inc | 25 --- .../__Major__/__Minor__/flexform/__Name__.php | 28 +-- 14 files changed, 358 insertions(+), 353 deletions(-) create mode 100644 src/Rector/FlexForm/AbstractFlexFormRector.php delete mode 100644 tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/sitemap.xml.inc diff --git a/src/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector.php b/src/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector.php index 48ea49f36..f40b31ca4 100644 --- a/src/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector.php +++ b/src/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector.php @@ -4,12 +4,10 @@ namespace Ssch\TYPO3Rector\FileProcessor\FlexForms\Rector\v7\v6; -use DOMDocument; use DOMElement; -use DOMNode; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; +use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -17,97 +15,114 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/7.6/Deprecation-69822-DeprecateSelectFieldTca.html * @see \Ssch\TYPO3Rector\Tests\FileProcessor\FlexForms\Rector\v7\v6\RenderTypeFlexFormRector\RenderTypeFlexFormRectorTest */ -final class RenderTypeFlexFormRector implements FlexFormRectorInterface +final class RenderTypeFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//TCEforms/config'); + use FlexFormHelperTrait; - $hasChanged = false; - foreach ($elements as $element) { - $type = $element->getElementsByTagName('type') - ->item(0); + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Add renderType node in FlexForm', [ + new CodeSample( + <<<'CODE_SAMPLE' + + + aTitle + array + + + + + select + + + Label + + + + + + + +CODE_SAMPLE + , + <<<'CODE_SAMPLE' + + + aTitle + array + + + + + select + selectSingle + + + Label + + + + + + + +CODE_SAMPLE + ), + ]); + } - if (! $type instanceof DOMElement) { - continue; - } + protected function refactorColumn(?DOMElement $configElement): void + { + if (! $configElement instanceof DOMElement) { + return; + } - if ($type->textContent !== 'select') { - continue; - } + if (! $this->isConfigType($configElement, 'select')) { + return; + } - $renderType = $element->getElementsByTagName('renderType') - ->item(0); + // Do not handle field where the render type is set. + if ($this->hasRenderType($configElement)) { + return; + } - // If renderType is already set, migration can be skipped - if ($renderType instanceof DOMElement) { - continue; + $renderModeDomElement = $this->extractDomElementByKey($configElement, 'renderMode'); + if ($renderModeDomElement instanceof DOMElement) { + $renderMode = $renderModeDomElement->nodeValue; + switch ($renderMode) { + case 'tree': + $renderTypeName = 'selectTree'; + break; + case 'singlebox': + $renderTypeName = 'selectSingleBox'; + break; + case 'checkbox': + $renderTypeName = 'selectCheckBox'; + break; + default: + $renderTypeName = null; } - $renderMode = $element->getElementsByTagName('renderMode') - ->item(0); - $size = $element->getElementsByTagName('size') - ->item(0); + if ($renderTypeName !== null) { + $configElement->appendChild($this->domDocument->createElement('renderType', $renderTypeName)); - $renderTypeName = 'selectSingle'; - $insertBefore = $type; - - if ($renderMode instanceof DOMNode) { - $renderTypeName = 'selectTree'; - $insertBefore = $renderMode; - } elseif ($size instanceof DOMNode && (int) $size->textContent > 1) { - // Could be also selectCheckBox. This is a sensitive default - $renderTypeName = 'selectMultipleSideBySide'; + $this->domDocumentHasBeenChanged = true; } - $renderType = $domDocument->createElement('renderType', $renderTypeName); + return; + } - if (! $insertBefore->parentNode instanceof DOMNode) { - continue; - } + $maxItemsDomElement = $this->extractDomElementByKey($configElement, 'maxitems'); + if ($maxItemsDomElement instanceof DOMElement) { + $maxItems = (int) $maxItemsDomElement->nodeValue; - if (! $insertBefore->nextSibling instanceof DOMNode) { - continue; - } - - $hasChanged = true; - $insertBefore->parentNode->insertBefore($renderType, $insertBefore->nextSibling); - $insertBefore->parentNode->insertBefore($domDocument->createTextNode("\n"), $insertBefore->nextSibling); + $renderTypeName = $maxItems <= 1 ? 'selectSingle' : 'selectMultipleSideBySide'; + } else { + $renderTypeName = 'selectSingle'; } - return $hasChanged; - } + $configElement->appendChild($this->domDocument->createElement('renderType', $renderTypeName)); - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition('Add renderType node in Flexforms xml', [ - new CodeSample( - <<<'CODE_SAMPLE' -select - - - - LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.no-constraint - - - -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -select -selectSingle - - - - LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.no-constraint - - - -CODE_SAMPLE - ), - ]); + $this->domDocumentHasBeenChanged = true; } } diff --git a/src/Rector/FlexForm/AbstractFlexFormRector.php b/src/Rector/FlexForm/AbstractFlexFormRector.php new file mode 100644 index 000000000..ad21d1f0c --- /dev/null +++ b/src/Rector/FlexForm/AbstractFlexFormRector.php @@ -0,0 +1,39 @@ +domDocument = $domDocument; + + $xpath = new DOMXPath($domDocument); + + /** @var DOMNodeList $elements */ + $elements = $xpath->query('//config'); + + if ($elements->count() === 0) { + return false; + } + + foreach ($elements as $element) { + $this->refactorColumn($element); + } + + return $this->domDocumentHasBeenChanged; + } + + abstract protected function refactorColumn(?DOMElement $configElement): void; +} diff --git a/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php index 5aa10ce09..0304c505d 100644 --- a/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateEmailFlagToEmailTypeFlexFormRector.php @@ -4,14 +4,12 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; use Ssch\TYPO3Rector\Helper\StringUtility; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,30 +17,10 @@ * @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\flexform\MigrateEmailFlagToEmailTypeFlexFormRector\MigrateEmailFlagToEmailTypeFlexFormRectorTest */ -final class MigrateEmailFlagToEmailTypeFlexFormRector implements FlexFormRectorInterface +final class MigrateEmailFlagToEmailTypeFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -87,7 +65,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -116,7 +94,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle } // Set the TCA type to "email" - $this->changeTcaType($domDocument, $configElement, 'email'); + $this->changeTcaType($this->domDocument, $configElement, 'email'); $this->removeChildElementFromDomElementByKey($configElement, 'max'); @@ -128,7 +106,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle if ($evalList !== []) { // Write back filtered 'eval' $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode(implode(',', $evalList))); + $evalDomElement->appendChild($this->domDocument->createTextNode(implode(',', $evalList))); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); diff --git a/src/Rector/v12/v0/flexform/MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector.php index a22bf71ed..b4feaad82 100644 --- a/src/Rector/v12/v0/flexform/MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector.php @@ -4,14 +4,12 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; use Ssch\TYPO3Rector\Helper\StringUtility; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,7 +17,7 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97193-NewTCATypeNumber.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector\MigrateEvalIntAndDouble2ToTypeNumberFlexFormRectorTest */ -final class MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector implements FlexFormRectorInterface +final class MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; @@ -33,26 +31,6 @@ final class MigrateEvalIntAndDouble2ToTypeNumberFlexFormRector implements FlexFo */ private const DOUBLE2 = 'double2'; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -94,7 +72,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -135,16 +113,16 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle if ($evalList !== []) { // Write back filtered 'eval' $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode(implode(',', $evalList))); + $evalDomElement->appendChild($this->domDocument->createTextNode(implode(',', $evalList))); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); } - $this->changeTcaType($domDocument, $configElement, 'number'); + $this->changeTcaType($this->domDocument, $configElement, 'number'); if (StringUtility::inList($evalListValue, self::DOUBLE2)) { - $configElement->appendChild($domDocument->createElement('format', 'decimal')); + $configElement->appendChild($this->domDocument->createElement('format', 'decimal')); } $this->domDocumentHasBeenChanged = true; diff --git a/src/Rector/v12/v0/flexform/MigrateInternalTypeFolderToTypeFolderFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateInternalTypeFolderToTypeFolderFlexFormRector.php index e011e2056..9244545a7 100644 --- a/src/Rector/v12/v0/flexform/MigrateInternalTypeFolderToTypeFolderFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateInternalTypeFolderToTypeFolderFlexFormRector.php @@ -4,12 +4,10 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -17,30 +15,10 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-96983-TCATypeFolder.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateInternalTypeFolderToTypeFolderFlexFormRector\MigrateInternalTypeFolderToTypeFolderFlexFormRectorTest */ -final class MigrateInternalTypeFolderToTypeFolderFlexFormRector implements FlexFormRectorInterface +final class MigrateInternalTypeFolderToTypeFolderFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -84,7 +62,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -105,7 +83,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle } if ($internalTypeDomElement->nodeValue === 'folder') { - $this->changeTcaType($domDocument, $configElement, 'folder'); + $this->changeTcaType($this->domDocument, $configElement, 'folder'); } $this->domDocumentHasBeenChanged = true; diff --git a/src/Rector/v12/v0/flexform/MigrateNullFlagFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateNullFlagFlexFormRector.php index a6f6bd300..d7aac04d1 100644 --- a/src/Rector/v12/v0/flexform/MigrateNullFlagFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateNullFlagFlexFormRector.php @@ -4,14 +4,12 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; use Ssch\TYPO3Rector\Helper\StringUtility; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -20,7 +18,7 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97384-TCAOptionNullable.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateNullFlagFlexFormRector\MigrateNullFlagFlexFormRectorTest */ -final class MigrateNullFlagFlexFormRector implements FlexFormRectorInterface +final class MigrateNullFlagFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; @@ -29,26 +27,6 @@ final class MigrateNullFlagFlexFormRector implements FlexFormRectorInterface */ private const NULL = 'null'; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -71,7 +49,6 @@ public function getRuleDefinition(): RuleDefinition CODE_SAMPLE - , <<<'CODE_SAMPLE' @@ -92,7 +69,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -124,7 +101,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle if ($evalList !== []) { // Write back filtered 'eval' $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode(implode(',', $evalList))); + $evalDomElement->appendChild($this->domDocument->createTextNode(implode(',', $evalList))); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); @@ -132,7 +109,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle $nullableDomElement = $this->extractDomElementByKey($configElement, 'nullable'); if (! $nullableDomElement instanceof DOMElement) { - $configElement->appendChild($domDocument->createElement('nullable', '1')); + $configElement->appendChild($this->domDocument->createElement('nullable', '1')); } $this->domDocumentHasBeenChanged = true; diff --git a/src/Rector/v12/v0/flexform/MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector.php b/src/Rector/v12/v0/flexform/MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector.php index e9d21c749..cb24c97b8 100644 --- a/src/Rector/v12/v0/flexform/MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector.php @@ -4,14 +4,12 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; use Ssch\TYPO3Rector\Helper\StringUtility; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,7 +17,7 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97159-NewTCATypeLink.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector\MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRectorTest */ -final class MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector implements FlexFormRectorInterface +final class MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; @@ -33,26 +31,6 @@ final class MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector impleme */ private const SALTED_PASSWORD = 'saltedPassword'; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -110,7 +88,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -141,7 +119,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle } // Set the TCA type to "password" - $this->changeTcaType($domDocument, $configElement, self::PASSWORD); + $this->changeTcaType($this->domDocument, $configElement, self::PASSWORD); // Remove 'max' and 'search' config $this->removeChildElementFromDomElementByKey($configElement, 'max'); @@ -151,13 +129,13 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle // Disable password hashing, if eval=password is used standalone if (in_array('password', $evalList, true) && ! in_array('saltedPassword', $evalList, true)) { - $configElement->appendChild($domDocument->createElement('hashed', '0')); + $configElement->appendChild($this->domDocument->createElement('hashed', '0')); } if (in_array('null', $evalList, true)) { // Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=password $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode('null')); + $evalDomElement->appendChild($this->domDocument->createTextNode('null')); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); diff --git a/src/Rector/v12/v0/flexform/MigrateRenderTypeColorpickerToTypeColorFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateRenderTypeColorpickerToTypeColorFlexFormRector.php index 9a30799ab..6e60293b8 100644 --- a/src/Rector/v12/v0/flexform/MigrateRenderTypeColorpickerToTypeColorFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateRenderTypeColorpickerToTypeColorFlexFormRector.php @@ -4,13 +4,11 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,30 +16,10 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97271-NewTCATypeColor.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateRenderTypeColorpickerToTypeColorFlexFormRector\MigrateRenderTypeColorpickerToTypeColorFlexFormRectorTest */ -final class MigrateRenderTypeColorpickerToTypeColorFlexFormRector implements FlexFormRectorInterface +final class MigrateRenderTypeColorpickerToTypeColorFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -107,7 +85,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -122,7 +100,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle } // Set the TCA type to "color" - $this->changeTcaType($domDocument, $configElement, 'color'); + $this->changeTcaType($this->domDocument, $configElement, 'color'); // Remove 'max' and 'renderType' config $this->removeChildElementFromDomElementByKey($configElement, 'max'); @@ -143,7 +121,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle if (in_array('null', $evalList, true)) { // Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=color $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode('null')); + $evalDomElement->appendChild($this->domDocument->createTextNode('null')); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); diff --git a/src/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector.php b/src/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector.php index c276119d1..8dacca84d 100644 --- a/src/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector.php +++ b/src/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector.php @@ -4,14 +4,12 @@ namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\ArrayUtility; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; use Ssch\TYPO3Rector\Helper\StringUtility; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -20,30 +18,10 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97035-UtilizeRequiredDirectlyInTCAFieldConfiguration.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateRequiredFlagFlexFormRector\MigrateRequiredFlagFlexFormRectorTest */ -final class MigrateRequiredFlagFlexFormRector implements FlexFormRectorInterface +final class MigrateRequiredFlagFlexFormRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -87,7 +65,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; @@ -119,7 +97,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle if ($evalList !== []) { // Write back filtered 'eval' $evalDomElement->nodeValue = ''; - $evalDomElement->appendChild($domDocument->createTextNode(implode(',', $evalList))); + $evalDomElement->appendChild($this->domDocument->createTextNode(implode(',', $evalList))); } elseif ($evalDomElement->parentNode instanceof DOMElement) { // 'eval' is empty, remove whole configuration $evalDomElement->parentNode->removeChild($evalDomElement); @@ -127,7 +105,7 @@ private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configEle $requiredDomElement = $this->extractDomElementByKey($configElement, 'required'); if (! $requiredDomElement instanceof DOMElement) { - $configElement->appendChild($domDocument->createElement('required', '1')); + $configElement->appendChild($this->domDocument->createElement('required', '1')); } $this->domDocumentHasBeenChanged = true; diff --git a/src/Rector/v12/v3/flexform/MigrateItemsToIndexedArrayKeysForFlexFormItemsRector.php b/src/Rector/v12/v3/flexform/MigrateItemsToIndexedArrayKeysForFlexFormItemsRector.php index 83ab8af82..f35d7af58 100644 --- a/src/Rector/v12/v3/flexform/MigrateItemsToIndexedArrayKeysForFlexFormItemsRector.php +++ b/src/Rector/v12/v3/flexform/MigrateItemsToIndexedArrayKeysForFlexFormItemsRector.php @@ -4,12 +4,10 @@ namespace Ssch\TYPO3Rector\Rector\v12\v3\flexform; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -20,34 +18,10 @@ * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.3/Feature-99739-AssociativeArrayKeysForTCAItems.html * @see \Ssch\TYPO3Rector\Tests\Rector\v12\v3\flexform\MigrateItemsToIndexedArrayKeysForFlexFormItemsRector\MigrateItemsToIndexedArrayKeysForFlexFormItemsRectorTest */ -final class MigrateItemsToIndexedArrayKeysForFlexFormItemsRector implements FlexFormRectorInterface +final class MigrateItemsToIndexedArrayKeysForFlexFormItemsRector extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private DOMDocument $domDocument; - - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $this->domDocument = $domDocument; - - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -103,7 +77,7 @@ public function getRuleDefinition(): RuleDefinition ); } - private function refactorColumn(?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return; diff --git a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms.xml.inc b/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms.xml.inc index 08d949a48..b34df5f9a 100644 --- a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms.xml.inc +++ b/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms.xml.inc @@ -29,6 +29,96 @@ + + + + select + tree + + + + + + + select + singlebox + + + + + + + select + checkbox + + + + + + + select + unknown + + + + + + + select + + + + + + + select + 0 + + + + + + + select + 1 + + + + + + + select + 2 + + + + + + + select + tree + 1 + + + + + + + select + singlebox + 1 + + + + + + + select + checkbox + 1 + + + @@ -49,7 +139,6 @@ select -selectSingle Label @@ -64,9 +153,99 @@ 2 - + selectSingle + + + + select + tree + selectTree + + + + + + select + singlebox + selectSingleBox + + + + + + select + checkbox + selectCheckBox + + + + + + select + unknown + + + + + + + select + selectSingle + + + + + + select + 0 + selectSingle + + + + + + select + 1 + selectSingle + + + + + + select + 2 + selectMultipleSideBySide + + + + + + select + tree + 1 + selectTree + + + + + + select + singlebox + 1 + selectSingleBox + + + + + + select + checkbox + 1 + selectCheckBox + + diff --git a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms_no_change.xml.inc b/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms_no_change.xml.inc index 72212aa25..184534b64 100644 --- a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms_no_change.xml.inc +++ b/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/flexforms_no_change.xml.inc @@ -16,7 +16,7 @@ Label - + Label 1 diff --git a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/sitemap.xml.inc b/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/sitemap.xml.inc deleted file mode 100644 index d1e78d33d..000000000 --- a/tests/FileProcessor/FlexForms/Rector/v7/v6/RenderTypeFlexFormRector/Fixture/sitemap.xml.inc +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - {item.loc} - - - {settings.list.rss.channel.title} - {settings.list.rss.channel.language} - - {item.data.datetime} - {item.data.title} - - - - - diff --git a/utils/generator/templates/src/Rector/__Major__/__Minor__/flexform/__Name__.php b/utils/generator/templates/src/Rector/__Major__/__Minor__/flexform/__Name__.php index 0b64d30b3..b1e649255 100644 --- a/utils/generator/templates/src/Rector/__Major__/__Minor__/flexform/__Name__.php +++ b/utils/generator/templates/src/Rector/__Major__/__Minor__/flexform/__Name__.php @@ -4,12 +4,10 @@ namespace Ssch\TYPO3Rector\Rector\__Major__\__Minor__\__Type__; -use DOMDocument; use DOMElement; -use DOMNodeList; -use DOMXPath; use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; +use Ssch\TYPO3Rector\Rector\FlexForm\AbstractFlexFormRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -17,30 +15,10 @@ * @changelog __Changelog_Url__ * @see \Ssch\TYPO3Rector\Tests\Rector\__Major__\__Minor__\__Type__\__Test_Directory__\__Name__Test */ -final class __Name__ implements FlexFormRectorInterface +final class __Name__ extends AbstractFlexFormRector implements FlexFormRectorInterface { use FlexFormHelperTrait; - private bool $domDocumentHasBeenChanged = false; - - public function transform(DOMDocument $domDocument): bool - { - $xpath = new DOMXPath($domDocument); - - /** @var DOMNodeList $elements */ - $elements = $xpath->query('//config'); - - if ($elements->count() === 0) { - return false; - } - - foreach ($elements as $element) { - $this->refactorColumn($domDocument, $element); - } - - return $this->domDocumentHasBeenChanged; - } - /** * @codeCoverageIgnore */ @@ -73,7 +51,7 @@ public function getRuleDefinition(): RuleDefinition )]); } - private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void + protected function refactorColumn(?DOMElement $configElement): void { if (! $configElement instanceof DOMElement) { return;