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 | 740 | public function __construct(Generator $generator, $name) |
|
75 | /** |
||
76 | * @uses AbstractModel::getInheritedMoel() |
||
77 | * @uses AbstractModel::getPackagedName() |
||
78 | * @uses AbstractModel::getExtends() |
||
79 | * @uses Struct::getIsStruct() |
||
80 | * @return string |
||
81 | */ |
||
82 | 208 | public function getExtendsClassName() |
|
93 | /** |
||
94 | * Returns the name of the class the current class inherits from |
||
95 | * @return string |
||
96 | */ |
||
97 | 268 | public function getInheritance() |
|
101 | /** |
||
102 | * Sets the name of the class the current class inherits from |
||
103 | * @param AbstractModel |
||
104 | */ |
||
105 | 172 | public function setInheritance($inheritance = '') |
|
110 | /** |
||
111 | * @uses AbstractGeneratorAware::getGenerator() |
||
112 | * @uses Generator::getStruct() |
||
113 | * @uses AbstractModel::getInheritance() |
||
114 | * @return Struct |
||
115 | */ |
||
116 | 208 | public function getInheritedMoel() |
|
120 | /** |
||
121 | * Returns the meta |
||
122 | * @return string[] |
||
123 | */ |
||
124 | 288 | public function getMeta() |
|
128 | /** |
||
129 | * Sets the meta |
||
130 | * @param string[] $meta |
||
131 | * @return AbstractModel |
||
132 | */ |
||
133 | 4 | public function setMeta(array $meta = array()) |
|
138 | /** |
||
139 | * Add meta information to the operation |
||
140 | * @uses AbstractModel::getMeta() |
||
141 | * @throws \InvalidArgumentException |
||
142 | * @param string $metaName |
||
143 | * @param mixed $metaValue |
||
144 | * @return AbstractModel |
||
145 | */ |
||
146 | 364 | public function addMeta($metaName, $metaValue) |
|
166 | /** |
||
167 | * Sets the documentation meta value. |
||
168 | * Documentation is set as an array so if multiple documentation nodes are set for an unique element, it will gather them. |
||
169 | * @uses AbstractModel::META_DOCUMENTATION |
||
170 | * @uses AbstractModel::addMeta() |
||
171 | * @param string $documentation the documentation from the WSDL |
||
172 | * @return AbstractModel |
||
173 | */ |
||
174 | 104 | public function setDocumentation($documentation) |
|
178 | /** |
||
179 | * Returns a meta value according to its name |
||
180 | * @uses AbstractModel::getMeta() |
||
181 | * @param string $metaName the meta information name |
||
182 | * @param mixed $fallback the fallback value if unset |
||
183 | * @return mixed the meta information value |
||
184 | */ |
||
185 | 232 | public function getMetaValue($metaName, $fallback = null) |
|
190 | /** |
||
191 | * Returns the value of the first meta value assigned to the name |
||
192 | * @param array $names the meta names to check |
||
193 | * @param mixed $fallback the fallback value if anyone is set |
||
194 | * @return mixed the meta information value |
||
195 | */ |
||
196 | 104 | public function getMetaValueFirstSet(array $names, $fallback = null) |
|
206 | /** |
||
207 | * Returns the original name extracted from the WSDL |
||
208 | * @return string |
||
209 | */ |
||
210 | 640 | public function getName() |
|
214 | /** |
||
215 | * Sets the original name extracted from the WSDL |
||
216 | * @param string $name |
||
217 | * @return AbstractModel |
||
218 | */ |
||
219 | 740 | public function setName($name) |
|
224 | /** |
||
225 | * Returns a valid clean name for PHP |
||
226 | * @uses AbstractModel::getName() |
||
227 | * @uses AbstractModel::cleanString() |
||
228 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
229 | * @return string |
||
230 | */ |
||
231 | 516 | public function getCleanName($keepMultipleUnderscores = true) |
|
235 | /** |
||
236 | * Returns the owner model object |
||
237 | * @return AbstractModel |
||
238 | */ |
||
239 | 516 | public function getOwner() |
|
243 | /** |
||
244 | * Sets the owner model object |
||
245 | * @param AbstractModel $owner object the owner of the current model |
||
246 | * @return AbstractModel |
||
247 | */ |
||
248 | 564 | public function setOwner(AbstractModel $owner) |
|
253 | /** |
||
254 | * @return bool |
||
255 | */ |
||
256 | 212 | public function getIsAbstract() |
|
260 | /** |
||
261 | * @param bool $isAbstract |
||
262 | * @return AbstractModel |
||
263 | */ |
||
264 | 20 | public function setIsAbstract($isAbstract) |
|
269 | /** |
||
270 | * Returns true if the original name is safe to use as a PHP property, variable name or class name |
||
271 | * @uses AbstractModel::getName() |
||
272 | * @uses AbstractModel::getCleanName() |
||
273 | * @return bool |
||
274 | */ |
||
275 | 180 | public function nameIsClean() |
|
279 | /** |
||
280 | * Returns the packaged name |
||
281 | * @uses AbstractModel::getNamespace() |
||
282 | * @uses AbstractModel::getCleanName() |
||
283 | * @uses AbstractModel::getContextualPart() |
||
284 | * @uses AbstractModel::uniqueName() |
||
285 | * @uses AbstractModel::replacePhpReservedKeyword() |
||
286 | * @uses AbstractGeneratorAware::getGenerator() |
||
287 | * @uses Generator::getOptionPrefix() |
||
288 | * @uses Generator::getOptionSuffix() |
||
289 | * @uses AbstractModel::uniqueName() to ensure unique naming of struct case sensitively |
||
290 | * @return string |
||
291 | */ |
||
292 | 504 | public function getPackagedName($namespaced = false) |
|
310 | /** |
||
311 | * Allows to define the contextual part of the class name for the package |
||
312 | * @return string |
||
313 | */ |
||
314 | 64 | public function getContextualPart() |
|
318 | /** |
||
319 | * Allows to define from which class the curent model extends |
||
320 | * @param bool $short |
||
321 | * @return string|null |
||
322 | */ |
||
323 | 32 | public function getExtends($short = false) |
|
327 | /** |
||
328 | * @uses AbstractGeneratorAware::getGenerator() |
||
329 | * @uses Generator::getOptionNamespacePrefix() |
||
330 | * @uses Generator::getOptionPrefix() |
||
331 | * @uses Generator::getOptionSuffix() |
||
332 | * @uses AbstractModel::getSubDirectory() |
||
333 | * @return string |
||
334 | */ |
||
335 | 236 | public function getNamespace() |
|
353 | /** |
||
354 | * Returns directory where to store class and create it if needed |
||
355 | * @uses AbstractGeneratorAware::getGenerator() |
||
356 | * @uses AbstractModel::getOptionCategory() |
||
357 | * @uses AbstractGeneratorAware::getContextualPart() |
||
358 | * @uses GeneratorOptions::VALUE_CAT |
||
359 | * @return string |
||
360 | */ |
||
361 | 260 | public function getSubDirectory() |
|
369 | /** |
||
370 | * Returns the sub package name which the model belongs to |
||
371 | * Must be overridden by sub classes |
||
372 | * @return array |
||
373 | */ |
||
374 | 4 | public function getDocSubPackages() |
|
378 | /** |
||
379 | * Clean a string to make it valid as PHP variable |
||
380 | * @uses GeneratorUtils::cleanString() |
||
381 | * @param string $string the string to clean |
||
382 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
383 | * @return string |
||
384 | */ |
||
385 | 520 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
389 | /** |
||
390 | * Returns a usable keyword for a original keyword |
||
391 | * @uses PhpReservedKeyword::instance() |
||
392 | * @uses PhpReservedKeyword::is() |
||
393 | * @param string $keyword the keyword |
||
394 | * @param string $context the context |
||
395 | * @return string |
||
396 | */ |
||
397 | 500 | public static function replacePhpReservedKeyword($keyword, $context = null) |
|
415 | /** |
||
416 | * @throws \InvalidArgumentException |
||
417 | * @param $filename |
||
418 | * @return AbstractReservedWord |
||
419 | */ |
||
420 | 4 | public function getReservedMethodsInstance($filename = null) |
|
424 | /** |
||
425 | * Returns a usable method for a original method |
||
426 | * @uses PhpReservedKeywords::instance() |
||
427 | * @uses PhpReservedKeywords::is() |
||
428 | * @param string $keyword the keyword |
||
|
|||
429 | * @param string $context the context |
||
430 | * @return string |
||
431 | */ |
||
432 | 504 | public function replaceReservedMethod($methodName, $context = null) |
|
450 | /** |
||
451 | * Static method wich returns a unique name case sensitively |
||
452 | * Useful to name methods case sensitively distinct, see http://the-echoplex.net/log/php-case-sensitivity |
||
453 | * @param string $name the original name |
||
454 | * @param string $context the context where the name is needed unique |
||
455 | * @return string |
||
456 | */ |
||
457 | 508 | protected static function uniqueName($name, $context) |
|
472 | /** |
||
473 | * Gives the availability for test purpose and multiple package generation to purge unique names |
||
474 | * @todo see if it can be removed by reviewing how unique names are generated |
||
475 | */ |
||
476 | 284 | public static function purgeUniqueNames() |
|
480 | /** |
||
481 | * Gives the availability for test purpose and multiple package generation to purge reserved keywords usage |
||
482 | * @todo see if it can be removed by reviewing how reserved keywords are generated |
||
483 | */ |
||
484 | 260 | public static function purgePhpReservedKeywords() |
|
488 | /** |
||
489 | * Gives the availability for test purpose and multiple package generation to purge reserved methods usage |
||
490 | * @todo see if it can be removed by reviewing how reserved methods are generated |
||
491 | */ |
||
492 | public static function purgeReservedMethods() |
||
496 | } |
||
497 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.