-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add MigrateRequiredFlagFlexFormRector
Resolves: #3363
- Loading branch information
1 parent
219f61e
commit 1d4e4e7
Showing
7 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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 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/Deprecation-97035-RequiredOptionInEvalKeyword.html | ||
* @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 | ||
{ | ||
use FlexFormHelperTrait; | ||
|
||
private bool $domDocumentHasBeenChanged = false; | ||
|
||
public function transform(DOMDocument $domDocument): bool | ||
{ | ||
$xpath = new DOMXPath($domDocument); | ||
|
||
/** @var DOMNodeList<DOMElement> $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 required flag', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
<T3DataStructure> | ||
<ROOT> | ||
<sheetTitle>aTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<some_column> | ||
<title>foo</title> | ||
<config> | ||
<eval>trim,required</eval> | ||
</config> | ||
</some_column> | ||
</el> | ||
</ROOT> | ||
</T3DataStructure> | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
<T3DataStructure> | ||
<ROOT> | ||
<sheetTitle>aTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<some_column> | ||
<title>foo</title> | ||
<config> | ||
<eval>trim</eval> | ||
<required>1</required> | ||
</config> | ||
</some_column> | ||
</el> | ||
</ROOT> | ||
</T3DataStructure> | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void | ||
{ | ||
if (! $configElement instanceof DOMElement) { | ||
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, 'required')) { | ||
return; | ||
} | ||
|
||
$evalList = ArrayUtility::trimExplode(',', $evalListValue, true); | ||
|
||
// Remove "required" from $evalList | ||
$evalList = array_filter($evalList, static fn (string $eval) => $eval !== 'required'); | ||
|
||
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); | ||
} | ||
|
||
$requiredDomElement = $this->extractDomElementByKey($configElement, 'required'); | ||
if (! $requiredDomElement instanceof DOMElement) { | ||
$configElement->appendChild($domDocument->createElement('required', '1')); | ||
} | ||
|
||
$this->domDocumentHasBeenChanged = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector/Fixture/fixture.xml.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<T3DataStructure> | ||
<sheets> | ||
<sDEF> | ||
<ROOT> | ||
<sheetTitle>sheetTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<settings.fieldContainsEvalRequired> | ||
<config> | ||
<type>input</type> | ||
<eval>required</eval> | ||
</config> | ||
</settings.fieldContainsEvalRequired> | ||
<settings.fieldContainsEvalTrimRequired> | ||
<config> | ||
<type>input</type> | ||
<eval>trim,required</eval> | ||
</config> | ||
</settings.fieldContainsEvalTrimRequired> | ||
<settings.fieldDoesNotContainEvalWithRequire> | ||
<config> | ||
<type>input</type> | ||
<eval>trim</eval> | ||
</config> | ||
</settings.fieldDoesNotContainEvalWithRequire> | ||
<settings.fieldDoesNotContainEval> | ||
<config> | ||
<type>input</type> | ||
</config> | ||
</settings.fieldDoesNotContainEval> | ||
</el> | ||
</ROOT> | ||
</sDEF> | ||
</sheets> | ||
</T3DataStructure> | ||
----- | ||
<T3DataStructure> | ||
<sheets> | ||
<sDEF> | ||
<ROOT> | ||
<sheetTitle>sheetTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<settings.fieldContainsEvalRequired> | ||
<config> | ||
<type>input</type> | ||
<required>1</required></config> | ||
</settings.fieldContainsEvalRequired> | ||
<settings.fieldContainsEvalTrimRequired> | ||
<config> | ||
<type>input</type> | ||
<eval>trim</eval> | ||
<required>1</required></config> | ||
</settings.fieldContainsEvalTrimRequired> | ||
<settings.fieldDoesNotContainEvalWithRequire> | ||
<config> | ||
<type>input</type> | ||
<eval>trim</eval> | ||
</config> | ||
</settings.fieldDoesNotContainEvalWithRequire> | ||
<settings.fieldDoesNotContainEval> | ||
<config> | ||
<type>input</type> | ||
</config> | ||
</settings.fieldDoesNotContainEval> | ||
</el> | ||
</ROOT> | ||
</sDEF> | ||
</sheets> | ||
</T3DataStructure> |
32 changes: 32 additions & 0 deletions
32
...2/v0/flexform/MigrateRequiredFlagFlexFormRector/MigrateRequiredFlagFlexFormRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateRequiredFlagFlexFormRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MigrateRequiredFlagFlexFormRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.xml.inc'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rector/v12/v0/flexform/MigrateRequiredFlagFlexFormRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Ssch\TYPO3Rector\Rector\v12\v0\flexform\MigrateRequiredFlagFlexFormRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(MigrateRequiredFlagFlexFormRector::class); | ||
}; |