-
-
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.
[FEATURE] Add MigrateExpressionBuilderTrimMethodSecondParameterRector (…
…#4171) * [FEATURE] Add MigrateExpressionBuilderTrimMethodSecondParameterRector Resolves: #4091 * TASK: Test * TASK: Fix
- Loading branch information
1 parent
12db6fa
commit 3c742ac
Showing
12 changed files
with
359 additions
and
10 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
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
117 changes: 117 additions & 0 deletions
117
rules/TYPO313/v0/MigrateExpressionBuilderTrimMethodSecondParameterRector.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,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; | ||
} | ||
} |
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,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; | ||
} |
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
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
57 changes: 57 additions & 0 deletions
57
...Rector/v13/v0/MigrateExpressionBuilderTrimMethodSecondParameterRector/Fixture/fixture.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,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) | ||
); |
95 changes: 95 additions & 0 deletions
95
...or/v13/v0/MigrateExpressionBuilderTrimMethodSecondParameterRector/Fixture/fixture.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,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) | ||
); | ||
|
||
?> |
Oops, something went wrong.