Larastan: Bug with autoloading on phpstan 0.12.22

Created on 4 May 2020  路  20Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.5.7
  • PhpStan Version: 0.12.22
  • --level used: max

Description

For latest version of phpstan (0.12.22)!

On analyze action:

 ErrorException thrown in /builds/trv3/core/vendor/nunomaduro/larastan/src/ApplicationResolver.php on line 109 while loading bootstrap file /builds/trv3/core/vendor/phpstan/phpstan/../../nunomaduro/larastan/bootstrap.php: Undefined index: AI\Core\

Method ApplicationResolver@getProjectSearchDirs has wrong composer dir:

Expected: /home/vagrant/projects/core/vendor/composer
Actual: phar:///home/vagrant/projects/ai/core/vendor/phpstan/phpstan/phpstan/vendor/composer

phpstan code reference: https://github.com/phpstan/phpstan-src/commit/a1d3990c95b7a064a0451111153a6bef39f5d9a2

bug

Most helpful comment

PHPStan 0.12.21 gives the same error for me, using 0.12.20 however works

All 20 comments

Thank you for your report.

We're experiencing the same issue.

Yep, Larastan's own tests are also failing for the same reason.

You can use PHPStan 0.12.21 0.12.20 until we fix the issue.

PHPStan 0.12.21 gives the same error for me, using 0.12.20 however works

cc @ondrejmirtes.

Looks like the issue is here: https://github.com/nunomaduro/larastan/blob/master/src/ApplicationResolver.php#L102-L104

When we request ClassLoader via reflection looks like it gets the vendor/phpstan/phpstan/phpstan/vendor/composer/ClassLoader.php file. Not the vendor/composer/ClassLoader.php

Right now I don't have the time to look deeper. Just a pointer if anyone wants to attempt a fix.

The function https://www.php.net/manual/en/function.realpath.php may help to solve this.

Can you explain what the code in https://github.com/nunomaduro/larastan/blob/master/src/ApplicationResolver.php does and why are you inspecting the PSR4 maps?

I'm quite sure you could ask PHPStan for %composerAutoloaderProjectPaths% instead and discover autoloaders that way. It might return up to three autoloader paths I think.

I have installed a fresh Laravel application with Larastan. And it works. Can someone explaining me how can I reproduce the issue?

Well it鈥檚 true that the ClassLoader inside PHAR gets loaded as the first one now so the behaviour others are seeing in this issue is expected...

When I tried to run Larastans tests with composer test it was failing for the same reason as in the original issue.

Got it. Give me a second.

Can you explain what the code in /src/ApplicationResolver.php@master does and why are you inspecting the PSR4 maps?

I'm quite sure you could ask PHPStan for %composerAutoloaderProjectPaths% instead and discover autoloaders that way. It might return up to three autoloader paths I think.

In that class, we are trying to find all the service providers that the application uses. And register them to the Laravel app.

I am a little bit tired today, maybe someone on the community would like to jump in: https://twitter.com/enunomaduro/status/1257400309212680197.

Something along these lines will probably be sufficient: https://github.com/nunomaduro/larastan/pull/561

You don't even need my suggestion with %composerAutoloaderProjectPaths% because I noticed that you're getting composer.json from the current working directory anyway, so there's no need for reflection to tell us the path to the Composer class loader...

I am a little bit tired today, maybe someone on the community would like to jump in: https://twitter.com/enunomaduro/status/1257400309212680197.

The problem is related to the ApplicationResolver class which is only used in a package environment and not in a Laravel Application environment (see bootstrap.php file). The first autoload.psr-4 namespace (first key) is read from the composer.json in order to resolve all project classes. The method getProjectSearchDirs is intent to obtain the relative directory with source code (in my case ./src).

I think the solution is maybe a lot simpler because the source code directory is also in the composer.json file, namely the corresponding value within autoload.psr-4.

Finally, some points which might be relevant:

  1. I wonder why only the first item from autoload.psr-4 is read from composer.json. There could be multiple items here right?

  2. It might be a good moment to reflect on the relation with the 'paths' parameter in the phpstan.neon file. In my case this also points to the ./src directory.

  3. The main task of the ApplicationResolver is to register all the serviceproviders. These might also be found in the composer.json -> extra.laravel.providers.

https://github.com/nunomaduro/larastan/pull/561 fixes this issue.

@SocyList I just restored how it previously worked before I changed the order of autoloaders in PHPStan itself. Of course we could take a deeper look at the code but it's not necessary now.

Faced with this issue.
Fixed by requiring laravel framework with composer

composer req --dev laravel/framework

without laravel it show mentioned error with unknown key

Version of larastan is: v0.7.5

Could you tell us about your project that uses Larastan but not Laravel?

I worked with lot of laravel projects. As well I write some small composer packages for these projects.
Sometimes I explicitly require laravel as dev dependency for package. My last attempt was not required to install laravel, but I tried to configure larastan as usual with composer req nunomaduro/larastan and it started to run but hungs with mentioned error

As well I would mention that orchestra/testbench-dusk package also required and it is minimal to have it together with laravel for start using larastan.
Probably these packages should be configured as dependencies for larastan, but up to you.

I left my comment just to help somebody else faced with this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

szepeviktor picture szepeviktor  路  4Comments

JeroenVanOort picture JeroenVanOort  路  3Comments

tranba picture tranba  路  4Comments

gitetsu picture gitetsu  路  4Comments

AJenbo picture AJenbo  路  4Comments