Ever since upgrading to PHPStan 0.12 I'm often running into the following issue when using PHPStorm. While searching my code with Ctrl+Shift+F, PHPStorm sometimes also searches this file. (It's both in vendor/bin/phpstan and vendor/phpstan/phpstan/phpstan.) It doesn't have the .phar suffix so I can't tell PHPStorm to exclude it and PHPStorm doesn't consider it a binary file so it tries to search it regardless of the size. This is causing my PHPStorm to freeze for a while when searching sometimes which is extremely annoying.
As far as I can tell this file is identical to the phpstan.phar. Do we need both? It would help me a lot if you could remove this file from the project or replace it with some tiny script that just calls phpstan.phar.
Hi, I'd like to remove it too. There was a symlink previously, but it didn't work for Windows installations. If you can come up with a script that works in both Linux+Windows and just calls phpstan.phar (while passing all the options and arguments) then I'd really love it.
This seems to work for me on linux but I don't have a windows PC to try it on.
#!/usr/bin/env php
<?php
include(__DIR__ . '/phpstan.phar');
Except that phpstan.phar also has #!/usr/bin/env php on the first line which causes some issues. Could that be removed?
It cannot. On Windows this won't work because it won't understand #!/usr/bin/env php. But maybe there's some Composer magic that creates another phpstan.bat file.
So unless you make sure it works on Windows, we cannot proceed. This repo has enabled GitHub Actions, so you can send a PR and try various things, and add a step that tests the new script on both Windows and Linux (and Mac).
I see. I think it can be solved using include 'phar://...'. Works fine for me on linux and seems like it should work on Windows too but we need to test it.
add a step that tests the new script on both Windows and Linux (and Mac)
I have no experience with GitHub actions, let alone testing something like this on all platforms. Can you help me with setting this up?
It would be nice to have to get rid of the big phpstan file but I have other priorities myself right now what to work on in PHPStan.
Question: when I include/require a phar file, how does PHP know which file from the phar should be executed? For phpstan it seems to run phpstan.phar/bin/phpstan but how does this work?
Also where can I find the code that compiles PHPStan's phar?
ping @ondrejmirtes
I don鈥檛 know the details, it鈥檚 all done by humbug/box for me: https://github.com/phpstan/phpstan-src/tree/master/compiler
Okay, it bothered me enough to figure out a workaround. Posting it here for anyone who might want it:
composer.json:
"scripts": {
"post-install-cmd": [
"bin/fix-phpstan"
]
}
bin/fix-phpstan (needs chmod +x)
#!/usr/bin/env php
<?php
file_put_contents(
__DIR__ . '/../vendor/bin/phpstan',
<<<'NOWDOC'
#!/usr/bin/env php
<?php
include 'phar://' . __DIR__ . '/phpstan.phar';
NOWDOC
);
OK, I just tried it, let's see what it does: https://github.com/phpstan/phpstan/commit/ac74795943bcc1ffbee5e315ad4b81745eaa3d3a (https://github.com/phpstan/phpstan/blob/master/phpstan)
Looks like it works even on Windows :) So I'm gonna release this soon. Additionally, phpstan.phar is now around 500 files and 2 MB lighter thanks to this change :) https://github.com/phpstan/phpstan-src/commit/f31ee7c1baa1cec8dd746db4d13c13b55c88ba09
Awesome! Thanks!
Unfortunately I had to revert this decision :(
Because phpstan.phar contains the shebang line #!/usr/bin/env php, when it's require'd by PHP, this line is printed out. Which wouldn't be a big deal (besides being ugly), but it breaks when PHPStan loads Nette DI container which fails because "the headers have already been sent".
I tried to have the actual PHAR in file like phpstan-phar.phar (without the shebang) and have both phpstan and phpstan.phar be just plain PHP files with the same contents - requiring phpstan-phar.phar. But that doesn't work because currently people can have code that refers to some include file inside the PHAR like this:
file_get_contents('phar://' . __DIR__ . '/vendor/phpstan/phpstan/phpstan.phar/src/TrinaryLogic.php');
And these changes would break compatibility with that.
What might work in my opinion is bin/phpstan using something like this:
require 'phar://' . __DIR__ . '/vendor/phpstan/phpstan/phpstan.phar/bin/shebang-less-executor';
But I was unable to create a phar with such file. Maybe you'll have more luck.
That's a nice idea! Let's try it again when I'm in the mood :)
I excluded the file phpstan.phar in settings ( Settings -> Editor -> File Types -> Ignore Files and Folders)

i didnt find the answer so so i ask the question :
phpstan file is called from ./vendor/bin/phpstan
but why do we need phpstan.phar file ?
It's there mostly for backwards compatibility, and also it easily allows inspecting the PHAR contents + setting breakpoints inside with Xdebug.
so can it be done like https://github.com/Dealerdirect/phpcodesniffer-composer-installer do ?
to be precise, we can call a "plugin" at install to copy ./vendor/phpstan/phpstan/phpstan to ./vendor/bin/phpstan.phar
It wouldn't actually solve the OP issue.
why that ? the only diff will be do not have the ./vendor/phpstan/phpstan/phpstan.phar
to have breaking point capability you could run phpstan from ./vendor/bin/phpstan.phar, no ?
@fezfez The problem is the very existence of the ./vendor/bin/phpstan and ./vendor/phpstan/phpstan/phpstan files. Or rather the fact that they ARE the phars instead of being just symlinks (or something similar) to the actual phar files.
The actual phar files being there is correct and desired because unlike the extension-less phars, PHPStorm allows me to browse them which I do relatively often. The "bug" is not the presence of the phpstan.phar files but the fact that they're not actually used.