phpstan: PHP Fatal error: Cannot declare class because the name is already in use - 1.7.2

Bug report

Ref: https://github.com/owncloud/core/issues/40105

phpstan 1.7.1 is passing in https://github.com/owncloud/core but 1.7.2 reports:

Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing phpstan/phpstan (1.7.2): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
php -d zend.enable_gc=0 vendor-bin/phpstan/vendor/bin/phpstan analyse --memory-limit=2G --configuration=./phpstan.neon --level=0 apps core settings lib/private lib/public ocs ocs-provider
 1466/1466
 -- ------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                        
 -- ------------------------------------------------------------------------------------------------------------- 
     Child process error (exit code 255): PHP Fatal error:  Cannot declare class Laminas\Validator\EmailAddress,  
     because the name is already in use in                                                                        
     /home/phil/git/owncloud/core/lib/composer/laminas/laminas-validator/src/EmailAddress.php on line 31          
                                                                                                                  
     Child process error (exit code 255): PHP Fatal error:  Cannot declare class Laminas\InputFilter\Factory,     
     because the name is already in use in                                                                        
     /home/phil/git/owncloud/core/lib/composer/laminas/laminas-inputfilter/src/Factory.php on line 12             
                                                                                                                  
 -- ------------------------------------------------------------------------------------------------------------- 
                                                                                                                     
 [ERROR] Found 2 errors                                                                                  

I can stop the first error by removing any mention of Laminas\Validator\EmailAddress from https://github.com/owncloud/core/blob/master/core/Command/User/ResetPassword.php - but that code only does:

use Laminas\Validator\EmailAddress;
...
	protected function hasValidEmailAddress($emailAddress) {
		return (new EmailAddress())->isValid($emailAddress);
	}

It only uses the class, it does not declare it.

I am struggling to see where the code in https://github.com/owncloud/core has something wrong with it. Maybe something in phpstan 1.7.2 is now internally finding and trying to create the class twice?

Code snippet that reproduces the problem

I will try to get a smaller example over the weekend!

Expected output

No errors reported.

Did PHPStan help you today? Did it make you happy in any way?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/owncloud/core/pull/40111/commits/95644827d7ce3e8ddcdf8f6c252ddedeb41d3d8a I unpinned the phpstan dependency. It passes with 1.7.6.

Notes: 1.7.5 gives:

Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking phpstan/phpstan (1.7.5)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading phpstan/phpstan (1.7.5)
  - Installing phpstan/phpstan (1.7.5): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
php -d zend.enable_gc=0 vendor-bin/phpstan/vendor/bin/phpstan analyse --memory-limit=2G --configuration=./phpstan.neon --level=0 apps core settings lib/private lib/public ocs ocs-provider
 1466/1466  100%
 ------ ------------------------------------------------------------------------------------------------------------- 
  Line   lib/private/Files/Cache/HomeCache.php                                                                        
 ------ ------------------------------------------------------------------------------------------------------------- 
  32     Non-abstract class OC\Files\Cache\HomeCache contains abstract method put() from class OC\Files\Cache\Cache.  
 ------ ------------------------------------------------------------------------------------------------------------- 
 [ERROR] Found 1 error
make: *** [Makefile:237: test-php-phpstan] Error 1

1.7.4 gives the original reported errors:

Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking phpstan/phpstan (1.7.4)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading phpstan/phpstan (1.7.4)
  - Installing phpstan/phpstan (1.7.4): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
php -d zend.enable_gc=0 vendor-bin/phpstan/vendor/bin/phpstan analyse --memory-limit=2G --configuration=./phpstan.neon --level=0 apps core settings lib/private lib/public ocs ocs-provider
 1466/1466  100%

 -- --------------------------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                                              
 -- --------------------------------------------------------------------------------------------------------------------------------------------------- 
     Child process error (exit code 255): PHP Fatal error:  Cannot declare class Laminas\Validator\EmailAddress, because the name is already in use in  
     /home/phil/git/owncloud/core/lib/composer/laminas/laminas-validator/src/EmailAddress.php on line 31                                                
                                                                                                                                                        
     Child process error (exit code 255): PHP Fatal error:  Cannot declare class Laminas\InputFilter\Factory, because the name is already in use in     
     /home/phil/git/owncloud/core/lib/composer/laminas/laminas-inputfilter/src/Factory.php on line 12                                                   
                                                                                                                                                       
 -- --------------------------------------------------------------------------------------------------------------------------------------------------- 
 [ERROR] Found 2 errors
make: *** [Makefile:237: test-php-phpstan] Error 1

So 1.7.6 is really needed.

Maybe there is a hint here: https://github.com/owncloud/core/pull/40108/commits/5d229cc73e5acce9f829561dfd5d431f6f1b2fc8#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34

If I remove "vendor-dir": "lib/composer", from composer.json and let the dependencies be in the default vendor directory then phpstan 1.7.2 passes. So maybe something changed about processing “vendor-dir”?