Skip to content

Commit

Permalink
[FEATURE] Add MigrateRenderTypeColorpickerToTypeColorRector
Browse files Browse the repository at this point in the history
Resolves: #97271
  • Loading branch information
simonschaufi committed Jan 2, 2024
1 parent 48acd07 commit bdf3292
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 2 deletions.
6 changes: 4 additions & 2 deletions config/v12/tca-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigrateInternalTypeFolderToTypeFolderRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigrateNullFlagRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigratePasswordAndSaltedPasswordToPasswordTypeRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigrateRenderTypeColorpickerToTypeColorRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigrateRequiredFlagRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\MigrateToEmailTypeRector;
use Ssch\TYPO3Rector\Rector\v12\v0\tca\RemoveCruserIdRector;
Expand All @@ -18,14 +19,15 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(MigrateColsToSizeForTcaTypeNoneRector::class);
$rectorConfig->rule(MigrateEvalIntAndDouble2ToTypeNumberRector::class);
$rectorConfig->rule(MigrateInputDateTimeRector::class);
$rectorConfig->rule(MigrateInternalTypeFolderToTypeFolderRector::class);
$rectorConfig->rule(MigrateNullFlagRector::class);
$rectorConfig->rule(MigratePasswordAndSaltedPasswordToPasswordTypeRector::class);
$rectorConfig->rule(MigrateRenderTypeColorpickerToTypeColorRector::class);
$rectorConfig->rule(MigrateRequiredFlagRector::class);
$rectorConfig->rule(MigrateToEmailTypeRector::class);
$rectorConfig->rule(RemoveTCAInterfaceAlwaysDescriptionRector::class);
$rectorConfig->rule(RemoveCruserIdRector::class);
$rectorConfig->rule(RemoveTableLocalPropertyRector::class);
$rectorConfig->rule(MigrateEvalIntAndDouble2ToTypeNumberRector::class);
$rectorConfig->rule(RemoveTCAInterfaceAlwaysDescriptionRector::class);
};
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;
}
}
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',
],
],
],
];
?>
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';
}
}
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);
};

0 comments on commit bdf3292

Please sign in to comment.