Don't import classes on specific path in rectorphp/rector

359 views Asked by At

I have a rector setup for upgrading to laravel9 and php8.1, and it works good.

My question is, how to set up the config so that it imports the classes everywhere, but not in config/app.php?

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->parallel(seconds: 240, maxNumberOfProcess: 6);

    $rectorConfig->paths([
        __DIR__ . '/app',
        __DIR__ . '/bootstrap/app.php',
        __DIR__ . '/config',
        __DIR__ . '/database',
        __DIR__ . '/lang',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ]);

    $rectorConfig->importNames();
    $rectorConfig->importShortClasses(false);

   ...

I'm avare of skip(), but that seems to work for files or rules, not config parameters...

1

There are 1 answers

0
hdomos On

How I solved this is maybe not the best solution but it worked:

I created three rector config files:

  • rector_base.php
  • rector_without_import.php
  • rector.php

The rector_base.php contains the rules I want to actually use for all files:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    // any rule what you want to actually use
};

rector_without_import.php:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(__DIR__ . '/rector_base.php');

    $rectorConfig->paths([
        __DIR__ . '/config',
    ]);
};

rector.php

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(__DIR__ . '/rector_base.php');

    $rectorConfig->paths([
        __DIR__ . '/app',
        __DIR__ . '/bootstrap/app.php',
        __DIR__ . '/database',
        __DIR__ . '/lang',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ]);

    $rectorConfig->importNames();
    $rectorConfig->importShortClasses(false);
};

I can run two different rector processes with the different configs with this setup:

./vendor/bin/rector process
./vendor/bin/rector process --config ./rector_without_import.php