-
-
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.
[TASK] Add MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverr…
…idesRector Resolves: #4234
- Loading branch information
1 parent
7346838
commit 566d6fa
Showing
14 changed files
with
345 additions
and
9 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
169 changes: 169 additions & 0 deletions
169
...eQuality/General/MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector.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,169 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\CodeQuality\General; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\NodeTraverser; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\PhpParser\Printer\BetterStandardPrinter; | ||
use Rector\Rector\AbstractRector; | ||
use Ssch\TYPO3Rector\Contract\FilesystemInterface; | ||
use Ssch\TYPO3Rector\Filesystem\FilesFinder; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://review.typo3.org/c/Packages/TYPO3.CMS/+/52437 | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRectorTest | ||
*/ | ||
class MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector extends AbstractRector | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private FilesFinder $filesFinder; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private FilesystemInterface $filesystem; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private ValueResolver $valueResolver; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private BetterStandardPrinter $betterStandardPrinter; | ||
|
||
public function __construct( | ||
FilesFinder $filesFinder, | ||
FilesystemInterface $filesystem, | ||
ValueResolver $valueResolver, | ||
BetterStandardPrinter $betterStandardPrinter | ||
) { | ||
$this->filesFinder = $filesFinder; | ||
$this->filesystem = $filesystem; | ||
$this->valueResolver = $valueResolver; | ||
$this->betterStandardPrinter = $betterStandardPrinter; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Move ExtensionManagementUtility::addToAllTCAtypes into table specific Configuration/TCA/Overrides file', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('table', 'new_field', '', 'after:existing_field'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
// Move to table specific Configuration/TCA/Overrides/table.php file | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Expression::class]; | ||
} | ||
|
||
/** | ||
* @param Expression $node | ||
*/ | ||
public function refactor(Node $node) | ||
{ | ||
$staticMethodCall = $node->expr; | ||
if (! $staticMethodCall instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if ($this->shouldSkip($staticMethodCall)) { | ||
return null; | ||
} | ||
|
||
$tableNameArgument = $staticMethodCall->args[0] ?? null; | ||
if ($tableNameArgument === null) { | ||
return null; | ||
} | ||
|
||
$tableNameValue = $tableNameArgument->value; | ||
if (! $tableNameValue instanceof String_ && ! $tableNameValue instanceof Variable) { | ||
return null; | ||
} | ||
|
||
$resolvedTableName = $this->resolveTableName($tableNameValue); | ||
if ($resolvedTableName instanceof String_) { | ||
$staticMethodCall->args[0] = $resolvedTableName; | ||
$tableNameAsString = $resolvedTableName->value; | ||
} else { | ||
$tableNameAsString = 'unknown'; | ||
} | ||
|
||
$content = $this->betterStandardPrinter->prettyPrint([$staticMethodCall]) . ';'; | ||
|
||
$newConfigurationFile = dirname($this->file->getFilePath()) . '/Configuration/TCA/Overrides/' . $tableNameAsString . '.php'; | ||
if ($this->filesystem->fileExists($newConfigurationFile)) { | ||
$this->filesystem->appendToFile($newConfigurationFile, $content . PHP_EOL); | ||
} else { | ||
$this->filesystem->write($newConfigurationFile, <<<CODE | ||
<?php | ||
{$content} | ||
CODE | ||
); | ||
} | ||
|
||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
|
||
private function shouldSkip(StaticCall $staticMethodCall): bool | ||
{ | ||
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( | ||
$staticMethodCall, | ||
new ObjectType('TYPO3\CMS\Core\Utility\ExtensionManagementUtility') | ||
)) { | ||
return true; | ||
} | ||
|
||
if (! $this->isName($staticMethodCall->name, 'addToAllTCAtypes')) { | ||
return true; | ||
} | ||
|
||
return ! $this->filesFinder->isExtTables($this->file->getFilePath()); | ||
} | ||
|
||
/** | ||
* @param Variable|String_ $contentArgumentValue | ||
*/ | ||
private function resolveTableName($contentArgumentValue): ?String_ | ||
{ | ||
if ($contentArgumentValue instanceof String_) { | ||
return $contentArgumentValue; | ||
} | ||
|
||
if (! $contentArgumentValue instanceof Variable) { | ||
return null; | ||
} | ||
|
||
$tableName = $this->valueResolver->getValue($contentArgumentValue); | ||
|
||
return new String_($tableName); | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
...anagementUtilityAddStaticFileIntoTCAOverridesRector/Fixture/extension1/ext_tables.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture; | ||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture; | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('extension1', 'Configuration/TypoScript', 'Title'); | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture; | ||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture; | ||
|
||
?> |
6 changes: 2 additions & 4 deletions
6
...anagementUtilityAddStaticFileIntoTCAOverridesRector/Fixture/extension2/ext_tables.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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture; | ||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture; | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('extension2', 'Configuration/TypoScript', 'Title'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture; | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture; | ||
|
||
?> |
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
3 changes: 3 additions & 0 deletions
3
...esIntoTCAOverridesRector/Assertions/extension1/Configuration/TCA/Overrides/tt_content.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,3 @@ | ||
<?php | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:a'); |
5 changes: 5 additions & 0 deletions
5
...esIntoTCAOverridesRector/Assertions/extension2/Configuration/TCA/Overrides/tt_content.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,5 @@ | ||
<?php | ||
|
||
# tt_content.php file exists | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:b'); |
5 changes: 5 additions & 0 deletions
5
...esIntoTCAOverridesRector/Assertions/extension3/Configuration/TCA/Overrides/tt_content.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,5 @@ | ||
<?php | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:a'); | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:b'); |
12 changes: 12 additions & 0 deletions
12
...gementUtilityAddToAllTCAtypesIntoTCAOverridesRector/Fixture/extension1/ext_tables.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,12 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:a'); | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...gementUtilityAddToAllTCAtypesIntoTCAOverridesRector/Fixture/extension2/ext_tables.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,12 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:b'); | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...gementUtilityAddToAllTCAtypesIntoTCAOverridesRector/Fixture/extension3/ext_tables.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,21 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
$variable = 'tt_content'; | ||
|
||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes($variable, 'new_field', '', 'after:a'); | ||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'new_field', '', 'after:b'); | ||
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('doing' . $variable . 'weirdStuff', 'new_field', '', 'after:c'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
$variable = 'tt_content'; | ||
ExtensionManagementUtility::addToAllTCAtypes('doing' . $variable . 'weirdStuff', 'new_field', '', 'after:c'); | ||
|
||
?> |
Oops, something went wrong.