Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ORM-300] add missing set, run fixer and add CastDoctrineExprToStringRector #372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions config/sets/doctrine-orm-300.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Orm30\Rector\MethodCall\CastDoctrineExprToStringRector;
use Rector\Doctrine\Orm30\Rector\MethodCall\SetParametersArrayToCollectionRector;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
SetParametersArrayToCollectionRector::class,
]);
$rectorConfig->rules([SetParametersArrayToCollectionRector::class, CastDoctrineExprToStringRector::class]);

$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Doctrine\ORM\ORMException' => 'Doctrine\ORM\Exception\ORMException',
Expand Down
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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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' . '%'))->__toString());
}
}
?>
-----
<?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' . '%'))->__toString());
}
}
?>
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'));
}
}
?>
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 rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php
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;
}
}
2 changes: 1 addition & 1 deletion src/Set/DoctrineSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class DoctrineSetList
/**
* @var string
*/
public const DOCTRINE_ORM_219 = __DIR__.'/../../config/sets/doctrine-orm-219.php';
public const DOCTRINE_ORM_219 = __DIR__ . '/../../config/sets/doctrine-orm-219.php';

/**
* @var string
Expand Down
6 changes: 6 additions & 0 deletions src/Set/SetProvider/DoctrineSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function provide(): array
'2.14',
__DIR__ . '/../../../config/sets/doctrine-orm-214.php',
),
new ComposerTriggeredSet(
SetGroup::DOCTRINE,
'doctrine/orm',
'3.0',
__DIR__ . '/../../../config/sets/doctrine-orm-300.php',
),

new Set(SetGroup::ATTRIBUTES, 'Doctrine ORM', __DIR__ . '/../../../config/sets/attributes/doctrine.php'),
new Set(SetGroup::ATTRIBUTES, 'Gedmo', __DIR__ . '/../../../config/sets/attributes/gedmo.php'),
Expand Down
Loading