-
-
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 MigrateRenderTypeColorpickerToTypeColorRector
Resolves: #97271
- Loading branch information
1 parent
48acd07
commit bdf3292
Showing
5 changed files
with
234 additions
and
2 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
126 changes: 126 additions & 0 deletions
126
src/Rector/v12/v0/tca/MigrateRenderTypeColorpickerToTypeColorRector.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,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v12\v0\tca; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Scalar\String_; | ||
use Ssch\TYPO3Rector\Helper\ArrayUtility; | ||
use Ssch\TYPO3Rector\Rector\Tca\AbstractTcaRector; | ||
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-97271-NewTCATypeColor.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateRenderTypeColorpickerToTypeColorRector\MigrateRenderTypeColorpickerToTypeColorRectorTest | ||
*/ | ||
final class MigrateRenderTypeColorpickerToTypeColorRector extends AbstractTcaRector | ||
{ | ||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('migrateRenderTypeColorpickerToTypeColor', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
return [ | ||
'columns' => [ | ||
'a_color_field' => [ | ||
'label' => 'Color field', | ||
'config' => [ | ||
'type' => 'input', | ||
'renderType' => 'colorpicker', | ||
'required' => true, | ||
'size' => 20, | ||
'max' => 1024, | ||
'eval' => 'trim', | ||
'valuePicker' => [ | ||
'items' => [ | ||
['typo3 orange', '#FF8700'], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
return [ | ||
'columns' => [ | ||
'a_color_field' => [ | ||
'label' => 'Color field', | ||
'config' => [ | ||
'type' => 'color', | ||
'required' => true, | ||
'size' => 20, | ||
'valuePicker' => [ | ||
'items' => [ | ||
['typo3 orange', '#FF8700'], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
protected function refactorColumn(Expr $columnName, Expr $columnTca): void | ||
{ | ||
$configArray = $this->extractSubArrayByKey($columnTca, self::CONFIG); | ||
if (! $configArray instanceof Array_) { | ||
return; | ||
} | ||
|
||
// Early return in case column is not of type=input with renderType=inputDateTime | ||
if (! $this->isConfigType($configArray, 'input')) { | ||
return; | ||
} | ||
|
||
if (! $this->configIsOfRenderType($configArray, 'colorpicker')) { | ||
return; | ||
} | ||
|
||
// Set the TCA type to "color" | ||
$this->changeTcaType($configArray, 'color'); | ||
|
||
// Remove 'max' config | ||
$maxArrayItem = $this->extractArrayItemByKey($configArray, 'max'); | ||
if ($maxArrayItem instanceof ArrayItem) { | ||
$this->removeNode($maxArrayItem); | ||
} | ||
|
||
// Remove 'renderType' config | ||
$renderTypeArrayItem = $this->extractArrayItemByKey($configArray, 'renderType'); | ||
if ($renderTypeArrayItem instanceof ArrayItem) { | ||
$this->removeNode($renderTypeArrayItem); | ||
} | ||
|
||
$evalArrayItem = $this->extractArrayItemByKey($configArray, 'eval'); | ||
if (! $evalArrayItem instanceof ArrayItem) { | ||
return; | ||
} | ||
|
||
$evalListValue = $this->valueResolver->getValue($evalArrayItem->value); | ||
if (! is_string($evalListValue)) { | ||
return; | ||
} | ||
|
||
$evalList = ArrayUtility::trimExplode(',', $evalListValue, true); | ||
|
||
if (in_array('null', $evalList, true)) { | ||
// Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=color | ||
$evalArrayItem->value = new String_('null'); | ||
} else { | ||
// 'eval' is empty, remove whole configuration | ||
$this->removeNode($evalArrayItem); | ||
} | ||
|
||
$this->hasAstBeenChanged = true; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...s/Rector/v12/v0/tca/MigrateRenderTypeColorpickerToTypeColorRector/Fixture/fixture.php.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,61 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateRenderTypeColorpickerToTypeColorRector\Fixture; | ||
|
||
return [ | ||
'ctrl' => [], | ||
'columns' => [ | ||
'colorPicker' => [ | ||
'config' => [ | ||
'type' => 'input', | ||
'renderType' => 'colorpicker', | ||
'required' => true, | ||
'size' => 20, | ||
'max' => 1234, | ||
'eval' => 'trim,null', | ||
'valuePicker' => [ | ||
'items' => [ | ||
['typo3 orange', '#FF8700'], | ||
], | ||
], | ||
], | ||
], | ||
'colorPicker2' => [ | ||
'config' => [ | ||
'type' => 'input', | ||
'renderType' => 'colorpicker', | ||
'eval' => 'trim', | ||
], | ||
], | ||
], | ||
]; | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateRenderTypeColorpickerToTypeColorRector\Fixture; | ||
|
||
return [ | ||
'ctrl' => [], | ||
'columns' => [ | ||
'colorPicker' => [ | ||
'config' => [ | ||
'type' => 'color', | ||
'required' => true, | ||
'size' => 20, | ||
'eval' => 'null', | ||
'valuePicker' => [ | ||
'items' => [ | ||
['typo3 orange', '#FF8700'], | ||
], | ||
], | ||
], | ||
], | ||
'colorPicker2' => [ | ||
'config' => [ | ||
'type' => 'color', | ||
], | ||
], | ||
], | ||
]; | ||
?> |
32 changes: 32 additions & 0 deletions
32
...derTypeColorpickerToTypeColorRector/MigrateRenderTypeColorpickerToTypeColorRectorTest.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\tca\MigrateRenderTypeColorpickerToTypeColorRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MigrateRenderTypeColorpickerToTypeColorRectorTest 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'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ector/v12/v0/tca/MigrateRenderTypeColorpickerToTypeColorRector/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\tca\MigrateRenderTypeColorpickerToTypeColorRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(MigrateRenderTypeColorpickerToTypeColorRector::class); | ||
}; |