Why this matters
Silencing exceptions hides failures that may leave the system in an inconsistent state or make debugging difficult. Proper exception handling (even just logging) ensures you’re aware of issues and handle them appropriately.
Avoid empty catch blocks. When you catch an exception, implement meaningful handling: log the error, perform a fallback action, or rethrow after processing. Do not catch exceptions generically just to suppress them.
Silencing exceptions hides failures that may leave the system in an inconsistent state or make debugging difficult. Proper exception handling (even just logging) ensures you’re aware of issues and handle them appropriately.
Side-by-side examples engineers can pattern-match during review.
<?php
try {
\$data = file_get_contents('config.json');
} catch (Exception \$e) {
// do nothing
}
?><?php
try {
\$data = file_get_contents('config.json');
} catch (Exception \$e) {
error_log('Failed to load config: ' . \$e->getMessage());
throw \$e; // or throw a custom exception
}
?><?php
try {
\$data = file_get_contents('config.json');
} catch (Exception \$e) {
// do nothing
}
?><?php
try {
\$data = file_get_contents('config.json');
} catch (Exception \$e) {
error_log('Failed to load config: ' . \$e->getMessage());
throw \$e; // or throw a custom exception
}
?>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.