|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cego\phpstan\SpatieLaravelData\Collectors; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use RuntimeException; |
| 7 | +use PhpParser\Node\Name; |
| 8 | +use PhpParser\Node\Param; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use Spatie\LaravelData\Data; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\ComplexType; |
| 13 | +use PHPStan\Collectors\Collector; |
| 14 | +use PHPStan\Node\InClassMethodNode; |
| 15 | +use Cego\phpstan\TypeSystem\UnionType; |
| 16 | +use PHPStan\Reflection\ClassReflection; |
| 17 | +use Cego\phpstan\SpatieLaravelData\Data\Constructor; |
| 18 | +use Cego\phpstan\SpatieLaravelData\Data\KeyTypePair; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Collector<InClassMethodNode, array<string, array<string, array<int, string>>> |
| 22 | + */ |
| 23 | +class ConstructorCollector implements Collector |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Returns the node type, this collector operates on |
| 27 | + * |
| 28 | + * @phpstan-return class-string<InClassMethodNode> |
| 29 | + */ |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return InClassMethodNode::class; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Process the nodes and stores value in the collector instance |
| 37 | + * |
| 38 | + * @phpstan-param InClassMethodNode $node |
| 39 | + * |
| 40 | + * @return string|null Collected data |
| 41 | + */ |
| 42 | + public function processNode(Node $node, Scope $scope): ?string |
| 43 | + { |
| 44 | + if ( ! $node instanceof InClassMethodNode) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + if ($this->isNotSpatieLaravelDataConstructor($node)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return serialize(new Constructor( |
| 53 | + $node->getMethodReflection()->getDeclaringClass()->getName(), |
| 54 | + collect($node->getOriginalNode()->getParams())->map($this->getParameterTypes(...))->all() |
| 55 | + )); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns a key-value mapping of the parameter name and its allowed types |
| 60 | + * |
| 61 | + * @param Param $parameter |
| 62 | + * |
| 63 | + * @return KeyTypePair |
| 64 | + */ |
| 65 | + private function getParameterTypes(Param $parameter): KeyTypePair |
| 66 | + { |
| 67 | + return new KeyTypePair( |
| 68 | + $this->getParameterName($parameter), |
| 69 | + UnionType::fromRaw($this->parseType($parameter->type)), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param null|Identifier|Name|ComplexType $type |
| 75 | + * |
| 76 | + * @return array<int, array<int, string>> |
| 77 | + */ |
| 78 | + private function parseType($type): array |
| 79 | + { |
| 80 | + // If no type is defined, then return mixed. |
| 81 | + if ($type === null) { |
| 82 | + return [['mixed']]; |
| 83 | + } |
| 84 | + |
| 85 | + // Simple type (int, string, bool) |
| 86 | + if ($type instanceof Identifier) { |
| 87 | + return [[$type->name]]; |
| 88 | + } |
| 89 | + |
| 90 | + // Class types |
| 91 | + if ($type instanceof Name) { |
| 92 | + // We do not support special type checking (self, parent, static) |
| 93 | + // since we are unlikely to use this feature, |
| 94 | + // and implementing it is currently not straight forward. |
| 95 | + if ($type->isSpecialClassName()) { |
| 96 | + return [['mixed']]; |
| 97 | + } |
| 98 | + |
| 99 | + return [[$type->toCodeString()]]; |
| 100 | + } |
| 101 | + |
| 102 | + // Complex types |
| 103 | + if ($type instanceof Node\ComplexType) { |
| 104 | + if ($type instanceof Node\NullableType) { |
| 105 | + return [ |
| 106 | + ...$this->parseType($type->type), |
| 107 | + ['null'], |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + if ($type instanceof Node\UnionType) { |
| 112 | + return collect($type->types) |
| 113 | + ->map(fn ($unionType) => $this->parseType($unionType)) |
| 114 | + ->flatten(1) |
| 115 | + ->all(); |
| 116 | + } |
| 117 | + |
| 118 | + if ($type instanceof Node\IntersectionType) { |
| 119 | + return [ |
| 120 | + collect($type->types) |
| 121 | + ->map(fn ($intersectionType) => $this->parseType($intersectionType)) |
| 122 | + ->flatten(2) |
| 123 | + ->all(), |
| 124 | + ]; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return [['mixed']]; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the name of the given parameter |
| 133 | + * |
| 134 | + * @param Param $parameter |
| 135 | + * |
| 136 | + * @return string |
| 137 | + */ |
| 138 | + private function getParameterName(Param $parameter): string |
| 139 | + { |
| 140 | + if ( ! is_string($parameter->var->name)) { |
| 141 | + throw new RuntimeException('A constructor property name cannot be an expression'); |
| 142 | + } |
| 143 | + |
| 144 | + return $parameter->var->name; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns true if the given node is not a laravel data constructor |
| 149 | + * |
| 150 | + * @param InClassMethodNode $node |
| 151 | + * |
| 152 | + * @return bool |
| 153 | + */ |
| 154 | + private function isNotSpatieLaravelDataConstructor(InClassMethodNode $node): bool |
| 155 | + { |
| 156 | + return $this->isNotConstructor($node) |
| 157 | + || $this->isNotSpatieLaravelDataClass($node->getMethodReflection()->getDeclaringClass()); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns true if the given node is not a constructor class |
| 162 | + * |
| 163 | + * @param InClassMethodNode $node |
| 164 | + * |
| 165 | + * @return bool |
| 166 | + */ |
| 167 | + private function isNotConstructor(InClassMethodNode $node): bool |
| 168 | + { |
| 169 | + return $node->getMethodReflection()->getName() !== '__construct'; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Returns true if the given class is not a laravel data class |
| 174 | + * |
| 175 | + * @param ClassReflection $class |
| 176 | + * |
| 177 | + * @return bool |
| 178 | + */ |
| 179 | + private function isNotSpatieLaravelDataClass(ClassReflection $class): bool |
| 180 | + { |
| 181 | + return ! in_array(Data::class, $class->getParentClassesNames(), true); |
| 182 | + } |
| 183 | +} |
0 commit comments