Skip to content

Commit

Permalink
Refactor the OrderByKeyToClassConstRector to use the new enum only in…
Browse files Browse the repository at this point in the history
… `Criteria::orderBy` method calls (#336)

* Refactor the OrderByKeyToClassConstRector to use the new enum only in Criteria::orderBy method call, and remove usage of Criteria::ASC and Criteria::DESC where not recommended

Relates to:

- doctrine/collections#389;
- doctrine/orm#11313 (comment)

* Add check for first class callable in CriteriaOrderingRector

* Refactor condition check in `CriteriaOrderingConstantsDeprecationRector`

Simplify the type-checking condition by directly verifying if the criteria object type is a super type and ensuring it returns a positive result.

* Replaced `toCodeString` with `toString` to get the class name
  • Loading branch information
julienfastre authored Aug 26, 2024
1 parent 862f2dd commit a302734
Show file tree
Hide file tree
Showing 24 changed files with 414 additions and 230 deletions.
3 changes: 0 additions & 3 deletions config/sets/doctrine-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Rector\Doctrine\CodeQuality\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
use Rector\Doctrine\CodeQuality\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector;
use Rector\Doctrine\CodeQuality\Rector\Property\OrderByKeyToClassConstRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToManyRelationTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector;
Expand All @@ -31,8 +30,6 @@
TypedPropertyFromColumnTypeRector::class,
TypedPropertyFromToOneRelationTypeRector::class,
TypedPropertyFromToManyRelationTypeRector::class,

OrderByKeyToClassConstRector::class,
]);

$rectorConfig->ruleWithConfiguration(AttributeKeyToClassConstFetchRector::class, [
Expand Down
17 changes: 1 addition & 16 deletions config/sets/doctrine-collection-22.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(RenameClassConstFetchRector::class, [
new RenameClassAndConstFetch(
'Doctrine\\Common\\Collections\\Criteria',
'ASC',
'Doctrine\\Common\\Collections\\Order',
'Ascending'
),
new RenameClassAndConstFetch(
'Doctrine\\Common\\Collections\\Criteria',
'DESC',
'Doctrine\\Common\\Collections\\Order',
'Descending'
),
]);
$rectorConfig->rules([\Rector\Doctrine\Collection22\Rector\CriteriaOrderingConstantsDeprecationRector::class]);
};

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\OrderByKeyToClassConstRector;
namespace Rector\Doctrine\Tests\Collection22\Rector\CriteriaOrderingConstantsDeprecations;

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

final class OrderByKeyToClassConstRectorTest extends AbstractRectorTestCase
final class CriteriaOrderingConstantDeprecationTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Doctrine\Common\Collections\Criteria as SomeAliasedCriteria;

$crit = new SomeAliasedCriteria();
$crit->orderBy(['param1' => \Doctrine\Common\Collections\Order::Ascending, 'param2' => SomeAliasedCriteria::DESC, 'param3' => 'asc']);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria as SomeAliasedCriteria;

$crit = new SomeAliasedCriteria();
$crit->orderBy(['param1' => \Doctrine\Common\Collections\Order::Ascending, 'param2' => \Doctrine\Common\Collections\Order::Descending, 'param3' => \Doctrine\Common\Collections\Order::Ascending]);

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

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => Criteria::ASC, 'desc' => Criteria::DESC]);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => \Doctrine\Common\Collections\Order::Ascending, 'desc' => \Doctrine\Common\Collections\Order::Descending]);

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

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => 'asc', 'desc' => 'desc']);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => \Doctrine\Common\Collections\Order::Ascending, 'desc' => \Doctrine\Common\Collections\Order::Descending]);

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

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => 'ASC', 'desc' => 'DESC']);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => \Doctrine\Common\Collections\Order::Ascending, 'desc' => \Doctrine\Common\Collections\Order::Descending]);

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

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['param1' => \Doctrine\Common\Collections\Order::Ascending, 'param2' => Criteria::DESC, 'param3' => 'asc']);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['param1' => \Doctrine\Common\Collections\Order::Ascending, 'param2' => \Doctrine\Common\Collections\Order::Descending, 'param3' => \Doctrine\Common\Collections\Order::Ascending]);

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

use Doctrine\Common\Collections\Criteria;

$query->addOrderBy('someParameter', Criteria::ASC);

?>
-----
<?php

use Doctrine\Common\Collections\Criteria;

$query->addOrderBy('someParameter', 'ASC');

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

namespace Rector\Doctrine\Tests\Collection22\Rector\CriteriaOrderingConstantsDeprecations\Fixture;

use Doctrine\ORM\Mapping as ORM;

class ReplaceOrderByAscWithClassConstant
{
#[ORM\OrderBy(['createdAt' => 'asc'])]
protected \DateTimeInterface $messages;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Doctrine\Tests\Collection22\Rector\CriteriaOrderingConstantsDeprecations\Fixture;

use Doctrine\ORM\Mapping as ORM;

class ReplaceOrderByAscWithClassConstant
{
#[ORM\OrderBy(['createdAt' => 'ASC'])]
protected \DateTimeInterface $messages;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Doctrine\Tests\Collection22\Rector\CriteriaOrderingConstantsDeprecations\Fixture;

use Doctrine\ORM\Mapping as ORM;

class ReplaceOrderByAscWithClassConstant
{
#[ORM\OrderBy(['createdAt' => 'desc'])]
protected \DateTimeInterface $messages;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Doctrine\Common\Collections\Criteria;

$criteria = new Criteria();
$criteria->orderBy(['asc' => \Doctrine\Common\Collections\Order::Ascending, 'desc' => \Doctrine\Common\Collections\Order::Descending]);

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

use SomeClass;

$query->addOrderBy('someParameter', SomeClass::ASC);

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

$object = new SomeClass();
$object->orderBy(['someType' => 'ASC']);

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Collection22\Rector\CriteriaOrderingConstantsDeprecationRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CriteriaOrderingConstantsDeprecationRector::class);
};
Loading

0 comments on commit a302734

Please sign in to comment.