Skip to content

Commit

Permalink
[CS] Fix dynamic and broken indent detection, allow to configure spac…
Browse files Browse the repository at this point in the history
…ing via RectorConfig::indent() method
  • Loading branch information
TomasVotruba committed Jun 6, 2022
1 parent 3ae5f0c commit 66665bd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 40 deletions.
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

$rectorConfig->disableImportNames();
$rectorConfig->importShortClasses();
$rectorConfig->indent(' ', 4);

$rectorConfig->fileExtensions(['php']);
$rectorConfig->nestedChainMethodCallLimit(60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ private function _resolveTagFullyQualifiedName(
/**
* @param Use_[]|GroupUse[] $uses
*/
private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag, bool $returnNullOnUnknownClass): ?string
private function resolveFullyQualifiedClass(
array $uses,
Node $node,
string $tag,
bool $returnNullOnUnknownClass
): ?string
{
$scope = $node->getAttribute(AttributeKey::SCOPE);

Expand Down Expand Up @@ -127,7 +132,7 @@ private function resolveAsAliased(array $uses, string $tag, bool $returnNullOnUn

private function resolveClass(?string $class, bool $returnNullOnUnknownClass): ?string
{
if (null === $class) {
if ($class === null) {
return null;
}
$resolvedClass = $this->reflectionProvider->hasClass($class) ? $class : null;
Expand Down
10 changes: 10 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,14 @@ public function cacheClass(string $cacheClass): void
$parameters = $this->parameters();
$parameters->set(Option::CACHE_CLASS, $cacheClass);
}

/**
* @see https://github.com/nikic/PHP-Parser/issues/723#issuecomment-712401963
*/
public function indent(string $character, int $count): void
{
$parameters = $this->parameters();
$parameters->set(Option::INDENT_CHAR, $character);
$parameters->set(Option::INDENT_SIZE, $count);
}
}
12 changes: 12 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,16 @@ final class Option
* @var string
*/
public const MEMORY_LIMIT = 'memory-limit';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::indent() method
* @var string
*/
public const INDENT_CHAR = 'indent-char';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::indent() method
* @var string
*/
public const INDENT_SIZE = 'indent-size';
}
10 changes: 10 additions & 0 deletions src/Configuration/RectorConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function getSymfonyContainerXml(): string
{
return $this->parameterProvider->provideStringParameter(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER);
}

public function getIndentChar(): string
{
return $this->parameterProvider->provideStringParameter(Option::INDENT_CHAR);
}

public function getIndentSize(): int
{
return $this->parameterProvider->provideIntParameter(Option::INDENT_SIZE);
}
}
7 changes: 3 additions & 4 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\PrettyPrinter\Standard;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\Printer\Whitespace\IndentCharacterDetector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand Down Expand Up @@ -80,8 +80,8 @@ final class BetterStandardPrinter extends Standard implements NodePrinterInterfa
* @param mixed[] $options
*/
public function __construct(
private readonly IndentCharacterDetector $indentCharacterDetector,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly RectorConfigProvider $rectorConfigProvider,
array $options = []
) {
parent::__construct($options);
Expand All @@ -103,8 +103,7 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
{
$newStmts = $this->resolveNewStmts($stmts);

// detect per print
$this->tabOrSpaceIndentCharacter = $this->indentCharacterDetector->detect($origTokens);
$this->tabOrSpaceIndentCharacter = $this->rectorConfigProvider->getIndentChar();

$content = parent::printFormatPreserving($newStmts, $origStmts, $origTokens);

Expand Down
34 changes: 0 additions & 34 deletions src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DoNotReplaceUnknownClasses;

use Iterator;
Expand Down

0 comments on commit 66665bd

Please sign in to comment.