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 Url |
||
9 | { |
||
10 | /** |
||
11 | * Filter an url |
||
12 | * |
||
13 | * Filters value as URL (according to » http://www.faqs.org/rfcs/rfc2396) |
||
14 | * |
||
15 | * The return value is the url, as expected by the \TraderInteractive\Filterer class. |
||
16 | * By default, nulls are not allowed. |
||
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 | * |
||
21 | * @return string|null The passed in $value. |
||
22 | * |
||
23 | * @throws Exception if the value did not pass validation. |
||
24 | * @throws \InvalidArgumentException if one of the parameters was not correctly typed. |
||
25 | */ |
||
26 | public static function filter($value, bool $allowNull = false) |
||
43 | } |
||
44 |
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.