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

Truthy isset($arr[$k]) should narrow $k #3453

Merged
merged 19 commits into from
Sep 22, 2024
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 19, 2024

@staabm
Copy link
Contributor Author

staabm commented Sep 19, 2024

I am puzzled by the test and its expectations:

private const MAP = [
'10' => 'Test1',
'20' => 'Test2',
];
public function validate(string $value): void
{
$value = trim($value);
if ($value === '') {
throw new \RuntimeException();
}
$value = self::MAP[$value] ?? $value;
assertType("'Test1'|'Test2'", self::MAP[$value]);
// ...

I feel the assertion in this test is wrong, see https://3v4l.org/WD7OG
after this PR we now get a *ERROR* which is not better either though ;)

@@ -818,6 +818,60 @@ public function specifyTypesInCondition(
$rootExpr,
),
);
} elseif ($var->dim instanceof Expr\Variable) {
Copy link
Member

@ondrejmirtes ondrejmirtes Sep 22, 2024

Choose a reason for hiding this comment

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

Doesn't have to be just a variable. Getting rid of instanceof like these leads to bugfixes:

@staabm staabm marked this pull request as ready for review September 22, 2024 09:49
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Sep 22, 2024

in a followup I will look into implications for $k in array_key_exists

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.

I think this is solid enough to merge this :) One more thing: regression test for phpstan/phpstan#8559 please.

@ondrejmirtes ondrejmirtes merged commit 80fdfab into phpstan:1.12.x Sep 22, 2024
482 of 499 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants