Why this matters
Malformed payloads and encoding issues are common. Exceptions make failures explicit and prevent using partial/invalid data.
Use JSON_THROW_ON_ERROR (PHP ≥7.3) and wrap json_encode/json_decode in try/catch; validate the decoded shape.
Malformed payloads and encoding issues are common. Exceptions make failures explicit and prevent using partial/invalid data.
Side-by-side examples engineers can pattern-match during review.
$data = json_decode($json, true); // ignores errorstry {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new RuntimeException('Invalid JSON payload', 0, $e);
}json_decode($json, true)json_decode($json, true, 512, JSON_THROW_ON_ERROR)From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.