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 | 676 | public function __construct(Generator $generator, $name) |
|
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 176 | public function getExtendsClassName() |
|
| 73 | { |
||
| 74 | 176 | $extends = ''; |
|
| 75 | 176 | if (($model = $this->getInheritedMoel()) instanceof Struct && $model->getIsStruct()) { |
|
| 76 | 12 | $extends = $model->getPackagedName(); |
|
| 77 | 9 | } |
|
| 78 | 176 | if (empty($extends)) { |
|
| 79 | 172 | $extends = $this->getExtends(true); |
|
| 80 | 129 | } |
|
| 81 | 176 | return $extends; |
|
| 82 | } |
||
| 83 | /** |
||
| 84 | * Returns the name of the class the current class inherits from |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 228 | public function getInheritance() |
|
| 91 | /** |
||
| 92 | * Sets the name of the class the current class inherits from |
||
| 93 | * @param AbstractModel |
||
| 94 | */ |
||
| 95 | 140 | public function setInheritance($inheritance = '') |
|
| 100 | /** |
||
| 101 | * @return Struct |
||
| 102 | */ |
||
| 103 | 176 | public function getInheritedMoel() |
|
| 107 | /** |
||
| 108 | * Returns the meta |
||
| 109 | * @return string[] |
||
| 110 | */ |
||
| 111 | 248 | public function getMeta() |
|
| 115 | /** |
||
| 116 | * Sets the meta |
||
| 117 | * @param string[] $meta |
||
| 118 | * @return AbstractModel |
||
| 119 | */ |
||
| 120 | 4 | public function setMeta(array $meta = array()) |
|
| 125 | /** |
||
| 126 | * Add meta information to the operation |
||
| 127 | * @uses AbstractModel::getMeta() |
||
| 128 | * @throws \InvalidArgumentException |
||
| 129 | * @param string $metaName |
||
| 130 | * @param mixed $metaValue |
||
| 131 | * @return AbstractModel |
||
| 132 | */ |
||
| 133 | 324 | public function addMeta($metaName, $metaValue) |
|
| 153 | /** |
||
| 154 | * Sets the documentation meta value. |
||
| 155 | * Documentation is set as an array so if multiple documentation nodes are set for an unique element, it will gather them. |
||
| 156 | * @uses AbstractModel::META_DOCUMENTATION |
||
| 157 | * @uses AbstractModel::addMeta() |
||
| 158 | * @param string $documentation the documentation from the WSDL |
||
| 159 | * @return AbstractModel |
||
| 160 | */ |
||
| 161 | 88 | public function setDocumentation($documentation) |
|
| 165 | /** |
||
| 166 | * Returns a meta value according to its name |
||
| 167 | * @uses AbstractModel::getMeta() |
||
| 168 | * @param string $metaName the meta information name |
||
| 169 | * @param mixed $fallback the fallback value if unset |
||
| 170 | * @return mixed the meta information value |
||
| 171 | */ |
||
| 172 | 200 | public function getMetaValue($metaName, $fallback = null) |
|
| 177 | /** |
||
| 178 | * Returns the value of the first meta value assigned to the name |
||
| 179 | * @param array $names the meta names to check |
||
| 180 | * @param mixed $fallback the fallback value if anyone is set |
||
| 181 | * @return mixed the meta information value |
||
| 182 | */ |
||
| 183 | 84 | public function getMetaValueFirstSet(array $names, $fallback = null) |
|
| 193 | /** |
||
| 194 | * Returns the original name extracted from the WSDL |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 580 | public function getName() |
|
| 201 | /** |
||
| 202 | * Sets the original name extracted from the WSDL |
||
| 203 | * @param string $name |
||
| 204 | * @return AbstractModel |
||
| 205 | */ |
||
| 206 | 676 | public function setName($name) |
|
| 211 | /** |
||
| 212 | * Returns a valid clean name for PHP |
||
| 213 | * @uses AbstractModel::getName() |
||
| 214 | * @uses AbstractModel::cleanString() |
||
| 215 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 472 | public function getCleanName($keepMultipleUnderscores = true) |
|
| 222 | /** |
||
| 223 | * Returns the owner model object |
||
| 224 | * @return AbstractModel |
||
| 225 | */ |
||
| 226 | 468 | public function getOwner() |
|
| 230 | /** |
||
| 231 | * Sets the owner model object |
||
| 232 | * @param AbstractModel $owner object the owner of the current model |
||
| 233 | * @return AbstractModel |
||
| 234 | */ |
||
| 235 | 512 | public function setOwner(AbstractModel $owner) |
|
| 240 | /** |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 180 | public function getIsAbstract() |
|
| 247 | /** |
||
| 248 | * @param bool $isAbstract |
||
| 249 | * @return AbstractModel |
||
| 250 | */ |
||
| 251 | 12 | public function setIsAbstract($isAbstract) |
|
| 256 | /** |
||
| 257 | * Returns true if the original name is safe to use as a PHP property, variable name or class name |
||
| 258 | * @uses AbstractModel::getName() |
||
| 259 | * @uses AbstractModel::getCleanName() |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | 148 | public function nameIsClean() |
|
| 266 | /** |
||
| 267 | * Returns the packaged name |
||
| 268 | * @uses Generator::getPackageName() |
||
| 269 | * @uses AbstractModel::getCleanName() |
||
| 270 | * @uses AbstractModel::getContextualPart() |
||
| 271 | * @uses AbstractModel::uniqueName() to ensure unique naming of struct case sensitively |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | 460 | public function getPackagedName($namespaced = false) |
|
| 293 | 64 | /** |
|
| 294 | * Allows to define the contextual part of the class name for the package |
||
| 295 | 64 | * @return string |
|
| 296 | */ |
||
| 297 | public function getContextualPart() |
||
| 301 | /** |
||
| 302 | 32 | * Allows to define from which class the curent model extends |
|
| 303 | * @param bool $short |
||
| 304 | 32 | * @return string|null |
|
| 305 | */ |
||
| 306 | public function getExtends($short = false) |
||
| 310 | /** |
||
| 311 | 204 | * @return string |
|
| 312 | 204 | */ |
|
| 313 | 204 | public function getNamespace() |
|
| 331 | 228 | /** |
|
| 332 | * Returns directory where to store class and create it if needed |
||
| 333 | 228 | * @return string |
|
| 334 | 228 | */ |
|
| 335 | 228 | public function getSubDirectory() |
|
| 343 | /** |
||
| 344 | 4 | * Returns the sub package name which the model belongs to |
|
| 345 | * Must be overridden by sub classes |
||
| 346 | 4 | * @return array |
|
| 347 | */ |
||
| 348 | public function getDocSubPackages() |
||
| 352 | /** |
||
| 353 | * Clean a string to make it valid as PHP variable |
||
| 354 | 476 | * @param string $string the string to clean |
|
| 355 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 356 | 476 | * @return string |
|
| 357 | */ |
||
| 358 | public static function cleanString($string, $keepMultipleUnderscores = true) |
||
| 362 | /** |
||
| 363 | * Returns a usable keyword for a original keyword |
||
| 364 | 456 | * @param string $keyword the keyword |
|
| 365 | * @param string $context the context |
||
| 366 | 456 | * @return string |
|
| 367 | 456 | */ |
|
| 368 | 60 | public static function replaceReservedPhpKeyword($keyword, $context = null) |
|
| 387 | /** |
||
| 388 | 464 | * Static method wich returns a unique name case sensitively |
|
| 389 | 464 | * Useful to name methods case sensitively distinct, see http://the-echoplex.net/log/php-case-sensitivity |
|
| 390 | 464 | * @param string $name the original name |
|
| 391 | 460 | * @param string $context the context where the name is needed unique |
|
| 392 | 392 | * @return string |
|
| 393 | 388 | */ |
|
| 394 | 291 | protected static function uniqueName($name, $context) |
|
| 409 | /** |
||
| 410 | * Gives the availability for test purpose and multiple package generation to purge unique names |
||
| 411 | * @todo see if it can be removed by reviewing how unique names are generated |
||
| 412 | */ |
||
| 413 | 228 | public static function purgeUniqueNames() |
|
| 417 | /** |
||
| 418 | * Gives the availability for test purpose and multiple package generation to purge reserved keywords usage |
||
| 419 | * @todo see if it can be removed by reviewing how reserved keywords are generated |
||
| 420 | */ |
||
| 421 | public static function purgeReservedKeywords() |
||
| 425 | } |
||
| 426 |