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 |
||
| 14 | abstract class AbstractModel extends AbstractGeneratorAware |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Constant used to define the key to store documentation value in meta |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | const META_DOCUMENTATION = 'documentation'; |
||
| 21 | /** |
||
| 22 | * Original name od the element |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $name = ''; |
||
| 26 | /** |
||
| 27 | * Values associated to the operation |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | private $meta = array(); |
||
| 31 | /** |
||
| 32 | * Define the inheritance of a struct by the name of the parent struct or type |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $inheritance = ''; |
||
| 36 | /** |
||
| 37 | * Store the object which owns the current model |
||
| 38 | * @var AbstractModel |
||
| 39 | */ |
||
| 40 | private $owner = null; |
||
| 41 | /** |
||
| 42 | * Indicates that the current elemen is an abstract element. |
||
| 43 | * It allows to generated an abstract class. |
||
| 44 | * This will happen for element/complexType that are defined with abstract="true" |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $isAbstract = false; |
||
| 48 | /** |
||
| 49 | * Replaced keywords time in order to generate unique new keyword |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private static $replacedReservedPhpKeywords = array(); |
||
| 53 | /** |
||
| 54 | * Unique name generated in order to ensure unique naming (for struct constructor and setters/getters even for different case attribute name whith same value) |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private static $uniqueNames = array(); |
||
| 58 | /** |
||
| 59 | * Main constructor |
||
| 60 | * @uses AbstractModel::setName() |
||
| 61 | * @param Generator $generator |
||
| 62 | * @param string $name the original name |
||
| 63 | */ |
||
| 64 | 684 | public function __construct(Generator $generator, $name) |
|
| 69 | /** |
||
| 70 | * @uses AbstractModel::getInheritedMoel() |
||
| 71 | * @uses AbstractModel::getPackagedName() |
||
| 72 | * @uses AbstractModel::getExtends() |
||
| 73 | * @uses Struct::getIsStruct() |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | 184 | public function getExtendsClassName() |
|
| 87 | /** |
||
| 88 | * Returns the name of the class the current class inherits from |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 236 | public function getInheritance() |
|
| 95 | /** |
||
| 96 | * Sets the name of the class the current class inherits from |
||
| 97 | * @param AbstractModel |
||
| 98 | */ |
||
| 99 | 148 | public function setInheritance($inheritance = '') |
|
| 104 | /** |
||
| 105 | * @uses AbstractGeneratorAware::getGenerator() |
||
| 106 | * @uses Generator::getStruct() |
||
| 107 | * @uses AbstractModel::getInheritance() |
||
| 108 | * @return Struct |
||
| 109 | */ |
||
| 110 | 184 | public function getInheritedMoel() |
|
| 114 | /** |
||
| 115 | * Returns the meta |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | 256 | public function getMeta() |
|
| 122 | /** |
||
| 123 | * Sets the meta |
||
| 124 | * @param string[] $meta |
||
| 125 | * @return AbstractModel |
||
| 126 | */ |
||
| 127 | 4 | public function setMeta(array $meta = array()) |
|
| 132 | /** |
||
| 133 | * Add meta information to the operation |
||
| 134 | * @uses AbstractModel::getMeta() |
||
| 135 | * @throws \InvalidArgumentException |
||
| 136 | * @param string $metaName |
||
| 137 | * @param mixed $metaValue |
||
| 138 | * @return AbstractModel |
||
| 139 | */ |
||
| 140 | 332 | public function addMeta($metaName, $metaValue) |
|
| 160 | /** |
||
| 161 | * Sets the documentation meta value. |
||
| 162 | * Documentation is set as an array so if multiple documentation nodes are set for an unique element, it will gather them. |
||
| 163 | * @uses AbstractModel::META_DOCUMENTATION |
||
| 164 | * @uses AbstractModel::addMeta() |
||
| 165 | * @param string $documentation the documentation from the WSDL |
||
| 166 | * @return AbstractModel |
||
| 167 | */ |
||
| 168 | 96 | public function setDocumentation($documentation) |
|
| 172 | /** |
||
| 173 | * Returns a meta value according to its name |
||
| 174 | * @uses AbstractModel::getMeta() |
||
| 175 | * @param string $metaName the meta information name |
||
| 176 | * @param mixed $fallback the fallback value if unset |
||
| 177 | * @return mixed the meta information value |
||
| 178 | */ |
||
| 179 | 208 | public function getMetaValue($metaName, $fallback = null) |
|
| 184 | /** |
||
| 185 | * Returns the value of the first meta value assigned to the name |
||
| 186 | * @param array $names the meta names to check |
||
| 187 | * @param mixed $fallback the fallback value if anyone is set |
||
| 188 | * @return mixed the meta information value |
||
| 189 | */ |
||
| 190 | 84 | public function getMetaValueFirstSet(array $names, $fallback = null) |
|
| 200 | /** |
||
| 201 | * Returns the original name extracted from the WSDL |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 588 | public function getName() |
|
| 208 | /** |
||
| 209 | * Sets the original name extracted from the WSDL |
||
| 210 | * @param string $name |
||
| 211 | * @return AbstractModel |
||
| 212 | */ |
||
| 213 | 684 | public function setName($name) |
|
| 218 | /** |
||
| 219 | * Returns a valid clean name for PHP |
||
| 220 | * @uses AbstractModel::getName() |
||
| 221 | * @uses AbstractModel::cleanString() |
||
| 222 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | 480 | public function getCleanName($keepMultipleUnderscores = true) |
|
| 229 | /** |
||
| 230 | * Returns the owner model object |
||
| 231 | * @return AbstractModel |
||
| 232 | */ |
||
| 233 | 476 | public function getOwner() |
|
| 237 | /** |
||
| 238 | * Sets the owner model object |
||
| 239 | * @param AbstractModel $owner object the owner of the current model |
||
| 240 | * @return AbstractModel |
||
| 241 | */ |
||
| 242 | 520 | public function setOwner(AbstractModel $owner) |
|
| 247 | /** |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 188 | public function getIsAbstract() |
|
| 254 | /** |
||
| 255 | * @param bool $isAbstract |
||
| 256 | * @return AbstractModel |
||
| 257 | */ |
||
| 258 | 16 | public function setIsAbstract($isAbstract) |
|
| 263 | /** |
||
| 264 | * Returns true if the original name is safe to use as a PHP property, variable name or class name |
||
| 265 | * @uses AbstractModel::getName() |
||
| 266 | * @uses AbstractModel::getCleanName() |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 156 | public function nameIsClean() |
|
| 273 | /** |
||
| 274 | * Returns the packaged name |
||
| 275 | * @uses AbstractModel::getNamespace() |
||
| 276 | * @uses AbstractModel::getCleanName() |
||
| 277 | * @uses AbstractModel::getContextualPart() |
||
| 278 | * @uses AbstractModel::uniqueName() |
||
| 279 | * @uses AbstractModel::replaceReservedPhpKeyword() |
||
| 280 | * @uses AbstractGeneratorAware::getGenerator() |
||
| 281 | * @uses Generator::getOptionPrefix() |
||
| 282 | * @uses Generator::getOptionSuffix() |
||
| 283 | * @uses AbstractModel::uniqueName() to ensure unique naming of struct case sensitively |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 468 | public function getPackagedName($namespaced = false) |
|
| 304 | /** |
||
| 305 | * Allows to define the contextual part of the class name for the package |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | 64 | public function getContextualPart() |
|
| 312 | /** |
||
| 313 | * Allows to define from which class the curent model extends |
||
| 314 | * @param bool $short |
||
| 315 | * @return string|null |
||
| 316 | */ |
||
| 317 | 32 | public function getExtends($short = false) |
|
| 321 | /** |
||
| 322 | * @uses AbstractGeneratorAware::getGenerator() |
||
| 323 | * @uses Generator::getOptionNamespacePrefix() |
||
| 324 | * @uses Generator::getOptionPrefix() |
||
| 325 | * @uses Generator::getOptionSuffix() |
||
| 326 | * @uses AbstractModel::getSubDirectory() |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 212 | public function getNamespace() |
|
| 347 | /** |
||
| 348 | * Returns directory where to store class and create it if needed |
||
| 349 | * @uses AbstractGeneratorAware::getGenerator() |
||
| 350 | * @uses AbstractModel::getOptionCategory() |
||
| 351 | * @uses AbstractGeneratorAware::getContextualPart() |
||
| 352 | * @uses GeneratorOptions::VALUE_CAT |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 236 | public function getSubDirectory() |
|
| 363 | /** |
||
| 364 | * Returns the sub package name which the model belongs to |
||
| 365 | * Must be overridden by sub classes |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 4 | public function getDocSubPackages() |
|
| 372 | /** |
||
| 373 | * Clean a string to make it valid as PHP variable |
||
| 374 | * @uses GeneratorUtils::cleanString() |
||
| 375 | * @param string $string the string to clean |
||
| 376 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | 484 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
| 383 | /** |
||
| 384 | * Returns a usable keyword for a original keyword |
||
| 385 | * @uses ReservedKeywords::instance() |
||
| 386 | * @uses ReservedKeywords::is() |
||
| 387 | * @param string $keyword the keyword |
||
| 388 | * @param string $context the context |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 464 | public static function replaceReservedPhpKeyword($keyword, $context = null) |
|
| 410 | /** |
||
| 411 | * Static method wich returns a unique name case sensitively |
||
| 412 | * Useful to name methods case sensitively distinct, see http://the-echoplex.net/log/php-case-sensitivity |
||
| 413 | * @param string $name the original name |
||
| 414 | * @param string $context the context where the name is needed unique |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 472 | protected static function uniqueName($name, $context) |
|
| 432 | /** |
||
| 433 | * Gives the availability for test purpose and multiple package generation to purge unique names |
||
| 434 | * @todo see if it can be removed by reviewing how unique names are generated |
||
| 435 | */ |
||
| 436 | 260 | public static function purgeUniqueNames() |
|
| 440 | /** |
||
| 441 | * Gives the availability for test purpose and multiple package generation to purge reserved keywords usage |
||
| 442 | * @todo see if it can be removed by reviewing how reserved keywords are generated |
||
| 443 | */ |
||
| 444 | 236 | public static function purgeReservedKeywords() |
|
| 448 | } |
||
| 449 |