-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OrderByKeyToClassConstRector] Add rule
- Loading branch information
Showing
10 changed files
with
257 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
...derByKeyToClassConstRector/Fixture/replace_order_by_asc_lower_with_class_constant.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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => 'asc'])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::ASC])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> |
25 changes: 25 additions & 0 deletions
25
...ute/OrderByKeyToClassConstRector/Fixture/replace_order_by_asc_with_class_constant.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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => 'ASC'])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::ASC])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> |
25 changes: 25 additions & 0 deletions
25
...te/OrderByKeyToClassConstRector/Fixture/replace_order_by_desc_with_class_constant.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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => 'desc'])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::DESC])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> |
13 changes: 13 additions & 0 deletions
13
...ty/Rector/Attribute/OrderByKeyToClassConstRector/Fixture/skip_already_using_const.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\Fixture; | ||
|
||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
class SkipAlreadyUsingConst | ||
{ | ||
#[ORM\OrderBy(['createdAt' => Criteria::ASC])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> |
28 changes: 28 additions & 0 deletions
28
...uality/Rector/Attribute/OrderByKeyToClassConstRector/OrderByKeyToClassConstRectorTest.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\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class OrderByKeyToClassConstRectorTest 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__ . '/config/configured_set.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...tests/CodeQuality/Rector/Attribute/OrderByKeyToClassConstRector/config/configured_set.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
use Rector\Doctrine\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(OrderByKeyToClassConstRector::class); | ||
}; |
105 changes: 105 additions & 0 deletions
105
rules/CodeQuality/Rector/Attribute/OrderByKeyToClassConstRector.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,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\CodeQuality\Rector\Attribute; | ||
|
||
use Doctrine\Common\Collections\Criteria; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Attribute\OrderByKeyToClassConstRector\OrderByKeyToClassConstRectorTest | ||
*/ | ||
final class OrderByKeyToClassConstRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replace OrderBy Attribute ASC/DESC with class constant from Criteria', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\Mapping as ORM; | ||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => 'ASC'])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\Mapping as ORM; | ||
class ReplaceOrderByAscWithClassConstant | ||
{ | ||
#[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::ASC])] | ||
protected \DateTimeInterface $messages; | ||
} | ||
?> | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Attribute::class]; | ||
} | ||
|
||
/** | ||
* @param Attribute $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// If Attribute is not OrderBy, return null | ||
if ($node->name->toString() !== 'Doctrine\ORM\Mapping\OrderBy') { | ||
return null; | ||
} | ||
|
||
if (!isset($node->args[0])) { | ||
return null; | ||
} | ||
|
||
if (!$node->args[0]->value instanceof Array_){ | ||
return null; | ||
} | ||
|
||
if (!isset($node->args[0]->value->items[0])) { | ||
return null; | ||
} | ||
|
||
if (!$node->args[0]->value->items[0] instanceof Node\Expr\ArrayItem) { | ||
return null; | ||
} | ||
|
||
if (!$node->args[0]->value->items[0]->value instanceof String_) { | ||
return null; | ||
} | ||
|
||
// If Attribute value from key is not `ASC` or `DESC`, return null | ||
if (! in_array($node->args[0]->value->items[0]->value->value, ['ASC', 'asc', 'DESC', 'desc'])) { | ||
return null; | ||
} | ||
|
||
$upper = strtoupper($node->args[0]->value->items[0]->value->value); | ||
$node->args[0]->value->items[0]->value = $this->nodeFactory->createClassConstFetch( | ||
Criteria::class, | ||
$upper | ||
); | ||
|
||
return $node; | ||
} | ||
} |