Larastan: Call to undefined method PhpParser\Node\Expr\Variable::toCodeString()

Created on 23 Oct 2019  路  5Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.4.3
  • --level used: 5

Description

I am getting the following error when running php artisan code:analyse --debug

   Symfony\Component\Debug\Exception\FatalThrowableError  : Call to undefined method PhpParser\Node\Expr\Variable::toCodeString()

  at /project/vendor/nunomaduro/larastan/src/ReturnTypes/EloquentBuilderExtension.php:81
    77| 
    78|         if ($methodCall->var instanceof StaticCall || $methodCall->var instanceof New_) {
    79|             /** @var FullyQualified $fullQualifiedClass */
    80|             $fullQualifiedClass = $methodCall->var->class;
  > 81|             $modelType = new ObjectType($fullQualifiedClass->toCodeString());
    82|         } elseif ($methodCall->var instanceof Variable || $methodCall->var instanceof PropertyFetch) {
    83|             /** @var ObjectType $modelType */
    84|             $modelType = $scope->getType($methodCall->var);
    85|         }

  Exception trace:

  1   NunoMaduro\Larastan\ReturnTypes\EloquentBuilderExtension::getTypeFromMethodCall(Object(PHPStan\Reflection\Php\PhpMethodReflection), Object(PhpParser\Node\Expr\MethodCall), Object(NunoMaduro\Larastan\Analyser\Scope))
      /project/vendor/phpstan/phpstan/src/Analyser/Scope.php:1432

  2   PHPStan\Analyser\Scope::resolveType(Object(PhpParser\Node\Expr\MethodCall))
      /project/vendor/phpstan/phpstan/src/Analyser/Scope.php:372

Laravel code where the issue was found

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class ScoutModelCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'scout:model {model} {id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import the given model into the search index';

    /**
     * Execute the console command.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function handle(Dispatcher $events)
    {
        /** @var Model $class */
        $class = $this->argument('model');
        $id = $this->argument('id');

        /** @var Searchable $object */
        $object = $class::query()->find($id);
        $object->searchable();
    }
}

It seems to be that calling methods on a class name variable is tripping up larastan.

bug

Most helpful comment

@radykal-com I don't think we will do that. Laravel 6 is the new LTS version. And 5.8 is stopped receiving bugfixes since over 7 months ago.

All 5 comments

Thank you! I'll look into it.

Ok. I can reproduce the error. But there is also an error on your part.

/** @var Model $class */
$class = $this->argument('model');

Annotation is here not correct. It should be string not Model

Is there any chance to apply the same fix to a 0.4.x branch and release it to make it work with laravel 5.8?

@radykal-com I don't think we will do that. Laravel 6 is the new LTS version. And 5.8 is stopped receiving bugfixes since over 7 months ago.

Yeah we know, but we have some dependencies that haven't been update to laravel 6.x so we cannot update yet. Nevermind, we will fork and apply the patch there.

Thanks you,

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdrieghe picture jdrieghe  路  4Comments

tranba picture tranba  路  4Comments

Gummibeer picture Gummibeer  路  3Comments

zlodes picture zlodes  路  3Comments

grcasanova picture grcasanova  路  4Comments