Skip to content

Commit

Permalink
[TASK] Add MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverr…
Browse files Browse the repository at this point in the history
…idesRector

Resolves: #4234
  • Loading branch information
simonschaufi committed Apr 16, 2024
1 parent 7346838 commit 566d6fa
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Ssch\TYPO3Rector\CodeQuality\General\ExtEmConfRector;
use Ssch\TYPO3Rector\CodeQuality\General\InjectMethodToConstructorInjectionRector;
use Ssch\TYPO3Rector\CodeQuality\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector;
use Ssch\TYPO3Rector\CodeQuality\General\MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector;
use Ssch\TYPO3Rector\CodeQuality\General\UseExtensionKeyInLocalizationUtilityRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down Expand Up @@ -46,5 +47,6 @@
$rectorConfig->rule(ConvertImplicitVariablesToExplicitGlobalsRector::class);
$rectorConfig->rule(InjectMethodToConstructorInjectionRector::class);
$rectorConfig->rule(MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector::class);
$rectorConfig->rule(MoveExtensionManagementUtilityAddToAllTCAtypesIntoTCAOverridesRector::class);
$rectorConfig->rule(UseExtensionKeyInLocalizationUtilityRector::class);
};
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);
}
}
7 changes: 6 additions & 1 deletion stubs/TYPO3/CMS/Core/Utility/ExtensionManagementUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public static function getFileFieldTCAConfig($fieldName, array $customSettingOve
/**
* @param string $string
*/
public static function addTCAcolumns($string, array $columns) {
public static function addTCAcolumns($string, array $columns)
{

}

Expand All @@ -101,6 +102,10 @@ public static function allowTableOnStandardPages(string $table)

}

public static function addToAllTCAtypes(string $table, string $newFieldsString, string $typeList = '', string $position = ''): void
{
}

public static function addPageTSConfig(string $content): void
{
}
Expand Down
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;

?>
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;

?>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture;
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture;

$variable = 'extension3';
$_EXTKEY = 'extension3';
Expand All @@ -13,7 +13,7 @@ $_EXTKEY = 'extension3';
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateAddPageTSConfigToPageTsConfigFileRector\Fixture;
namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\Rector\General\MoveExtensionManagementUtilityAddStaticFileIntoTCAOverridesRector\Fixture;

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
$variable = 'extension3';
Expand Down
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');
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');
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');
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;

?>
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;

?>
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');

?>
Loading

0 comments on commit 566d6fa

Please sign in to comment.