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 |
||
21 | final class ClassUtils |
||
22 | { |
||
23 | private function __construct() { /* noop */ } |
||
24 | |||
25 | /** |
||
26 | * Check if class name is valid. |
||
27 | * |
||
28 | * @param string $fqcn |
||
29 | * |
||
30 | * @return bool |
||
31 | */ |
||
32 | 1 | public static function isClassNameValid($fqcn) |
|
43 | |||
44 | /** |
||
45 | * Check if class can have its builder pattern class implemented. |
||
46 | * |
||
47 | * @param ClassMetadata $class |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public static function isBuildable(ClassMetadata $class) |
||
63 | |||
64 | /** |
||
65 | * Check if class is implemented builder class. |
||
66 | * |
||
67 | * @param ClassMetadata $class |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public static function isBuilder(ClassMetadata $class) |
||
91 | |||
92 | /** |
||
93 | * Get namespace of class. |
||
94 | * |
||
95 | * @param $class |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | View Code Duplication | public static function getNamespace($class) |
|
110 | |||
111 | /** |
||
112 | * Get short name of class. |
||
113 | * |
||
114 | * @param $class |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | View Code Duplication | public static function getShortName($class) |
|
128 | } |
||
129 |
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.