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

Use dedicated Type methods over isSuperTypeOf() #2772

Merged
merged 3 commits into from Nov 26, 2023

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Nov 24, 2023

while reviewing #2710 I noticed that we can simplify stuff by using dedicated Type methods

@@ -281,7 +281,7 @@ public function testVariableCertaintyInIsset(): void
116,
],
[
'Variable $variableInSecondCase in isset() always exists and is always null.',
'Variable $variableInSecondCase in isset() always exists and is not nullable.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this one is actually interessting, as I think it actually fixes a bug - which is suprising.

isset($variableInSecondCase); // always defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

having another look at the test-case I think our assertions could be more precise:

switch (true) {
case $variableInFirstCase = true:
isset($variableInSecondCase); // does not exist yet
case $variableInSecondCase = true:
isset($variableInFirstCase); // always defined
$variableAssignedInSecondCase = true;
break;
case whatever():
isset($variableInFirstCase); // always defined
isset($variableInSecondCase); // always defined
$variableInFallthroughCase = true;
isset($variableAssignedInSecondCase); // surely undefined
case foo():
isset($variableInFallthroughCase); // fine
default:
}

in case 1 and 2 the case expression is always true and we are only assigning values. as the switch-case is constructed we will always run into break on line 114, which effectively means the cases below line 114 are dead code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

found the reason for the change in behaviour:

$neverType->isNull() -> no
$nullType->isSuperTypeOf($neverType) -> yes

@staabm
Copy link
Contributor Author

staabm commented Nov 24, 2023

the doctrine error is valid: upstream fix in doctrine/orm#11085

Comment on lines +48 to +55
$type = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr);
if (!$type instanceof NeverType) {
return $this->generateError(
$type,
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this changes address the comments in #2772 (comment)

everything else in this PR was a mechnical change from
((new ConstantBooleanType(false))->isSuperTypeOf($type)->yes()) to $type->isFalse()->yes() etc.

@staabm
Copy link
Contributor Author

staabm commented Nov 26, 2023

the drupal error is caused by invalid parameter types: phpstan/phpstan#10189

therefore I think this is good to go.

@staabm staabm marked this pull request as ready for review November 26, 2023 15:42
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit a714d33 into phpstan:1.10.x Nov 26, 2023
423 of 425 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the use-types branch November 26, 2023 17:50
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