Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
11 | * Filters $value to a float strictly. |
||
12 | * |
||
13 | * The return value is the float, as expected by the \TraderInteractive\Filterer class. |
||
14 | * |
||
15 | * @param string|float $value the value to filter to a float |
||
16 | * @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is |
||
17 | * NULL |
||
18 | * @param float $minValue The minimum acceptable value |
||
19 | * @param float $maxValue The maximum acceptable value |
||
20 | * @param bool $castInts Flag to cast $value to float if it is an integer |
||
21 | * |
||
22 | * @return float|null The filtered value |
||
23 | * |
||
24 | * @throws Exception if $value is greater than $maxValue |
||
25 | * @see is_numeric |
||
26 | */ |
||
27 | public static function filter( |
||
76 | } |
||
77 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.