-
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
4 changed files
with
97 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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\Orm30\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
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 | ||
{ | ||
private array $targetMethods = [ | ||
Check failure on line 20 in rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php
|
||
'like', 'notLike', 'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'between', | ||
'in', 'notIn', 'isMemberOf', 'isInstanceOf', | ||
]; | ||
|
||
private array $exprFuncMethods = [ | ||
Check failure on line 25 in rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php
|
||
'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 | ||
foreach ($node->args as $arg) { | ||
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); | ||
} | ||
} | ||
|
||
return $node; | ||
} | ||
} |
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