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:
Complex classes like SiteMapA often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SiteMapA, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class SiteMapA extends BaseObject { |
||
14 | /** |
||
15 | * the base regex for the current map |
||
16 | * @todo this needs to be deprecated in favour of regexes of the parent module |
||
17 | * @var string |
||
18 | */ |
||
19 | private $aMaps = array(); |
||
20 | /** |
||
21 | * @var ModuleMap |
||
22 | */ |
||
23 | private $oCurrentModuleMap; |
||
24 | |||
25 | public function __construct() {} |
||
26 | |||
27 | /** |
||
28 | * @return string |
||
|
|||
29 | */ |
||
30 | 1 | public function getBaseRegex() { |
|
37 | |||
38 | /** |
||
39 | * |
||
40 | * @param string $sRegex |
||
41 | * @param string $sPath |
||
42 | * @returns MappingA |
||
43 | */ |
||
44 | 2 | View Code Duplication | public function addMap($sRegex, $sPath) { |
66 | |||
67 | /** |
||
68 | * @param string $sRegex |
||
69 | * @param string $sPath |
||
70 | * @throws ExceptionSitemap |
||
71 | * @returns MappingA |
||
72 | */ |
||
73 | 20 | View Code Duplication | protected function addClassMap($sRegex, $sPath) { |
93 | |||
94 | /** |
||
95 | * |
||
96 | * @param string $sRegex |
||
97 | * @param string $sPath |
||
98 | * @returns MappingA |
||
99 | */ |
||
100 | 19 | public function addModuleMap($sRegex, $sPath) { |
|
127 | |||
128 | /** |
||
129 | * |
||
130 | * @param string $sRegex |
||
131 | * @param string $sPath |
||
132 | * @returns MappingA |
||
133 | */ |
||
134 | 2 | public function addStaticMap($sRegex, $sPath) { |
|
139 | |||
140 | /** |
||
141 | * @returns ClassMap[] |
||
142 | */ |
||
143 | 20 | public function getMaps() { |
|
146 | |||
147 | /** |
||
148 | * This tells us if $sPath belongs to a file that can be used as a static resource |
||
149 | * eg. Javascript, CSS, etc. |
||
150 | * @param string $sPath |
||
151 | * @return bool |
||
152 | */ |
||
153 | 3 | public static function isValidStaticPath($sPath) { |
|
156 | |||
157 | /** |
||
158 | * Gets the class name of based on the included path |
||
159 | * In order for it to work the file needs to be already include()-d |
||
160 | * @param string $sPath |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public static function getClassName($sPath) { |
|
171 | |||
172 | /** |
||
173 | * @returns ModuleMap |
||
174 | */ |
||
175 | 20 | public function getCurrentModuleMap() { |
|
178 | |||
179 | /** |
||
180 | * @return MappingA|null |
||
181 | */ |
||
182 | 2 | public function getParentModuleMap() { |
|
192 | |||
193 | /** |
||
194 | * |
||
195 | * @param string $sRegex |
||
196 | * @param string $sPath |
||
197 | * @throws ExceptionSitemap |
||
198 | * @returns MappingA |
||
199 | */ |
||
200 | 19 | public function map($sRegex, $sPath) { |
|
232 | |||
233 | /** |
||
234 | * @returns ModuleMap[] |
||
235 | */ |
||
236 | 2 | protected function getAllModules() { |
|
251 | |||
252 | /** |
||
253 | * @returns ClassMap[] |
||
254 | */ |
||
255 | 2 | protected function getAllControllers() { |
|
266 | |||
267 | public function getControllerMappings() { |
||
274 | |||
275 | public function getModuleMappings() { |
||
282 | |||
283 | /** |
||
284 | * @returns ClassMap[] |
||
285 | */ |
||
286 | public function getProcessorMappings() { |
||
293 | |||
294 | /** |
||
295 | * @param ProcessorA $oProcessor |
||
296 | * @return MappingA |
||
297 | */ |
||
298 | 2 | public function findProcessorMap(ProcessorA $oProcessor) { |
|
307 | } |
||
308 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.