Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.96% |
20 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| EntityGetIdShouldBeSpecific | |
86.96% |
20 / 23 |
|
75.00% |
3 / 4 |
11.27 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNodeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| processNode | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
8.22 | |||
| getClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\ApiePhpstanRules; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Apie\Core\Identifiers\IdentifierInterface; |
| 6 | use PhpParser\Node; |
| 7 | use PhpParser\Node\Stmt\Class_; |
| 8 | use PHPStan\Analyser\Scope; |
| 9 | use PHPStan\Reflection\ClassReflection; |
| 10 | use PHPStan\Reflection\ReflectionProvider; |
| 11 | use PHPStan\Rules\Rule; |
| 12 | use PHPStan\Rules\RuleErrorBuilder; |
| 13 | |
| 14 | /** |
| 15 | * EntityInterface only implements getId() with return type IdentifierInterface. To get better |
| 16 | * reflection data it is better if the entity returns the specific identifier class and not identifierInterface. |
| 17 | * |
| 18 | * @implements Rule<Class_> |
| 19 | */ |
| 20 | final class EntityGetIdShouldBeSpecific implements Rule |
| 21 | { |
| 22 | public function __construct( |
| 23 | private ReflectionProvider $reflectionProvider |
| 24 | ) { |
| 25 | } |
| 26 | |
| 27 | public function getNodeType(): string |
| 28 | { |
| 29 | return Class_::class; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param Class_ $node |
| 34 | */ |
| 35 | public function processNode(Node $node, Scope $scope): array |
| 36 | { |
| 37 | $nodeName = $node->name->toString(); |
| 38 | if ($node->isAbstract() || str_starts_with($nodeName, 'Anonymous') || $node->isAnonymous()) { |
| 39 | return []; |
| 40 | } |
| 41 | $class = $this->getClass($node, $scope); |
| 42 | if ($class->implementsInterface(EntityInterface::class)) { |
| 43 | $method = $class->getMethod('getId', $scope); |
| 44 | foreach ($method->getVariants() as $variant) { |
| 45 | $type = $variant->getNativeReturnType(); |
| 46 | if ($type->isObject()->yes() && in_array(IdentifierInterface::class, $type->getReferencedClasses())) { |
| 47 | return [ |
| 48 | RuleErrorBuilder::message( |
| 49 | sprintf( |
| 50 | "Class '%s' is an entity, but the getId() implementation has still IdentifierInterface return type.", |
| 51 | $nodeName |
| 52 | ) |
| 53 | )->identifier('apie.get.id.specific') |
| 54 | ->build() |
| 55 | ]; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return [ |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | private function getClass(Class_ $node, Scope $scope): ClassReflection |
| 64 | { |
| 65 | return $this->reflectionProvider->getClass($scope->getNamespace() . '\\' . $node->name->toString()); |
| 66 | } |
| 67 | } |