Skip to content

Commit

Permalink
[DX] Allow Arg in value resolver, as often used and intuitive (#5512)
Browse files Browse the repository at this point in the history
* allow Arg in value resolver, as often used and intuitive

* complexity
  • Loading branch information
TomasVotruba committed Jan 28, 2024
1 parent 040b839 commit 1b4395c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions src/PhpParser/Node/Value/ValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
7 changes: 6 additions & 1 deletion src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1b4395c

Please sign in to comment.