Skip to content

Commit

Permalink
[DX] Make use of SimpleParameterProvider directly, deprecate RectorCo…
Browse files Browse the repository at this point in the history
…nfigProvider (#4460)
  • Loading branch information
TomasVotruba authored Jul 9, 2023
1 parent ab9d321 commit 68d092c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -36,7 +37,6 @@ public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly CurrentNodeProvider $currentNodeProvider,
private readonly UseImportsResolver $useImportsResolver,
private readonly RectorConfigProvider $rectorConfigProvider
) {
}

Expand Down Expand Up @@ -67,7 +67,7 @@ public function enterNode(Node $node): ?Node
$identifier->name = $this->resolveNamespacedName($identifier, $phpParserNode, $node->name);
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($identifier, $phpParserNode);

$shouldImport = $this->rectorConfigProvider->shouldImportNames();
$shouldImport = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES);
$isNoNamespacedName = ! str_starts_with($identifier->name, '\\') && substr_count($identifier->name, '\\') === 0;

// tweak overlapped import + rename
Expand Down
6 changes: 3 additions & 3 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\Scope;
use Rector\CodingStyle\Application\UseImportsRemover;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NonPhpFile\Rector\RenameClassNonPhpRector;
Expand All @@ -28,7 +29,6 @@ final class ClassRenamingPostRector extends AbstractPostRector implements PostRe
public function __construct(
private readonly ClassRenamer $classRenamer,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly RectorConfigProvider $rectorConfigProvider,
private readonly UseImportsRemover $useImportsRemover
) {
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public function enterNode(Node $node): ?Node
$scope = $originalNode->getAttribute(AttributeKey::SCOPE);
$result = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);

if (! $this->rectorConfigProvider->shouldImportNames()) {
if (! SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES)) {
return $result;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use PhpParser\Node\Stmt\UseUse;
use PhpParser\NodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
Expand All @@ -26,13 +27,12 @@ final class UnusedImportRemovingPostRector extends AbstractPostRector
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly RectorConfigProvider $rectorConfigProvider
) {
}

public function enterNode(Node $node): ?Node
{
if (! $this->rectorConfigProvider->shouldRemoveUnusedImports()) {
if (! SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS)) {
return null;
}

Expand Down
23 changes: 3 additions & 20 deletions src/Configuration/RectorConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
/**
* Rector native configuration provider, to keep deprecated options hidden,
* but also provide configuration that custom rules can check
*
* @api
* @deprecated Use @see SimpleParametersProvider directly
*/
final class RectorConfigProvider
{
public function shouldImportNames(): bool
{
return SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES);
}

public function shouldRemoveUnusedImports(): bool
{
return SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS);
}

/**
* @api symfony
*/
Expand All @@ -37,14 +30,4 @@ public function getSymfonyContainerXml(): string
{
return SimpleParameterProvider::provideStringParameter(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER);
}

public function getIndentChar(): string
{
return SimpleParameterProvider::provideStringParameter(Option::INDENT_CHAR, ' ');
}

public function getIndentSize(): int
{
return SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);
}
}
12 changes: 7 additions & 5 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\PrettyPrinter\Standard;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -81,7 +82,6 @@ final class BetterStandardPrinter extends Standard
*/
public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly RectorConfigProvider $rectorConfigProvider,
array $options = []
) {
parent::__construct($options);
Expand All @@ -94,7 +94,7 @@ public function __construct(
$this->insertionMap['Expr_Closure->returnType'] = [')', false, ': ', null];
$this->insertionMap['Expr_ArrowFunction->returnType'] = [')', false, ': ', null];

$this->tabOrSpaceIndentCharacter = $this->rectorConfigProvider->getIndentChar();
$this->tabOrSpaceIndentCharacter = SimpleParameterProvider::provideStringParameter(Option::INDENT_CHAR, ' ');
}

/**
Expand Down Expand Up @@ -177,8 +177,10 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string
return parent::pExpr_ArrowFunction($arrowFunction);
}

$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

$indent = str_repeat($this->tabOrSpaceIndentCharacter, $this->indentLevel) .
str_repeat($this->tabOrSpaceIndentCharacter, $this->rectorConfigProvider->getIndentSize());
str_repeat($this->tabOrSpaceIndentCharacter, $indentSize);

$text = "\n" . $indent;
foreach ($comments as $key => $comment) {
Expand Down Expand Up @@ -212,7 +214,7 @@ protected function setIndentLevel(int $level): void
*/
protected function indent(): void
{
$indentSize = $this->rectorConfigProvider->getIndentSize();
$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

$this->indentLevel += $indentSize;
$this->nl .= str_repeat($this->tabOrSpaceIndentCharacter, $indentSize);
Expand Down

0 comments on commit 68d092c

Please sign in to comment.