|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Weebly\PHPStan\Laravel; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Builder; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Database\Query\Builder as QueryBuilder; |
| 8 | +use PHPStan\Broker\Broker; |
| 9 | +use PHPStan\Reflection\BrokerAwareExtension; |
| 10 | +use PHPStan\Reflection\ClassReflection; |
| 11 | +use PHPStan\Reflection\MethodsClassReflectionExtension; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | + |
| 14 | +class BuilderMethodExtension implements MethodsClassReflectionExtension, BrokerAwareExtension |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \PHPStan\Broker\Broker |
| 18 | + */ |
| 19 | + private $broker; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \PHPStan\Reflection\MethodReflection[] |
| 23 | + */ |
| 24 | + private $methods = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Weebly\PHPStan\Laravel\MethodReflectionFactory |
| 28 | + */ |
| 29 | + private $methodReflectionFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * BuilderMethodExtension constructor. |
| 33 | + * |
| 34 | + * @param \Weebly\PHPStan\Laravel\MethodReflectionFactory $methodReflectionFactory |
| 35 | + */ |
| 36 | + public function __construct(MethodReflectionFactory $methodReflectionFactory) |
| 37 | + { |
| 38 | + $this->methodReflectionFactory = $methodReflectionFactory; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritdoc |
| 43 | + */ |
| 44 | + public function setBroker(Broker $broker) |
| 45 | + { |
| 46 | + $this->broker = $broker; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritdoc |
| 51 | + */ |
| 52 | + public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 53 | + { |
| 54 | + if ($classReflection->isSubclassOf(Model::class) && !isset($this->methods[$classReflection->getName()])) { |
| 55 | + $builder = $this->broker->getClass(Builder::class); |
| 56 | + $this->methods[$classReflection->getName()] = $this->createWrappedMethods($classReflection, $builder); |
| 57 | + |
| 58 | + $queryBuilder = $this->broker->getClass(QueryBuilder::class); |
| 59 | + $this->methods[$classReflection->getName()] += $this->createMethods($classReflection, $queryBuilder); |
| 60 | + } |
| 61 | + |
| 62 | + if ($classReflection->getName() === Builder::class && !isset($this->methods[Builder::class])) { |
| 63 | + $queryBuilder = $this->broker->getClass(QueryBuilder::class); |
| 64 | + $this->methods[Builder::class] = $this->createMethods($classReflection, $queryBuilder); |
| 65 | + } |
| 66 | + |
| 67 | + return isset($this->methods[$classReflection->getName()][$methodName]); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritdoc |
| 72 | + */ |
| 73 | + public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 74 | + { |
| 75 | + return $this->methods[$classReflection->getName()][$methodName]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param \PHPStan\Reflection\ClassReflection $classReflection |
| 80 | + * @param \PHPStan\Reflection\ClassReflection $queryBuilder |
| 81 | + * |
| 82 | + * @return \PHPStan\Reflection\MethodReflection[] |
| 83 | + */ |
| 84 | + private function createMethods(ClassReflection $classReflection, ClassReflection $queryBuilder): array |
| 85 | + { |
| 86 | + $methods = []; |
| 87 | + foreach ($queryBuilder->getNativeReflection()->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
| 88 | + if ($method->isStatic()) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $methods[$method->getName()] = $this->methodReflectionFactory->create($classReflection, $method); |
| 93 | + } |
| 94 | + |
| 95 | + return $methods; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param ClassReflection $classReflection |
| 100 | + * @param \PHPStan\Reflection\ClassReflection $builder |
| 101 | + * |
| 102 | + * @return \PHPStan\Reflection\MethodReflection[] |
| 103 | + * |
| 104 | + * @throws \PHPStan\ShouldNotHappenException |
| 105 | + */ |
| 106 | + private function createWrappedMethods(ClassReflection $classReflection, ClassReflection $builder): array |
| 107 | + { |
| 108 | + $methods = []; |
| 109 | + foreach ($builder->getNativeReflection()->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
| 110 | + $methods[$method->getName()] = $this->methodReflectionFactory->create( |
| 111 | + $classReflection, |
| 112 | + $method, |
| 113 | + ReflectionMethodAlwaysStatic::class |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return $methods; |
| 118 | + } |
| 119 | +} |
0 commit comments