Skip to content

Commit

Permalink
[DX] Remove too detailed --type in init command, use fw documentation…
Browse files Browse the repository at this point in the history
… instead (#2430)
  • Loading branch information
TomasVotruba committed Jun 4, 2022
1 parent 506c282 commit 83f7e9f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 97 deletions.
3 changes: 3 additions & 0 deletions easy-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Contract\Template\TemplateResolverInterface;
use Rector\Core\NodeManipulator\MethodCallManipulator;
use Rector\Defluent\NodeAnalyzer\SameClassMethodCallAnalyzer;
use Rector\DependencyInjection\NodeManipulator\PropertyConstructorInjectionManipulator;
Expand Down Expand Up @@ -77,5 +78,7 @@
ParentNodeReadAnalyzerInterface::class,
StmtsAwareInterface::class,
\Rector\CodeQuality\NodeTypeGroup::class,
// deprecated, keep it for now
TemplateResolverInterface::class,
]);
};
2 changes: 2 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ final class Option
public const NO_DIFFS = 'no-diffs';

/**
* @deprecated This know-how should be mentioned in framework-specific documentation of the package instead.
* @var string
*/
public const TEMPLATE_TYPE = 'template-type';

/**
* @deprecated
* @var string
*/
public const ENABLE_EDITORCONFIG = 'enable_editorconfig';
Expand Down
59 changes: 16 additions & 43 deletions src/Console/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@
use Nette\Utils\Strings;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Template\TemplateResolverInterface;
use Rector\Core\Exception\Template\TemplateTypeNotFoundException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Template\DefaultResolver;
use Stringable;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\SmartFileSystem\FileSystemGuard;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\SmartFileSystem\SmartFileSystem;

final class InitCommand extends Command
{
/**
* @param TemplateResolverInterface[] $templateResolvers
* @var string
*/
private const TEMPLATE_PATH = __DIR__ . '/../../../templates/rector.php.dist';

public function __construct(
private readonly FileSystemGuard $fileSystemGuard,
private readonly SmartFileSystem $smartFileSystem,
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly array $templateResolvers,
private readonly PhpVersionProvider $phpVersionProvider
private readonly PhpVersionProvider $phpVersionProvider,
private readonly SymfonyStyle $symfonyStyle
) {
parent::__construct();
}
Expand All @@ -40,30 +37,33 @@ protected function configure(): void

$this->setDescription('Generate rector.php configuration file');

// deprecated
$this->addOption(
Option::TEMPLATE_TYPE,
null,
InputOption::VALUE_OPTIONAL,
'A template type like default, nette, doctrine etc.',
DefaultResolver::TYPE,
'A template type like default, nette, doctrine etc.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$templateType = (string) $input->getOption(Option::TEMPLATE_TYPE);

$rectorTemplateFilePath = $this->resolveTemplateFilePathByType($templateType);

$this->fileSystemGuard->ensureFileExists($rectorTemplateFilePath, __METHOD__);
if ($templateType !== '') {
// notice warning
$this->symfonyStyle->warning(
'The option "--type" is deprecated. Custom config should be part of project documentation instead.'
);
sleep(3);
}

$rectorRootFilePath = getcwd() . '/rector.php';

$doesFileExist = $this->smartFileSystem->exists($rectorRootFilePath);
if ($doesFileExist) {
$this->rectorOutputStyle->warning('Config file "rector.php" already exists');
} else {
$this->smartFileSystem->copy($rectorTemplateFilePath, $rectorRootFilePath);
$this->smartFileSystem->copy(self::TEMPLATE_PATH, $rectorRootFilePath);

$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = Strings::substring($fullPHPVersion, 0, 1) . Strings::substring($fullPHPVersion, 2, 1);
Expand All @@ -81,31 +81,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return Command::SUCCESS;
}

private function resolveTemplateFilePathByType(string $templateType): string
{
$rectorTemplateFilePath = null;

foreach ($this->templateResolvers as $templateResolver) {
if ($templateResolver->supports($templateType)) {
$rectorTemplateFilePath = $templateResolver->provide();
break;
}
}

if ($rectorTemplateFilePath === null) {
$templateResolverTypes = [];
foreach ($this->templateResolvers as $templateResolver) {
if (method_exists($templateResolver, 'getType')) {
$templateResolverTypes[] = $templateResolver->getType();
} elseif ($templateResolver instanceof Stringable) {
$templateResolverTypes[] = (string) $templateResolver;
}
}

throw new TemplateTypeNotFoundException($templateType, $templateResolverTypes);
}

return $rectorTemplateFilePath;
}
}
5 changes: 3 additions & 2 deletions src/Contract/Template/TemplateResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Rector\Core\Contract\Template;

/**
* @deprecated This know-how should be mentioned in framework-specific documentation of the package instead.
*/
interface TemplateResolverInterface
{
// public function getType(): string;

public function provide(): string;

public function supports(string $type): bool;
Expand Down
22 changes: 0 additions & 22 deletions src/Exception/Template/TemplateTypeNotFoundException.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Template/DefaultResolver.php

This file was deleted.

0 comments on commit 83f7e9f

Please sign in to comment.