-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ORM-300] add missing set, run fixer and add CastDoctrineExprToString…
…Rector
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 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
28 changes: 28 additions & 0 deletions
28
...0/Rector/MethodCall/CastDoctrineExprToStringRector/CastDoctrineExprToStringRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\Tests\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class CastDoctrineExprToStringRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/configured_rule.php'; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...Rector/MethodCall/CastDoctrineExprToStringRector/Fixture/epr-lower-cast-to-string.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,32 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector\Fixture; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
final class ExprBuilderCall | ||
{ | ||
public function createCustomExprBuilder() | ||
{ | ||
$expr = new Expr(); | ||
$expr->like($expr->lower('test'), $expr->lower($expr->literal('%' . 'query' . '%'))); | ||
$expr->like($expr->lower('test')->__toString(), $expr->lower($expr->literal('%' . 'query' . '%'))); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector\Fixture; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
|
||
final class ExprBuilderCall | ||
{ | ||
public function createCustomExprBuilder() | ||
{ | ||
$expr = new Expr(); | ||
$expr->like((string)$expr->lower('test'), (string)$expr->lower($expr->literal('%' . 'query' . '%'))); | ||
$expr->like($expr->lower('test')->__toString(), $expr->lower($expr->literal('%' . 'query' . '%'))); | ||
} | ||
} | ||
?> |
15 changes: 15 additions & 0 deletions
15
rules-tests/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector/Fixture/no-change.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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector\Fixture; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
|
||
final class ExprBuilderCall | ||
{ | ||
public function createCustomExprBuilder() | ||
{ | ||
$expr = new Expr(); | ||
$expr->eq('column', $expr->literal('value')); | ||
} | ||
} | ||
?> |
8 changes: 8 additions & 0 deletions
8
rules-tests/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector/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,8 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return RectorConfig::configure() | ||
->withRules([\Rector\Doctrine\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector::class]); |
100 changes: 100 additions & 0 deletions
100
rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\Orm30\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\Cast\String_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://github.com/doctrine/orm/commit/4d73e3ce7801d3bf3254257332e903d8ecea4096 | ||
*/ | ||
final class CastDoctrineExprToStringRector extends AbstractRector | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private array $targetMethods = [ | ||
'like', 'notLike', 'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'between', | ||
'in', 'notIn', 'isMemberOf', 'isInstanceOf', | ||
]; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
private array $exprFuncMethods = [ | ||
'lower', 'upper', 'length', 'trim', 'avg', 'max', 'min', 'count', | ||
'countDistinct', 'exists', 'all', 'some', 'any', 'not', 'abs', 'sqrt', | ||
]; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Casts Doctrine Expr\x to string where necessary.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$statements->add( | ||
$builder->expr()->like( | ||
$builder->expr()->lower($column), | ||
$builder->expr()->lower($builder->expr()->literal('%'.$like.'%')) | ||
) | ||
); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$statements->add( | ||
$builder->expr()->like( | ||
(string) $builder->expr()->lower($column), | ||
(string) $builder->expr()->lower($builder->expr()->literal('%'.$like.'%')) | ||
) | ||
); | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
if (! $this->isObjectType($node->var, new ObjectType('Doctrine\ORM\Query\Expr'))) { | ||
return null; | ||
} | ||
|
||
if (! in_array($this->getName($node->name), $this->targetMethods, true)) { | ||
return null; | ||
} | ||
|
||
// Iterate through method arguments and cast `Expr\Func` calls to string | ||
$hasChanged = false; | ||
foreach ($node->args as $arg) { | ||
if (! $arg instanceof Arg) { | ||
return null; | ||
} | ||
if ($arg->value instanceof MethodCall | ||
&& $this->isObjectType($arg->value->var, new ObjectType('Doctrine\ORM\Query\Expr')) | ||
&& in_array($this->getName($arg->value->name), $this->exprFuncMethods, true) | ||
) { | ||
$arg->value = new String_($arg->value); | ||
$hasChanged = true; | ||
} | ||
} | ||
|
||
return $hasChanged ? $node : null; | ||
} | ||
} |
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