Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
17 / 19 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ValueObjectHasNoConstructor | |
89.47% |
17 / 19 |
|
75.00% |
3 / 4 |
10.12 | |
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 | |
87.50% |
14 / 16 |
|
0.00% |
0 / 1 |
7.10 | |||
| getClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\ApiePhpstanRules; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 5 | use PhpParser\Node; |
| 6 | use PhpParser\Node\Stmt\Class_; |
| 7 | use PHPStan\Analyser\Scope; |
| 8 | use PHPStan\Reflection\ClassReflection; |
| 9 | use PHPStan\Reflection\ReflectionProvider; |
| 10 | use PHPStan\Rules\Rule; |
| 11 | use PHPStan\Rules\RuleErrorBuilder; |
| 12 | |
| 13 | /** |
| 14 | * Value object without constructor is often a mistake. |
| 15 | * |
| 16 | * Because if there is no constructor people can bypass caling fromNative and call new ValueObject() without |
| 17 | * arguments. That is very likely a mistake. |
| 18 | * |
| 19 | * @implements Rule<Class_> |
| 20 | */ |
| 21 | final class ValueObjectHasNoConstructor implements Rule |
| 22 | { |
| 23 | public function __construct( |
| 24 | private ReflectionProvider $reflectionProvider |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | public function getNodeType(): string |
| 29 | { |
| 30 | return Class_::class; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param Class_ $node |
| 35 | */ |
| 36 | public function processNode(Node $node, Scope $scope): array |
| 37 | { |
| 38 | $nodeName = $node->name->toString(); |
| 39 | if ($node->isAbstract() || str_starts_with($nodeName, 'Anonymous') || $node->isAnonymous() || $node->getMethod('__construct')) { |
| 40 | return []; |
| 41 | } |
| 42 | $class = $this->getClass($node, $scope); |
| 43 | if ($class->implementsInterface(ValueObjectInterface::class) && !$class->hasConstructor()) { |
| 44 | return [ |
| 45 | RuleErrorBuilder::message( |
| 46 | sprintf( |
| 47 | "Class '%s' is a value object, but it has no constructor.", |
| 48 | $node->name->toString() |
| 49 | ) |
| 50 | )->identifier('apie.no.constructor') |
| 51 | ->build() |
| 52 | ]; |
| 53 | } |
| 54 | return [ |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | private function getClass(Class_ $node, Scope $scope): ClassReflection |
| 59 | { |
| 60 | return $this->reflectionProvider->getClass($scope->getNamespace() . '\\' . $node->name->toString()); |
| 61 | } |
| 62 | } |