Exclude rules and files from Rector php

Reading Time: < 1
Discover how to configure Rector using a rector.php file in your project. Learn how to skip rules and tailor it to your needs.
exclude rector rules

Table of Contents

At the root of you project you can add a rector.php file to add the configuration of Rector. In the parameters you can add a section setParameters with the options SKIP. This allows you this skip some rules for Rector on your project. See configuration below.

				
					<?php // rector.php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\Assign\RemoveUnusedAssignVariableRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
use Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector;
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    // get parameters
    $parameters = $containerConfigurator->parameters();

    $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);

    // paths to refactor; solid alternative to CLI arguments
    $parameters->set(Option::PATHS, [
    ]);

    // here we can define, what sets of rules will be applied
    $containerConfigurator->import(SetList::DEAD_CODE);
    $containerConfigurator->import(SetList::CODE_QUALITY);
    $containerConfigurator->import(SetList::TYPE_DECLARATION);
    $containerConfigurator->import(SetList::TYPE_DECLARATION_STRICT);
    $containerConfigurator->import(SetList::PHP_52);
    $containerConfigurator->import(SetList::PHP_53);
    $containerConfigurator->import(SetList::PHP_54);
    $containerConfigurator->import(SetList::PHP_55);
    $containerConfigurator->import(SetList::PHP_56);
    $containerConfigurator->import(SetList::PHP_70);
    $containerConfigurator->import(SetList::PHP_71);
    $containerConfigurator->import(SetList::PHP_72);
    $containerConfigurator->import(SetList::PHP_73);
    $containerConfigurator->import(SetList::PHP_74);

    $parameters->set(Option::SKIP, [
        __DIR__ . '/module/Application/src/Entity',

        RemoveUnusedPrivateMethodRector::class => [
            __DIR__ . '/module/Nexus/src/*Task.php',
        ],
        RemoveUnreachableStatementRector::class => [
            __DIR__ . '/module/Spaceship/src/StarforceController.php',
            __DIR__ . '/module/Spaceship/src/JetTask.php',
        ],
        RemoveCodeAfterReturnRector::class => [
            __DIR__ . '/module/Hatch/src/Command.php',
        ],
        RemoveDeadReturnRector::class => [
            __DIR__ . '/module/Forgery/src/PaperTask.php',
        ],

        RemoveUnusedAssignVariableRector::class,
        RemoveConcatAutocastRector::class,
        RemoveDeadInstanceOfRector::class,
        TypedPropertyRector::class,
        CountOnNullRector::class,
        ArraySpreadInsteadOfArrayMergeRector::class,
        UseIdenticalOverEqualWithSameTypeRector::class,
    ]);
};

				
			

Share it on:

Twitter
LinkedIn
Facebook
WhatsApp

About the Author

Gary Gitton
Hello, I'm Gary Gitton - an accomplished Software Engineer, Tech Lead, specializing in PHP, API Engineering, DevOps, and Cloud Management. Throughout my career, I've had the privilege to enhance multiple software solutions with my expertise in multiple languages and platforms. I bring my unique blend of technical and business acumen to every project, ensuring efficient, scalable, and innovative outcomes. I'm also passionate about fostering a culture of continuous learning, mentoring developers, and leading projects with agile methodologies. Whether concocting a bespoke API or orchestrating cloud environments, I'm committed to delivering quality and excellence. Let's connect and transform your vision into a digital reality.

You might also like