diff --git a/composer.json b/composer.json index 63e62d36..cfe0f224 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/rules/Orm32/Rector/MethodCall/DoctrineQueryBuilderSortDirectionRector.php b/rules/Orm32/Rector/MethodCall/DoctrineQueryBuilderSortDirectionRector.php index 05285e51..21c0bb87 100644 --- a/rules/Orm32/Rector/MethodCall/DoctrineQueryBuilderSortDirectionRector.php +++ b/rules/Orm32/Rector/MethodCall/DoctrineQueryBuilderSortDirectionRector.php @@ -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; @@ -24,7 +22,7 @@ final class DoctrineQueryBuilderSortDirectionRector extends AbstractRector implements ComposerPackageConstraintInterface { public function __construct( - private readonly ValueResolver $valueResolver, + private readonly SortDirectionResolver $sortDirectionResolver, ) { } @@ -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; @@ -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; - } } diff --git a/rules/Orm37/Rector/Attribute/DoctrineOrderByAttributeSortDirectionRector.php b/rules/Orm37/Rector/Attribute/DoctrineOrderByAttributeSortDirectionRector.php index 92378894..b7a64911 100644 --- a/rules/Orm37/Rector/Attribute/DoctrineOrderByAttributeSortDirectionRector.php +++ b/rules/Orm37/Rector/Attribute/DoctrineOrderByAttributeSortDirectionRector.php @@ -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; @@ -23,7 +21,7 @@ final class DoctrineOrderByAttributeSortDirectionRector extends AbstractRector implements ComposerPackageConstraintInterface { public function __construct( - private readonly ValueResolver $valueResolver, + private readonly SortDirectionResolver $sortDirectionResolver, ) { } @@ -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'); @@ -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; - } } diff --git a/src/NodeAnalyzer/SortDirectionResolver.php b/src/NodeAnalyzer/SortDirectionResolver.php new file mode 100644 index 00000000..90758ca8 --- /dev/null +++ b/src/NodeAnalyzer/SortDirectionResolver.php @@ -0,0 +1,45 @@ +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; + } +}