1.2.278. Could Drop Variable

Suggest removing the variable in catch clause where the variable is not used. The type of the exception is sufficient to make the catch clause work. Although, it is recommended to use the caught exception, for chaining or logging, for example.

<?php

try {
     doSomething();
} catch(Exception1 $e) {
     // No usage of $e : just drop it from the clause
} catch(Exception2 $e2) {
     // $e2 is caught and used.
     echo $e2->getMessage();
}

?>

1.2.278.1. Suggestions

  • Remove the unused variable

1.2.278.2. Specs

Short name

Exceptions/CouldDropVariable

Rulesets

All, Changed Behavior, Dead code, Suggestions

Exakat since

2.6.4

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

catch

Available in

Entreprise Edition, Exakat Cloud