Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"require-dev": {
"doctrine/doctrine-bundle": "^3.0",
"doctrine/orm": "^3.2",
"doctrine/orm": "^3.7",
"symplify/easy-coding-standard": "^13.2",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.2",
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\Orm37\Rector\Attribute\DoctrineOrderByAttributeSortDirectionRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DoctrineOrderByAttributeSortDirectionRectorTest 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,35 @@
<?php

namespace Rector\Doctrine\Tests\Orm37\Rector\Attribute\DoctrineOrderByAttributeSortDirectionRector\Fixture;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Order;

class ReplaceOrderByAscWithClassConstant
{
#[ORM\OrderBy(['createdAt' => 'ASC'])]
protected \DateTimeInterface $messages;

#[ORM\OrderBy(['updatedAt' => 'desc', 'id' => Order::Ascending])]
protected \DateTimeInterface $logs;
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\Orm37\Rector\Attribute\DoctrineOrderByAttributeSortDirectionRector\Fixture;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Order;

class ReplaceOrderByAscWithClassConstant
{
#[ORM\OrderBy(['createdAt' => \SortDirection::Ascending])]
protected \DateTimeInterface $messages;

#[ORM\OrderBy(['updatedAt' => \SortDirection::Descending, 'id' => \SortDirection::Ascending])]
protected \DateTimeInterface $logs;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Orm37\Rector\Attribute\DoctrineOrderByAttributeSortDirectionRector;

return RectorConfig::configure()
->withRules([DoctrineOrderByAttributeSortDirectionRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Orm37\Rector\Attribute;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/doctrine/orm/pull/12454
* @see \Rector\Doctrine\Tests\Orm37\Rector\Attribute\DoctrineOrderByAttributeSortDirectionRector\DoctrineOrderByAttributeSortDirectionRectorTest
*/
final class DoctrineOrderByAttributeSortDirectionRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replaces string sort directions with \SortDirection enum in Doctrine #[ORM\OrderBy] Attributes',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;

class SomeClass
{
#[ORM\OrderBy(['createdAt' => 'ASC'])]
protected $messages;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;

class SomeClass
{
#[ORM\OrderBy(['createdAt' => \SortDirection::Ascending])]
protected $messages;
}
CODE_SAMPLE
),
]
);
}

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
return new ComposerPackageConstraint('doctrine/orm', '>=3.7');
}

public function getNodeTypes(): array
{
return [Attribute::class];
}

/**
* @param Attribute $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'Doctrine\ORM\Mapping\OrderBy')) {
return null;
}

$args = $node->args;
if ($args === []) {
return null;
}

$firstArgValue = $args[0]->value;

if (! $firstArgValue instanceof Array_) {
return null;
}

$hasChanged = false;

foreach ($firstArgValue->items as $arrayItem) {
$direction = $this->resolveSortDirectionValue($arrayItem->value);

if ($direction === 'asc') {
$arrayItem->value = $this->nodeFactory->createClassConstFetch('SortDirection', 'Ascending');
$hasChanged = true;
} elseif ($direction === 'desc') {
$arrayItem->value = $this->nodeFactory->createClassConstFetch('SortDirection', 'Descending');
$hasChanged = true;
}
}

return $hasChanged ? $node : null;
}

private function resolveSortDirectionValue(Node\Expr $expr): ?string
{
if ($expr instanceof ClassConstFetch) {
$constName = $this->getName($expr->name);

if (is_string($constName)) {
$normalized = strtolower($constName);
if (in_array($normalized, ['asc', 'ascending'], true)) {
return 'asc';
}
if (in_array($normalized, ['desc', 'descending'], true)) {
return 'desc';
}
}
}

$value = $this->valueResolver->getValue($expr);
if (is_string($value)) {
$normalized = strtolower($value);
if ($normalized === 'asc' || $normalized === 'desc') {
return $normalized;
}
}

return null;
}
}
Loading