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
$stringof methodCake\Http\Response::withStringBody()expectsstring|null,string|falsegiven.
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?
(string)json_encode()false, don't pass it towithStringBody. If it does returnfalseit indicates a problem with the encoding so I'd have thought you'd want to detect that and log an error and so on anyway. You can also set json-throw-on-error to make it automatically throw an exception when it fails, rather than having to detect it.