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 |
||
47 | trait SharedManagerTrait |
||
48 | { |
||
49 | use ShareableTrait; |
||
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | View Code Duplication | public static function onEvent( |
|
67 | |||
68 | /** |
||
69 | * {@inheritDoc} |
||
70 | */ |
||
71 | View Code Duplication | public static function offEvent( |
|
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public static function onGlobalEvent( |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | public static function offGlobalEvent( |
||
106 | |||
107 | /** |
||
108 | * Override `getOwnScopes()` in ShareableTrait |
||
109 | * |
||
110 | * For $this's own scopes, looking for any parent class or interface in |
||
111 | * all the available scopes. |
||
112 | * |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | protected function getOwnScopes()/*# : array */ |
||
134 | |||
135 | /** |
||
136 | * Get all types (class or interface) from the given scopes |
||
137 | * |
||
138 | * @param array $scopes |
||
139 | * @return array |
||
140 | * @access protected |
||
141 | */ |
||
142 | protected function getAllTypes(array $scopes)/*# : array */ |
||
152 | |||
153 | /** |
||
154 | * Is $type a classname or interface name ? |
||
155 | * |
||
156 | * @param string $type |
||
157 | * @return bool |
||
158 | * @access protected |
||
159 | */ |
||
160 | protected function isAType(/*# string */ $type)/*# : bool */ |
||
164 | |||
165 | /** |
||
166 | * is $childType child type of one of the $typesToCheck. |
||
167 | * |
||
168 | * Returns the matched types |
||
169 | * |
||
170 | * @param string $childType |
||
171 | * @param array $typesToCheck |
||
172 | * @param array &$result |
||
173 | * @access protected |
||
174 | */ |
||
175 | protected function matchParentType( |
||
188 | } |
||
189 |
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.