Skip to content

Fix several issues with nullsafe type specification#2577

Merged
ondrejmirtes merged 5 commits intophpstan:1.10.xfrom
schlndh:fix-nullsafeCallGreaterThanScalar
Aug 19, 2023
Merged

Fix several issues with nullsafe type specification#2577
ondrejmirtes merged 5 commits intophpstan:1.10.xfrom
schlndh:fix-nullsafeCallGreaterThanScalar

Conversation

@schlndh
Copy link
Copy Markdown
Contributor

@schlndh schlndh commented Aug 18, 2023

The goal of this PR is to fix https://phpstan.org/r/c17b503c-a789-4d90-87a7-31f3436f1b68 .

These issues were also fixed:

The problem is that TypeCombinator::containsNull returns false for MixedType (there's even a test for that and some usages definitely rely on this behavior). That seems quite strange to me and it's possible that there are other issues due to this confusing method. But I replaced only those usages for which I could come up with a test.

@schlndh schlndh force-pushed the fix-nullsafeCallGreaterThanScalar branch from 050fd24 to 162e3e3 Compare August 19, 2023 06:56
@schlndh schlndh force-pushed the fix-nullsafeCallGreaterThanScalar branch from 162e3e3 to 12bc2c1 Compare August 19, 2023 07:02
@ondrejmirtes
Copy link
Copy Markdown
Member

Yes, you got it right, there's a difference between TypeCombinator::containsNull() and Type::isNull(). Before there was Type::isNull() we used (new NullType())->isSuperTypeOf($type). This way might still be used in some places and should be roughly equivalent to Type::isNull().

These two methods are different on purpose, sometimes you want one and sometimes you want the other one. It's possible that the usage is wrong in some places as you already figured out :)

Copy link
Copy Markdown
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.

Please move the tests out of tests/PHPStan/Analyser/AnalyserIntegrationTest.php. This test class is reserved for tests that crash PHPStan or cause performance problems.

Where to correctly put these tests? It depends on whether the issue creator complained about a specific error message (then the test belongs in the relevant rule test class), or if they complained about something that can be tested with assertType - then the test belongs to NodeScopeResolverTest.

It's also possible to reference the same file from a rule test class and from NodeScopeResolverTest if you'd want.

Thank you.

@ondrejmirtes
Copy link
Copy Markdown
Member

Of course this fix is hugely valuable and also satisfying given the number of issues issue-bot found it fixes :) Thank you very much.

@schlndh schlndh force-pushed the fix-nullsafeCallGreaterThanScalar branch from 83a0825 to 2cd8400 Compare August 19, 2023 12:34
@schlndh
Copy link
Copy Markdown
Contributor Author

schlndh commented Aug 19, 2023

@ondrejmirtes Ok, thanks for the feedback. I moved the tests out of the AnalyserIntegrationTest. I kept most of them both in a rule test and in the NodeScopeResolverTest to make sure that both the assertTypes are valid and that the rule is not triggered.

@ondrejmirtes ondrejmirtes merged commit d435f65 into phpstan:1.10.x Aug 19, 2023
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

@herndlm
Copy link
Copy Markdown
Contributor

herndlm commented Aug 19, 2023

6 issues fixed with a two-line-change, nice! :)

@schlndh schlndh deleted the fix-nullsafeCallGreaterThanScalar branch August 19, 2023 17:23
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.

3 participants