|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Recoded\PHPStanLaravel\Extensions\Eloquent; |
| 6 | + |
| 7 | +use PHPStan\Reflection\ClassMemberReflection; |
| 8 | +use PHPStan\Reflection\ClassReflection; |
| 9 | +use PHPStan\Reflection\FunctionVariant; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Reflection\ParametersAcceptor; |
| 12 | +use PHPStan\TrinaryLogic; |
| 13 | +use PHPStan\Type\NeverType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\ThisType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | + |
| 18 | +final class RelationMixinMethodReflection implements MethodReflection |
| 19 | +{ |
| 20 | + /** @var \PHPStan\Reflection\ParametersAcceptor[] */ |
| 21 | + private array $variants; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private ClassReflection $classReflection, |
| 25 | + private MethodReflection $methodReflection, |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + public function getDeclaringClass(): ClassReflection |
| 30 | + { |
| 31 | + return $this->methodReflection->getDeclaringClass(); |
| 32 | + } |
| 33 | + |
| 34 | + public function isStatic(): bool |
| 35 | + { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + public function isPrivate(): bool |
| 40 | + { |
| 41 | + return $this->methodReflection->isPrivate(); |
| 42 | + } |
| 43 | + |
| 44 | + public function isPublic(): bool |
| 45 | + { |
| 46 | + return $this->methodReflection->isPublic(); |
| 47 | + } |
| 48 | + |
| 49 | + public function getDocComment(): ?string |
| 50 | + { |
| 51 | + return $this->methodReflection->getDocComment(); |
| 52 | + } |
| 53 | + |
| 54 | + public function getName(): string |
| 55 | + { |
| 56 | + return $this->methodReflection->getName(); |
| 57 | + } |
| 58 | + |
| 59 | + public function getPrototype(): ClassMemberReflection |
| 60 | + { |
| 61 | + return $this->methodReflection->getPrototype(); |
| 62 | + } |
| 63 | + |
| 64 | + public function getVariants(): array |
| 65 | + { |
| 66 | + if (isset($this->variants)) { |
| 67 | + return $this->variants; |
| 68 | + } |
| 69 | + |
| 70 | + $builderType = new ObjectType('Illuminate\Database\Eloquent\Builder'); |
| 71 | + $thisType = new ThisType($this->classReflection); |
| 72 | + |
| 73 | + return $this->variants = array_map( |
| 74 | + static function (ParametersAcceptor $variant) use ($builderType, $thisType) { |
| 75 | + $returnsBuilder = $variant->getReturnType() instanceof NeverType === false |
| 76 | + && $builderType->isSuperTypeOf($variant->getReturnType())->yes(); |
| 77 | + |
| 78 | + return new FunctionVariant( |
| 79 | + $variant->getTemplateTypeMap(), |
| 80 | + $variant->getResolvedTemplateTypeMap(), |
| 81 | + $variant->getParameters(), |
| 82 | + $variant->isVariadic(), |
| 83 | + $returnsBuilder ? $thisType : $variant->getReturnType(), |
| 84 | + ); |
| 85 | + }, |
| 86 | + $this->methodReflection->getVariants(), |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function isDeprecated(): TrinaryLogic |
| 91 | + { |
| 92 | + return $this->methodReflection->isDeprecated(); |
| 93 | + } |
| 94 | + |
| 95 | + public function getDeprecatedDescription(): ?string |
| 96 | + { |
| 97 | + return $this->methodReflection->getDeprecatedDescription(); |
| 98 | + } |
| 99 | + |
| 100 | + public function isFinal(): TrinaryLogic |
| 101 | + { |
| 102 | + return $this->methodReflection->isFinal(); |
| 103 | + } |
| 104 | + |
| 105 | + public function isInternal(): TrinaryLogic |
| 106 | + { |
| 107 | + return $this->methodReflection->isInternal(); |
| 108 | + } |
| 109 | + |
| 110 | + public function getThrowType(): ?Type |
| 111 | + { |
| 112 | + return $this->methodReflection->getThrowType(); |
| 113 | + } |
| 114 | + |
| 115 | + public function hasSideEffects(): TrinaryLogic |
| 116 | + { |
| 117 | + return $this->methodReflection->hasSideEffects(); |
| 118 | + } |
| 119 | +} |
0 commit comments