larastan / larastan

⚗️ Adds code analysis to Laravel improving developer productivity and code quality.
MIT License
5.53k stars 416 forks source link

Internal error: Class App\Models\User was not found while trying to analyse it - discovering symbols is probably not configured properly. while analysing file /Users/joshuacagle/newtrul/crm/app/Listeners/UpdateLastLoginOnLogin.php #2035

Closed 5thAgent closed 1 month ago

5thAgent commented 1 month ago

Description

When running phpstan I consistenly get the following internal error: Internal error: Class App\Models\User was not found while trying to analyse it - discovering symbols is probably not configured properly. while analysing file /Users/joshuacagle/newtrul/crm/app/Listeners/UpdateLastLoginOnLogin.php

The file in question does not use the user model which is located in App\User\Models.

Here's the error and its stack trace:

 -- --------------------------------------------------------------------------------------------------------------------------------------------------
     Error
 -- --------------------------------------------------------------------------------------------------------------------------------------------------
     Internal error: Class App\Models\User was not found while trying to analyse it - discovering symbols is probably not configured
     properly. while analysing file /Users/joshuacagle/newtrul/crm/app/Listeners/UpdateLastLoginOnLogin.php
     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:
     ##
     phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflection/BetterReflectionProvider.php(182)
     #0
     phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProvider/MemoizingReflectionProvider.php(43):
     PHPStan\Reflection\BetterReflection\BetterReflectionProvider->getClass('App\\Models\\User')
     #1 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Passable.php(96):
     PHPStan\Reflection\ReflectionProvider\MemoizingReflectionProvider->getClass('App\\Models\\User')
     #2 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Pipes/Auths.php(46):
     Larastan\Larastan\Methods\Passable->sendToPipeline('App\\Models\\User')
     #3 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183):
     Larastan\Larastan\Methods\Pipes\Auths->handle(Object(Larastan\Larastan\Methods\Passable), Object(Closure))
     #4 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Pipes/Managers.php(51):
     Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Larastan\Larastan\Methods\Passable))
     #5 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183):
     Larastan\Larastan\Methods\Pipes\Managers->handle(Object(Larastan\Larastan\Methods\Passable), Object(Closure))
     #6 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Pipes/Facades.php(68):
     Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Larastan\Larastan\Methods\Passable))
     #7 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183):
     Larastan\Larastan\Methods\Pipes\Facades->handle(Object(Larastan\Larastan\Methods\Passable), Object(Closure))
     #8 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Pipes/Contracts.php(35):
     Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Larastan\Larastan\Methods\Passable))
     #9 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183):
     Larastan\Larastan\Methods\Pipes\Contracts->handle(Object(Larastan\Larastan\Methods\Passable), Object(Closure))
     #10 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Pipes/SelfClass.php(23):
     Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Larastan\Larastan\Methods\Passable))
     #11 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183):
     Larastan\Larastan\Methods\Pipes\SelfClass->handle(Object(Larastan\Larastan\Methods\Passable), Object(Closure))
     #12 /Users/joshuacagle/newtrul/crm/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119):
     Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Larastan\Larastan\Methods\Passable))
     #13 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Kernel.php(41):
     Illuminate\Pipeline\Pipeline->then(Object(Closure))
     #14 /Users/joshuacagle/newtrul/crm/vendor/larastan/larastan/src/Methods/Extension.php(39):
     Larastan\Larastan\Methods\Kernel->handle(Object(PHPStan\Reflection\ClassReflection), 'forceFill')
     #15 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ClassReflection.php(515):
     Larastan\Larastan\Methods\Extension->hasMethod(Object(PHPStan\Reflection\ClassReflection), 'forceFill')
     #16 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Type/ObjectType.php(562):
     PHPStan\Reflection\ClassReflection->hasMethod('forceFill')
     #17 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3869):
     PHPStan\Type\ObjectType->hasMethod('forceFill')
     #18 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3897):
     PHPStan\Analyser\MutatingScope->filterTypeWithMethod(Object(PHPStan\Type\ObjectType), 'forceFill')
     #19 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1616):
     PHPStan\Analyser\MutatingScope->methodCallReturnType(Object(PHPStan\Type\ObjectType), 'forceFill',
     Object(PhpParser\Node\Expr\MethodCall))
     #20 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1622):
     PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
     #21 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(600):
     PHPStan\Analyser\MutatingScope->resolveType('$event->user->f...', Object(PhpParser\Node\Expr\MethodCall))
     #22 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1616):
     PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
     #23 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1622):
     PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
     #24 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(600):
     PHPStan\Analyser\MutatingScope->resolveType('$event->user->f...', Object(PhpParser\Node\Expr\MethodCall))
     #25 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(1798):
     PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
     #26 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(724):
     PHPStan\Analyser\NodeScopeResolver->findEarlyTerminatingExpr(Object(PhpParser\Node\Expr\MethodCall),
     Object(PHPStan\Analyser\MutatingScope))
     #27 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(464):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Expression), Object(PHPStan\Analyser\MutatingScope),
     Object(Closure), Object(PHPStan\Analyser\StatementContext))
     #28 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(652):
     PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\ClassMethod), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     #29 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(464):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\ClassMethod),
     Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))
     #30 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(789):
     PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Class_), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))
     #31 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(464):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope),
     Object(Closure), Object(PHPStan\Analyser\StatementContext))
     #32 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(759):
     PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Namespace_), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     #33 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(429):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Namespace_), Object(PHPStan\Analyser\MutatingScope),
     Object(Closure), Object(PHPStan\Analyser\StatementContext))
     #34 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(179):
     PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
     #35 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(139):
     PHPStan\Analyser\FileAnalyser->analyseFile('/Users/joshuaca...', Array, Object(PHPStan\Rules\LazyRegistry),
     Object(PHPStan\Collectors\Registry), NULL)
     #36
     phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/EventEmitterTrait.php(111):
     PHPStan\Command\WorkerCommand::PHPStan\Command\{closure}(Array)
     #37 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(117):
     _PHPStan_4f7beffdf\Evenement\EventEmitter->emit('data', Array)
     #38
     phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/EventEmitterTrait.php(111):
     _PHPStan_4f7beffdf\Clue\React\NDJson\Decoder->handleData(Array)
     #39 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62):
     _PHPStan_4f7beffdf\Evenement\EventEmitter->emit('data', Array)
     #40
     phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/EventEmitterTrait.php(111):
     _PHPStan_4f7beffdf\React\Stream\Util::_PHPStan_4f7beffdf\React\Stream\{closure}('{"action":"anal...')
     #41 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(168):
     _PHPStan_4f7beffdf\Evenement\EventEmitter->emit('data', Array)
     #42 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(201):
     _PHPStan_4f7beffdf\React\Stream\DuplexResourceStream->handleData(Resource id #7621)
     #43 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(173):
     _PHPStan_4f7beffdf\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
     #44 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(99):
     _PHPStan_4f7beffdf\React\EventLoop\StreamSelectLoop->run()
     #45 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259):
     PHPStan\Command\WorkerCommand->execute(Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Output\ConsoleOutput))
     #46 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(870):
     _PHPStan_4f7beffdf\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Output\ConsoleOutput))
     #47 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(261):
     _PHPStan_4f7beffdf\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\WorkerCommand),
     Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Output\ConsoleOutput))
     #48 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157):
     _PHPStan_4f7beffdf\Symfony\Component\Console\Application->doRun(Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_4f7beffdf\Symfony\Component\Console\Output\ConsoleOutput))
     #49 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(124):
     _PHPStan_4f7beffdf\Symfony\Component\Console\Application->run()
     #50 phar:///Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(125): _PHPStan_4f7beffdf\{closure}()
     #51 /Users/joshuacagle/newtrul/crm/vendor/phpstan/phpstan/phpstan(8): require('phar:///Users/j...')
     #52 /Users/joshuacagle/newtrul/crm/vendor/bin/phpstan(119): include('/Users/joshuaca...')
     #53 {main}
 -- --------------------------------------------------------------------------------------------------------------------------------------------------

Laravel code where the issue was found

<?php

namespace App\Listeners;

use Carbon\Carbon;
use Illuminate\Auth\Events\Authenticated;

class UpdateLastLoginOnLogin
{
    /**
     * Handle the event.
     *
     * @param  \Illuminate\Auth\Events\Authenticated  $event
     * @return void
     */
    public function handle(Authenticated $event)
    {
        $event->user->forceFill([
            'last_login_at' => Carbon::now(),
            'last_login_ip' => request()->ip(),
        ])->save();
    }
}

I was directed here after reporting the issue with phpstan.

calebdw commented 1 month ago

Can you show your phpstan config? And it might be helpful to show your config/auth.php

ondrejmirtes commented 1 month ago

Before asking for BetterReflectionProvider->getClass() you should guard that call with BetterReflectionProvider->hasClass() before that.