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 |
||
8 | final class Floats |
||
9 | { |
||
10 | /** |
||
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 NULL |
||
17 | * @param int $minValue The minimum acceptable value |
||
18 | * @param int $maxValue The maximum acceptable value |
||
19 | * @param bool $castInts Flag to cast $value to float if it is an integer |
||
20 | * |
||
21 | * @return float|null The filtered value |
||
22 | * |
||
23 | * @see is_numeric |
||
24 | * @throws \InvalidArgumentException if $allowNull is not a boolean |
||
25 | * @throws \InvalidArgumentException if $minValue is not null and not a float |
||
26 | * @throws \InvalidArgumentException if $maxValue is not null and not a float |
||
27 | * @throws \InvalidArgumentException if $castInts is not a boolean |
||
28 | * @throws Exception if $value does not pass is_numeric |
||
29 | * @throws Exception if $value is hex format |
||
30 | * @throws Exception if $value is not a string or float |
||
31 | * @throws Exception if $value overflow or underflows |
||
32 | * @throws Exception if $value is less than $minValue |
||
33 | * @throws Exception if $value is greater than $maxValue |
||
34 | */ |
||
35 | public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false) |
||
95 | } |
||
96 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.