Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Very rudimentary starting point for Model::where*() methods #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/BuilderMethodExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
)
];
}
}
78 changes: 78 additions & 0 deletions src/CustomMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types = 1);

namespace Weebly\PHPStan\Laravel;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

class CustomMethod implements MethodReflection
{
protected $class;
protected $name;
protected $returnType;

public function __construct(
ClassReflection $class,
string $name,
$returnType
) {
$this->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);
}
}