Skip to content
This repository has been archived by the owner on Dec 9, 2019. It is now read-only.

Installing along with extensions installs phpstan/phpstan as well #2

Closed
jiripudil opened this issue May 15, 2017 · 12 comments
Closed

Comments

@jiripudil
Copy link

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:

snimek obrazovky 2017-05-15 v 16 07 19

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?

@jkuchar
Copy link

jkuchar commented May 15, 2017

realated: fprochazka/phpstan-compiler#1

@fprochazka
Copy link
Contributor

fprochazka commented May 15, 2017

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.

@JanJakes
Copy link

@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?

@hrach
Copy link

hrach commented Jul 12, 2017

@JanJakes this is needed: nette/di#153

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jul 12, 2017 via email

@fprochazka
Copy link
Contributor

fprochazka commented Jul 12, 2017

@JanJakes

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

@ondrejmirtes
Copy link
Member

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.

@fprochazka
Copy link
Contributor

FYI, the original compiler was meant to compile the PHAR with the extensions so you would only enable them, no additional installing.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Nov 12, 2017 via email

@ondrejmirtes
Copy link
Member

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!

@vasekbrychta
Copy link

I've tried it with phpstan-nette and seems working. Only phpstan-shim gets installed. Thanks!

@ondrejmirtes
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants