Why this matters
Filesystem calls can fail due to permissions, missing files, or I/O errors. Explicit handling prevents silent data loss and cryptic warnings.
Wrap file reads/writes (e.g., file_get_contents, file_put_contents, fopen) with checks and exceptions; never assume the filesystem is available.
Filesystem calls can fail due to permissions, missing files, or I/O errors. Explicit handling prevents silent data loss and cryptic warnings.
Side-by-side examples engineers can pattern-match during review.
$json = file_get_contents($path);
$data = json_decode($json, true); // no checksif (!is_readable($path)) {
throw new RuntimeException("File not readable: {$path}");
}
$content = file_get_contents($path);
if ($content === false) {
throw new RuntimeException("Failed to read file: {$path}");
}$data = json_decode(file_get_contents($p), true);$c = file_get_contents($p); if($c===false) throw new RuntimeException('read fail');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.