Skip to content

Commit

Permalink
[DX] Add note warning about using old config (#2174)
Browse files Browse the repository at this point in the history
* fix cs

* use import short classes

* add notificaiton in case of loading of old ContainerConfigurator

* add imports short classes
  • Loading branch information
TomasVotruba committed Apr 26, 2022
1 parent 7fb8254 commit ae45be1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

// FQN class importing
$rectorConfig->disableImportNames();
$parameters->set(Option::IMPORT_SHORT_CLASSES, true);
$rectorConfig->importShortClasses();

$parameters->set(Option::NESTED_CHAIN_METHOD_CALL_LIMIT, 60);

Expand Down
12 changes: 12 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public function importNames(): void
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
}

public function importShortClasses(): void
{
$parameters = $this->parameters();
$parameters->set(Option::IMPORT_SHORT_CLASSES, true);
}

public function disableImportShortClasses(): void
{
$parameters = $this->parameters();
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);
}

public function disableImportNames(): void
{
$parameters = $this->parameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\Configuration\Option;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$parameters = $rectorConfig->parameters();
$rectorConfig->importNames();
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);
$rectorConfig->disableImportShortClasses();

$rectorConfig->rule(RenameClassRector::class);
};
1 change: 1 addition & 0 deletions rules/DogFood/Rector/Closure/UpgradeRectorConfigRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class UpgradeRectorConfigRector extends AbstractRector
Option::SKIP => 'skip',
Option::AUTOLOAD_PATHS => 'autoloadPaths',
Option::BOOTSTRAP_FILES => 'bootstrapFiles',
Option::IMPORT_SHORT_CLASSES => 'importShortClasses',
Option::AUTO_IMPORT_NAMES => 'importNames',
Option::PARALLEL => 'parallel',
Option::PHPSTAN_FOR_RECTOR_PATH => 'phpstanConfig',
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final class Option
public const AUTO_IMPORT_NAMES = 'auto_import_names';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::importShortClasses() instead
* @var string
*/
public const IMPORT_SHORT_CLASSES = 'import_short_classes';
Expand Down
20 changes: 20 additions & 0 deletions src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Rector\Core\DependencyInjection;

use Nette\Utils\FileSystem;
use Psr\Container\ContainerInterface;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Stubs\PHPStanStubLoader;
use Rector\Core\ValueObject\Bootstrap\BootstrapConfigs;
use Symfony\Component\Console\Style\SymfonyStyle;

final class RectorContainerFactory
{
Expand All @@ -17,7 +19,25 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):
$container = $this->createFromConfigs($bootstrapConfigs->getConfigFiles());

$mainConfigFile = $bootstrapConfigs->getMainConfigFile();

if ($mainConfigFile !== null) {
// warning about old syntax before RectorConfig
$fileContents = FileSystem::read($mainConfigFile);
if (str_contains($fileContents, 'ContainerConfigurator $containerConfigurator')) {
/** @var SymfonyStyle $symfonyStyle */
$symfonyStyle = $container->get(SymfonyStyle::class);

// @todo add link to blog post after release
$warningMessage = sprintf(
'Your "%s" config is using old syntax with "ContainerConfigurator".%sPlease upgrade to "RectorConfig" that allows better autocomplete and future standard.',
$mainConfigFile,
PHP_EOL,
);
$symfonyStyle->warning($warningMessage);
// to make message noticable
sleep(1);
}

/** @var ChangedFilesDetector $changedFilesDetector */
$changedFilesDetector = $container->get(ChangedFilesDetector::class);
$changedFilesDetector->setFirstResolvedConfigFileInfo($mainConfigFile);
Expand Down

0 comments on commit ae45be1

Please sign in to comment.