-
-
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.
Showing
7 changed files
with
263 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
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v11\v3; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
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 | ||
*/ | ||
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 (null === $ctrl) { | ||
return null; | ||
} | ||
|
||
$value = $this->extractArrayValueByKey($ctrl, 'languageField'); | ||
|
||
if (! $value instanceof Node\Scalar\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 (null === $configuration) { | ||
return; | ||
} | ||
|
||
$foreignTable = $this->extractArrayItemByKey($configuration->value, 'foreign_table'); | ||
|
||
if (null === $foreignTable) { | ||
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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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(\Ssch\TYPO3Rector\Rector\v11\v3\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