From 66665bd329857bbd6177bffbb4d789c3335e5d20 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 6 Jun 2022 10:56:05 +0200 Subject: [PATCH] [CS] Fix dynamic and broken indent detection, allow to configure spacing via RectorConfig::indent() method --- config/config.php | 1 + .../PhpDocParser/ClassAnnotationMatcher.php | 9 +++-- packages/Config/RectorConfig.php | 10 ++++++ src/Configuration/Option.php | 12 +++++++ src/Configuration/RectorConfigProvider.php | 10 ++++++ .../Printer/BetterStandardPrinter.php | 7 ++-- .../Whitespace/IndentCharacterDetector.php | 34 ------------------- .../DoNotReplaceUnknownClassesTest.php | 2 ++ 8 files changed, 45 insertions(+), 40 deletions(-) delete mode 100644 src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php diff --git a/config/config.php b/config/config.php index 980e986d0ff..4874a8d586f 100644 --- a/config/config.php +++ b/config/config.php @@ -64,6 +64,7 @@ $rectorConfig->disableImportNames(); $rectorConfig->importShortClasses(); + $rectorConfig->indent(' ', 4); $rectorConfig->fileExtensions(['php']); $rectorConfig->nestedChainMethodCallLimit(60); diff --git a/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php b/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php index c089445dbad..11cf414b31c 100644 --- a/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php +++ b/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php @@ -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); @@ -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; diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index 0c3ee1042fd..0e8b83037e9 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -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); + } } diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index b1b78cb095a..6adf72c11c1 100644 --- a/src/Configuration/Option.php +++ b/src/Configuration/Option.php @@ -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'; } diff --git a/src/Configuration/RectorConfigProvider.php b/src/Configuration/RectorConfigProvider.php index 19f6efa4dc7..38479ca7086 100644 --- a/src/Configuration/RectorConfigProvider.php +++ b/src/Configuration/RectorConfigProvider.php @@ -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); + } } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index aa6a307cb3f..ab20d29122b 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -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; @@ -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); @@ -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); diff --git a/src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php b/src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php deleted file mode 100644 index 3707c63af47..00000000000 --- a/src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php +++ /dev/null @@ -1,34 +0,0 @@ -