From b770192dc5d0a54ae6f12de7409e6d476c1b0ea1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 6 Aug 2022 10:46:40 +0200 Subject: [PATCH 1/3] implement LtrimFunctionReturnTypeExtension to support class-string --- conf/config.neon | 5 ++ .../Php/LtrimFunctionReturnTypeExtension.php | 50 +++++++++++++++++++ .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-7483.php | 17 +++++++ 4 files changed, 73 insertions(+) create mode 100644 src/Type/Php/LtrimFunctionReturnTypeExtension.php create mode 100644 tests/PHPStan/Analyser/data/bug-7483.php diff --git a/conf/config.neon b/conf/config.neon index 83a69fd93e..0f54599481 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1418,6 +1418,11 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\LtrimFunctionReturnTypeExtension + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension + - class: PHPStan\Type\Php\MbFunctionsReturnTypeExtension tags: diff --git a/src/Type/Php/LtrimFunctionReturnTypeExtension.php b/src/Type/Php/LtrimFunctionReturnTypeExtension.php new file mode 100644 index 0000000000..a1e86efed5 --- /dev/null +++ b/src/Type/Php/LtrimFunctionReturnTypeExtension.php @@ -0,0 +1,50 @@ +getName() === 'ltrim'; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type + { + if (count($functionCall->getArgs()) !== 2) { + return null; + } + + $string = $scope->getType($functionCall->getArgs()[0]->value); + $trimChars = $scope->getType($functionCall->getArgs()[1]->value); + + if ($trimChars instanceof ConstantStringType && $trimChars->getValue() === '\\') { + if ($string instanceof ConstantStringType && $string->isClassString()) { + return new ConstantStringType(ltrim($string->getValue(), $trimChars->getValue()), true); + } + + if ($string instanceof ClassStringType) { + return new ClassStringType(); + } + } + + return null; + } + +} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 01b3febe36..6f91873dd7 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -964,6 +964,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5223.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7698.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/non-falsy-string.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7483.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-7483.php b/tests/PHPStan/Analyser/data/bug-7483.php new file mode 100644 index 0000000000..d5d3fe9943 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-7483.php @@ -0,0 +1,17 @@ + Date: Sat, 6 Aug 2022 11:02:02 +0200 Subject: [PATCH 2/3] cover generic class string --- src/Type/Php/LtrimFunctionReturnTypeExtension.php | 3 +-- tests/PHPStan/Analyser/data/bug-7483.php | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Type/Php/LtrimFunctionReturnTypeExtension.php b/src/Type/Php/LtrimFunctionReturnTypeExtension.php index a1e86efed5..f9d1537c09 100644 --- a/src/Type/Php/LtrimFunctionReturnTypeExtension.php +++ b/src/Type/Php/LtrimFunctionReturnTypeExtension.php @@ -4,7 +4,6 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\Scope; -use PHPStan\Php\PhpVersion; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\ClassStringType; use PHPStan\Type\Constant\ConstantStringType; @@ -16,7 +15,7 @@ class LtrimFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension { - public function __construct(private PhpVersion $phpVersion) + public function __construct() { } diff --git a/tests/PHPStan/Analyser/data/bug-7483.php b/tests/PHPStan/Analyser/data/bug-7483.php index d5d3fe9943..c6fe12d899 100644 --- a/tests/PHPStan/Analyser/data/bug-7483.php +++ b/tests/PHPStan/Analyser/data/bug-7483.php @@ -6,6 +6,14 @@ class A {} +/** + * @param class-string $class + */ +function bar($class): string +{ + assertType('class-string', ltrim($class, '\\')); +} + /** * @param class-string $class * @return class-string From 2a14dd44c9b6f2b98094c7f924fa5df89ff83149 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 6 Aug 2022 13:42:17 +0200 Subject: [PATCH 3/3] Remove construct() --- src/Type/Php/LtrimFunctionReturnTypeExtension.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Type/Php/LtrimFunctionReturnTypeExtension.php b/src/Type/Php/LtrimFunctionReturnTypeExtension.php index f9d1537c09..a70f736661 100644 --- a/src/Type/Php/LtrimFunctionReturnTypeExtension.php +++ b/src/Type/Php/LtrimFunctionReturnTypeExtension.php @@ -15,10 +15,6 @@ class LtrimFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension { - public function __construct() - { - } - public function isFunctionSupported(FunctionReflection $functionReflection): bool { return $functionReflection->getName() === 'ltrim';