Complex classes like AbstractModel 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 AbstractModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class AbstractModel extends AbstractGeneratorAware |
||
16 | { |
||
17 | /** |
||
18 | * Constant used to define the key to store documentation value in meta |
||
19 | * @var string |
||
20 | */ |
||
21 | const META_DOCUMENTATION = 'documentation'; |
||
22 | /** |
||
23 | * Original name od the element |
||
24 | * @var string |
||
25 | */ |
||
26 | private $name = ''; |
||
27 | /** |
||
28 | * Values associated to the operation |
||
29 | * @var string[] |
||
30 | */ |
||
31 | private $meta = array(); |
||
32 | /** |
||
33 | * Define the inheritance of a struct by the name of the parent struct or type |
||
34 | * @var string |
||
35 | */ |
||
36 | private $inheritance = ''; |
||
37 | /** |
||
38 | * Store the object which owns the current model |
||
39 | * @var AbstractModel |
||
40 | */ |
||
41 | private $owner = null; |
||
42 | /** |
||
43 | * Indicates that the current elemen is an abstract element. |
||
44 | * It allows to generated an abstract class. |
||
45 | * This will happen for element/complexType that are defined with abstract="true" |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $isAbstract = false; |
||
49 | /** |
||
50 | * Replaced keywords time in order to generate unique new keyword |
||
51 | * @var array |
||
52 | */ |
||
53 | private static $replacedPhpReservedKeywords = array(); |
||
54 | /** |
||
55 | * Replaced methods time in order to generate unique new method |
||
56 | * @var array |
||
57 | */ |
||
58 | private $replacedReservedMethods = array(); |
||
59 | /** |
||
60 | * Unique name generated in order to ensure unique naming (for struct constructor and setters/getters even for different case attribute name whith same value) |
||
61 | * @var array |
||
62 | */ |
||
63 | private static $uniqueNames = array(); |
||
64 | /** |
||
65 | * Main constructor |
||
66 | * @uses AbstractModel::setName() |
||
67 | * @param Generator $generator |
||
68 | * @param string $name the original name |
||
69 | */ |
||
70 | 752 | public function __construct(Generator $generator, $name) |
|
75 | /** |
||
76 | * @uses AbstractModel::getInheritedModel() |
||
77 | * @uses AbstractModel::getPackagedName() |
||
78 | * @uses AbstractModel::getExtends() |
||
79 | * @uses Struct::getIsStruct() |
||
80 | * @return string |
||
81 | */ |
||
82 | 216 | public function getExtendsClassName() |
|
93 | /** |
||
94 | * Returns the name of the class the current class inherits from |
||
95 | * @return string |
||
96 | */ |
||
97 | 276 | public function getInheritance() |
|
101 | /** |
||
102 | * Sets the name of the class the current class inherits from |
||
103 | * @param string $inheritance |
||
104 | * @return AbstractModel |
||
105 | */ |
||
106 | 180 | public function setInheritance($inheritance = '') |
|
111 | /** |
||
112 | * @uses AbstractGeneratorAware::getGenerator() |
||
113 | * @uses Generator::getStruct() |
||
114 | * @uses AbstractModel::getInheritance() |
||
115 | * @return Struct |
||
116 | */ |
||
117 | 216 | public function getInheritedModel() |
|
118 | { |
||
119 | 216 | return $this->getGenerator()->getStruct($this->getInheritance()); |
|
120 | } |
||
121 | /** |
||
122 | * Returns the meta |
||
123 | * @return string[] |
||
124 | */ |
||
125 | 296 | public function getMeta() |
|
129 | /** |
||
130 | * Sets the meta |
||
131 | * @param string[] $meta |
||
132 | * @return AbstractModel |
||
133 | */ |
||
134 | 4 | public function setMeta(array $meta = array()) |
|
139 | /** |
||
140 | * Add meta information to the operation |
||
141 | * @uses AbstractModel::getMeta() |
||
142 | * @throws \InvalidArgumentException |
||
143 | * @param string $metaName |
||
144 | * @param mixed $metaValue |
||
145 | * @return AbstractModel |
||
146 | */ |
||
147 | 372 | public function addMeta($metaName, $metaValue) |
|
167 | /** |
||
168 | * Sets the documentation meta value. |
||
169 | * Documentation is set as an array so if multiple documentation nodes are set for an unique element, it will gather them. |
||
170 | * @uses AbstractModel::META_DOCUMENTATION |
||
171 | * @uses AbstractModel::addMeta() |
||
172 | * @param string $documentation the documentation from the WSDL |
||
173 | * @return AbstractModel |
||
174 | */ |
||
175 | 112 | public function setDocumentation($documentation) |
|
181 | /** |
||
182 | * Returns a meta value according to its name |
||
183 | * @uses AbstractModel::getMeta() |
||
184 | * @param string $metaName the meta information name |
||
185 | * @param mixed $fallback the fallback value if unset |
||
186 | * @return mixed the meta information value |
||
187 | */ |
||
188 | 240 | public function getMetaValue($metaName, $fallback = null) |
|
193 | /** |
||
194 | * Returns the value of the first meta value assigned to the name |
||
195 | * @param string[] $names the meta names to check |
||
196 | * @param mixed $fallback the fallback value if anyone is set |
||
197 | * @return mixed the meta information value |
||
198 | */ |
||
199 | 104 | public function getMetaValueFirstSet(array $names, $fallback = null) |
|
209 | /** |
||
210 | * Returns the original name extracted from the WSDL |
||
211 | * @return string |
||
212 | */ |
||
213 | 652 | public function getName() |
|
217 | /** |
||
218 | * Sets the original name extracted from the WSDL |
||
219 | * @param string $name |
||
220 | * @return AbstractModel |
||
221 | */ |
||
222 | 752 | public function setName($name) |
|
227 | /** |
||
228 | * Returns a valid clean name for PHP |
||
229 | * @uses AbstractModel::getName() |
||
230 | * @uses AbstractModel::cleanString() |
||
231 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
232 | * @return string |
||
233 | */ |
||
234 | 524 | public function getCleanName($keepMultipleUnderscores = true) |
|
238 | /** |
||
239 | * Returns the owner model object |
||
240 | * @return AbstractModel |
||
241 | */ |
||
242 | 524 | public function getOwner() |
|
246 | /** |
||
247 | * Sets the owner model object |
||
248 | * @param AbstractModel $owner object the owner of the current model |
||
249 | * @return AbstractModel |
||
250 | */ |
||
251 | 572 | public function setOwner(AbstractModel $owner) |
|
256 | /** |
||
257 | * @return bool |
||
258 | */ |
||
259 | 220 | public function getIsAbstract() |
|
263 | /** |
||
264 | * @param bool $isAbstract |
||
265 | * @return AbstractModel |
||
266 | */ |
||
267 | 20 | public function setIsAbstract($isAbstract) |
|
272 | /** |
||
273 | * Returns true if the original name is safe to use as a PHP property, variable name or class name |
||
274 | * @uses AbstractModel::getName() |
||
275 | * @uses AbstractModel::getCleanName() |
||
276 | * @return bool |
||
277 | */ |
||
278 | 180 | public function nameIsClean() |
|
282 | /** |
||
283 | * Returns the packaged name |
||
284 | * @uses AbstractModel::getNamespace() |
||
285 | * @uses AbstractModel::getCleanName() |
||
286 | * @uses AbstractModel::getContextualPart() |
||
287 | * @uses AbstractModel::uniqueName() |
||
288 | * @uses AbstractModel::replacePhpReservedKeyword() |
||
289 | * @uses AbstractGeneratorAware::getGenerator() |
||
290 | * @uses Generator::getOptionPrefix() |
||
291 | * @uses Generator::getOptionSuffix() |
||
292 | * @uses AbstractModel::uniqueName() to ensure unique naming of struct case sensitively |
||
293 | * @return string |
||
294 | */ |
||
295 | 512 | public function getPackagedName($namespaced = false) |
|
313 | /** |
||
314 | * Allows to define the contextual part of the class name for the package |
||
315 | * @return string |
||
316 | */ |
||
317 | 76 | public function getContextualPart() |
|
321 | /** |
||
322 | * Allows to define from which class the curent model extends |
||
323 | * @param bool $short |
||
324 | * @return string|null |
||
325 | */ |
||
326 | 40 | public function getExtends($short = false) |
|
330 | /** |
||
331 | * @uses AbstractGeneratorAware::getGenerator() |
||
332 | * @uses Generator::getOptionNamespacePrefix() |
||
333 | * @uses Generator::getOptionPrefix() |
||
334 | * @uses Generator::getOptionSuffix() |
||
335 | * @uses AbstractModel::getSubDirectory() |
||
336 | * @return string |
||
337 | */ |
||
338 | 248 | public function getNamespace() |
|
356 | /** |
||
357 | * Returns directory where to store class and create it if needed |
||
358 | * @uses AbstractGeneratorAware::getGenerator() |
||
359 | * @uses AbstractModel::getOptionCategory() |
||
360 | * @uses AbstractGeneratorAware::getContextualPart() |
||
361 | * @uses GeneratorOptions::VALUE_CAT |
||
362 | * @return string |
||
363 | */ |
||
364 | 272 | public function getSubDirectory() |
|
372 | /** |
||
373 | * Returns the sub package name which the model belongs to |
||
374 | * Must be overridden by sub classes |
||
375 | * @return array |
||
376 | */ |
||
377 | 4 | public function getDocSubPackages() |
|
381 | /** |
||
382 | * Clean a string to make it valid as PHP variable |
||
383 | * @uses GeneratorUtils::cleanString() |
||
384 | * @param string $string the string to clean |
||
385 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
386 | * @return string |
||
387 | */ |
||
388 | 528 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
392 | /** |
||
393 | * Returns a usable keyword for a original keyword |
||
394 | * @uses PhpReservedKeyword::instance() |
||
395 | * @uses PhpReservedKeyword::is() |
||
396 | * @param string $keyword the keyword |
||
397 | * @param string $context the context |
||
398 | * @return string |
||
399 | */ |
||
400 | 508 | public static function replacePhpReservedKeyword($keyword, $context = null) |
|
418 | /** |
||
419 | * @throws \InvalidArgumentException |
||
420 | * @param $filename |
||
421 | * @return AbstractReservedWord |
||
422 | */ |
||
423 | 4 | public function getReservedMethodsInstance($filename = null) |
|
427 | /** |
||
428 | * Returns a usable method for a original method |
||
429 | * @uses PhpReservedKeywords::instance() |
||
430 | * @uses PhpReservedKeywords::is() |
||
431 | * @param string $methodName the method name |
||
432 | * @param string $context the context |
||
433 | * @return string |
||
434 | */ |
||
435 | 512 | public function replaceReservedMethod($methodName, $context = null) |
|
453 | /** |
||
454 | * Static method wich returns a unique name case sensitively |
||
455 | * Useful to name methods case sensitively distinct, see http://the-echoplex.net/log/php-case-sensitivity |
||
456 | * @param string $name the original name |
||
457 | * @param string $context the context where the name is needed unique |
||
458 | * @return string |
||
459 | */ |
||
460 | 516 | protected static function uniqueName($name, $context) |
|
475 | /** |
||
476 | * Gives the availability for test purpose and multiple package generation to purge unique names |
||
477 | * @todo see if it can be removed by reviewing how unique names are generated |
||
478 | */ |
||
479 | 292 | public static function purgeUniqueNames() |
|
483 | /** |
||
484 | * Gives the availability for test purpose and multiple package generation to purge reserved keywords usage |
||
485 | * @todo see if it can be removed by reviewing how reserved keywords are generated |
||
486 | */ |
||
487 | 268 | public static function purgePhpReservedKeywords() |
|
491 | /** |
||
492 | * Gives the availability for test purpose and multiple package generation to purge reserved methods usage |
||
493 | * @todo see if it can be removed by reviewing how reserved methods are generated |
||
494 | */ |
||
495 | public static function purgeReservedMethods() |
||
499 | } |
||
500 |