Skip to content

Taking advantage of static reflection #3490

@ondrejmirtes

Description

@ondrejmirtes

Hi, I'm reacting to #3488 (comment) which is about PHPStan 0.12.26.

Rector can take advantage of it, you should take these steps:

  1. Use ReflectionProvider interface instead of Broker class in places where you're obtaining class/function/constant reflections. They have the same API.
  2. I'm not sure how Rector's DI works. In PHPStan the current stable ReflectionProvider looks like this:
ChainReflectionProvider
- ClassBlacklistReflectionProvider
  - RuntimeReflectionProvider
- BetterReflectionProvider

The class blacklist is there so that some certain classes are skipped for the runtime reflection and go straight to static reflection.

If you enable the experimental flag featureToggles.disableRuntimeReflectionProvider (set to true) then the reflection provider instance is just going to be the BetterReflectionProvider.

The most important thing about BetterReflectionProvider is the source locator chain it uses. In PHPStan it's created in this class: https://github.com/phpstan/phpstan-src/blob/master/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php (you might want to write your own).

It's quite difficult to construct BetterReflectionProvider by hand but fortunately there's a Nette DI-generated factory based on this interface: https://github.com/phpstan/phpstan-src/blob/master/src/Reflection/BetterReflection/BetterReflectionProviderFactory.php

Here are the services definitions: https://github.com/phpstan/phpstan-src/blob/master/conf/config.neon#L1095-L1125

Let me know if you have any questions 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions