Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of static reflection from PHPStan #5665

Merged
merged 21 commits into from
Feb 28, 2021

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Feb 23, 2021

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:

// in Rector
include __DIR__ . '/some_file.php';
// some_file.php
mysql_connect('...');

Fatal error: canont connect to mysql

The only way to avoid this is to use static reflections. That means the code is parsed, so Rector knows about mysql_connect function call, but it is not included and does not produce side effects. Win win 🎉


Todo

  • break rename test without autoload
  • replace ClassExistenceStaticHelper::doesClassLikeExists() with ReflectionProvider->hasClass()
  • replace class_exists() with ReflectionProvider->hasClass()
  • fix AnnotationReader to work with native reflections

SourceLocatorFactory

Tests

  • make test pass again
  • make test pass without autoload
  • replace is_a(), instanceof, $objectType->hasMethod(), $objectType->hasProperty() with ClassReflection API

@TomasVotruba TomasVotruba force-pushed the static-reflection-config branch 2 times, most recently from 3f80faa to c3c3084 Compare February 23, 2021 17:18
@TomasVotruba TomasVotruba force-pushed the static-reflection-config branch 6 times, most recently from 2f3a909 to 93672f4 Compare February 23, 2021 21:22
@TomasVotruba TomasVotruba changed the title [WIP] Make use of static reflectoin [WIP] Make use of static reflection Feb 23, 2021
@TomasVotruba TomasVotruba force-pushed the static-reflection-config branch 2 times, most recently from f036a30 to d8bd4e3 Compare February 23, 2021 23:40
@TomasVotruba TomasVotruba changed the title [WIP] Make use of static reflection [WIP] Make use of static reflection from PHPStan Feb 23, 2021
@TomasVotruba TomasVotruba force-pushed the static-reflection-config branch 4 times, most recently from 4c9a89e to 0885286 Compare February 24, 2021 00:41
@TomasVotruba TomasVotruba force-pushed the static-reflection-config branch 9 times, most recently from ad3c3dc to b9cddc0 Compare February 25, 2021 01:21
@ondrejmirtes
Copy link
Contributor

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.

@TomasVotruba
Copy link
Member Author

Rector is using the same ReflectionProvider service, and adding files and directories from rector.php and paths arguments from vendor/bin/rector process src to SourceLocator.

@mpdude
Copy link
Contributor

mpdude commented Mar 5, 2021

(How) can I run Rector with a version of PHPStan that is not packed up as a Phar?

@ondrejmirtes
Copy link
Contributor

Nope, PHPStan is only distributed as PHAR. What are your needs?

@mpdude
Copy link
Contributor

mpdude commented Mar 5, 2021

Debug into Rector, see when/how it uses ReflectionProvider and subclasses, being able to tinker around with that code to understand if/when/how/why runtime reflection comes into play.

(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.)

@ondrejmirtes
Copy link
Contributor

Xdebug works even with PHARs.

@mpdude
Copy link
Contributor

mpdude commented Mar 5, 2021

... but changing code (commenting out things etc) does not.

@mpdude
Copy link
Contributor

mpdude commented Mar 5, 2021

But I'll take this as "there is no easy way to install a non-Phar source version of PHPStan" and deal with it 😉

@ondrejmirtes
Copy link
Contributor

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.

mpdude added a commit to mpdude/rector that referenced this pull request Mar 15, 2021
Since rectorphp#5665, we might get away with the PHP 8.0 image variant only,
and also no longer need to scope the Rector source code.
TomasVotruba added a commit that referenced this pull request Feb 25, 2024
TomasVotruba added a commit that referenced this pull request Feb 26, 2024
TomasVotruba added a commit that referenced this pull request Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Taking advantage of static reflection
3 participants