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..f46c054e6d4 100644 --- a/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php +++ b/packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php @@ -72,8 +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); if ($scope instanceof Scope) { @@ -88,7 +92,7 @@ private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag return $this->resolveAsAliased($uses, $tag, $returnNullOnUnknownClass); } - if (str_starts_with($tag, '\\') && $this->reflectionProvider->hasClass($tag)) { + if ($this->isPreslashedExistingClass($tag)) { // Global or absolute Class return $tag; } @@ -127,10 +131,20 @@ 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; return $returnNullOnUnknownClass ? $resolvedClass : $class; } + + private function isPreslashedExistingClass(string $tag): bool + { + if (! str_starts_with($tag, '\\')) { + return false; + } + + return $this->reflectionProvider->hasClass($tag); + } } 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/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this.php.inc b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this.php.inc index 9fd15ea49ff..24c4abe3dcb 100644 --- a/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this.php.inc +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this.php.inc @@ -4,14 +4,14 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi final class FixtureRemoveMethodCallOnThis { - public function __construct() - { - $this->validateLineLengths(); - } - - protected function validateLineLengths(): void - { - } + public function __construct() + { + $this->validateLineLengths(); + } + + protected function validateLineLengths(): void + { + } } ?> @@ -22,13 +22,13 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi final class FixtureRemoveMethodCallOnThis { - public function __construct() - { - } + public function __construct() + { + } - protected function validateLineLengths(): void - { - } + protected function validateLineLengths(): void + { + } } ?> diff --git a/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this_use_abstract.php.inc b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this_use_abstract.php.inc index de9732c89fd..9a6d5a7ae5d 100644 --- a/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this_use_abstract.php.inc +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/fixture_remove_method_call_on_this_use_abstract.php.inc @@ -4,17 +4,17 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi abstract class Validator { - protected function validateLineLengths(): void - { - } + protected function validateLineLengths(): void + { + } } final class GeneratorStubUseAbstract extends Validator { - public function __construct() - { - $this->validateLineLengths(); - } + public function __construct() + { + $this->validateLineLengths(); + } } ?> @@ -25,16 +25,16 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi abstract class Validator { - protected function validateLineLengths(): void - { - } + protected function validateLineLengths(): void + { + } } final class GeneratorStubUseAbstract extends Validator { - public function __construct() - { - } + public function __construct() + { + } } ?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/copy_doc.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/copy_doc.php.inc index 31b260503c5..ff4d1f2b818 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/copy_doc.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/copy_doc.php.inc @@ -9,23 +9,23 @@ class Z {} final class CopyDoc { - public Y $y; - - /** - * @var Z[] - * @Assert\Valid() - * @Assert\NotBlank() - */ - public array $z = []; - - /** - * @param Z[] $z - */ - public function __construct(Y $y, array $z = []) - { - $this->y = $y; - $this->z = $z; - } + public Y $y; + + /** + * @var Z[] + * @Assert\Valid() + * @Assert\NotBlank() + */ + public array $z = []; + + /** + * @param Z[] $z + */ + public function __construct(Y $y, array $z = []) + { + $this->y = $y; + $this->z = $z; + } } ?> ----- @@ -40,18 +40,18 @@ class Z {} final class CopyDoc { - /** - * @param Z[] $z - */ - public function __construct( - public Y $y, - /** - * @Assert\Valid() - * @Assert\NotBlank() - */ - public array $z = [] - ) - { - } + /** + * @param Z[] $z + */ + public function __construct( + public Y $y, + /** + * @Assert\Valid() + * @Assert\NotBlank() + */ + public array $z = [] + ) + { + } } ?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/generic.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/generic.php.inc index 4172dfbc3ab..5141289357d 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/generic.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/generic.php.inc @@ -7,18 +7,18 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo */ final class Generic { - /** - * @var T - */ - public mixed $value; + /** + * @var T + */ + public mixed $value; - /** - * @param T $value - */ - public function __construct(mixed $value) - { - $this->value = $value; - } + /** + * @param T $value + */ + public function __construct(mixed $value) + { + $this->value = $value; + } } ?> ----- @@ -31,11 +31,11 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo */ final class Generic { - /** - * @param T $value - */ - public function __construct(public mixed $value) - { - } + /** + * @param T $value + */ + public function __construct(public mixed $value) + { + } } ?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/iterable_typed.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/iterable_typed.php.inc index 5caed2a2262..5c3f4c00130 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/iterable_typed.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/iterable_typed.php.inc @@ -4,18 +4,18 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo class IterableTyped { - /** - * @var iterable - */ - private iterable $property; + /** + * @var iterable + */ + private iterable $property; - /** - * @param iterable $property - */ - public function __construct(iterable $property) - { - $this->property = $property; - } + /** + * @param iterable $property + */ + public function __construct(iterable $property) + { + $this->property = $property; + } } ?> ----- @@ -25,11 +25,11 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo class IterableTyped { - /** - * @param iterable $property - */ - public function __construct(private iterable $property) - { - } + /** + * @param iterable $property + */ + public function __construct(private iterable $property) + { + } } ?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_fully_qualified.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_fully_qualified.php.inc index 1600844c9de..06eec3cd2a8 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_fully_qualified.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_fully_qualified.php.inc @@ -10,20 +10,20 @@ class Z {} final class UnionFullyQualified { - public Y $y; + public Y $y; - public Z $z; + public Z $z; - public function __construct(Y $y, Z $z) - { - $this->y = $y; - $this->z = $z; - } + public function __construct(Y $y, Z $z) + { + $this->y = $y; + $this->z = $z; + } - public function getX(): X - { - return $this->y; - } + public function getX(): X + { + return $this->y; + } } ?> ----- @@ -39,13 +39,13 @@ class Z {} final class UnionFullyQualified { - public function __construct(public Y $y, public Z $z) - { - } - - public function getX(): X - { - return $this->y; - } + public function __construct(public Y $y, public Z $z) + { + } + + public function getX(): X + { + return $this->y; + } } ?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_typed.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_typed.php.inc index c0ac8d55eaa..0502e85cab1 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_typed.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/union_typed.php.inc @@ -7,12 +7,12 @@ use DateTime; final class UnionTyped { - public stdClass|DateTime $property; + public stdClass|DateTime $property; - public function __construct(stdClass|DateTime $property) - { - $this->property = $property; - } + public function __construct(stdClass|DateTime $property) + { + $this->property = $property; + } } ?> @@ -26,9 +26,9 @@ use DateTime; final class UnionTyped { - public function __construct(public stdClass|DateTime $property) - { - } + public function __construct(public stdClass|DateTime $property) + { + } } ?> 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 @@ -