JsonEncodingApiUsage
info on by default quick-fixGroup: Type compatibility · PhpStorm name: JsonEncodingApiUsageInspection
json_decode() without its second argument silently returns stdClass objects, which readers often confuse with arrays; and both json_decode() and json_encode() report failures only through json_last_error() unless the JSON_THROW_ON_ERROR flag (PHP 7.3+) is passed. This rule asks for an explicit decoding target and for exception-based error handling.
Example
Configuration (a): T only, decode as array.
php
<?php
namespace Feed;
function load(string $body, string $alt) {
$a = json_decode($body);
$b = \json_decode(trim($alt));
$c = json_decode($body, false);
$d = json_decode($body, associative: true);
$e = json_decode();
return [$a, $b, $c, $d, $e];
}php
<?php
namespace Feed;
function load(string $body, string $alt) {
$a = json_decode($body, true);
$b = \json_decode(trim($alt), true);
$c = json_decode($body, false);
$d = json_decode($body, associative: true);
$e = json_decode();
return [$a, $b, $c, $d, $e];
}Reported:
- line 5: Pass the second argument to state whether JSON decodes to arrays or objects.
- line 6: Pass the second argument to state whether JSON decodes to arrays or objects.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| HARDEN_DECODING_RESULT_TYPE | bool | true | Reports json_decode() calls that leave the decoded type (objects or arrays) implicit. Also decides the associative argument the error-handling quick-fix writes. |
| DECODE_AS_ARRAY | bool | false | Fixes insert true (decode to arrays) instead of false. |
| DECODE_AS_OBJECT | bool | true | Radio-button companion of DECODE_AS_ARRAY; has no effect on detection or fixes by itself. |
| HARDEN_ERRORS_HANDLING | bool | true | Reports JSON calls whose flags do not make errors throw (PHP 7.3 and later). |
Configure
In custos.json:
json
{
"rules": {
"JsonEncodingApiUsage": {
"enabled": false,
"options": {
"HARDEN_DECODING_RESULT_TYPE": true,
"DECODE_AS_ARRAY": false,
"DECODE_AS_OBJECT": true,
"HARDEN_ERRORS_HANDLING": true
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore JsonEncodingApiUsage
/**
* @noinspection JsonEncodingApiUsageInspection
*/