|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tpetry\PostgresqlEnhanced\Support\Phpstan; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Database\Query\Builder as BuilderContract; |
| 8 | +use Illuminate\Database\Query\Builder as BuilderQuery; |
| 9 | +use Illuminate\Database\Schema\IndexDefinition; |
| 10 | +use PHPStan\Reflection\ClassReflection; |
| 11 | +use PHPStan\Reflection\FunctionVariant; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +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; |
| 26 | + |
| 27 | +class SchemaIndexDefinitionExtension implements MethodsClassReflectionExtension |
| 28 | +{ |
| 29 | + public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 30 | + { |
| 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 | + 'weight' => new ReflectedMethod($classReflection, $methodName, [ |
| 37 | + $this->createFunctionVariant([new ReflectedParameter('labels', new ArrayType(new IntegerType(), new StringType()))]), |
| 38 | + ]), |
| 39 | + 'where' => new ReflectedMethod($classReflection, $methodName, [ |
| 40 | + $this->createFunctionVariant([new ReflectedParameter('columns', new StringType())]), |
| 41 | + $this->createFunctionVariant([new ReflectedParameter('columns', new CallableType([new ReflectedParameter('builder', new ObjectType(BuilderContract::class))], new ObjectType(BuilderContract::class), false))]), |
| 42 | + $this->createFunctionVariant([new ReflectedParameter('columns', new CallableType([new ReflectedParameter('builder', new ObjectType(BuilderQuery::class))], new ObjectType(BuilderContract::class), false))]), |
| 43 | + ]), |
| 44 | + 'with' => new ReflectedMethod($classReflection, $methodName, [ |
| 45 | + $this->createFunctionVariant([new ReflectedParameter('options', new ArrayType(new StringType(), TypeCombinator::union(new BooleanType(), new FloatType(), new IntegerType(), new StringType())))]), |
| 46 | + ]), |
| 47 | + default => throw new UnexpectedValueException("'{$methodName}' is not defined for IndexDefinition."), |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 52 | + { |
| 53 | + if (IndexDefinition::class !== $classReflection->getName()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return \in_array($methodName, ['include', 'weight', 'where', 'with']); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param array<int, \PHPStan\Reflection\ParameterReflection> $parameters |
| 62 | + */ |
| 63 | + private function createFunctionVariant(array $parameters): FunctionVariant |
| 64 | + { |
| 65 | + return new FunctionVariant( |
| 66 | + templateTypeMap: TemplateTypeMap::createEmpty(), |
| 67 | + resolvedTemplateTypeMap: null, |
| 68 | + parameters: $parameters, |
| 69 | + isVariadic: false, |
| 70 | + returnType: new ObjectType(IndexDefinition::class), |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private function createReflectedMethod(ClassReflection $classReflection, string $methodName, array $variants): ReflectedMethod |
| 75 | + { |
| 76 | + return new ReflectedMethod( |
| 77 | + classReflection: $classReflection, |
| 78 | + name: $methodName, |
| 79 | + variants: [ |
| 80 | + $this->createFunctionVariant([new ReflectedParameter('columns', new StringType())]), |
| 81 | + $this->createFunctionVariant([new ReflectedParameter('columns', new ArrayType(new IntegerType(), new StringType()))]), |
| 82 | + ], |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
0 commit comments