phpstan: [Bug] Version 0.12.12 and baseline

It might be me doing something wrong. But I’ve got issues running baseline with version 0.12.12.

➜  happyr git:(phpstan) ✗ phpstan --version
PHPStan - PHP Static Analysis Tool 0.12.12
➜  happyr git:(phpstan) ✗     phpstan clear-result-cache                                     
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
Result cache cleared from directory:
/var/folders/38/5p1d21_d5m565304nl8t8qq00000gn/T/phpstan
➜  happyr git:(phpstan) ✗ phpstan analyze --error-format baselineNeon > phpstan-baseline.neon
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
 2525/2525 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

➜  happyr git:(phpstan) ✗ phpstan analyze                                                    
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
 2525/2525 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ---------------------------------------------------------------------------------------------- 
  Line   App/DoctrineMigrations/Version20151110150236.php                                              
 ------ ---------------------------------------------------------------------------------------------- 
  24     Class Doctrine\ORM\EntityManager referenced with incorrect case: Doctrine\Orm\EntityManager.  
 ------ ---------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 1 error                                                                                                  
                                                                                                                        

If I update baseline again, it will work.

➜  happyr git:(phpstan) ✗ phpstan analyze --error-format baselineNeon > phpstan-baseline.neon
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
 2525/2525 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

➜  happyr git:(phpstan) ✗ phpstan analyze                                                    
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
 2525/2525 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        


But if I clear the cache I get more issues:

➜  happyr git:(phpstan) ✗     phpstan clear-result-cache
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
^[[AResult cache cleared from directory:
/var/folders/38/5p1d21_d5m565304nl8t8qq00000gn/T/phpstan
➜  happyr git:(phpstan) ✗                           
➜  happyr git:(phpstan) ✗ phpstan analyze           
Note: Using configuration file /Users/tobias/Workspace/PHPStorm/happyr/phpstan.neon.dist.
 2525/2525 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ---------------------------------------------------------------------------------------------------------------------- 
  Line   App/DoctrineMigrations/Version20131120152300.php                                                                      
 ------ ---------------------------------------------------------------------------------------------------------------------- 
  23     Property Application\Migrations\Version20131120152300::$em has unknown class Doctrine\Orm\EntityManager as its type.  
 ------ ---------------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------------------------------- 
  Line   App/DoctrineMigrations/Version20150902154240.php                                                                      
 ------ ---------------------------------------------------------------------------------------------------------------------- 
  24     Property Application\Migrations\Version20150902154240::$em has unknown class Doctrine\Orm\EntityManager as its type.  
 ------ ---------------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------- 
  Line   App/DoctrineMigrations/Version20151110150236.php                                              
 ------ ---------------------------------------------------------------------------------------------- 
  24     Class Doctrine\ORM\EntityManager referenced with incorrect case: Doctrine\Orm\EntityManager.  
 ------ ---------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 3 errors                                                                                                 
                                                                                                                        


Here is my config file.

includes:
    - ./phpstan-baseline.neon

parameters:
  level: 5
  reportUnmatchedIgnoredErrors: false
  autoload_directories:
    - bin/.phpunit/phpunit-8.3-0/src
    - %currentWorkingDirectory%/src/App/DoctrineMigrations/
  paths:
    - src

It is only complaining about files in the DoctrineMigrations folder.

Note that I did not change any files or run any commands but the ones I posted here.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (18 by maintainers)

Most upvoted comments

Wow, you’re awesome, this is pretty actionable info 😊 I’ll think about it.

@rainbow-alex This should no longer happen on dev-master 😃 Feel free to try it.

@ondrejmirtes I’ve nailed it down to a minimal case. Took a while, but in the end the cause is pretty obvious.

When phpstan encounters a classname in the incorrect case, two things can happen:

  • The class hasn’t been loaded yet; the autoloader fails and phpstan continues as if the class doesn’t exist. In subsequent code it can do less checks and will not be able to report all errors.
  • Some other file has already loaded the class, presumably by referencing it with the correct casing. It can be resolved and phpstan emits an error about the casing and continues happily.

So unfortunately, the errors emitted depend on the order in which files are scanned/loaded. Parallel analysis makes this more likely to occur, but it’s not the root cause. You can reproduce the issue with these files and commands:

$ phpstan a Aardvark.php Zygote.php
=> Parameter $p of method Aardvark::foo() has invalid typehint type Casesensitiveclassname.

$ phpstan a Zygote.php Aardvark.php
=> Class CaseSensitiveClassName referenced with incorrect case: Casesensitiveclassname.

Aardvark.php

<?php

class Aardvark
{
	function foo(Casesensitiveclassname $p)
	{
	}
}

CaseSensitiveClassName.php

<?php

class CaseSensitiveClassName
{
}

Zygote.php:

<?php

class Zygote
{
	function foo(CaseSensitiveClassName $p)
	{
	}
}

phpstan.neon

parameters:
	level: 5
	bootstrap: ./autoload.php
	paths:
		- Zygote.php
		- Aardvark.php

I’m not sure what the right fix is here. Class names are not case sensitive but autoloaders usually are. I suppose you could check at the very end of all analysis if a missing class turns out to be defined after all and emit an additional, non-baseline error?