• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rector upgrade rules for Symfony

Rector Rules for Symfony

See available Symfony rules

Install

This package is already part of rector/rector package, so it works out of the box.

All you need to do is install the main package, and you're good to go:

composer require rector/rector --dev

Use Sets

To add a set to your config, use Rector\Symfony\Set\SymfonySetList class and pick one of constants:

use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml');

    $rectorConfig->sets([
        SymfonySetList::SYMFONY_62,
        SymfonySetList::SYMFONY_CODE_QUALITY,
        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
    ]);
};

Configuration

Provide Symfony XML Service List

Some rules like StringFormTypeToClassRector need access to your Symfony container dumped XML. It contains list of form types with their string names, so it can convert them to class references.

How to add it? Check your var/cache/ directory and find the XML file for your test env. Then add it in rector.php:

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/test/App_KernelTestDebugContainer.xml');
};

That's it! Now you can run the StringFormTypeToClassRector and get your form classes converted safely.


Provide Symfony PHP Container

Some rules like AddRouteAnnotationRector require additional access to your Symfony container. The rule takes container service "router" to load metadata about your routes.

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->symfonyContainerPhp(__DIR__ . '/tests/symfony-container.php');
};

The tests/symfony-container.php should provide your dependency injection container. The way you create the container is up to you. It can be as simple as:

// tests/symfony-container.php

use App\Kernel;

require __DIR__ . '/bootstrap.php';

$appKernel = new Kernel('test', false);
$appKernel->boot();

return $appKernel->getContainer();

The version of your Symfony can be quite old. Public methods are stable from Symfony 2 to through 6 and the router have not changed much. The AddRouteAnnotationRector rule was tested and developed on Symfony 2.8 project.


Note: in this case, container cache PHP file located in /var/cache/<env>/appProjectContainer.php is not enough. Why? Few services require Kernel to be set, e.g. routes that are resolved in lazy way. This container file is only dumped without Kernel and would crash with missing "kernel" error. That's why the rule needs full blown container.


Learn Rector Faster

Rector is a tool that we develop and share for free, so anyone can save hundreds of hours on refactoring. But not everyone has time to understand Rector and AST complexity. You have 2 ways to speed this process up:

Both ways support us to and improve Rector in sustainable way by learning from practical projects.

More Repositories

1

rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
PHP
8,596
star
2

rector-src

Development repository for Rector
PHP
97
star
3

swiss-knife

Swiss knife in pocket of every upgrade architect!
PHP
85
star
4

type-perfect

Next level type declaration check PHPStan rules
PHP
73
star
5

rector-phpunit

Rector upgrade rules for PHPUnit
PHP
62
star
6

rector-doctrine

Rector upgrade rules for Doctrine
PHP
57
star
7

rector-downgrade-php

Rector downgrade rules for PHP
PHP
35
star
8

php-parser-nodes-docs

Visual documentation of PHP Parser nodes, to help you learn AST, how to create nodes and analyse them
PHP
31
star
9

getrector-com

Rector Website
PHP
25
star
10

rector-book-feedback

[BOOK] Repository for feedback from readers of Rector book, to improve it continuously
15
star
11

extension-installer

Composer Plugin for simpler registration of Rector extension configs
PHP
15
star
12

phpstan-rules

PHPStan Rules for Rector Rules developers
PHP
15
star
13

rector-pest

PHP
10
star
14

rector-phpoffice

[DISCONTUINED] Rector upgrades rules for PHP Office
PHP
8
star
15

rector-book-code-examples

Code examples from the Rector book up-to-date with Rector 1.0
PHP
8
star
16

rector-php56

[SPONSORWARE] Rector running natively on PHP 5.6 for the real legacy project
PHP
3
star
17

reusable-workflows

Re-usable workfows for Rector repositories
3
star
18

vendor-patches

Vendor patches for Rector packages
1
star
19

rector-debugging

[DEPRECATED] Rector debugging functions for dump and printing nodes
PHP
1
star
20

rector-php-parser

PHP
1
star
21

custom-phpspec-to-phpunit

From PHPSpec and Prophecy to PHPUnit - use at own risk
PHP
1
star
22

release-notes-generator

The way we generate release notes for Rector repository
PHP
1
star