Complex classes like AbstractModelFile 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 AbstractModelFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class AbstractModelFile extends AbstractFile |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Long annotation string |
||
| 25 | */ |
||
| 26 | const ANNOTATION_LONG_LENGTH = '250'; |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const ANNOTATION_PACKAGE = 'package'; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const ANNOTATION_SUB_PACKAGE = 'subpackage'; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const ANNOTATION_RETURN = 'return'; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const ANNOTATION_USES = 'uses'; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const ANNOTATION_PARAM = 'param'; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const ANNOTATION_VAR = 'var'; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | const ANNOTATION_SEE = 'see'; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const ANNOTATION_THROWS = 'throws'; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | const METHOD_CONSTRUCT = '__construct'; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | const METHOD_SET_STATE = '__set_state'; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | const TYPE_STRING = 'string'; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | const TYPE_ARRAY = 'array'; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | const SRC_FOLDER = 'src/'; |
||
| 79 | /** |
||
| 80 | * @var AbstractModel |
||
| 81 | */ |
||
| 82 | protected $model; |
||
| 83 | /** |
||
| 84 | * @param bool $withSrc |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 192 | public function getFileDestination($withSrc = true) |
|
| 91 | /** |
||
| 92 | * @param bool $withSrc |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 192 | public function getDestinationFolder($withSrc = true) |
|
| 99 | /** |
||
| 100 | * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile() |
||
| 101 | * @param bool $withSrc |
||
| 102 | * @return int|bool |
||
| 103 | */ |
||
| 104 | 172 | public function writeFile($withSrc = true) |
|
| 117 | /** |
||
| 118 | * @return AbstractModelFile |
||
| 119 | */ |
||
| 120 | 168 | protected function addAnnotationBlock() |
|
| 127 | /** |
||
| 128 | * @param AbstractModel $model |
||
| 129 | * @return AbstractModelFile |
||
| 130 | */ |
||
| 131 | 216 | public function setModel(AbstractModel $model) |
|
| 137 | /** |
||
| 138 | * @return AbstractModel |
||
| 139 | */ |
||
| 140 | 220 | public function getModel() |
|
| 144 | /** |
||
| 145 | * @param string $name |
||
| 146 | * @return StructModel|null |
||
| 147 | */ |
||
| 148 | 16 | protected function getModelByName($name) |
|
| 152 | /** |
||
| 153 | * @param PhpAnnotationBlock $block |
||
| 154 | * @return AbstractModelFile |
||
| 155 | */ |
||
| 156 | 156 | protected function definePackageAnnotations(PhpAnnotationBlock $block) |
|
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 156 | protected function getPackageName() |
|
| 180 | /** |
||
| 181 | * @param PhpAnnotationBlock $block |
||
| 182 | * @return AbstractModelFile |
||
| 183 | */ |
||
| 184 | 156 | protected function defineGeneralAnnotations(PhpAnnotationBlock $block) |
|
| 191 | /** |
||
| 192 | * @return PhpAnnotationBlock |
||
| 193 | */ |
||
| 194 | 156 | protected function getClassAnnotationBlock() |
|
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 156 | protected function getClassDeclarationLine() |
|
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | 140 | protected function getClassDeclarationLineText() |
|
| 219 | /** |
||
| 220 | * @param PhpAnnotationBlock $block |
||
| 221 | * @param AbstractModel $model |
||
| 222 | * @return AbstractModelFile |
||
| 223 | */ |
||
| 224 | 156 | protected function defineModelAnnotationsFromWsdl(PhpAnnotationBlock $block, AbstractModel $model = null) |
|
| 229 | /** |
||
| 230 | * @param PhpAnnotationBlock $block |
||
| 231 | * @return AbstractModelFile |
||
| 232 | */ |
||
| 233 | 156 | protected function defineModelAnnotationsFromInheritance(PhpAnnotationBlock $block) |
|
| 244 | /** |
||
| 245 | * @param AbstractModel $model |
||
| 246 | * @return string[] |
||
| 247 | */ |
||
| 248 | protected function getValidMetaValues(AbstractModel $model) |
||
| 252 | /** |
||
| 253 | * @return AbstractModelFile |
||
| 254 | */ |
||
| 255 | 168 | protected function addClassElement() |
|
| 267 | /** |
||
| 268 | * @return AbstractModelFile |
||
| 269 | */ |
||
| 270 | 168 | protected function defineNamespace() |
|
| 279 | /** |
||
| 280 | * @return AbstractModelFile |
||
| 281 | */ |
||
| 282 | 168 | protected function defineUseStatement() |
|
| 291 | /** |
||
| 292 | * @param PhpClass $class |
||
| 293 | * @return AbstractModelFile |
||
| 294 | */ |
||
| 295 | 168 | protected function defineConstants(PhpClass $class) |
|
| 308 | /** |
||
| 309 | * @param PhpClass $class |
||
| 310 | * @return AbstractModelFile |
||
| 311 | */ |
||
| 312 | 168 | protected function defineProperties(PhpClass $class) |
|
| 325 | /** |
||
| 326 | * @param PhpClass $class |
||
| 327 | * @return AbstractModelFile |
||
| 328 | */ |
||
| 329 | 168 | protected function defineMethods(PhpClass $class) |
|
| 342 | /** |
||
| 343 | * @param Constant $constants |
||
| 344 | */ |
||
| 345 | abstract protected function getClassConstants(Constant $constants); |
||
| 346 | /** |
||
| 347 | * @param PhpConstant $constant |
||
| 348 | * @return PhpAnnotationBlock|null |
||
| 349 | */ |
||
| 350 | abstract protected function getConstantAnnotationBlock(PhpConstant $constant); |
||
| 351 | /** |
||
| 352 | * @param Property $properties |
||
| 353 | */ |
||
| 354 | abstract protected function getClassProperties(Property $properties); |
||
| 355 | /** |
||
| 356 | * @param PhpProperty $property |
||
| 357 | * @return PhpAnnotationBlock|null |
||
| 358 | */ |
||
| 359 | abstract protected function getPropertyAnnotationBlock(PhpProperty $property); |
||
| 360 | /** |
||
| 361 | * @param Method $methods |
||
| 362 | */ |
||
| 363 | abstract protected function getClassMethods(Method $methods); |
||
| 364 | /** |
||
| 365 | * @param PhpMethod $method |
||
| 366 | * @return PhpAnnotationBlock|null |
||
| 367 | */ |
||
| 368 | abstract protected function getMethodAnnotationBlock(PhpMethod $method); |
||
| 369 | /** |
||
| 370 | * @param PhpClass $class |
||
| 371 | */ |
||
| 372 | 156 | protected function defineStringMethod(PhpClass $class) |
|
| 378 | /** |
||
| 379 | * @return PhpAnnotationBlock |
||
| 380 | */ |
||
| 381 | 156 | protected function getToStringMethodAnnotationBlock() |
|
| 388 | /** |
||
| 389 | * @return PhpMethod |
||
| 390 | */ |
||
| 391 | 156 | protected function getToStringMethod() |
|
| 397 | /** |
||
| 398 | * @param StructAttributeModel $attribute |
||
| 399 | * @return StructAttributeModel |
||
| 400 | */ |
||
| 401 | 76 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
|
| 409 | /** |
||
| 410 | * @param StructAttributeModel $attribute |
||
| 411 | * @return StructModel|null |
||
| 412 | */ |
||
| 413 | 76 | protected function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 422 | /** |
||
| 423 | * @param StructAttributeModel $attribute |
||
| 424 | * @param bool $namespaced |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 60 | protected function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
|
| 445 | /** |
||
| 446 | * @param StructAttributeModel $attribute |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 76 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null) { |
|
| 453 | /** |
||
| 454 | * @param StructAttributeModel $attribute |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | 76 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null) { |
|
| 461 | /** |
||
| 462 | * @param StructAttributeModel $attribute |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | 56 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null) { |
|
| 469 | /** |
||
| 470 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 471 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 472 | * @param mixed $type |
||
| 473 | * @param mixed $fallback |
||
| 474 | * @return mixed |
||
| 475 | */ |
||
| 476 | 56 | public static function getValidType($type, $fallback = null) |
|
| 480 | /** |
||
| 481 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 482 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 483 | * @param mixed $type |
||
| 484 | * @param mixed $fallback |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | 4 | public static function getPhpType($type, $fallback = self::TYPE_STRING) |
|
| 491 | } |
||
| 492 |