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 @@ -41,7 +41,7 @@
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --ansi",
"duplicated-code": "vendor/bin/swiss-knife duplicated-code src rules --min-tokens 150 --min-lines 5",
"duplicated-code": "vendor/bin/swiss-knife duplicated-code src rules --min-tokens 100 --min-lines 5",
"rector": "vendor/bin/rector --ansi",
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace Rector\Doctrine\Orm32\Rector\MethodCall;

use PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Doctrine\NodeAnalyzer\SortDirectionResolver;
use Rector\Rector\AbstractRector;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
Expand All @@ -24,7 +22,7 @@
final class DoctrineQueryBuilderSortDirectionRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
private readonly SortDirectionResolver $sortDirectionResolver,
) {
}

Expand Down Expand Up @@ -90,7 +88,7 @@ public function refactor(Node $node): ?Node
}

$orderArg = $args[1];
$orderValue = $this->resolveSortDirectionValue($orderArg->value);
$orderValue = $this->sortDirectionResolver->resolve($orderArg->value);

if (! is_string($orderValue)) {
return null;
Expand Down Expand Up @@ -128,35 +126,4 @@ private function isTargetType(Node $node, string $className): bool

return false;
}

/**
* Extracts a normalized 'asc' or 'desc' string from Strings, Constants
*/
private function resolveSortDirectionValue(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Rector\Doctrine\Orm37\Rector\Attribute;

use PhpParser\Node\Expr;
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\Doctrine\NodeAnalyzer\SortDirectionResolver;
use Rector\Rector\AbstractRector;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
Expand All @@ -23,7 +21,7 @@
final class DoctrineOrderByAttributeSortDirectionRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
private readonly SortDirectionResolver $sortDirectionResolver,
) {
}

Expand Down Expand Up @@ -90,7 +88,7 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

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

if ($direction === 'asc') {
$arrayItem->value = $this->nodeFactory->createClassConstFetch('SortDirection', 'Ascending');
Expand All @@ -103,32 +101,4 @@ public function refactor(Node $node): ?Node

return $hasChanged ? $node : null;
}

private function resolveSortDirectionValue(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;
}
}
45 changes: 45 additions & 0 deletions src/NodeAnalyzer/SortDirectionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class SortDirectionResolver
{
public function __construct(
private ValueResolver $valueResolver,
) {
}

/**
* Extracts a normalized 'asc' or 'desc' string from strings and constants.
*/
public function resolve(Expr $expr): ?string
{
if ($expr instanceof ClassConstFetch && $expr->name instanceof Identifier) {
$normalized = strtolower($expr->name->toString());
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