Make use of static reflection from PHPStan#5665
Conversation
3f80faa to
c3c3084
Compare
packages/better-php-doc-parser/src/AnnotationReader/NodeAnnotationReader.php
Show resolved
Hide resolved
packages/node-type-resolver/src/DependencyInjection/PHPStanServicesFactory.php
Outdated
Show resolved
Hide resolved
packages/better-php-doc-parser/src/AnnotationReader/NodeAnnotationReader.php
Show resolved
Hide resolved
2f3a909 to
93672f4
Compare
f036a30 to
d8bd4e3
Compare
packages/better-php-doc-parser/src/AnnotationReader/NodeAnnotationReader.php
Show resolved
Hide resolved
4c9a89e to
0885286
Compare
packages/node-type-resolver/config/phpstan/static-reflection.neon
Outdated
Show resolved
Hide resolved
ad3c3dc to
b9cddc0
Compare
|
Is this solved by your contribution? |
|
@TomasVotruba #5772 seems to solve the issue I had with running a non-scoped Rector (from Docker) on a Symfony 2.3 project (a comment I initially left here but moved to the PR). But could you review my question above? If it is right what I am writing, we could update the Docker build process to only create PHP 8.0 images and to omit the scoping step... /cc @JanMikes |
I got lost in the long text. Could you narrow it down what exactly is your issue? |
|
Under which conditions would Rector still use dynamic reflection, so that code would have to be loaded/parsed by the running PHP interpreter and would possibly end up as a declared class/function/... in the current process? (Maybe also @ondrejmirtes could answer this?) |
|
It depends on how the ReflectionProvider instance looks like in Rector. In PHPStan by default it's a chain of various ReflectionProvider implementations. In layman's terms, if a class is reachable through a correctly configured Composer autoloader, a runtime reflection will be used. Otherwise static reflection is used. |
|
Rector is using the same ReflectionProvider service, and adding files and directories from |
|
(How) can I run Rector with a version of PHPStan that is not packed up as a Phar? |
|
Nope, PHPStan is only distributed as PHAR. What are your needs? |
|
Debug into Rector, see when/how it uses (To understand when/why scoping Rector is needed, and to understand when/why Rector needs to run with the same PHP version as the project it analyzes.) |
|
Xdebug works even with PHARs. |
|
... but changing code (commenting out things etc) does not. |
|
But I'll take this as "there is no easy way to install a non-Phar source version of PHPStan" and deal with it 😉 |
|
You can always clone phpstan-src alongside a project and run plain PHPStan using ../phpstan-src/phpstan analyse ... on a codebase, but not in combination with Rector. |
Closes #3490
Now Rector autoloads all the code that analyses. That means all your projects has to be loaded with composer, custom autoload or included. That can lead to side-effects like these:
↓
Fatal error: canont connect to mysqlThe only way to avoid this is to use static reflections. That means the code is parsed, so Rector knows about
mysql_connectfunction call, but it is not included and does not produce side effects. Win win 🎉Todo
ClassExistenceStaticHelper::doesClassLikeExists()withReflectionProvider->hasClass()class_exists()withReflectionProvider->hasClass()SourceLocatorFactory
phpstan.neonfor Rector internalls - here 9071f1b#diff-d4ecbf7aff4875121457666bba3371091eb5009fda360c3c51068e135075bf7cphpstan.neonconfig - Taking advantage of static reflection #3490 (comment)Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocator, e.g.vendor/bin/rector process <src>Tests
is_a(),instanceof,$objectType->hasMethod(),$objectType->hasProperty()withClassReflectionAPI