|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RectorLaravel\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Attribute; |
| 9 | +use PhpParser\Node\AttributeGroup; |
| 10 | +use PhpParser\Node\Identifier; |
| 11 | +use PhpParser\Node\Name\FullyQualified; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PHPStan\Reflection\ReflectionProvider; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; |
| 16 | +use RectorLaravel\AbstractRector; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see \RectorLaravel\Tests\Rector\ClassMethod\ScopeNamedClassMethodToScopeAttributedClassMethodRector\ScopeNamedClassMethodToScopeAttributedClassMethodRectorTest |
| 22 | + */ |
| 23 | +final class ScopeNamedClassMethodToScopeAttributedClassMethodRector extends AbstractRector |
| 24 | +{ |
| 25 | + private const string SCOPE_ATTRIBUTE = 'Illuminate\Database\Eloquent\Attributes\Scope'; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer, |
| 29 | + private readonly ReflectionProvider $reflectionProvider, |
| 30 | + ) {} |
| 31 | + |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition( |
| 35 | + 'Changes model scope methods to use the scope attribute', |
| 36 | + [new CodeSample( |
| 37 | + <<<'CODE_SAMPLE' |
| 38 | +class User extends Model |
| 39 | +{ |
| 40 | + public function scopeActive($query) |
| 41 | + { |
| 42 | + return $query->where('active', 1); |
| 43 | + } |
| 44 | +} |
| 45 | +CODE_SAMPLE, |
| 46 | + <<<'CODE_SAMPLE' |
| 47 | +class User extends Model |
| 48 | +{ |
| 49 | + #[\Illuminate\Database\Eloquent\Attributes\Scope] |
| 50 | + public function active($query) |
| 51 | + { |
| 52 | + return $query->where('active', 1); |
| 53 | + } |
| 54 | +} |
| 55 | +CODE_SAMPLE |
| 56 | + )] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<class-string<Node>> |
| 62 | + */ |
| 63 | + public function getNodeTypes(): array |
| 64 | + { |
| 65 | + return [Class_::class]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param Class_ $node |
| 70 | + */ |
| 71 | + public function refactor(Node $node): ?Node |
| 72 | + { |
| 73 | + if (! $this->isObjectType($node, new ObjectType('Illuminate\Database\Eloquent\Model'))) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + if (! is_string($className = $this->getName($node))) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 82 | + |
| 83 | + $changes = false; |
| 84 | + foreach ($node->getMethods() as $classMethod) { |
| 85 | + $name = $this->getName($classMethod); |
| 86 | + // make sure it starts with scope and the next character is upper case |
| 87 | + if (! str_starts_with($name, 'scope') || ! ctype_upper(substr($name, 5, 1))) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + $newName = lcfirst(str_replace('scope', '', $name)); |
| 92 | + |
| 93 | + if ($classReflection->hasMethod($newName)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::SCOPE_ATTRIBUTE)) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $classMethod->name = new Identifier($newName); |
| 102 | + $classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified(self::SCOPE_ATTRIBUTE))]); |
| 103 | + $changes = true; |
| 104 | + } |
| 105 | + |
| 106 | + if ($changes === false) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + return $node; |
| 111 | + } |
| 112 | +} |
0 commit comments