From 1b4395caa0579c559e484b75b7fd9baadc307c0c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 28 Jan 2024 10:58:04 +0100 Subject: [PATCH] [DX] Allow Arg in value resolver, as often used and intuitive (#5512) * allow Arg in value resolver, as often used and intuitive * complexity --- phpstan.neon | 4 +++ .../Printer/PhpDocInfoPrinter.php | 6 +++- src/PhpParser/Node/Value/ValueResolver.php | 30 ++++++++++++++----- .../PHPUnit/AbstractRectorTestCase.php | 7 ++++- .../PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php | 16 ++-------- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index c37437091c6..be449492ccd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -474,3 +474,7 @@ parameters: # closure detailed - '#Method Rector\\Config\\RectorConfig\:\:singleton\(\) has parameter \$concrete with no signature specified for Closure#' + + - + message: '#Class cognitive complexity is 52, keep it under 50#' + path: src/PhpParser/Node/Value/ValueResolver.php diff --git a/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php b/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php index f633a2cfded..be691ed4f28 100644 --- a/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php +++ b/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php @@ -181,7 +181,11 @@ private function printPhpDocNode(PhpDocNode $phpDocNode): string $output .= ' */'; } - return Strings::replace($output, self::NEW_LINE_WITH_SPACE_REGEX, static fn (array $match) => $match['new_line']); + return Strings::replace( + $output, + self::NEW_LINE_WITH_SPACE_REGEX, + static fn (array $match) => $match['new_line'] + ); } private function hasDocblockStart(string $output): bool diff --git a/src/PhpParser/Node/Value/ValueResolver.php b/src/PhpParser/Node/Value/ValueResolver.php index 6b92b3fd4c1..52d4eb40547 100644 --- a/src/PhpParser/Node/Value/ValueResolver.php +++ b/src/PhpParser/Node/Value/ValueResolver.php @@ -6,6 +6,7 @@ use PhpParser\ConstExprEvaluationException; use PhpParser\ConstExprEvaluator; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\ClassConstFetch; @@ -18,6 +19,7 @@ use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\ConstantScalarType; +use PHPStan\Type\ConstantType; use PHPStan\Type\TypeWithClassName; use Rector\Enum\ObjectReference; use Rector\Exception\ShouldNotHappenException; @@ -54,8 +56,12 @@ public function isValue(Expr $expr, mixed $value): bool return $this->getValue($expr) === $value; } - public function getValue(Expr $expr, bool $resolvedClassReference = false): mixed + public function getValue(Arg|Expr $expr, bool $resolvedClassReference = false): mixed { + if ($expr instanceof Arg) { + $expr = $expr->value; + } + if ($expr instanceof Concat) { return $this->processConcat($expr, $resolvedClassReference); } @@ -86,13 +92,8 @@ public function getValue(Expr $expr, bool $resolvedClassReference = false): mixe } $nodeStaticType = $this->nodeTypeResolver->getType($expr); - - if ($nodeStaticType instanceof ConstantArrayType) { - return $this->extractConstantArrayTypeValue($nodeStaticType); - } - - if ($nodeStaticType instanceof ConstantScalarType) { - return $nodeStaticType->getValue(); + if ($nodeStaticType instanceof ConstantType) { + return $this->resolveConstantType($nodeStaticType); } return null; @@ -341,4 +342,17 @@ private function resolveClassFromSelfStaticParent(ClassConstFetch $classConstFet return $parentClassName; } + + private function resolveConstantType(ConstantType $constantType): mixed + { + if ($constantType instanceof ConstantArrayType) { + return $this->extractConstantArrayTypeValue($constantType); + } + + if ($constantType instanceof ConstantScalarType) { + return $constantType->getValue(); + } + + return null; + } } diff --git a/src/Testing/PHPUnit/AbstractRectorTestCase.php b/src/Testing/PHPUnit/AbstractRectorTestCase.php index 4ed22d8cecb..714747c40e7 100644 --- a/src/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractRectorTestCase.php @@ -161,7 +161,12 @@ protected function doTestFile(string $fixtureFilePath): void // write temp file FileSystem::write($inputFilePath, $inputFileContents); - $this->doTestFileMatchesExpectedContent($inputFilePath, $inputFileContents, $expectedFileContents, $fixtureFilePath); + $this->doTestFileMatchesExpectedContent( + $inputFilePath, + $inputFileContents, + $expectedFileContents, + $fixtureFilePath + ); } protected function forgetRectorsRulesAndCollectors(): void diff --git a/tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php b/tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php index 60a51439e0f..585dc6f0f9f 100644 --- a/tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php +++ b/tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php @@ -67,16 +67,9 @@ public function testReplaceTagByAnother(): void $printedPhpDocInfo = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo); $printedPhpDocInfo = str_replace("\r\n", "\n", $printedPhpDocInfo); - $fileContent = str_replace( - "\r\n", - "\n", - FileSystem::read(__DIR__ . '/Source/expected-replaced-tag.txt') - ); + $fileContent = str_replace("\r\n", "\n", FileSystem::read(__DIR__ . '/Source/expected-replaced-tag.txt')); - $this->assertSame( - $fileContent, - $printedPhpDocInfo - ); + $this->assertSame($fileContent, $printedPhpDocInfo); } public function testDoNotAddSpaseWhenAddEmptyString(): void @@ -93,10 +86,7 @@ public function testDoNotAddSpaseWhenAddEmptyString(): void FileSystem::read(__DIR__ . '/Source/expected-without-space-when-add-empty-string.txt') ); - $this->assertSame( - $fileContent, - $printedPhpDocInfo - ); + $this->assertSame($fileContent, $printedPhpDocInfo); } private function createPhpDocInfoFromFile(string $path): ?PhpDocInfo