Skip to content

Commit

Permalink
fix: handle the case when a custom query builder does not have generi…
Browse files Browse the repository at this point in the history
…c annotations
  • Loading branch information
canvural committed Jul 24, 2020
1 parent 29a9023 commit c54b517
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Methods/EloquentBuilderForwardsCallsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace NunoMaduro\Larastan\Methods;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use NunoMaduro\Larastan\Concerns;
use NunoMaduro\Larastan\Reflection\EloquentBuilderMethodReflection;
Expand Down Expand Up @@ -89,9 +90,13 @@ public function getMethod(ClassReflection $classReflection, string $methodName):

$templateTypeMap = $classReflection->getActiveTemplateTypeMap();

/** @var Type|ObjectType|TemplateMixedType $modelType */
/** @var Type|ObjectType|TemplateMixedType|Null $modelType */
$modelType = $templateTypeMap->getType('TModelClass');

if ($modelType === null) {
$modelType = new ObjectType(Model::class);
}

if ($this->getBuilderReflection()->hasNativeMethod($methodName) && (! $modelType instanceof ObjectType)) {
$methodReflection = $this->getBuilderReflection()->getNativeMethod($methodName);
$builderClass = EloquentBuilder::class;
Expand All @@ -113,7 +118,13 @@ public function getMethod(ClassReflection $classReflection, string $methodName):

if ($modelType instanceof ObjectType) {
$builderHelper = new BuilderHelper($this->getBroker());
$eloquentBuilderClass = $builderHelper->determineBuilderType($modelType->getClassName());

if ($classReflection->isSubclassOf(EloquentBuilder::class)) {
$eloquentBuilderClass = $classReflection->getName();
} else {
$eloquentBuilderClass = $builderHelper->determineBuilderType($modelType->getClassName());
}

$returnMethodReflection = $builderHelper->getMethodReflectionFromBuilder(
$classReflection,
$methodName,
Expand Down
16 changes: 16 additions & 0 deletions tests/Features/ReturnTypes/CustomEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,19 @@ public function newEloquentBuilder($query): CustomBuilder2
class CustomBuilder2 extends Builder
{
}

abstract class AbstractQueryBuilder extends Builder
{
}

class QueryBuilderWithoutDocBlock extends AbstractQueryBuilder
{
public function whereFooBar(): QueryBuilderWithoutDocBlock
{
return $this
->leftJoin('foo', 'bar.id', '=', 'foo.bar')
->whereNull('bar.baz')
->orWhereNull('foo.bar')
->orWhereNotNull('foo.biz');
}
}

0 comments on commit c54b517

Please sign in to comment.