Skip to content

Commit

Permalink
[FEATURE] Add MigrateExpressionBuilderTrimMethodSecondParameterRector (
Browse files Browse the repository at this point in the history
…#4171)

* [FEATURE] Add MigrateExpressionBuilderTrimMethodSecondParameterRector

Resolves: #4091

* TASK: Test

* TASK: Fix
  • Loading branch information
sabbelasichon authored May 28, 2024
1 parent 12db6fa commit 3c742ac
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/v13/typo3-130.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ssch\TYPO3Rector\TYPO313\v0\IntroduceCapabilitiesBitSetRector;
use Ssch\TYPO3Rector\TYPO313\v0\MigrateAddPageTSConfigToPageTsConfigFileRector;
use Ssch\TYPO3Rector\TYPO313\v0\MigrateAddUserTSConfigToUserTsConfigFileRector;
use Ssch\TYPO3Rector\TYPO313\v0\MigrateExpressionBuilderTrimMethodSecondParameterRector;
use Ssch\TYPO3Rector\TYPO313\v0\MigrateExtbaseHashServiceToUseCoreHashServiceRector;
use Ssch\TYPO3Rector\TYPO313\v0\StrictTypesPersistenceManagerRector;
use Ssch\TYPO3Rector\TYPO313\v0\SubstituteItemFormElIDRector;
Expand Down Expand Up @@ -118,4 +119,5 @@
$rectorConfig->rule(SubstituteItemFormElIDRector::class);
$rectorConfig->rule(MigrateAddPageTSConfigToPageTsConfigFileRector::class);
$rectorConfig->rule(MigrateAddUserTSConfigToUserTsConfigFileRector::class);
$rectorConfig->rule(MigrateExpressionBuilderTrimMethodSecondParameterRector::class);
};
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ public function refactor(Node $node)
$tableNames = $this->valueResolver->getValue($tableArgument);

foreach (explode(',', $tableNames) as $tableName) {
if (!$tableName) {
if ($tableName === '') {
continue;
}

$directoryName = dirname($this->file->getFilePath());
$newConfigurationFile = $directoryName . '/Configuration/TCA/Overrides/' . $tableName . '.php';
$this->writeConfigurationToFile($newConfigurationFile, $tableName);
}

return NodeTraverser::REMOVE_NODE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO313\v0;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.0/Breaking-102875-ExpressionBuilderChanges.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateExpressionBuilderTrimMethodSecondParameterRector\MigrateExpressionBuilderTrimMethodSecondParameterRectorTest
*/
final class MigrateExpressionBuilderTrimMethodSecondParameterRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var array<int, string>
*/
private static array $integerToTrimMode = [
0 => 'UNSPECIFIED',
1 => 'LEADING',
2 => 'TRAILING',
3 => 'BOTH',
];

/**
* @readonly
*/
private ValueResolver $valueResolver;

public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate second parameter of trim method to enum', [new CodeSample(
<<<'CODE_SAMPLE'
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim($fieldName, 1),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim($fieldName, TrimMode::LEADING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);
CODE_SAMPLE
)]);
}

public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'trim')) {
return null;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\\CMS\\Core\\Database\\Query\\Expression\\ExpressionBuilder')
)) {
return null;
}

$secondArgument = $node->args[1] ?? null;

if ($secondArgument === null) {
return null;
}

$trimMode = $this->valueResolver->getValue($secondArgument->value);

if (! is_numeric($trimMode)) {
return null;
}

if (! isset(self::$integerToTrimMode[((int) $trimMode)])) {
return null;
}

$trimModeConstant = self::$integerToTrimMode[((int) $trimMode)];

$node->args[1]->value = $this->nodeFactory->createClassConstFetch(
'Doctrine\\DBAL\\Platforms\\TrimMode',
$trimModeConstant
);

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ENUM;
}
}
16 changes: 16 additions & 0 deletions stubs/Doctrine/DBAL/Platforms/TrimMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Doctrine\DBAL\Platforms;

if(enum_exists('Doctrine\DBAL\Platforms\TrimMode')) {
return;
}

enum TrimMode: int
{
case UNSPECIFIED = 0;
case LEADING = 1;
case TRAILING = 2;
case BOTH = 3;
}
16 changes: 11 additions & 5 deletions stubs/TYPO3/CMS/Core/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TYPO3\CMS\Core\Database;

use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;

if (class_exists('TYPO3\CMS\Core\Database\Connection')) {
return;
Expand Down Expand Up @@ -43,14 +44,19 @@ class Connection extends \Doctrine\DBAL\Connection
/** @var ExpressionBuilder */
protected $_expr;

/**
* @return ExpressionBuilder
*/
public function getExpressionBuilder()
public function getExpressionBuilder(): ExpressionBuilder
{
return $this->_expr;
}

public function createQueryBuilder(): QueryBuilder
{
}

public function lastInsertId()
{}
{

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class ExpressionBuilder
{
const EQ = 1;

public function eq(string $fieldName, $value): string
{
return '';
Expand All @@ -30,4 +32,13 @@ public function and(...$expressions): void
public function or(...$expressions): void
{
}

public function comparison($trim, int $EQ, string $createNamedParameter)
{
}

public function trim(string $string, int $int): string
{
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\PhpVersionFeature;
use Ssch\TYPO3Rector\General\Renaming\RenameAttributeRector;
use Ssch\TYPO3Rector\General\Renaming\ValueObject\RenameAttribute;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php');
$rectorConfig->phpVersion(PhpVersion::PHP_80);
$rectorConfig->phpVersion(PhpVersionFeature::ATTRIBUTES);
$rectorConfig->ruleWithConfiguration(RenameAttributeRector::class, [
new RenameAttribute('TYPO3\CMS\Backend\Attribute\Controller', 'TYPO3\CMS\Backend\Attribute\AsController'),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateExpressionBuilderTrimMethodSecondParameterRector\Fixture;

use Doctrine\DBAL\Platforms\TrimMode;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$queryBuilder = GeneralUtility::makeInstance(Connection::class)->createQueryBuilder();
$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', 0),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', 1),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', 2),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', 3),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', 5),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()
->comparison(
$queryBuilder->expr()
->trim('string', TrimMode::LEADING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateExpressionBuilderTrimMethodSecondParameterRector\Fixture;

use Doctrine\DBAL\Platforms\TrimMode;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$queryBuilder = GeneralUtility::makeInstance(Connection::class)->createQueryBuilder();
$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 0),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 1),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 2),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 3),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 5),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::LEADING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v13\v0\MigrateExpressionBuilderTrimMethodSecondParameterRector\Fixture;

use Doctrine\DBAL\Platforms\TrimMode;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$queryBuilder = GeneralUtility::makeInstance(Connection::class)->createQueryBuilder();
$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::UNSPECIFIED),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::LEADING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::TRAILING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::BOTH),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', 5),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

$queryBuilder->expr()->comparison(
$queryBuilder->expr()->trim('string', TrimMode::LEADING),
ExpressionBuilder::EQ,
$queryBuilder->createNamedParameter('', Connection::PARAM_STR)
);

?>
Loading

0 comments on commit 3c742ac

Please sign in to comment.