-
-
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.
Add rector for language TCA type (#2782)
* Add rector for language TCA type Resolves: #2590 * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
f2eba89
commit a51f955
Showing
8 changed files
with
271 additions
and
1 deletion.
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
144 changes: 144 additions & 0 deletions
144
src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.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,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v11\v3; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Scalar\String_; | ||
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/master/en-us/Changelog/11.3/Deprecation-94165-SysLanguageDatabaseTable.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector\UseLanguageTypeForLanguageFieldColumnRectorTest | ||
*/ | ||
final class UseLanguageTypeForLanguageFieldColumnRector extends AbstractTcaRector | ||
{ | ||
private ?string $languageField = null; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'use the new TCA type language instead of foreign_table => sys_language for selecting a records', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
return [ | ||
'ctrl' => [ | ||
'languageField' => 'sys_language_uid', | ||
], | ||
'columns' => [ | ||
'sys_language_uid' => [ | ||
'exclude' => 1, | ||
'label' => 'Language', | ||
'config' => [ | ||
'type' => 'select', | ||
'renderType' => 'selectSingle', | ||
'foreign_table' => 'sys_language', | ||
'foreign_table_where' => 'ORDER BY sys_language.title', | ||
'eval' => 'int', | ||
'items' => [ | ||
[$_LLL_general . ':LGL.allLanguages', -1], | ||
[$_LLL_general . ':LGL.default_value', 0] | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
return [ | ||
'ctrl' => [ | ||
'languageField' => 'sys_language_uid', | ||
], | ||
'columns' => [ | ||
'sys_language_uid' => [ | ||
'exclude' => 1, | ||
'label' => 'Language', | ||
'config' => [ | ||
'type' => 'language' | ||
], | ||
], | ||
], | ||
]; | ||
CODE_SAMPLE | ||
), | ||
|
||
] | ||
); | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
$this->hasAstBeenChanged = false; | ||
if (! $this->isFullTcaDefinition($node)) { | ||
return null; | ||
} | ||
|
||
$ctrl = $this->extractSubArrayByKey($node, 'ctrl'); | ||
if (! $ctrl instanceof Array_) { | ||
return null; | ||
} | ||
|
||
$value = $this->extractArrayValueByKey($ctrl, 'languageField'); | ||
|
||
if (! $value instanceof String_) { | ||
return null; | ||
} | ||
|
||
$this->languageField = $this->valueResolver->getValue($value); | ||
|
||
if (null === $this->languageField) { | ||
return null; | ||
} | ||
|
||
// we found a tca definition of a full table. Process it as a whole: | ||
$columns = $this->extractSubArrayByKey($node, 'columns'); | ||
if (null !== $columns) { | ||
$this->refactorColumnList($columns); | ||
} | ||
|
||
return $this->hasAstBeenChanged ? $node : null; | ||
} | ||
|
||
protected function refactorColumn(Expr $columnName, Expr $columnTca): void | ||
{ | ||
$column = $this->valueResolver->getValue($columnName); | ||
|
||
if ($column !== $this->languageField) { | ||
return; | ||
} | ||
|
||
$configuration = $this->extractArrayItemByKey($columnTca, 'config'); | ||
|
||
if (! $configuration instanceof ArrayItem) { | ||
return; | ||
} | ||
|
||
$foreignTable = $this->extractArrayItemByKey($configuration->value, 'foreign_table'); | ||
|
||
if (! $foreignTable instanceof ArrayItem) { | ||
return; | ||
} | ||
|
||
if (! $this->valueResolver->isValue($foreignTable->value, 'sys_language')) { | ||
return; | ||
} | ||
|
||
$configuration->value = $this->nodeFactory->createArray([ | ||
'type' => 'language', | ||
]); | ||
|
||
$this->hasAstBeenChanged = true; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...tor/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/skip_language_type.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,29 @@ | ||
<?php | ||
|
||
$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf'; | ||
|
||
return [ | ||
'ctrl' => [ | ||
'languageField' => 'sys_language_uid', | ||
], | ||
'columns' => [ | ||
'other_language_field' => [ | ||
'exclude' => 1, | ||
'label' => $_LLL_general . ':LGL.language', | ||
'config' => [ | ||
'type' => 'select', | ||
'renderType' => 'selectSingle', | ||
'foreign_table' => 'sys_language', | ||
'foreign_table_where' => 'ORDER BY sys_language.title', | ||
'eval' => 'int', | ||
'items' => [ | ||
[$_LLL_general . ':LGL.allLanguages', -1], | ||
[$_LLL_general . ':LGL.default_value', 0] | ||
|
||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
?> |
48 changes: 48 additions & 0 deletions
48
...ctor/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/use_language_type.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,48 @@ | ||
<?php | ||
|
||
$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf'; | ||
|
||
return [ | ||
'ctrl' => [ | ||
'languageField' => 'sys_language_uid', | ||
], | ||
'columns' => [ | ||
'sys_language_uid' => [ | ||
'exclude' => 1, | ||
'label' => $_LLL_general . ':LGL.language', | ||
'config' => [ | ||
'type' => 'select', | ||
'renderType' => 'selectSingle', | ||
'foreign_table' => 'sys_language', | ||
'foreign_table_where' => 'ORDER BY sys_language.title', | ||
'eval' => 'int', | ||
'items' => [ | ||
[$_LLL_general . ':LGL.allLanguages', -1], | ||
[$_LLL_general . ':LGL.default_value', 0] | ||
|
||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf'; | ||
|
||
return [ | ||
'ctrl' => [ | ||
'languageField' => 'sys_language_uid', | ||
], | ||
'columns' => [ | ||
'sys_language_uid' => [ | ||
'exclude' => 1, | ||
'label' => $_LLL_general . ':LGL.language', | ||
'config' => ['type' => 'language'], | ||
], | ||
], | ||
]; | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...guageTypeForLanguageFieldColumnRector/UseLanguageTypeForLanguageFieldColumnRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseLanguageTypeForLanguageFieldColumnRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Ssch\TYPO3Rector\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->import(__DIR__ . '/../../../../../../config/config_test.php'); | ||
|
||
$services = $containerConfigurator->services(); | ||
$services->set(UseLanguageTypeForLanguageFieldColumnRector::class); | ||
}; |
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