Skip to content

Commit

Permalink
Merge pull request #345 from lptn/fix-typo
Browse files Browse the repository at this point in the history
Support template syntax for Model::query() calls
  • Loading branch information
lptn authored Nov 4, 2023
2 parents 3a24345 + efc5606 commit f27cb2b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Handlers/Eloquent/ModelMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use Psalm\FileManipulation;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\MethodIdentifier;
use Psalm\LaravelPlugin\Util\ProxyMethodReturnTypeProvider;
Expand All @@ -20,6 +19,7 @@
use Psalm\Type;
use Psalm\Type\Union;

use function is_string;
use function strtolower;

final class ModelMethodHandler implements MethodReturnTypeProviderInterface, AfterClassLikeVisitInterface
Expand All @@ -39,12 +39,28 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
return null;
}

$called_fq_classlike_name = $event->getCalledFqClasslikeName();

if (! is_string($called_fq_classlike_name)) {
return null;
}

// Model::query()
if ($event->getMethodNameLowercase() === 'query') {
return new Union([
new Type\Atomic\TGenericObject(Builder::class, [
new Union([
new Type\Atomic\TNamedObject($called_fq_classlike_name),
]),
])
]);
}

// proxy to builder object
if ($event->getMethodNameLowercase() === '__callstatic') {
$called_fq_classlike_name = $event->getCalledFqClasslikeName();
$called_method_name_lowercase = $event->getCalledMethodNameLowercase();

if (!$called_fq_classlike_name || !$called_method_name_lowercase) {
if (!$called_method_name_lowercase) {
return null;
}
$methodId = new MethodIdentifier($called_fq_classlike_name, $called_method_name_lowercase);
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f27cb2b

Please sign in to comment.