Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Oct 31, 2025

fixes 10 errors

]);
if (
!isset($unionTypeA->getTypes()[0])
|| !isset($unionTypeA->getTypes()[1])
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't get reported with reportPossiblyNonexistentConstantArrayOffset: true, right? I'm not sure why we'd need these conditions :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see

 ------ ------------------------------------------------------- 
  Line   UnionTypeTest.php                                      
 ------ ------------------------------------------------------- 
  186    Offset 0 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:186            
  192    Offset 1 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:192            
  272    Offset 0 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:272            
  278    Offset 1 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:278            
  501    Offset 0 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:501            
  507    Offset 1 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:507            
  594    Offset 0 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:594            
  600    Offset 1 might not exist on array<PHPStan\Type\Type>.  
         🪪  offsetAccess.notFound                              
         at tests/PHPStan/Type/UnionTypeTest.php:600            
 ------ ------------------------------------------------------- 

Copy link
Member

Choose a reason for hiding this comment

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

When do you see this? What config change causes it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reportPossiblyNonexistentGeneralArrayOffset: true

Copy link
Member

Choose a reason for hiding this comment

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

Oh, that's not surprising. But I don't know if I ever want to enable that, because of needing to write code like that. It's not a very big difference whether we get "internal error" or "access on an undefined offset" notice when someone runs that code path. It's just that we need more code for the former.

How many errors are currently remaining when you enable that option?

Copy link
Member

Choose a reason for hiding this comment

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

For similar reasons I don't plan to increase to level 9/10. Not enough benefits...

Copy link
Contributor Author

@staabm staabm Oct 31, 2025

Choose a reason for hiding this comment

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

I am currently looking into how easily we can get the count down.
I started with ~1500 and after open PRs are merged we are at ~1000

We might e.g. ignore this error in tests/ but not in src/ or similar

Copy link
Member

Choose a reason for hiding this comment

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

The improvements are useful if they don't make the code worse but better.

Also some situations might inspire us to improve the type inference.

But to add a bunch of ShouldNotHappen throws just so we can enable the option isn't great.

Also in tests it's often easier to fix by adding an assert which is okay in tests ("we assume this") but not in src/ where the assumption might actually be wrong.

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'd much rather refactor this by saving IntegerType into a variable and then reference it in both yields. Instead of these issets and throws.

@staabm
Copy link
Contributor Author

staabm commented Nov 1, 2025

done

@ondrejmirtes ondrejmirtes merged commit 7cb0ec7 into phpstan:2.1.x Nov 1, 2025
283 of 284 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the type-tests branch November 1, 2025 10:56
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.

2 participants