Skip to content

Commit dacee72

Browse files
committed
[dbal 3.8] Add MigrateQueryBuilderResetQueryPartRector
Resolves: #453
1 parent 0231850 commit dacee72

File tree

7 files changed

+233
-0
lines changed

7 files changed

+233
-0
lines changed

config/sets/doctrine-dbal-38.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(MigrateQueryBuilderResetQueryPartRector::class);
10+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector\Fixture;
4+
5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Driver\SQLite3\Driver;
7+
use Doctrine\DBAL\Query\QueryBuilder;
8+
9+
final class SomeRepository
10+
{
11+
public function getQueryPart()
12+
{
13+
$connection = new Connection([], new Driver());
14+
$queryBuilder = new QueryBuilder($connection);
15+
$queryBuilder->resetQueryPart('distinct');
16+
$queryBuilder->resetQueryPart('where');
17+
$queryBuilder->resetQueryPart('groupBy');
18+
$queryBuilder->resetQueryPart('having');
19+
$queryBuilder->resetQueryPart('orderBy');
20+
21+
$queryBuilder->resetQueryPart('select');
22+
$queryBuilder->resetQueryPart('values');
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
namespace Rector\Doctrine\Tests\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector\Fixture;
31+
32+
use Doctrine\DBAL\Connection;
33+
use Doctrine\DBAL\Driver\SQLite3\Driver;
34+
use Doctrine\DBAL\Query\QueryBuilder;
35+
36+
final class SomeRepository
37+
{
38+
public function getQueryPart()
39+
{
40+
$connection = new Connection([], new Driver());
41+
$queryBuilder = new QueryBuilder($connection);
42+
$queryBuilder->distinct(false);
43+
$queryBuilder->resetWhere();
44+
$queryBuilder->resetGroupBy();
45+
$queryBuilder->resetHaving();
46+
$queryBuilder->resetOrderBy();
47+
48+
$queryBuilder->resetQueryPart('select');
49+
$queryBuilder->resetQueryPart('values');
50+
}
51+
}
52+
53+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class MigrateQueryBuilderResetQueryPartRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(MigrateQueryBuilderResetQueryPartRector::class);
10+
};
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Dbal36\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Identifier;
11+
use PhpParser\Node\Scalar\String_;
12+
use PHPStan\Type\ObjectType;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see https://github.com/doctrine/dbal/blob/4.2.x/UPGRADE.md#deprecated-getting-query-parts-from-querybuilder
19+
* @see \Rector\Doctrine\Tests\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector\MigrateQueryBuilderResetQueryPartRectorTest
20+
*/
21+
final class MigrateQueryBuilderResetQueryPartRector extends AbstractRector
22+
{
23+
/**
24+
* @var array<string, string>
25+
*/
26+
private const PART_TO_METHOD_MAP = [
27+
'where' => 'resetWhere',
28+
'groupBy' => 'resetGroupBy',
29+
'having' => 'resetHaving',
30+
'orderBy' => 'resetOrderBy',
31+
];
32+
33+
/**
34+
* @return array<class-string<Node>>
35+
*/
36+
public function getNodeTypes(): array
37+
{
38+
return [MethodCall::class];
39+
}
40+
41+
public function getRuleDefinition(): RuleDefinition
42+
{
43+
return new RuleDefinition(
44+
'Change QueryBuilder::resetQueryPart() to $queryBuilder->reset*()',
45+
[
46+
new CodeSample(
47+
<<<'CODE_SAMPLE'
48+
class SomeRepository
49+
{
50+
public function resetQueryPart(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder)
51+
{
52+
$queryBuilder->resetQueryPart('distinct');
53+
$queryBuilder->resetQueryPart('where');
54+
$queryBuilder->resetQueryPart('groupBy');
55+
$queryBuilder->resetQueryPart('having');
56+
$queryBuilder->resetQueryPart('orderBy');
57+
}
58+
}
59+
CODE_SAMPLE
60+
,
61+
<<<'CODE_SAMPLE'
62+
class SomeRepository
63+
{
64+
public function resetQueryPart(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder)
65+
{
66+
$queryBuilder->distinct(false);
67+
$queryBuilder->resetWhere();
68+
$queryBuilder->resetGroupBy();
69+
$queryBuilder->resetHaving();
70+
$queryBuilder->resetOrderBy();
71+
}
72+
}
73+
CODE_SAMPLE
74+
),
75+
]
76+
);
77+
}
78+
79+
/**
80+
* @param MethodCall $node
81+
*/
82+
public function refactor(Node $node): ?Node
83+
{
84+
if (! $this->isObjectType($node->var, new ObjectType('Doctrine\DBAL\Query\QueryBuilder'))) {
85+
return null;
86+
}
87+
88+
if (! $this->isName($node->name, 'resetQueryPart')) {
89+
return null;
90+
}
91+
92+
$args = $node->args;
93+
if (! isset($args[0])) {
94+
return null;
95+
}
96+
97+
/** @var Arg $firstArgument */
98+
$firstArgument = $args[0];
99+
$argValue = $firstArgument->value;
100+
if (! $argValue instanceof String_) {
101+
return null;
102+
}
103+
104+
$queryPartName = $argValue->value;
105+
if ($queryPartName === 'distinct') {
106+
$node->name = new Identifier('distinct');
107+
$node->args = [$this->nodeFactory->createArg($this->nodeFactory->createFalse())];
108+
return $node;
109+
}
110+
111+
if (isset(self::PART_TO_METHOD_MAP[$queryPartName])) {
112+
$newMethodName = self::PART_TO_METHOD_MAP[$queryPartName];
113+
$node->name = new Identifier($newMethodName);
114+
$node->args = [];
115+
return $node;
116+
}
117+
118+
return null;
119+
}
120+
}

src/Set/DoctrineSetList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ final class DoctrineSetList
4848
*/
4949
public const DOCTRINE_DBAL_30 = __DIR__ . '/../../config/sets/doctrine-dbal-30.php';
5050

51+
/**
52+
* @deprecated Use withComposerBased() instead, see https://getrector.com/blog/introducing-composer-version-based-sets
53+
* @var string
54+
*/
55+
public const DOCTRINE_DBAL_38 = __DIR__ . '/../../config/sets/doctrine-dbal-38.php';
56+
5157
/**
5258
* @deprecated Use withComposerBased() instead, see https://getrector.com/blog/introducing-composer-version-based-sets
5359
* @var string

src/Set/SetProvider/DoctrineSetProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public function provide(): array
5757
'3.0',
5858
__DIR__ . '/../../../config/sets/doctrine-dbal-30.php',
5959
),
60+
new ComposerTriggeredSet(
61+
SetGroup::DOCTRINE,
62+
'doctrine/dbal',
63+
'3.8',
64+
__DIR__ . '/../../../config/sets/doctrine-dbal-38.php',
65+
),
6066
new ComposerTriggeredSet(
6167
SetGroup::DOCTRINE,
6268
'doctrine/dbal',

0 commit comments

Comments
 (0)