Slamdunk / phpstan-laminas-framework

Laminas Framework 3 extensions for PHPStan
MIT License
16 stars 10 forks source link

Interfaces vs. concrete implementations #13

Closed bitwombat closed 4 years ago

bitwombat commented 4 years ago

Hi, thanks for taking over this plugin!

I get this PHPStan error:

17     Call to an undefined method Laminas\View\Renderer\RendererInterface::partial().

And it's because of this code.

use Laminas\View\Helper\AbstractHelper;

class UfYouTube extends AbstractHelper
{
    /**
     * Sets the YouTube video identifier
     *
     * @param  string $vid The video identifier
     * @return string A link to the video
     */
    public function __invoke($vid)
    {
        return $this->view->partial('partial/uf-youtube', ['vid' => $vid]);
    }
}

So I think the problem for PHPStan is that the view is hinted as RendererInterface in AbstractHelper, but at runtime is actually a PhpRenderer, which has partial as a view plugin.

So to fix this am I supposed to extend AbstractHelper just to define $view as PhpRenderer?

Thanks.

Slamdunk commented 4 years ago

If you read the README.md you'd see:

getView() method on \Laminas\View\Helper\AbstractHelper returns the real Renderer instance instead of type-hinted interface

So just use $this->getView() method instead of the view attribute.

It is always advisable to rely on APIs instead of internals, even for child classes, so I've written this plugin to provide correct return types only for APIs.

bitwombat commented 4 years ago

Ah, thanks. I've been looking at this too long. I thought ->view was handled by a plugin or some other magic.