7

In PhpStorm I get the warning message "warning method 'withJson' not found" in \Psr\Http\Message\ResponseInterface" at te line:

return $response->withJson($toReturn, 200);

The code:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;


$app->get('/bedrijven', function (Request $request, Response $response) {
    require_once(CLASSES_PATH . "/class_bedrijven.php");
    $Bedrijven = new Bedrijven();

    $toReturn = $Bedrijven->get_bedrijven();
    return $response->withJson($toReturn, 200);
});

I already updated slim framework with composer to latest version 3.8.1 and added Slim as a plugin in PhpStorm. The Vendor directorie is set to Sources and Excluded.

The only answer I can find is to turn off the warning messages in PhpStorm in Editor -> Inspections -> PHP -> Undefined -> undefined method.

Is there a better solution?

Nima
  • 2,807
  • 6
  • 22
  • 36
Wow
  • 119
  • 8
  • 2
    There is no `withJson()` method in `\Psr\Http\Message\ResponseInterface` or parent interface -- https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php . So it must be implemented additionally in concrete ResponseInterface implementation class (e.g. in Slim framework classes). IDE is absolutely correct here. – LazyOne Aug 03 '17 at 13:32
  • It isn't just a PhpStorm issue, is it? I mean, the code itself doesn't run either, does it? – Álvaro González Aug 03 '17 at 13:45
  • That was the strange thing. I could not find the `withJson()` either, but is does work fine. The solution was given by @Nima in the answer. – Wow Aug 03 '17 at 15:33

1 Answers1

15

Method withJson is not defined in \Psr\Http\Message\ResponseInterface but in Slim\Http\Response (which implements the former), that means this method is something related to Slim framework. You can try this:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Slim\Http\Response as Response;
Nima
  • 2,807
  • 6
  • 22
  • 36