[docs] add Static Reflection and Autoload (#6238)

This commit is contained in:
Tomas Votruba 2021-04-25 11:04:04 +02:00 committed by GitHub
parent 0ac5412aa0
commit 31c06b2fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 19 deletions

View File

@ -71,6 +71,7 @@ It supports all versions of PHP from 5.3 and major open-source projects:
### Advanced
- [How to Ignore Rule or Paths](/docs/how_to_ignore_rule_or_paths.md)
- [Static Reflection and Autoload](/docs/static_reflection_and_autoload.md)
- [How to Configure Rule](/docs/how_to_configure_rules.md)
### Contributing
@ -87,7 +88,7 @@ It supports all versions of PHP from 5.3 and major open-source projects:
composer require rector/rector --dev
```
- Having conflicts during `composer require`? → Use the [Rector Prefixed](https://github.com/rectorphp/rector-prefixed)
- Having conflicts during `composer require`? → Use the [Rector Prefixed](https://github.com/rectorphp/rector-prefixed) with PHP 7.1+ version
- Using a different PHP version than Rector supports? → Use the [Docker image](/docs/how_to_run_rector_in_docker.md)
<br>
@ -96,8 +97,8 @@ composer require rector/rector --dev
There a 2 main ways to use Rector:
- a *single rule*, to have the change under control - you can choose [from over 600 rules](/docs/rector_rules_overview.md)
- or group of rules called *sets* - [pick from sets](/config/set)
- a *single rule*, to have the change under control
- or group of rules called *sets*
To use them, create a `rector.php` in your root directory:
@ -117,6 +118,8 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
return static function (ContainerConfigurator $containerConfigurator): void {
// here we can define, what sets of rules will be applied
$parameters = $containerConfigurator->parameters();
// tip: use "SetList" class to autocomplete sets
$parameters->set(Option::SETS, [SetList::CODE_QUALITY]);
// register single rule
@ -125,8 +128,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
};
```
<br>
Then dry run Rector:
```bash
@ -157,20 +158,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
// paths to refactor; solid alternative to CLI arguments
$parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests']);
// Rector is static reflection to load code without running it - see https://phpstan.org/blog/zero-config-analysis-with-static-reflection
$parameters->set(Option::AUTOLOAD_PATHS, [
// autoload specific file
__DIR__ . '/file-with-functions.php',
// or full directory
__DIR__ . '/project-without-composer',
]);
// do you need to include constants, class aliases or custom autoloader? files listed will be executed
$parameters->set(Option::BOOTSTRAP_FILES, [
__DIR__ . '/constants.php',
__DIR__ . '/project/special/autoload.php',
]);
// is your PHP version different from the one your refactor to? [default: your PHP version], uses PHP_VERSION_ID format
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72);

View File

@ -0,0 +1,54 @@
# Static Reflection and Autoload
Rector is using static reflection to load code without running it since version 0.10. That means your classes are found **without composer autoload and without running them**. Rector will find them and work with them as you have PSR-4 autoload properly setup. This comes very useful in legacy projects or projects with custom autoload.
Do you want to know more about it? Continue here:
- [From Doctrine Annotations Parser to Static Reflection](https://getrector.org/blog/from-doctrine-annotations-parser-to-static-reflection)
- [Legacy Refactoring made Easy with Static Reflection](https://getrector.org/blog/2021/03/15/legacy-refactoring-made-easy-with-static-reflection)
- [Zero Config Analysis with Static Reflection](https://phpstan.org/blog/zero-config-analysis-with-static-reflection) - from PHPStan
```php
// rector.php
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
// Rector is using static reflection to load code without running it - see https://phpstan.org/blog/zero-config-analysis-with-static-reflection
$parameters->set(Option::AUTOLOAD_PATHS, [
// discover specific file
__DIR__ . '/file-with-functions.php',
// or full directory
__DIR__ . '/project-without-composer',
]);
```
## Include Files
Do you need to include constants, class aliases or custom autoloader? Use `BOOTSTRAP_FILES` parameter:
```php
// rector.php
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::BOOTSTRAP_FILES, [
__DIR__ . '/constants.php',
__DIR__ . '/project/special/autoload.php',
]);
};
```
Listed files will be executed like:
```php
include $filePath;
```