Andy
Andy

Reputation: 5395

CakePHP 4 JSON response gives phpstan error

According to the CakePHP 4 docs

// If you want a json response
$response = $response->withType('application/json')
->withStringBody(json_encode(['Foo' => 'bar']));

If you run phpstan on level 7 it will give this error

Parameter #1 $string of method Cake\Http\Response::withStringBody() expects string|null, string|false given.

json_encode is a core PHP function which has return types string|false so can't be modified.

withStringBody() is a CakePHP core method which accepts string|null.

How can you get around that error since neither of those 2 things can be modified directly?

Upvotes: 1

Views: 40

Answers (0)

Related Questions