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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with ::class and Missing Return Statement in CI #335

Open
Morgy93 opened this issue Jan 16, 2025 · 9 comments
Open

Issue with ::class and Missing Return Statement in CI #335

Morgy93 opened this issue Jan 16, 2025 · 9 comments

Comments

@Morgy93
Copy link

Morgy93 commented Jan 16, 2025

Hello,

I’m encountering an issue where referencing a class with ::class causes the referenced class to throw the following error in CI (but not locally):

Method Two::getDependencies() should return array but return statement is missing.

As an example, this happens when working with data patches that have dependencies:

<?php

declare(strict_types=1);

use Magento\Framework\Setup\Patch\DataPatchInterface;

class One implements DataPatchInterface
{
    public static function getDependencies(): array
    {
        return [
            Two::class, // <--------------------------------------------------------------
        ];
    }
}
<?php

declare(strict_types=1);

use Magento\Framework\Setup\Patch\DataPatchInterface;

class Two implements DataPatchInterface
{
    public static function getDependencies(): array
    {
        return [];
    }
}

In this setup, the error is thrown in CI, even though everything runs fine locally.

Has anyone experienced this behavior? Could it be related to a specific configuration?
Any guidance on resolving this discrepancy would be greatly appreciated.

@shochdoerfer
Copy link
Member

Interesting. No one reported this so far, and I don't have a clue right now why I would fail.

Is this a Magento project or a Magento module? In the latter case, the only difference would be the way the generated files get created. Locally it's Magento logic, in CI it would be our module.

What happens if you replace the :class notation with a string with the fully qualified class name? Does the error appear in CI also?

And which PHPStan version are you using?

@Morgy93
Copy link
Author

Morgy93 commented Jan 16, 2025

I can currently reproduce this issue in two different custom modules: one using a data patch and another using a ViewModel.

For example, in the phtml file:
$viewModel = $viewModels->require(\Some\Example\Class::class);
This triggers the same "missing return type" error for the methods in Some\Example\Class.

Using the alternative:
$viewModel = $viewModels->require('Some\Example\Class');
doesn’t resolve the issue either; the error persists.
Additionally, phpcs complains with:
Use ::class notation instead. (Magento2.PHP.LiteralNamespaces.LiteralClassUsage) 😅

Versions:

  • phpstan/phpstan: 1.12.15
  • bitexpert/phpstan-magento: v0.32.0

@shochdoerfer
Copy link
Member

I've tested this today intensively, and I cannot reproduce the wrong behavior locally.

Since you say it works locally but breaks in CI, how do you invoke PHPStan locally and how do you invoke PHPStan in CI? Any other differences? Maybe different PHP versions?

@shochdoerfer
Copy link
Member

Just wondering, is this fixed with the dev-master version & PHPStan 2.0?

@Morgy93
Copy link
Author

Morgy93 commented Feb 24, 2025

Just wondering, is this fixed with the dev-master version & PHPStan 2.0?

It throws an error locally as well when I remove one of the ignored files. I'm not sure if this is due to PHPStan 2.0 or if it's a clue to the original issue.

     Internal error: Foo_PageBuilderInterface does not exist and has no                                                                                      
     extension interface while analysing file                                                                                                               
     src/foo/page-builder-extension/registration.php                                                                                           
     Post the following stack trace to                                                                                                                      
     https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:                                                                                

     ## vendor/bitexpert/phpstan-magento/src/bitExpert/PHPStan/Magento/Autoload/ExtensionInterfaceAutoloader.php(93)                             
     #0 vendor/bitexpert/phpstan-magento/src/bitExpert/PHPStan/Magento/Autoload/ExtensionInterfaceAutoloader.php(69):                            
     bitExpert\PHPStan\Magento\Autoload\ExtensionInterfaceAutoloader->getFileContents()                                                                     
     #1 [internal function]:                                                                                                                                
     bitExpert\PHPStan\Magento\Autoload\ExtensionInterfaceAutoloader->autoload()                                                                            
     #2 vendor/bitexpert/phpstan-magento/src/bitExpert/PHPStan/Magento/Autoload/ExtensionAutoloader.php(87):                                     
     ReflectionClass->__construct()                                                                                                                         
     #3 vendor/bitexpert/phpstan-magento/src/bitExpert/PHPStan/Magento/Autoload/ExtensionAutoloader.php(70):                                     
     bitExpert\PHPStan\Magento\Autoload\ExtensionAutoloader->getFileContents()                                                                              
     #4 phar://vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflection/SourceLocator/AutoloadFunctionsSourceLocator.php(35):         
     bitExpert\PHPStan\Magento\Autoload\ExtensionAutoloader->autoload()                                                                                     
     #5 phar://vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/AggregateSourceLocator.php(26):  
     PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadFunctionsSourceLocator->locateIdentifier()                                                   
     #6 phar://vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/MemoizingSourceLocator.php(30):  
     PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator->locateIdentifier()                                                                 
     #7 phar://vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflector/DefaultReflector.php(29):                 
     PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator->locateIdentifier()                                                                 
     #8 phar://vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflection/Reflector/MemoizingReflector.php(42):                         
     PHPStan\BetterReflection\Reflector\DefaultReflector->reflectClass()                                                                                    
     #9 phar://vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflection/BetterReflectionProvider.php(125):                            
     PHPStan\Reflection\BetterReflection\Reflector\MemoizingReflector->reflectClass()                                                                       
     ... and so on ...                                                           

registration.php

<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Foo_PageBuilderExtension',
    __DIR__ . '/src'
);

Maybe you'll be able to identify the error more quickly.

@Morgy93
Copy link
Author

Morgy93 commented Feb 24, 2025

Oh, wait - I see it now! It's probably because of the "Extension" in the module name, right? 😆

@shochdoerfer
Copy link
Member

Yeah the extension attribute autoloader is kicking in. Would probably be good to have some blacklist you can configure to ignore those errors.

Currently, your only option is to rename the class ;(

@shochdoerfer
Copy link
Member

The module name should not be the issue, the class name is probably the issue. You check check both autoloaders what name postfixes we are matching.

@Morgy93
Copy link
Author

Morgy93 commented Feb 24, 2025

Sorry about that - I was checking the wrong project and file. I should have looked at the original post.

With PHPStan 2.0, the behavior is the same: no error locally, but in CI, it throws the same issue as in the original post.

I could set up a project on gitlab.com that reproduces the error so you can debug it. What do you think?

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

No branches or pull requests

2 participants