Complex classes like Struct 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 Struct, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Struct extends AbstractModel |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | const DOC_SUB_PACKAGE_STRUCTS = 'Structs'; |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | const DOC_SUB_PACKAGE_ENUMERATIONS = 'Enumerations'; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | const DOC_SUB_PACKAGE_ARRAYS = 'Arrays'; |
||
29 | /** |
||
30 | * Attributes of the struct |
||
31 | * @var StructAttributeContainer |
||
32 | */ |
||
33 | protected $attributes; |
||
34 | /** |
||
35 | * Is the struct a restriction with defined values ? |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $isRestriction = false; |
||
39 | /** |
||
40 | * If the struct is a restriction with values, then store values |
||
41 | * @var StructValueContainer |
||
42 | */ |
||
43 | protected $values; |
||
44 | /** |
||
45 | * If the struct is a union with types, then store types |
||
46 | * @var string[] |
||
47 | */ |
||
48 | protected $types; |
||
49 | /** |
||
50 | * Define if the current struct is a concrete struct or just a virtual struct to store meta informations |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $isStruct = false; |
||
54 | /** |
||
55 | * Main constructor |
||
56 | * @see AbstractModel::__construct() |
||
57 | * @uses Struct::setStruct() |
||
58 | * @param Generator $generator |
||
59 | * @param string $name the original name |
||
60 | * @param bool $isStruct defines if it's a real struct or not |
||
61 | * @param bool $isRestriction defines if it's an enumeration or not |
||
62 | */ |
||
63 | 1220 | public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false) |
|
73 | /** |
||
74 | * Returns the contextual part of the class name for the package |
||
75 | * @see AbstractModel::getContextualPart() |
||
76 | * @uses Struct::isRestriction() |
||
77 | * @return string |
||
78 | */ |
||
79 | 365 | public function getContextualPart() |
|
89 | /** |
||
90 | * Returns the sub package name which the model belongs to |
||
91 | * Must be overridden by sub classes |
||
92 | * @see AbstractModel::getDocSubPackages() |
||
93 | * @uses Struct::isRestriction() |
||
94 | * @return array |
||
95 | */ |
||
96 | 205 | public function getDocSubPackages() |
|
108 | /** |
||
109 | * Returns true if the current struct is a collection of values (like an array) |
||
110 | * @uses AbstractModel::getName() |
||
111 | * @uses Struct::countOwnAttributes() |
||
112 | * @return bool |
||
113 | */ |
||
114 | 405 | public function isArray() |
|
118 | /** |
||
119 | * Returns the attributes of the struct and potentially from the parent class |
||
120 | * @uses AbstractModel::getInheritance() |
||
121 | * @uses Struct::isStruct() |
||
122 | * @uses Struct::getAttributes() |
||
123 | * @param bool $includeInheritanceAttributes include the attributes of parent class, default parent attributes are not included. If true, then the array is an associative array containing and index "attribute" for the StructAttribute object and an index "model" for the Struct object. |
||
124 | * @param bool $requiredFirst places the required attributes first, then the not required in order to have the _construct method with the required attribute at first |
||
125 | * @return StructAttributeContainer |
||
126 | */ |
||
127 | 420 | public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false) |
|
136 | /** |
||
137 | * @param bool $includeInheritanceAttributes |
||
138 | * @param bool $requiredFirst |
||
139 | * @return StructAttributeContainer |
||
140 | */ |
||
141 | 160 | protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst) |
|
157 | /** |
||
158 | * @param StructAttributeContainer $attributes |
||
159 | */ |
||
160 | 5 | protected function addInheritanceAttributes(StructAttributeContainer $attributes) |
|
171 | /** |
||
172 | * @param StructAttributeContainer $allAttributes |
||
173 | * @return StructAttributeContainer |
||
174 | */ |
||
175 | 160 | protected function putRequiredFirst(StructAttributeContainer $allAttributes) |
|
196 | /** |
||
197 | * Returns the number of own attributes |
||
198 | * @uses Struct::getAttributes() |
||
199 | * @return int |
||
200 | */ |
||
201 | 405 | public function countOwnAttributes() |
|
205 | /** |
||
206 | * Sets the attributes of the struct |
||
207 | * @param StructAttributeContainer $structAttributeContainer |
||
208 | * @return Struct |
||
209 | */ |
||
210 | 1220 | public function setAttributes(StructAttributeContainer $structAttributeContainer) |
|
215 | /** |
||
216 | * Adds attribute based on its original name |
||
217 | * @throws \InvalidArgumentException |
||
218 | * @param string $attributeName the attribute name |
||
219 | * @param string $attributeType the attribute type |
||
220 | * @return Struct |
||
221 | */ |
||
222 | 325 | public function addAttribute($attributeName, $attributeType) |
|
233 | /** |
||
234 | * Returns the attribute by its name, otherwise null |
||
235 | * @uses Struct::getAttributes() |
||
236 | * @param string $attributeName the original attribute name |
||
237 | * @return StructAttribute|null |
||
238 | */ |
||
239 | 250 | public function getAttribute($attributeName) |
|
243 | /** |
||
244 | * Returns the attribute by its cleaned name, otherwise null |
||
245 | * @uses Struct::getAttributes() |
||
246 | * @param string $attributeCleanName the cleaned attribute name |
||
247 | * @return StructAttribute|null |
||
248 | */ |
||
249 | 75 | public function getAttributeByCleanName($attributeCleanName) |
|
253 | /** |
||
254 | * Returns the isRestriction value |
||
255 | * @return bool |
||
256 | */ |
||
257 | 405 | public function isRestriction() |
|
261 | /** |
||
262 | * Sets the isRestriction value |
||
263 | * @param bool $isRestriction |
||
264 | * @return Struct |
||
265 | */ |
||
266 | 1220 | public function setRestriction($isRestriction = true) |
|
271 | /** |
||
272 | * Returns the isStruct value |
||
273 | * @return bool |
||
274 | */ |
||
275 | 440 | public function isStruct() |
|
279 | /** |
||
280 | * Sets the isStruct value |
||
281 | * @param bool $isStruct |
||
282 | * @return Struct |
||
283 | */ |
||
284 | 1220 | public function setStruct($isStruct = true) |
|
289 | /** |
||
290 | * Returns the values for an enumeration |
||
291 | * @return StructValueContainer |
||
292 | */ |
||
293 | 115 | public function getValues() |
|
297 | /** |
||
298 | * Sets the values for an enumeration |
||
299 | * @param StructValueContainer $structValueContainer |
||
300 | * @return Struct |
||
301 | */ |
||
302 | 1220 | protected function setValues(StructValueContainer $structValueContainer) |
|
307 | /** |
||
308 | * Adds value to values array |
||
309 | * @uses Struct::getValue() |
||
310 | * @uses Struct::getValues() |
||
311 | * @param mixed $value the original value |
||
312 | * @return Struct |
||
313 | */ |
||
314 | 80 | public function addValue($value) |
|
323 | /** |
||
324 | * Gets the value object for the given value |
||
325 | * @uses Struct::getValues() |
||
326 | * @uses AbstractModel::getName() |
||
327 | * @param string $value Value name |
||
328 | * @return StructValue|null |
||
329 | */ |
||
330 | 115 | public function getValue($value) |
|
334 | /** |
||
335 | * Allows to define from which class the current model extends |
||
336 | * @param bool $short |
||
337 | * @return string |
||
338 | */ |
||
339 | 195 | public function getExtends($short = false) |
|
349 | /** |
||
350 | * @return Struct|null |
||
351 | */ |
||
352 | 275 | public function getInheritanceStruct() |
|
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | 175 | public function getTopInheritance() |
|
374 | /** |
||
375 | * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta() |
||
376 | * @return string[] |
||
377 | */ |
||
378 | 265 | public function getMeta() |
|
383 | /** |
||
384 | * @param $filename |
||
385 | * @return StructReservedMethod|StructArrayReservedMethod |
||
386 | */ |
||
387 | 180 | public function getReservedMethodsInstance($filename = null) |
|
395 | /** |
||
396 | * @return string[] |
||
397 | */ |
||
398 | 5 | public function getTypes() |
|
402 | /** |
||
403 | * @return boolean |
||
404 | */ |
||
405 | 165 | public function isUnion() |
|
409 | /** |
||
410 | * @param string[] $types |
||
411 | * @return Struct |
||
412 | */ |
||
413 | 1220 | public function setTypes($types) |
|
418 | /** |
||
419 | * {@inheritDoc} |
||
420 | * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize() |
||
421 | */ |
||
422 | 5 | protected function toJsonSerialize() |
|
432 | /** |
||
433 | * @param array $attributes |
||
434 | */ |
||
435 | 380 | public function setAttributesFromSerializedJson(array $attributes) |
|
441 | /** |
||
442 | * @param array $values |
||
443 | */ |
||
444 | 380 | public function setValuesFromSerializedJson(array $values) |
|
450 | } |
||
451 |