Skip to content

Commit

Permalink
[TASK] Add RemoveElementTceFormsRector (#3460)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschaufi committed Oct 9, 2023
1 parent ffb81ad commit 72b1f9c
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/v12/flexform-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\Rector\v12\v0\flexform\RemoveElementTceFormsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(RemoveElementTceFormsRector::class);
};
36 changes: 35 additions & 1 deletion docs/all_rectors_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 279 Rules Overview
# 280 Rules Overview

## AbstractMessageGetSeverityFluidRector

Expand Down Expand Up @@ -2983,6 +2983,40 @@ Removed dividers2tabs functionality

<br>

## RemoveElementTceFormsRector

Remove TCEForms key from all elements in data structure

- class: [`Ssch\TYPO3Rector\Rector\v12\v0\flexform\RemoveElementTceFormsRector`](../src/Rector/v12/v0/flexform/RemoveElementTceFormsRector.php)

```diff
<T3DataStructure>
<ROOT>
- <TCEforms>
- <sheetTitle>aTitle</sheetTitle>
- </TCEforms>
+ <sheetTitle>aTitle</sheetTitle>
<type>array</type>
<el>
<aFlexField>
- <TCEforms>
- <label>aFlexFieldLabel</label>
- <config>
- <type>input</type>
- </config>
- </TCEforms>
+ <label>aFlexFieldLabel</label>
+ <config>
+ <type>input</type>
+ </config>
</aFlexField>
</el>
</ROOT>
</T3DataStructure>
```

<br>

## RemoveEnableMultiSelectFilterTextfieldRector

Remove "enableMultiSelectFilterTextfield" => true as its default
Expand Down
104 changes: 104 additions & 0 deletions src/Rector/v12/v0/flexform/RemoveElementTceFormsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 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-97126-TCEformsRemovedInFlexForm.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\RemoveElementTceFormsRector\RemoveElementTceFormsRectorTest
*/
final class RemoveElementTceFormsRector implements FlexFormRectorInterface
{
public function transform(DOMDocument $domDocument): bool
{
$hasChanged = false;

// Create a DOMXPath object to query the document
$xpath = new DOMXPath($domDocument);

// Find all elements with a <TCEforms> parent and move their children to the parent level
$tceformsElements = $xpath->query('//TCEforms');

if (! $tceformsElements instanceof DOMNodeList) {
return false;
}

foreach ($tceformsElements as $tceformsElement) {
$parent = $tceformsElement->parentNode;

if (! $parent instanceof DOMElement) {
return false;
}

// Move the children of <TCEforms> to the parent
foreach ($tceformsElement->childNodes as $childNode) {
if (! $childNode instanceof DOMElement) {
continue;
}

$parent->insertBefore($childNode->cloneNode(true), $tceformsElement);
}

// Remove the <TCEforms> element
$parent->removeChild($tceformsElement);
$hasChanged = true;
}

return $hasChanged;
}

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove TCEForms key from all elements in data structure', [new CodeSample(
<<<'CODE_SAMPLE'
<T3DataStructure>
<ROOT>
<TCEforms>
<sheetTitle>aTitle</sheetTitle>
</TCEforms>
<type>array</type>
<el>
<aFlexField>
<TCEforms>
<label>aFlexFieldLabel</label>
<config>
<type>input</type>
</config>
</TCEforms>
</aFlexField>
</el>
</ROOT>
</T3DataStructure>
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<T3DataStructure>
<ROOT>
<sheetTitle>aTitle</sheetTitle>
<type>array</type>
<el>
<aFlexField>
<label>aFlexFieldLabel</label>
<config>
<type>input</type>
</config>
</aFlexField>
</el>
</ROOT>
</T3DataStructure>
CODE_SAMPLE
)]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<TCEforms>
<sheetTitle>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_tab.settings
</sheetTitle>
</TCEforms>
<type>array</type>
<el>
<settings.a>
<TCEforms>
<label>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.a
</label>
<config>
<todo>TODO</todo>
</config>
</TCEforms>
</settings.a>
<settings.b>
<TCEforms>
<label>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.b
</label>
<config>
<todo>TODO</todo>
</config>
</TCEforms>
</settings.b>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
-----
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<sheetTitle>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_tab.settings
</sheetTitle>
<type>array</type>
<el>
<settings.a>
<label>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.a
</label><config>
<todo>TODO</todo>
</config>
</settings.a>
<settings.b>
<label>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.b
</label><config>
<todo>TODO</todo>
</config>
</settings.b>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<sheetTitle>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_tab.settings
</sheetTitle>
<type>array</type>
<el>
<settings.topNewsRestriction>
<label>
LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.topNewsRestriction
</label>
<config>
<todo>TODO</todo>
</config>
</settings.topNewsRestriction>
</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\RemoveElementTceFormsRector;

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

final class RemoveElementTceFormsRectorTest 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\RemoveElementTceFormsRector;

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

0 comments on commit 72b1f9c

Please sign in to comment.