Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison with get_class() in traits always evaluate to true #2044

Merged
merged 3 commits into from Jan 8, 2023

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Dec 3, 2022

closes phpstan/phpstan#3633

see https://3v4l.org/LalXd

test fails without the src change:

1) PHPStan\Rules\Comparison\StrictComparisonOfDifferentTypesRuleTest::testBug3633
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'23: Strict comparison using === between class-string<Bug3633\HelloWorld> and 'Bug3633\\OtherClass' will always evaluate to false.
+'10: Strict comparison using === between class-string<Bug3633\HelloWorld> and 'Bug3633\\OtherClass' will always evaluate to false.
+23: Strict comparison using === between class-string<Bug3633\HelloWorld> and 'Bug3633\\OtherClass' will always evaluate to false.
+07: Strict comparison using === between class-string<Bug3633\OtherClass> and 'Bug3633\\HelloWorld' will always evaluate to false.
 35: Strict comparison using === between class-string<Bug3633\OtherClass> and 'Bug3633\\HelloWorld' will always evaluate to false.
+07: Strict comparison using === between class-string<Bug3633\FinalClass> and 'Bug3633\\HelloWorld' will always evaluate to false.
+10: Strict comparison using === between class-string<Bug3633\FinalClass> and 'Bug3633\\OtherClass' will always evaluate to false.
 50: Strict comparison using === between class-string<Bug3633\FinalClass> and 'Bug3633\\HelloWorld' will always evaluate to false.
 53: Strict comparison using === between class-string<Bug3633\FinalClass> and 'Bug3633\\OtherClass' will always evaluate to false.
 59: Strict comparison using === between 'Bug3633\\FinalClass' and 'Bug3633\\FinalClass' will always evaluate to true.
 '

@staabm staabm marked this pull request as ready for review December 3, 2022 16:05
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -42,6 +44,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new ConstantBooleanType(false);
}

if ($scope->isInTrait()) {
return new StringType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can at least say that it's going to be ClassStringType.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just realized this PR is more wrong than right.

  1. We need to cover the same scenario for get_class() without arguments.
  2. We can still return correct specific result for an argument that isn't $this.

@ondrejmirtes ondrejmirtes merged commit 503e820 into phpstan:1.9.x Jan 8, 2023
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the trait-get-class branch January 8, 2023 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants