'PHP Laminas PHPStan - Call to an undefined method Laminas\Stdlib\RequestInterface::isPost()

We are running phpstan on a laminas project and running into errors.

As an example, in the controller we have some standard code which works fine.

$request = $this->getRequest();

if ($request->isPost()) { ... }

However phpstan is complaining:

Call to an undefined method Laminas\Stdlib\RequestInterface::isPost()

The problem appears that getRequest() is actually returning an instance of Laminas\Http\PhpEnvironment\Request which does inherit the isPost function from Laminas\Http\Request. But this function is not defined in RequestInterface.

One solution would be to define isPost in RequestInterface although I would prefer to avoid changes to the vendor code.

Is there a better way of getting round this?



Solution 1:[1]

Not sure about PhpStan but for vscode I just change this in AbstractController:line 42

    /** @var \Laminas\Http\PhpEnvironment\Request $request */
protected $request;

Which also corrects code completion for all of the methods etc.

Update: From what I read after looking it up, it seems as that should also satisfy PhpStan as well, maybe... If that doesnt work to stop PhpStan from complaining you may want to try a /** @method */ for the isPost() and see if that does the trick. The benefit there is that it doesnt require you to modify the AbstractController class, even if it is only a @var annotation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1