Complex classes like Map 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.
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 Map, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Map |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * List of Paths |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $paths = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * List of registered namespaces |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $namespaces = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Adds a Path to this map |
||
| 65 | * |
||
| 66 | * @param Path|array $path Path (or list of Paths) to be added |
||
| 67 | * |
||
| 68 | * @throws \InvalidArgumentException if $path is not Path and not an array |
||
| 69 | * @return Map |
||
| 70 | */ |
||
| 71 | 14 | public function add($path) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Removes a Path from this map |
||
| 89 | * |
||
| 90 | * @param Path $path Path object to be removed |
||
| 91 | * |
||
| 92 | * @return Map |
||
| 93 | */ |
||
| 94 | 1 | public function remove(Path $path) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Registers a namespace prefix |
||
| 110 | * |
||
| 111 | * @param string $nsPrefix NS prefix |
||
| 112 | * @param string $namespaceUrl URL to namespace definition |
||
| 113 | * |
||
| 114 | * @return Map |
||
| 115 | */ |
||
| 116 | 1 | public function registerNamespace($nsPrefix, $namespaceUrl) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Executes the Map against the XmlFile and return parsed values as a PHP |
||
| 125 | * array. |
||
| 126 | * |
||
| 127 | * @param XmlFile $file The XML file |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | 12 | public function execute(XmlFile $file) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Transform a Path to a value. |
||
| 154 | * |
||
| 155 | * @param array $sxml Current Xpath result |
||
| 156 | * @param Path $path Current Path |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | 11 | protected function pathToValue(array $sxml, Path $path) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Returns a value, filtered if needed. |
||
| 217 | * |
||
| 218 | * @param Path $path Current Path |
||
| 219 | * @param string $value Actual value |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | 9 | protected function getFilteredValue(Path $path, $value) |
|
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns Path attributes list |
||
| 235 | * |
||
| 236 | * @param Path $path Current Path |
||
| 237 | * @param SimpleXMLElement $node Current SimpleXML node |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 11 | protected function getAttributesArray(Path $path, |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Returns childrens values |
||
| 255 | * |
||
| 256 | * @param Path $path Current Path |
||
| 257 | * @param SimpleXMLElement $node Current SimpleXML node |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 5 | protected function getChildrens(Path $path, SimpleXMLElement $node) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Apply rules for registered namespaces |
||
| 285 | * |
||
| 286 | * @param XmlFile $file The current XmlFile object |
||
|
|
|||
| 287 | * @param array $sxml SimpleXML results |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 12 | protected function registerNamespaces(XmlFile $file = null, |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Returns Map's Paths |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 1 | public function getPaths() |
|
| 316 | } |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.