2

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?

Rhys
  • 41
  • 4
  • same happens with intelephense. In the file AbstractController (Laminas\Mvc\Controller\AbstractController) the return type of @Request is RequestInterface which doesn't have those methods, but the actual request variable of Type HttpRequest (which is just an alias for Laminas\Http\Request and implements RequestInterface ) has them. https://imgur.com/a/K7k8ITS – FCR Dec 27 '21 at 16:09
  • Did you ever find a solution for this issue? – jjwdesign Mar 02 '22 at 16:21

1 Answers1

1

(A very late answer, but perhaps someone will find it useful.)

Laminas\Mvc\Controller\AbstractController typehints its request as Laminas\Stdlib\RequestInterface. Technically, it does not have to be an instance of Laminas\Http\Request, since you can use Laminas controllers for example in console applications. Below are some solutions to the issue.

(A) Check the getRequest() type

PHPStan should not complain if you ensure that whatever the method returns actually is a Laminas\Http\Request:

$request = $this->getRequest();
if (! $request instanceof Laminas\Http\Request) {
    // throw or return
}

assert() should work as well.

(B) Give PHPStan more Laminas insights

The PHPStan extension for Laminas Framework makes getRequest() return, during PHPStan tests, an actual response instance instead of the interface. Refer to the package README for installation instructions.

(C) Ignore the warning

PHPStan supports various methods of ignoring its errors, see their website for details. By using the configuration file you can ignore errors that match a specified regular expression. In this case, a minimal configuration could look like this:

parameters:
    ignoreErrors:
        - '#Call to an undefined method Laminas\\Stdlib\\RequestInterface::isPost\(\)#'
gsc
  • 1,559
  • 1
  • 13
  • 17