From ee3d48c828b9ea40b6babe8f41652b1ff80a985a Mon Sep 17 00:00:00 2001 From: 3onyc <3onyc@x3tech.com> Date: Thu, 10 May 2018 17:04:34 +0200 Subject: [PATCH] [WIP] Very rudimentary starting point for Model::where*() methods --- src/BuilderMethodExtension.php | 29 +++++++++++++ src/CustomMethod.php | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/CustomMethod.php diff --git a/src/BuilderMethodExtension.php b/src/BuilderMethodExtension.php index 8b137fa..4e0dbde 100644 --- a/src/BuilderMethodExtension.php +++ b/src/BuilderMethodExtension.php @@ -70,6 +70,10 @@ public function hasMethod(ClassReflection $classReflection, string $methodName): $this->methods[$classReflection->getName()] += $this->createMethods($classReflection, $queryBuilder); } + if ($classReflection->isSubclassOf(Model::class)) { + $this->methods[$classReflection->getName()] += $this->createModelMethod($classReflection, $methodName); + } + if ($classReflection->getName() === Builder::class && !isset($this->methods[Builder::class])) { $queryBuilder = $this->broker->getClass(QueryBuilder::class); $this->methods[Builder::class] = $this->createMethods($classReflection, $queryBuilder); @@ -127,4 +131,29 @@ private function createWrappedMethods(ClassReflection $classReflection, ClassRef return $methods; } + + /** + * @param ClassReflection $classReflection + * + * @return \PHPStan\Reflection\MethodReflection + */ + private function createModelMethod(ClassReflection $classReflection, string $methodName): array + { + if (strpos($methodName, 'where') !== 0) { + return []; + } + + $propertyName = strtolower(substr($methodName, 5, 1)) . substr($methodName, 6); + if (!$classReflection->hasProperty($propertyName)) { + return []; + } + + return [ + $methodName => new CustomMethod( + $classReflection, + $methodName, + $classReflection->getName() + ) + ]; + } } diff --git a/src/CustomMethod.php b/src/CustomMethod.php new file mode 100644 index 0000000..3461675 --- /dev/null +++ b/src/CustomMethod.php @@ -0,0 +1,78 @@ +class = $class; + $this->name = $name; + $this->returnType = $returnType; + } + + public function getDeclaringClass(): ClassReflection + { + return $this->class; + } + + public function getPrototype(): MethodReflection + { + return $this; + } + + public function isStatic(): bool + { + return true; + } + + public function isPrivate(): bool + { + return false; + } + + public function isPublic(): bool + { + return true; + } + + public function getName(): string + { + return $this->name; + } + + /** + * @return \PHPStan\Reflection\ParameterReflection[] + */ + public function getParameters(): array + { + return []; + } + + public function isVariadic(): bool + { + return true; + } + + public function getReturnType(): Type + { + if (is_array($this->returnType)) { + return new UnionType($this->returnType); + } + + return new ObjectType($this->returnType); + } +}