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

missing check when using factory, di inyection and preference #224

Open
esteban-serfe opened this issue Mar 25, 2022 · 2 comments
Open

missing check when using factory, di inyection and preference #224

esteban-serfe opened this issue Mar 25, 2022 · 2 comments

Comments

@esteban-serfe
Copy link

My current module has a preference over a class set on the di.xml file.

I'm using the factory on the constructor of a overwrite.

When using a method that only exists on the overwrited class that is created through the Factory, PHPStan reports the method as not existent.

di.xml

<preference for="Magento\Framework\View\Result\Page"                 type="ClassA" />
<preference for="Magento\Framework\View\Page\Config\Renderer" type="ClassB" />

Class with factory on constructor:

class ClassA extends Magento\Framework\View\Result\Page  {
public function __construct(
 ....
 classBFactory $pageConfigRendererFactory
) {
 ...
 parent::__construct(.... );
}

public function overwritenFunction( .... ) {
  ....
  $this->pageConfigRenderer->onlyOnThisClassMethod();
  ....
}

Class B:

class ClassB extends \Magento\Framework\View\Page\Config\Renderer 
{
  public function onlyOnThisClassMethod() { .... }
}

This is reported as:
Call to an undefined method Magento\\Framework\\View\\Page\\Config\\RendererInterface::renderExtraFooter()

Complete code has been tested and battle proven on production, so it works as expected.

For now I'm just ignoring the issue reported.

@shochdoerfer
Copy link
Member

Thanks a lot for raising this issue. This is indeed currently a problem as the di.xml files do not yet get parsed. I will need to check with the PHPStan maintainer what options we have to implement this.

In your example, you could redeclare $pageConfigRenderer in ClassA and add the correct @var annotation. That should do the trick, I would think.

@esteban-serfe
Copy link
Author

The possible solution will not work on this specific case as the parameter used is passed into the parent constructor. It might help in other situations.

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