Jan0707 / phpstan-prophecy

MIT License
104 stars 30 forks source link

Upgrading to 0.7 breaks PHPStan #179

Closed flangofas closed 4 years ago

flangofas commented 4 years ago

Hey guys,

The following setup breaks PHPStan while the previous release (0.6.2) worked just fine.

PHP: 7.4 PHPStan: 0.12.23 PHPStan-Prophecy: 0.7 Relates to: https://github.com/slimphp/Slim-Skeleton/pull/163

Expected: [OK] No errors

Output: In StubPhpDocProvider.php line 150:

[PHPStan\ShouldNotHappenException] Internal error.

Exception trace:


  at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/PhpDoc/StubPhpDocProvider.php:150
 PHPStan\PhpDoc\StubPhpDocProvider->initializeKnownElements() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/PhpDoc/StubPhpDocProvider.php:133
 PHPStan\PhpDoc\StubPhpDocProvider->isKnownClass() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/PhpDoc/StubPhpDocProvider.php:56
 PHPStan\PhpDoc\StubPhpDocProvider->findClassPhpDoc() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Reflection/BetterReflection/BetterReflectionProvider.php:113
 PHPStan\Reflection\BetterReflection\BetterReflectionProvider->getClass() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Analyser/NodeScopeResolver.php:349
 PHPStan\Analyser\NodeScopeResolver->processStmtNode() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Analyser/NodeScopeResolver.php:168
 PHPStan\Analyser\NodeScopeResolver->processNodes() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Analyser/FileAnalyser.php:136
 PHPStan\Analyser\FileAnalyser->analyseFile() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/PhpDoc/StubValidator.php:78
 PHPStan\PhpDoc\StubValidator->validate() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseApplication.php:57
 PHPStan\Command\AnalyseApplication->analyse() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseCommand.php:126
 PHPStan\Command\AnalyseCommand->execute() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/vendor/symfony/console/Command/Command.php:228
 _HumbugBoxe8a38a0636f4\Symfony\Component\Console\Command\Command->run() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/vendor/symfony/console/Application.php:849
 _HumbugBoxe8a38a0636f4\Symfony\Component\Console\Application->doRunCommand() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/vendor/symfony/console/Application.php:235
 _HumbugBoxe8a38a0636f4\Symfony\Component\Console\Application->doRun() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/vendor/symfony/console/Application.php:136
 _HumbugBoxe8a38a0636f4\Symfony\Component\Console\Application->run() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/bin/phpstan:72
 _HumbugBoxe8a38a0636f4\{closure}() at phar:///Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan/bin/phpstan:73
 require() at /Users/user/Development/open-source/Slim-Skeleton/vendor/phpstan/phpstan/phpstan:6```
bendavies commented 4 years ago

Confirmed here too

ondrejmirtes commented 4 years ago

The stubs here are really simple, not sure what causes this. I'm gonna investigate.

ondrejmirtes commented 4 years ago

Unfortunately I'm not able to reproduce this. It runs fine. Running vendor/bin/phpstan analyse -c phpstan-with-extension.neon in this repo works so I'm not sure what else should I try. If you could pinpoint how the analysed source code where PHPStan crashes looks like, it'd be great.

l0gicgate commented 4 years ago

+1

ondrejmirtes commented 4 years ago

This doesn't help me reproduce it. I need some minimal code on which it crashes like this. Thanks.

l0gicgate commented 4 years ago

@ondrejmirtes I liked the issue in our repo for you to look. You can easily clone that branch to reproduce.

https://github.com/slimphp/Slim-Skeleton/pull/165

bendavies commented 4 years ago

@ondrejmirtes i haven't tracked this down yet but i'm getting there. the phpstan container is being build incorrectly?

    public function createServiceStubPhpDocProvider(): PHPStan\PhpDoc\StubPhpDocProvider
    {
        return new PHPStan\PhpDoc\StubPhpDocProvider(
            $this->getService('043'),
            $this->getService('083'),
            [
                '/Users/ben/Code/phpstan/phpstan-src/stubs/ReflectionClass.stub',
                '/Users/ben/Code/phpstan/phpstan-src/stubs/iterable.stub',
                '/Users/ben/Code/phpstan/phpstan-src/stubs/ArrayObject.stub',
                '/Users/ben/Code/phpstan/phpstan-src/stubs/WeakReference.stub',
                '/Users/ben/Code/phpstan/phpstan-src/stubs/ext-ds.stub',
                '/Users/ben/Code/phpstan/phpstan-src/stubs/PDOStatement.stub',
                '/Users/ben/Code/abacus-webapp/vendor/jangregor/phpstan-prophecy/src/stubs/ObjectProphecy.phpstub',
                '/Users/ben/Code/abacus-webapp/vendor/jangregor/phpstan-prophecy/src/stubs/ProphecyInterface.phpstub',
            ]
        );
    }

/Users/ben/Code/abacus-webapp/vendor/jangregor/phpstan-prophecy/src/stubs/ObjectProphecy.phpstub should be /Users/ben/Code/abacus-webapp/vendor/jangregor/phpstan-prophecy/stubs/ObjectProphecy.phpstub

bendavies commented 4 years ago

got it. Relative paths in the stubFiles key are resolved based on the directory of the config file is in.

This repo contains https://github.com/Jan0707/phpstan-prophecy/blob/master/extension.neon and then a symlink https://github.com/Jan0707/phpstan-prophecy/blob/master/src/extension.neon for BC with 0.6.2.

If you load the symlink, i.e.

includes:
    - %currentWorkingDirectory%/vendor/jangregor/phpstan-prophecy/src/extension.neon

the relative path resolution to the stubs doesn't work.

Either phpstan could resolve symlinks or...??

ondrejmirtes commented 4 years ago

If you’re running phpstan-src and not phpstan/phpstan distribution then nothing is guaranteed to work (phpstan-src is just for PHPStan development itself) but I still don’t see how this could break.

ondrejmirtes commented 4 years ago

It’d be great if someone created a small reproducing repo (with phpstan/phpstan as Composer dependency) so I can debug it.

bendavies commented 4 years ago

@ondrejmirtes did you see my comment?

localheinz commented 4 years ago

@bendavies @ondrejmirtes

I will remove the symbolic link in the next release, if that helps!

bendavies commented 4 years ago

That, or this line: https://github.com/phpstan/phpstan-src/blob/bde25044d1df8154e5e7a5989b39d270115fa7e3/src/DependencyInjection/NeonAdapter.php#L170

needs changing to

$dir = dirname(realpath($file));
localheinz commented 4 years ago

@bendavies @flangofas @ondrejmirtes

I have removed the symbolic link in #182. This will break installations for people who do not use phpstan/extension-installer, but I think it's best moving forward this way:

@ondrejmirtes

Not sure if there are other projects using symbolic links, but I believe we are good in regard to this project now, what do you think?

ondrejmirtes commented 4 years ago

@localheinz Thank you, symbolic links are generally problematic, IIRC they don't translate well on Windows. (I've wanted the phpstan file in https://github.com/phpstan/phpstan to be just a symlink to phpstan.phar but it didn't work.)

People are encouraged to use extension-installer but they can't always do that. For example if they only want to use some of the rules from phpstan-strict-rules, they can't use extension-installer. Since the package is just a convenient way to save a few lines from the configuration file, it's fine.

Anyway, 👍 on the fix, using symlinks in this context is discouraged.

ondrejmirtes commented 4 years ago

@bendavies Sorry, haven't seen your comment before replying.

localheinz commented 4 years ago

Thank you, @ondrejmirtes!

localheinz commented 4 years ago

Fixed with #182.

bendavies commented 4 years ago

thanks all

flangofas commented 4 years ago

thanks for your prompt answers and the quick fix.

localheinz commented 4 years ago

You are welcome, @bendavies and @flangofas!