Configure Rector and using a simple Rule with symfony 6/php8
Asked Answered
U

5

5

i want to use Rector for refactoring my code because i update my project to symfony 5.4 to 6.1. My php project version : 8.1.5

So, i want to use a simple rule who change my annotation to attribut with rector. but when i execute rector with command : vendor/bin/rector process src

rector inform me : [OK] Rector is done!

But my file don't change, my entity annotation don't change. i try to follow this documentation for config my rector.php and use rector with symfony.

https://github.com/rectorphp/rector-symfony

https://github.com/rectorphp/rector

what i do wrong ?

this is my rector.php

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Symfony\Rector\MethodCall\StringFormTypeToClassRector;
use Rector\Symfony\Rector\Class_\CommandPropertyToAttributeRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src'
    ]);

    $rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');

    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml');

    
    $rectorConfig->ruleWithConfiguration(
      AnnotationToAttributeRector::class,
      [new AnnotationToAttribute('Symfony\Routing\Annotation\Route')]
  );
    // register a single rule
    //$rectorConfig->rule(CommandPropertyToAttributeRector::class);

    // define sets of rules
      /* $rectorConfig->sets([
        SymfonySetList::SYMFONY_60,
        SymfonySetList::SYMFONY_CODE_QUALITY,
        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
        ]);*/
};

this is my composer.json


{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "1.11.99.1",
        "doctrine/annotations": "^1.12",
        "doctrine/dbal": "^2.0",
        "doctrine/doctrine-bundle": "^2.2",
        "doctrine/doctrine-fixtures-bundle": "^3.4",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.8",
        "fakerphp/faker": "^1.19",
        "gedmo/doctrine-extensions": "^3.8",
        "giggsey/libphonenumber-for-php": "^8.12",
        "knplabs/knp-paginator-bundle": "^5.3",
        "phpdocumentor/reflection-docblock": "^5.2",
        "sensio/framework-extra-bundle": "^6.2",
        "symfony/asset": "6.1.*",
        "symfony/console": "6.1.*",
        "symfony/doctrine-bridge": "6.1.*",
        "symfony/dotenv": "6.1.*",
        "symfony/expression-language": "6.1.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "6.1.*",
        "symfony/framework-bundle": "6.1.*",
        "symfony/http-client": "6.1.*",
        "symfony/http-foundation": "6.1.*",
        "symfony/intl": "6.1.*",
        "symfony/mailer": "6.1.*",
        "symfony/mime": "6.1.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/notifier": "6.1.*",
        "symfony/options-resolver": "6.1.*",
        "symfony/process": "6.1.*",
        "symfony/property-access": "6.1.*",
        "symfony/property-info": "5.4.*",
        "symfony/proxy-manager-bridge": "6.1.*",
        "symfony/rate-limiter": "6.1.*",
        "symfony/runtime": "6.1.*",
        "symfony/security-bundle": "6.1.*",
        "symfony/serializer": "6.1.*",
        "symfony/string": "6.1.*",
        "symfony/translation": "6.1.*",
        "symfony/twig-bundle": "6.1.*",
        "symfony/validator": "6.1.*",
        "symfony/web-link": "6.1.*",
        "symfony/webpack-encore-bundle": "^1.14",
        "symfony/yaml": "6.0.*",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0"
    },
    "require-dev": {
        "fakerphp/faker": "^1.19",
        "phpstan/phpstan": "^1.8",
        "phpstan/phpstan-doctrine": "^1.3",
        "rector/rector": "^0.13.9",
        "symfony/browser-kit": "6.1.*",
        "symfony/css-selector": "6.1.*",
        "symfony/debug-bundle": "6.1.*",
        "symfony/maker-bundle": "^1.30",
        "symfony/phpunit-bridge": "6.1.*",
        "symfony/stopwatch": "6.1.*",
        "symfony/var-dumper": "6.1.*",
        "symfony/web-profiler-bundle": "6.1.*"
    },

Thanks for your help !

Unroof answered 3/8, 2022 at 10:9 Comment(1)
I am facing the same problem. Files won't be changed. Did you find a solution yet?Unilingual
G
8

Clearing the rector cache worked for me:

vendor/bin/rector process --clear-cache --dry-run
Glockenspiel answered 3/3, 2023 at 14:47 Comment(2)
This worked for me, but I suggest people try vendor/bin/rector process --clear-cache --dry-run first... the command as written above does the whole shebang immediately (it doesn't just clear the cache!)Haematosis
Thanks for the suggestion caponica, I changed my response accordingly.Glockenspiel
U
5

So, the problem here is the version of php in composer.json

we just need to change php version 7 to 8

"php": ">=7.2.5" to "php": "^8.0"

Bye

Unroof answered 11/9, 2022 at 17:39 Comment(1)
This was literally the first page I opened to resolve my issue. I spent hours trying to fix my issue(I ignored your solution). but after by chance coming onto this page and not ignoring this solution. my problem got solved in minutes.Calvaria
O
2

It is working for me:

use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src'
    ]);
    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml');
    $rectorConfig->sets([
        SymfonySetList::SYMFONY_60,
        SymfonySetList::SYMFONY_CODE_QUALITY,
        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION
    ]);
    
    $rectorConfig->sets([
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
        SensiolabsSetList::FRAMEWORK_EXTRA_61 
       
    ]);


};
Oneidaoneil answered 2/10, 2022 at 21:18 Comment(0)
U
1

I am not sure why exactly but for me the solution was to remove the path config:

$rectorConfig->paths([
    __DIR__ . '/src'
]);

and only use the path as an argument to the binary call:

php vendor/bin/rector process <PATH>

Unilingual answered 8/9, 2022 at 14:28 Comment(0)
S
0

At the time there is a new way of configuring the rector.php it's this way

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    // register single rule
    ->withAttributesSets(doctrine: true);

According to the at the time documentation https://getrector.com/documentation/set-lists

Spancel answered 11/7 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.