|
4 | 4 |
|
5 | 5 | namespace Tpetry\PostgresqlEnhanced\Support\Phpstan;
|
6 | 6 |
|
7 |
| -use Illuminate\Contracts\Database\Query\Builder as BuilderContract; |
8 |
| -use Illuminate\Database\Query\Builder as BuilderQuery; |
9 |
| -use Illuminate\Database\Schema\IndexDefinition; |
| 7 | +use Illuminate\Database\Schema\IndexDefinition as BaseIndexDefinition; |
10 | 8 | use PHPStan\Reflection\ClassReflection;
|
11 |
| -use PHPStan\Reflection\FunctionVariant; |
12 | 9 | use PHPStan\Reflection\MethodReflection;
|
13 | 10 | use PHPStan\Reflection\MethodsClassReflectionExtension;
|
14 |
| -use PHPStan\Type\ArrayType; |
15 |
| -use PHPStan\Type\BooleanType; |
16 |
| -use PHPStan\Type\CallableType; |
17 |
| -use PHPStan\Type\FloatType; |
18 |
| -use PHPStan\Type\Generic\TemplateTypeMap; |
19 |
| -use PHPStan\Type\IntegerType; |
20 |
| -use PHPStan\Type\ObjectType; |
21 |
| -use PHPStan\Type\StringType; |
22 |
| -use PHPStan\Type\TypeCombinator; |
23 |
| -use Tpetry\PostgresqlEnhanced\Support\Phpstan\Values\ReflectedMethod; |
24 |
| -use Tpetry\PostgresqlEnhanced\Support\Phpstan\Values\ReflectedParameter; |
25 |
| -use UnexpectedValueException; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use Tpetry\PostgresqlEnhanced\Schema\IndexDefinition; |
26 | 13 |
|
27 | 14 | class SchemaIndexDefinitionExtension implements MethodsClassReflectionExtension
|
28 | 15 | {
|
| 16 | + public function __construct( |
| 17 | + private ReflectionProvider $reflectionProvider, |
| 18 | + ) { |
| 19 | + } |
| 20 | + |
29 | 21 | public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
|
30 | 22 | {
|
31 |
| - return match ($methodName) { |
32 |
| - 'include' => new ReflectedMethod($classReflection, $methodName, [ |
33 |
| - $this->createFunctionVariant([new ReflectedParameter('columns', new StringType())]), |
34 |
| - $this->createFunctionVariant([new ReflectedParameter('columns', new ArrayType(new IntegerType(), new StringType()))]), |
35 |
| - ]), |
36 |
| - 'nullsNotDistinct' => new ReflectedMethod($classReflection, $methodName, [ |
37 |
| - $this->createFunctionVariant([]), |
38 |
| - ]), |
39 |
| - 'weight' => new ReflectedMethod($classReflection, $methodName, [ |
40 |
| - $this->createFunctionVariant([new ReflectedParameter('labels', new ArrayType(new IntegerType(), new StringType()))]), |
41 |
| - ]), |
42 |
| - 'where' => new ReflectedMethod($classReflection, $methodName, [ |
43 |
| - $this->createFunctionVariant([new ReflectedParameter('columns', new StringType())]), |
44 |
| - $this->createFunctionVariant([new ReflectedParameter('columns', new CallableType([new ReflectedParameter('builder', new ObjectType(BuilderContract::class))], new ObjectType(BuilderContract::class), false))]), |
45 |
| - $this->createFunctionVariant([new ReflectedParameter('columns', new CallableType([new ReflectedParameter('builder', new ObjectType(BuilderQuery::class))], new ObjectType(BuilderContract::class), false))]), |
46 |
| - ]), |
47 |
| - 'with' => new ReflectedMethod($classReflection, $methodName, [ |
48 |
| - $this->createFunctionVariant([new ReflectedParameter('options', new ArrayType(new StringType(), TypeCombinator::union(new BooleanType(), new FloatType(), new IntegerType(), new StringType())))]), |
49 |
| - ]), |
50 |
| - default => throw new UnexpectedValueException("'{$methodName}' is not defined for IndexDefinition."), |
51 |
| - }; |
| 23 | + return $this->reflectionProvider->getClass(IndexDefinition::class)->getNativeMethod($methodName); |
52 | 24 | }
|
53 | 25 |
|
54 | 26 | public function hasMethod(ClassReflection $classReflection, string $methodName): bool
|
55 | 27 | {
|
56 |
| - if (IndexDefinition::class !== $classReflection->getName()) { |
| 28 | + if (BaseIndexDefinition::class !== $classReflection->getName()) { |
57 | 29 | return false;
|
58 | 30 | }
|
59 | 31 |
|
60 |
| - return \in_array($methodName, ['include', 'nullsNotDistinct', 'weight', 'where', 'with']); |
61 |
| - } |
62 |
| - |
63 |
| - /** |
64 |
| - * @param array<int, \PHPStan\Reflection\ParameterReflection> $parameters |
65 |
| - */ |
66 |
| - private function createFunctionVariant(array $parameters): FunctionVariant |
67 |
| - { |
68 |
| - return new FunctionVariant( |
69 |
| - templateTypeMap: TemplateTypeMap::createEmpty(), |
70 |
| - resolvedTemplateTypeMap: null, |
71 |
| - parameters: $parameters, |
72 |
| - isVariadic: false, |
73 |
| - returnType: new ObjectType(IndexDefinition::class), |
74 |
| - ); |
| 32 | + return $this->reflectionProvider->getClass(IndexDefinition::class)->hasNativeMethod($methodName); |
75 | 33 | }
|
76 | 34 | }
|
0 commit comments