|
11 | 11 | use PhpParser\Node; |
12 | 12 | use PhpParser\Node\Expr\Error; |
13 | 13 | use PHPStan\Analyser\Scope; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
14 | 15 | use PHPStan\Rules\Rule; |
| 16 | +use PHPStan\Rules\RuleError; |
15 | 17 | use PHPStan\Rules\RuleErrorBuilder; |
16 | 18 |
|
17 | 19 | /** |
18 | 20 | * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\ClassMethod> |
19 | 21 | */ |
20 | 22 | final class RequireAbstractionInDependenciesRule implements Rule |
21 | 23 | { |
| 24 | + private ReflectionProvider $reflectionProvider; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + ReflectionProvider $reflectionProvider |
| 28 | + ) { |
| 29 | + $this->reflectionProvider = $reflectionProvider; |
| 30 | + } |
| 31 | + |
22 | 32 | public function getNodeType(): string |
23 | 33 | { |
24 | 34 | return Node\Stmt\ClassMethod::class; |
25 | 35 | } |
26 | 36 |
|
27 | 37 | public function processNode(Node $node, Scope $scope): array |
28 | 38 | { |
29 | | - $errors = []; |
30 | | - |
31 | 39 | if (!$node->params) { |
32 | 40 | return []; |
33 | 41 | } |
34 | 42 |
|
35 | | - foreach ($node->params as $param) { |
36 | | - if (!$param->type instanceof Node\Name) { |
37 | | - continue; |
38 | | - } |
39 | | - if ($param->var instanceof Error) { |
40 | | - continue; |
41 | | - } |
42 | | - $typeName = $param->type->toString(); |
43 | | - |
44 | | - // Skip built-in types and primitives |
45 | | - if ($this->isBuiltInType($typeName)) { |
46 | | - continue; |
47 | | - } |
48 | | - |
49 | | - // Skip interfaces - they are always acceptable |
50 | | - if (interface_exists($typeName)) { |
51 | | - continue; |
52 | | - } |
| 43 | + $errors = []; |
53 | 44 |
|
54 | | - // Check if it's a class |
55 | | - if (class_exists($typeName)) { |
56 | | - $reflection = new \ReflectionClass($typeName); |
57 | | - |
58 | | - // Skip abstract classes - they are acceptable |
59 | | - if ($reflection->isAbstract()) { |
60 | | - continue; |
61 | | - } |
62 | | - |
63 | | - // This is a concrete class - check if it has interfaces or extends an abstract class |
64 | | - $interfaces = class_implements($typeName); |
65 | | - $parentClass = $reflection->getParentClass(); |
66 | | - $hasAbstractParent = $parentClass && $parentClass->isAbstract(); |
67 | | - |
68 | | - if (!empty($interfaces) || $hasAbstractParent) { |
69 | | - $suggestions = []; |
70 | | - |
71 | | - if (!empty($interfaces)) { |
72 | | - $suggestions[] = 'Available interfaces: ' . implode(', ', $interfaces); |
73 | | - } |
74 | | - |
75 | | - if ($hasAbstractParent) { |
76 | | - $suggestions[] = 'Abstract parent: ' . $parentClass->getName(); |
77 | | - } |
78 | | - |
79 | | - $errors[] = RuleErrorBuilder::message( |
80 | | - sprintf( |
81 | | - 'Parameter $%s uses concrete class %s instead of an interface or abstract class. %s', |
82 | | - is_string($param->var->name) ? $param->var->name : $param->var->name->getType(), |
83 | | - $typeName, |
84 | | - implode('. ', $suggestions) |
85 | | - ) |
86 | | - )->build(); |
87 | | - } |
| 45 | + foreach ($node->params as $param) { |
| 46 | + $error = $this->validateParameter($param); |
| 47 | + if ($error !== null) { |
| 48 | + $errors[] = $error; |
88 | 49 | } |
89 | 50 | } |
90 | 51 |
|
91 | 52 | return $errors; |
92 | 53 | } |
93 | 54 |
|
94 | | - private function isBuiltInType(string $type): bool |
| 55 | + private function validateParameter(Node\Param $param): ?RuleError |
95 | 56 | { |
96 | | - $builtInTypes = [ |
97 | | - 'string', 'int', 'float', 'bool', 'array', 'object', |
98 | | - 'callable', 'iterable', 'mixed', 'void', 'never', |
99 | | - ]; |
| 57 | + if (!$param->type instanceof Node\Name) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + if ($param->var instanceof Error) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + $typeName = $param->type->toString(); |
| 66 | + |
| 67 | + // Skip if the type doesn't exist in reflection |
| 68 | + if (!$this->reflectionProvider->hasClass($typeName)) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + $classReflection = $this->reflectionProvider->getClass($typeName); |
| 73 | + |
| 74 | + // Skip interfaces - they are always acceptable |
| 75 | + if ($classReflection->isInterface()) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + // Skip abstract classes - they are acceptable |
| 80 | + if ($classReflection->isAbstract()) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $reflection = $classReflection->getNativeReflection(); |
| 85 | + |
| 86 | + // This is a concrete class - check if it has interfaces or extends an abstract class |
| 87 | + $interfaces = class_implements($typeName); |
| 88 | + $parentClass = $reflection->getParentClass(); |
| 89 | + $hasAbstractParent = $parentClass && $parentClass->isAbstract(); |
| 90 | + |
| 91 | + // If there are no interfaces and no abstract parent, it's acceptable (no violation) |
| 92 | + if (empty($interfaces) && !$hasAbstractParent) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + // Build error with suggestions |
| 97 | + $suggestions = []; |
| 98 | + |
| 99 | + if (!empty($interfaces)) { |
| 100 | + $suggestions[] = 'Available interfaces: ' . implode(', ', $interfaces); |
| 101 | + } |
| 102 | + |
| 103 | + if ($hasAbstractParent) { |
| 104 | + $suggestions[] = 'Abstract parent: ' . $parentClass->getName(); |
| 105 | + } |
100 | 106 |
|
101 | | - return in_array(strtolower($type), $builtInTypes); |
| 107 | + return RuleErrorBuilder::message( |
| 108 | + sprintf( |
| 109 | + 'Parameter $%s uses concrete class %s instead of an interface or abstract class. %s', |
| 110 | + is_string($param->var->name) ? $param->var->name : $param->var->name->getType(), |
| 111 | + $typeName, |
| 112 | + implode('. ', $suggestions) |
| 113 | + ) |
| 114 | + )->build(); |
102 | 115 | } |
103 | 116 | } |
0 commit comments