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() { |
|
| 31 | 1 | $oModuleMap = $this->getCurrentModuleMap(); |
|
| 32 | 1 | if (ModuleMap::isValid($oModuleMap)) { |
|
| 33 | return (string)$oModuleMap->getRegex(); |
||
| 34 | } |
||
| 35 | 1 | return null; |
|
| 36 | } |
||
| 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) { |
| 45 | 2 | $oModuleMap = $this->getCurrentModuleMap(); |
|
| 46 | |||
| 47 | 2 | if (MappingA::isValid($oModuleMap)) { |
|
| 48 | 1 | $sRegex = $oModuleMap->getRegex() . $sRegex; |
|
| 49 | } |
||
| 50 | |||
| 51 | 2 | if (!array_key_exists($sRegex, $this->aMaps)) { |
|
| 52 | 2 | $oNewMap = new ClassMap($sPath, $sRegex); |
|
| 53 | |||
| 54 | 2 | if (MappingA::isValid($oModuleMap)) { |
|
| 55 | 1 | $oNewMap->merge($oModuleMap); |
|
| 56 | 1 | $oNewMap->setModuleMap($oModuleMap); |
|
| 57 | } |
||
| 58 | |||
| 59 | 2 | $this->aMaps[$sRegex] = $oNewMap; |
|
| 60 | } else { |
||
| 61 | $oNewMap = $this->aMaps[$sRegex]; |
||
| 62 | } |
||
| 63 | |||
| 64 | 2 | return $oNewMap; |
|
| 65 | } |
||
| 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) { |
|
| 101 | 19 | $oModuleMap = $this->getCurrentModuleMap(); |
|
| 102 | |||
| 103 | // setting the parent module map to the existing one |
||
| 104 | 19 | if (MappingA::isValid($oModuleMap)) { |
|
| 105 | $sRegex = $oModuleMap->getRegex() . $sRegex; |
||
| 106 | |||
| 107 | $oNewModuleMap = new ModuleMap($sPath, $sRegex); |
||
| 108 | |||
| 109 | $oNewModuleMap->setModuleMap($oModuleMap); |
||
| 110 | $oNewModuleMap->merge($oModuleMap); |
||
| 111 | } else { |
||
| 112 | 19 | $oNewModuleMap = new RootMap($sPath, $sRegex); |
|
| 113 | } |
||
| 114 | |||
| 115 | // switching the current module map to the new one |
||
| 116 | 19 | $this->oCurrentModuleMap = $oNewModuleMap; |
|
| 117 | |||
| 118 | 19 | include ($sPath); |
|
| 119 | |||
| 120 | 19 | if (ModuleMap::isValid($oNewModuleMap->getModuleMap())) { |
|
| 121 | // after we finished parsing the new module, we set the previous module map as current |
||
| 122 | 19 | $this->oCurrentModuleMap = $oNewModuleMap->getModuleMap(); |
|
| 123 | } |
||
| 124 | |||
| 125 | 19 | return $oNewModuleMap; |
|
| 126 | } |
||
| 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) { |
|
| 201 | 19 | if ($sRegex === null) { |
|
| 202 | throw new ExceptionSitemap('A regex URI must be present.'); |
||
| 203 | } |
||
| 204 | 19 | if (empty($sPath)) { |
|
| 205 | throw new ExceptionSitemap('A path must be present.'); |
||
| 206 | } |
||
| 207 | |||
| 208 | 19 | if (ClassMap::isValidMap($sPath)) { |
|
| 209 | // instead of a path we have a namespace |
||
| 210 | 19 | return $this->addClassMap($sRegex, $sPath); |
|
| 211 | } else { |
||
| 212 | 18 | if (!is_file($sPath)) { |
|
| 213 | 1 | $sPath = $this->getCurrentModuleMap()->getModulePath() . $sPath; |
|
| 214 | } |
||
| 215 | |||
| 216 | 18 | if (!is_file($sPath)) { |
|
| 217 | throw new ExceptionSitemap('The path associated with [' . $sRegex . '] can\'t be empty or an invalid file.'); |
||
| 218 | } |
||
| 219 | |||
| 220 | 18 | $sPath = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $sPath); |
|
| 221 | 18 | if (ModuleMap::isValidMap($sPath)) { |
|
| 222 | // Valid site map |
||
| 223 | 18 | return $this->addModuleMap($sRegex, $sPath); |
|
| 224 | } |
||
| 225 | 1 | if (self::isValidStaticPath($sPath)) { |
|
| 226 | // Valid static file |
||
| 227 | 1 | return $this->addStaticMap($sRegex, $sPath); |
|
| 228 | } |
||
| 229 | throw new ExceptionSitemap('[' . $sPath . '] could not be loaded.'); |
||
| 230 | } |
||
| 231 | } |
||
| 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() { |
||
| 268 | $aC = false; |
||
| 269 | foreach ($this->getAllControllers() as $sKey => $oController) { |
||
| 270 | $aC[$sKey] = $oController->getPath(); |
||
| 271 | } |
||
| 272 | return $aC; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function getModuleMappings() { |
||
| 276 | $aC = false; |
||
| 277 | foreach ($this->getAllModules() as $sKey => $oModule) { |
||
| 278 | $aC[$sKey] = $oModule->getPath(); |
||
| 279 | } |
||
| 280 | return $aC; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @returns ClassMap[] |
||
| 285 | */ |
||
| 286 | public function getProcessorMappings() { |
||
| 287 | $aC = false; |
||
| 288 | foreach ($this->getMaps() as $sKey => $oProcessor) { |
||
| 289 | $aC[$sKey] = $oProcessor->getPath(); |
||
| 290 | } |
||
| 291 | return $aC; |
||
| 292 | } |
||
| 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
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.