From 60c75b0d2cea65ec4b1b8986fe7454c1493c4602 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 3 Dec 2022 17:00:14 +0100 Subject: [PATCH] Fix comparison with get_class() in traits always evaluate to true --- .../GetClassDynamicReturnTypeExtension.php | 6 ++++ ...rictComparisonOfDifferentTypesRuleTest.php | 6 ++++ .../Rules/Comparison/data/bug-3633.php | 30 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 tests/PHPStan/Rules/Comparison/data/bug-3633.php diff --git a/src/Type/Php/GetClassDynamicReturnTypeExtension.php b/src/Type/Php/GetClassDynamicReturnTypeExtension.php index 863871f75ba..0f3754498e5 100644 --- a/src/Type/Php/GetClassDynamicReturnTypeExtension.php +++ b/src/Type/Php/GetClassDynamicReturnTypeExtension.php @@ -17,6 +17,7 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\StaticType; +use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeTraverser; use PHPStan\Type\TypeWithClassName; @@ -34,6 +35,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type { $args = $functionCall->getArgs(); + + if ($scope->isInTrait()) { + return new StringType(); + } + if (count($args) === 0) { if ($scope->isInClass()) { return new ConstantStringType($scope->getClassReflection()->getName(), true); diff --git a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php index d5f15687065..b0e28ebe8cb 100644 --- a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php @@ -613,4 +613,10 @@ public function testPhpUnitIntegration(): void $this->analyse([__DIR__ . '/../../Analyser/data/phpunit-integration.php'], []); } + public function testBug3633(): void + { + $this->checkAlwaysTrueStrictComparison = true; + $this->analyse([__DIR__ . '/data/bug-3633.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-3633.php b/tests/PHPStan/Rules/Comparison/data/bug-3633.php new file mode 100644 index 00000000000..8477f6d403f --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-3633.php @@ -0,0 +1,30 @@ +test(); + } +} + +class OtherClass { + use Foo; + + public function bar(): void { + $this->test(); + } +}