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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional types regressions #8467

Closed
ondrejmirtes opened this issue Dec 5, 2022 · 6 comments
Closed

Conditional types regressions #8467

ondrejmirtes opened this issue Dec 5, 2022 · 6 comments
Labels

Comments

@ondrejmirtes
Copy link
Member

Bug report

Hi @rajyan, there's some more work to do 馃槄

I updated the Composer integration test and there are some new errors. The build is green on 1.9.2:

 ------ ---------------------------------------------------------------- 
  Line   src/Composer/Command/ShowCommand.php                            
 ------ ---------------------------------------------------------------- 
  837    Parameter #2 $array of function implode expects array<string>,  
         array<int<0, max>|string, array<string>|string> given.          
 ------ ---------------------------------------------------------------- 

 ------ ------------------------------------------------------- 
  Line   src/Composer/Installer/SuggestedPackagesReporter.php   
 ------ ------------------------------------------------------- 
  129    Foreach overwrites $suggestion with its key variable.  
 ------ ------------------------------------------------------- 

 ------ -------------------------------------------------------------- 
  Line   src/Composer/Package/Package.php                              
 ------ -------------------------------------------------------------- 
  683    Parameter #4 $type of static method                           
         Composer\Util\ComposerMirror::processHgUrl() expects string,  
         string|null given.                                            
 ------ -------------------------------------------------------------- 

 ------ ------------------------------------------------------------- 
  Line   src/Composer/Util/Git.php                                    
 ------ ------------------------------------------------------------- 
  87     Parameter #1 $directory of method                            
         Composer\Util\Filesystem::removeDirectory() expects string,  
         string|null given.                                           
  251    Parameter #1 $directory of method                            
         Composer\Util\Filesystem::removeDirectory() expects string,  
         string|null given.                                           
 ------ ------------------------------------------------------------- 

In ShowCommand it should know the type is narrowed because the getter has an array shape type alias. So it should know it's already list<string>.

SuggestedPackagesReporter might be a bit tricky. This isn't new behaviour but it's reported in more cases because the conditonal types are now more "smart". The problem is in polluteScopeWithAlwaysIterableForeach: false (the configuration in phpstan-strict-rules which also adds the rule "foreach overwrites existing variable"). Here it is: https://phpstan.org/r/a58b7580-7018-418b-bbe6-157c5ee91758

The problem is that PHPStan with polluteScopeWithAlwaysIterableForeach: false SHOULD NOT know that a variable always exists if some array is non-empty. But here it is aware that if $k is non-empty then $v always exists. But with polluteScopeWithAlwaysIterableForeach: false we don't want this behaviour. It might be a bit tricky to fix...

And the Mercurial and Git problems are the same - the value is already checked and cannot be null at that point.

Thank you very much for looking into this, I'd be really gratefuly if you were able to fix at least some of those.

@rajyan
Copy link
Contributor

rajyan commented Dec 5, 2022

Thank you for investigating!
I think I have some ideas to fixed them, gonna try it tomorrow morning.

rajyan added a commit to rajyan/phpstan-src that referenced this issue Dec 6, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Dec 6, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Dec 7, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Dec 7, 2022
@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.9.x, PHPStan now reports different result with your code snippet:

@@ @@
-17: Foreach overwrites $v with its value variable.
+No errors

@rajyan
Copy link
Contributor

rajyan commented Dec 8, 2022

@ondrejmirtes
Copy link
Member Author

馃憤

@github-actions
Copy link

github-actions bot commented Jan 9, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants