Skip to content

Commit

Permalink
[FEATURE] Add MigrateEmailFlagToEmailTypeFlexFormRector
Browse files Browse the repository at this point in the history
Resolves: #3362
  • Loading branch information
simonschaufi committed Jan 4, 2024
1 parent fc4a60b commit 2177354
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/v12/flexform-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?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/Feature-97013-NewTCATypeEmail.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigrateEmailFlagToEmailTypeFlexFormRector\MigrateEmailFlagToEmailTypeFlexFormRectorTest
*/
final class MigrateEmailFlagToEmailTypeFlexFormRector 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 email flag to email type', [new CodeSample(
<<<'CODE_SAMPLE'
<T3DataStructure>
<ROOT>
<sheetTitle>aTitle</sheetTitle>
<type>array</type>
<el>
<email_field>
<label>Email</label>
<config>
<type>input</type>
<eval>trim,email</eval>
<max>255</max>
</config>
</email_field>
</el>
</ROOT>
</T3DataStructure>
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<T3DataStructure>
<ROOT>
<sheetTitle>aTitle</sheetTitle>
<type>array</type>
<el>
<email_field>
<label>Email</label>
<config>
<type>email</type>
</config>
</email_field>
</el>
</ROOT>
</T3DataStructure>
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<sheetTitle>sheetTitle</sheetTitle>
<type>array</type>
<el>
<aColumn>
<config>
<type>input</type>
<eval>email,trim,unique</eval>
<required>1</required>
</config>
</aColumn>
<differentColumn>
<config>
<type>input</type>
<eval>trim,unique</eval>
</config>
</differentColumn>
<wrongTypeColumn>
<config>
<type>text</type>
<eval>email,trim,unique</eval>
</config>
</wrongTypeColumn>
<alreadyMigratedColumn>
<config>
<type>email</type>
</config>
</alreadyMigratedColumn>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
-----
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<sheetTitle>sheetTitle</sheetTitle>
<type>array</type>
<el>
<aColumn>
<config>
<type>email</type>
<eval>unique</eval>
<required>1</required>
</config>
</aColumn>
<differentColumn>
<config>
<type>input</type>
<eval>trim,unique</eval>
</config>
</differentColumn>
<wrongTypeColumn>
<config>
<type>text</type>
<eval>email,trim,unique</eval>
</config>
</wrongTypeColumn>
<alreadyMigratedColumn>
<config>
<type>email</type>
</config>
</alreadyMigratedColumn>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
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\MigrateEmailFlagToEmailTypeFlexFormRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigrateEmailFlagToEmailTypeFlexFormRectorTest 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';
}
}
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\MigrateEmailFlagToEmailTypeFlexFormRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php');
$rectorConfig->rule(MigrateEmailFlagToEmailTypeFlexFormRector::class);
};

0 comments on commit 2177354

Please sign in to comment.