New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Classes aren't recognized when using a PHP extension as autoloader #7526
Comments
Hello, can you please create a small reproducing repository with a series of steps that lead to this problem? I tried to follow your instructions but failed - there's no |
Here you go: https://github.com/SMillerDev/phpstan-repro the Makefile default target should do all the things you need. I also added a target to install the extension, but you might have your own preference there. |
The problem is that the extension should load files by their absolute filenames but it doesn't do that, it loads I recommend you to use an alternative way to discover symbols, this parameters:
scanDirectories:
- Lunr.Config-0.7
- Lunr.Locator-0.7 Since the extension doesn't do anything special, PSR-0 / PSR-4 autoloading has been part of PHP ecosystem for a decade through Composer, I recommend you to use a different way of setting up PSR autoloading. Thanks for understanding. |
Sorry, I'm lost a little bit :( The autoloader constructs a relative file path that's then resolved against the configured include path by PHP. I don't really see where the absolute path would need to come in. It would have to traverse all configured members of the include path manually to do that. PHP already does that so why would it do that by hand?
|
PHPStan needs an absolute path to know which file to read to parse the sources. |
Alright, I get that. But |
Doing |
Since phpstan is trapping the filesystem calls, would it throw the exception after the first attempt and thus never get to the second one? Or is there perhaps something else to consider for a stacked autoloader scenario? |
No, this is the only file tried. I feel like it's not worth to spending more time on this - PSR-0/PSR-4 autoloading is a solved problem that doesn't require a custom PHP extension to achieve. I even gave you the right PHPStan configuration to fix it :) |
The problem is that doesn't work for us 😕 However, I also understand that you don't want to spend time on this. I'm not asking you to. But I hope you don't mind if I keep looking and try to get it working :) |
It's interesting that your extension is in a weird spot that's not currently covered by PHPStan locator mechanisms. If you used If you'd load absolute paths in your PHP extension autoloader, it'd also work. If you used PSR-4 autoloading through Composer, it'd work because that's how the majority of modern projects works. |
Perhaps even more interesting is that it actually is doing that, just not in the bootstrap file. It's calling |
@ondrejmirtes I found it :) However, not sure what the best solution is, so your input would be appreciated! Part 1 of the problem is that public function stream_open($path, $mode, $options, &$openedPath): bool
{
echo "stream_open: $path\n";
$exists = stream_resolve_include_path($path);
if ($exists)
{
self::$autoloadLocatedFiles[] = $path;
}
$this->readFromFile = false;
$this->seekPosition = 0;
return $exists ? TRUE : FALSE;
} Part 2 is what we already suspected before, public static function read(string $fileName): string
{
$path = $fileName;
if (!is_file($path)) {
$path = stream_resolve_include_path($fileName);
if (!$path) {
throw new CouldNotReadFileException($fileName);
}
}
$contents = @file_get_contents($path);
if ($contents === false) {
throw new CouldNotReadFileException($fileName);
}
return $contents;
} With these two modifications in place, phpstan works for me :) |
pprkut/phpstan-src@3b3f517 is the cleaned up version |
@ondrejmirtes I tried to add an e2e test for phpstan for this based on In While I'm not changing |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
When using https://github.com/pprkut/autoload-psr PHPStan fails to load any non-project classes. Instead returning:
This is apparently because the
php_stream_open_for_zend_ex
calls in the extension do not trigger the code in:https://github.com/phpstan/phpstan-src/blob/HEAD/src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php
This does however still work on 1.6.9.
Code snippet that reproduces the problem
If you have the extension installed and run the code in https://phpstan.org/r/232ef63b-c3a0-45ba-b8f3-e58df73c9a44 with https://github.com/M2mobi/lunr in the
Lunr-0.7
directory it should load the expected class, but doesn't.Expected output
PHPStan can use the autoloader to find classes. Or, alternatively, the autoloader learns how to trigger the correct functions in PHPStan here.
Did PHPStan help you today? Did it make you happy in any way?
PHPStan has been a gamechanger to improve our code, it's simple, powerful and I love to use it.
The text was updated successfully, but these errors were encountered: