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 |
||
7 | class UrlException extends Exception |
||
8 | { |
||
9 | /** |
||
10 | * The exception to be thrown when the process of creating a url has failed. |
||
11 | * |
||
12 | * @return static |
||
13 | */ |
||
14 | public static function createFailed(): self |
||
18 | |||
19 | /** |
||
20 | * The exception to be thrown when the process of updating a url has failed. |
||
21 | * |
||
22 | * @return static |
||
23 | */ |
||
24 | public static function updateFailed(): self |
||
28 | |||
29 | /** |
||
30 | * The exception to be thrown when the process of deleting a url has failed. |
||
31 | * |
||
32 | * @return static |
||
33 | */ |
||
34 | public static function deleteFailed(): self |
||
38 | |||
39 | /** |
||
40 | * The exception to be thrown when the "route url to" has not been supplied to the urlable functionality. |
||
41 | * |
||
42 | * @param string $class |
||
43 | * @return static |
||
44 | */ |
||
45 | View Code Duplication | public static function mandatoryRouting(string $class): self |
|
53 | |||
54 | /** |
||
55 | * The exception to be thrown when the "from field" has not been supplied to the urlable functionality. |
||
56 | * |
||
57 | * @param string $class |
||
58 | * @return static |
||
59 | */ |
||
60 | View Code Duplication | public static function mandatoryFromField(string $class): self |
|
68 | |||
69 | /** |
||
70 | * The exception to be thrown when the "to field" has not been supplied to the urlable functionality. |
||
71 | * |
||
72 | * @param string $class |
||
73 | * @return static |
||
74 | */ |
||
75 | View Code Duplication | public static function mandatoryToField(string $class): self |
|
83 | } |
||
84 |
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.