Installing along with extensions installs phpstan/phpstan as well
jiripudil opened this issue · 12 comments
What is the problem
provide doesn't work as expected. Consider this:
$ composer require --dev phpstan/phpstan-shim phpstan/phpstan-nette
It installs both phpstan-shim and phpstan. As a result, the bundled code only loads the
application's autoloader from cwd, which, obviously, fails to load the prefixed classes.
$ composer why phpstan/phpstan
phpstan/phpstan-nette 0.7 requires phpstan/phpstan (^0.7)
What seems to be the cause
Looking at the docs, provide section talks about virtual packages, which would explain why there is one called phpstan/phpstan on packagist:
However, Composer seems to install the real package of the same name.
What could be the solution
Maybe replace would do the trick instead of provide?
realated: fprochazka/phpstan-compiler#1
Extensions are an open problem right now, we've talked about this with @ondrejmirtes already.
The phar has to include all the extensions (at least the official ones), to make sure they refer to the prefixed classes. But only the extensions - the extra dependencies, they should be part of your project already.
My compiler already handles this case, but Ondra released it without it for now - the 0.7 was a big release already as is.
@jiripudil Actually I think composer require phpstan/phpstan-shim phpstan/phpstan-nette can't work in any case simply because phpstan-shim only contains a phpstan.phar archive and when you use it (vendor/bin/phpstan.phar ...) you don't load vendor/autoload.php at all.
Also @fprochazka I tried to compile PHPStan with the extensions, but how do I activate them? I can't reference a vendor/phpstan/phpstan-nette/extension.neon which is in a PHAR archive, right?
@JanJakes this is needed: nette/di#153
I can't reference a vendor/phpstan/phpstan-nette/extension.neon which is in a PHAR archive, right?
You can! Read the readme for 0.6.x: https://github.com/phpstan/phpstan-shim/tree/0.6.x#extensions
I replaced the provide section with replace and the result is much better. Although, the current problem with installing PHAR along the extensions is that the Dynamic*ReturnTypeExtension interface contains references to PhpParser nodes which are prefixed in the PHAR, rendering implementations incompatible with the interface.
I will probably try to turn off prefixing the PhpParser library which should help and the extensions should become usable.
FYI, the original compiler was meant to compile the PHAR with the extensions so you would only enable them, no additional installing.
I have good news! I just pushed a freshly compiled PHAR that should support installing along the extensions! So feel free to try it out: 5f02c1f
How I verified that it works:
{
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"phpstan/phpstan-shim":"^0.9",
"phpstan/phpstan-phpunit":"^0.9"
}
}results in this install log (I excluded PHPUnit dependencies):
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 30 installs, 0 updates, 0 removals
- Installing phpunit/phpunit (6.4.4): Loading from cache
- Installing phpstan/phpstan-shim (dev-master 5f02c1f): Cloning 5f02c1fa2f from cache
- Installing phpstan/phpstan-phpunit (dev-master 87f1be6): Cloning 87f1be64dc from cache
So no phpstan dependencies are installed directly because of the extension, because they are already met thanks to phpstan-shim and the replace key in its composer.json.
With this phpstan.neon:
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
parameters:
autoload_files:
- test.php
And this test.php:
<?php
class TestCase extends \PHPUnit\Framework\TestCase
{
public function testFoo()
{
$mock = $this->createMock(Foo::class);
$mock->doFoo();
}
}
class Foo
{
}Running:
vendor/bin/phpstan.phar analyse test.php -c phpstan.neon -l 7
Results in:
------ -----------------------------------------------------------------------------------
Line test.php
------ -----------------------------------------------------------------------------------
9 Call to an undefined method Foo&PHPUnit_Framework_MockObject_MockObject::doFoo().
------ -----------------------------------------------------------------------------------
As you can see, the type Foo&PHPUnit_Framework_MockObject_MockObject means that the PHPUnit extension is installed and registered correctly!
I urge everyone in this conversation to test out latest phpstan-shim dev-master whether it works for their use cases as well!
I've tried it with phpstan-nette and seems working. Only phpstan-shim gets installed. Thanks!
Thanks to everyone who tested it, seems it works. After 0.9 is out, I plan to automate this, having phpstan-shim dev-master built after every successful Travis build of the main repo + testing in the build whether it still works.
