Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Schema |
||
| 15 | { |
||
| 16 | |||
| 17 | protected $elementsQualification = false; |
||
| 18 | |||
| 19 | protected $attributesQualification = false; |
||
| 20 | |||
| 21 | protected $targetNamespace; |
||
| 22 | |||
| 23 | protected $schemas = array(); |
||
| 24 | |||
| 25 | protected $types = array(); |
||
| 26 | |||
| 27 | protected $elements = array(); |
||
| 28 | |||
| 29 | protected $groups = array(); |
||
| 30 | |||
| 31 | protected $attributeGroups = array(); |
||
| 32 | |||
| 33 | protected $attributes = array(); |
||
| 34 | |||
| 35 | protected $doc; |
||
| 36 | |||
| 37 | private $typeCache = array(); |
||
| 38 | |||
| 39 | |||
| 40 | public function getElementsQualification() |
||
| 44 | |||
| 45 | 44 | public function setElementsQualification($elementsQualification) |
|
| 49 | |||
| 50 | public function getAttributesQualification() |
||
| 54 | |||
| 55 | 44 | public function setAttributesQualification($attributesQualification) |
|
| 59 | |||
| 60 | 44 | public function getTargetNamespace() |
|
| 64 | |||
| 65 | 44 | public function setTargetNamespace($targetNamespace) |
|
| 69 | |||
| 70 | 4 | public function getTypes() |
|
| 74 | |||
| 75 | 1 | public function getElements() |
|
| 79 | |||
| 80 | 44 | public function getSchemas() |
|
| 84 | |||
| 85 | 1 | public function getAttributes() |
|
| 89 | |||
| 90 | 1 | public function getGroups() |
|
| 94 | |||
| 95 | public function getDoc() |
||
| 99 | |||
| 100 | 44 | public function setDoc($doc) |
|
| 104 | |||
| 105 | 44 | public function addType(Type $type) |
|
| 109 | |||
| 110 | 44 | public function addElement(ElementDef $element) |
|
| 114 | |||
| 115 | 44 | public function addSchema(Schema $schema, $namespace = null) |
|
| 127 | |||
| 128 | 44 | public function addAttribute(AttributeDef $attribute) |
|
| 132 | |||
| 133 | 44 | public function addGroup(Group $group) |
|
| 137 | |||
| 138 | 44 | public function addAttributeGroup(AttributeGroup $group) |
|
| 142 | |||
| 143 | 1 | public function getAttributeGroups() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | * @param string $name |
||
| 151 | * @return Group|false |
||
| 152 | */ |
||
| 153 | 44 | public function getGroup($name) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * |
||
| 163 | * @param string $name |
||
| 164 | * @return ElementItem|false |
||
| 165 | */ |
||
| 166 | 44 | public function getElement($name) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * |
||
| 176 | * @param string $name |
||
| 177 | * @return Type|false |
||
| 178 | */ |
||
| 179 | 44 | public function getType($name) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * @param string $name |
||
| 190 | * @return AttributeItem|false |
||
| 191 | */ |
||
| 192 | 44 | public function getAttribute($name) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * |
||
| 202 | * @param string $name |
||
| 203 | * @return AttributeGroup|false |
||
| 204 | */ |
||
| 205 | 44 | public function getAttributeGroup($name) |
|
| 212 | |||
| 213 | public function __toString() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * |
||
| 220 | * @param string $getter |
||
| 221 | * @param string $name |
||
| 222 | * @param string $namespace |
||
| 223 | * @throws TypeNotFoundException |
||
| 224 | * @return \GoetasWebservices\XML\XSDReader\Schema\SchemaItem |
||
| 225 | */ |
||
| 226 | 44 | protected function findSomething($getter, $name, $namespace = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $namespace |
||
| 250 | * @param array $calling |
||
| 251 | * @return \GoetasWebservices\XML\XSDReader\Schema\Schema|null |
||
| 252 | */ |
||
| 253 | 44 | protected function findSchemaByNamespace($namespace, &$calling = array()) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * |
||
| 273 | * @param string $name |
||
| 274 | * @param string $namespace |
||
| 275 | * @return Type |
||
| 276 | */ |
||
| 277 | 44 | public function findType($name, $namespace = null) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * |
||
| 284 | * @param string $name |
||
| 285 | * @param string $namespace |
||
| 286 | * @return Group |
||
| 287 | */ |
||
| 288 | 44 | public function findGroup($name, $namespace = null) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * @param string $namespace |
||
| 297 | * @return ElementDef |
||
| 298 | */ |
||
| 299 | 44 | public function findElement($name, $namespace = null) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * |
||
| 306 | * @param string $name |
||
| 307 | * @param string $namespace |
||
| 308 | * @return AttributeReal |
||
| 309 | */ |
||
| 310 | 44 | public function findAttribute($name, $namespace = null) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * |
||
| 317 | * @param string $name |
||
| 318 | * @param string $namespace |
||
| 319 | * @return AttributeGroup |
||
| 320 | */ |
||
| 321 | 44 | public function findAttributeGroup($name, $namespace = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | 44 | public function hasChildren() |
|
| 333 | } |
||
| 334 |