配置 Rector 并使用 symfony 6/php8 的简单规则

问题描述 投票:0回答:5

我想使用 Rector 重构我的代码,因为我将我的项目更新为 symfony 5.4 到 6.1。 我的php项目版本:8.1.5

所以,我想使用一个简单的规则,将我的注释更改为带有校长的属性。但是当我使用命令执行 ctor 时:vendor/bin/rector process src

校长通知我:[OK] 校长已完成!

但是我的文件没有改变,我的实体注释也没有改变。 我尝试按照此文档来配置我的rector.php并将rector与symfony一起使用。

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

https://github.com/rectorphp/rector

我做错了什么?

这是我的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,
        ]);*/
};

这是我的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.*"
    },

感谢您的帮助!

php symfony doctrine rector
5个回答
7
投票

清除rector缓存对我有用:

vendor/bin/rector process --clear-cache --dry-run

5
投票

所以,这里的问题是composer.json中php的版本

我们只需要将 php 版本 7 更改为 8

“php”:“>=7.2.5”到“php”:“^8.0”

再见


2
投票

它对我有用:

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 
       
    ]);


};

1
投票

我不确定到底为什么,但对我来说,解决方案是删除路径配置:

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

并且仅使用路径作为二进制调用的参数:

php vendor/bin/rector process <PATH>


0
投票

以下配置帮助我将注释更改为属性。

<?php

use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
    ->withPhpVersion(PhpVersion::PHP_80)
    ->withPaths([
        __DIR__ . '/src',
    ])
    ->withAttributesSets(symfony: true, doctrine: true);
© www.soinside.com 2019 - 2024. All rights reserved.