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 | * Filter a string. |
||
12 | * |
||
13 | * Verify that the passed in value is a string. By default, nulls are not allowed, and the length is restricted |
||
14 | * between 1 and PHP_INT_MAX. These parameters can be overwritten for custom behavior. |
||
15 | * |
||
16 | * The return value is the string, as expected by the \TraderInteractive\Filterer class. |
||
17 | * |
||
18 | * @param mixed $value The value to filter. |
||
19 | * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed. |
||
20 | * @param int $minLength Minimum length to allow for $value. |
||
21 | * @param int $maxLength Maximum length to allow for $value. |
||
22 | * @return string|null The passed in $value. |
||
23 | * |
||
24 | * @throws Exception if the value did not pass validation. |
||
25 | * @throws \InvalidArgumentException if one of the parameters was not correctly typed. |
||
26 | */ |
||
27 | public static function filter($value, bool $allowNull = false, int $minLength = 1, int $maxLength = PHP_INT_MAX) |
||
69 | |||
70 | /** |
||
71 | * Explodes a string into an array using the given delimiter. |
||
72 | * |
||
73 | * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz']. |
||
74 | * |
||
75 | * @param string $value The string to explode. |
||
76 | * @param string $delimiter The non-empty delimiter to explode on. |
||
77 | * @return array The exploded values. |
||
78 | * |
||
79 | * @throws \InvalidArgumentException if the delimiter does not pass validation. |
||
80 | */ |
||
81 | public static function explode(string $value, string $delimiter = ',') |
||
91 | } |
||
92 |
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.