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 string Meta annotation length |
||
| 25 | */ |
||
| 26 | const ANNOTATION_META_LENGTH = '250'; |
||
| 27 | /** |
||
| 28 | * @var string Long annotation string |
||
| 29 | */ |
||
| 30 | const ANNOTATION_LONG_LENGTH = '1000'; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const ANNOTATION_PACKAGE = 'package'; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const ANNOTATION_SUB_PACKAGE = 'subpackage'; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const ANNOTATION_RETURN = 'return'; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const ANNOTATION_USES = 'uses'; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const ANNOTATION_PARAM = 'param'; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | const ANNOTATION_VAR = 'var'; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const ANNOTATION_SEE = 'see'; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | const ANNOTATION_THROWS = 'throws'; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | const METHOD_CONSTRUCT = '__construct'; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | const METHOD_SET_STATE = '__set_state'; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | const TYPE_STRING = 'string'; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | const TYPE_ARRAY = 'array'; |
||
| 79 | /** |
||
| 80 | * @var AbstractModel |
||
| 81 | */ |
||
| 82 | protected $model; |
||
| 83 | /** |
||
| 84 | * @param bool $withSrc |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 330 | public function getFileDestination($withSrc = true) |
|
| 91 | /** |
||
| 92 | * @param bool $withSrc |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 330 | public function getDestinationFolder($withSrc = true) |
|
| 100 | /** |
||
| 101 | * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile() |
||
| 102 | * @param bool $withSrc |
||
| 103 | * @return int|bool |
||
| 104 | */ |
||
| 105 | 305 | public function writeFile($withSrc = true) |
|
| 114 | /** |
||
| 115 | * @return AbstractModelFile |
||
| 116 | */ |
||
| 117 | 300 | protected function addAnnotationBlock() |
|
| 122 | /** |
||
| 123 | * @param AbstractModel $model |
||
| 124 | * @return AbstractModelFile |
||
| 125 | */ |
||
| 126 | 360 | public function setModel(AbstractModel $model) |
|
| 132 | /** |
||
| 133 | * @return AbstractModel |
||
| 134 | */ |
||
| 135 | 405 | public function getModel() |
|
| 139 | /** |
||
| 140 | * @param string $name |
||
| 141 | * @return StructModel|null |
||
| 142 | */ |
||
| 143 | 30 | protected function getModelByName($name) |
|
| 147 | /** |
||
| 148 | * @param PhpAnnotationBlock $block |
||
| 149 | * @return AbstractModelFile |
||
| 150 | */ |
||
| 151 | 275 | protected function definePackageAnnotations(PhpAnnotationBlock $block) |
|
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | 275 | protected function getPackageName() |
|
| 175 | /** |
||
| 176 | * @param PhpAnnotationBlock $block |
||
| 177 | * @return AbstractModelFile |
||
| 178 | */ |
||
| 179 | 275 | protected function defineGeneralAnnotations(PhpAnnotationBlock $block) |
|
| 186 | /** |
||
| 187 | * @return PhpAnnotationBlock |
||
| 188 | */ |
||
| 189 | 275 | protected function getClassAnnotationBlock() |
|
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 275 | protected function getClassDeclarationLine() |
|
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 255 | protected function getClassDeclarationLineText() |
|
| 210 | /** |
||
| 211 | * @param PhpAnnotationBlock $block |
||
| 212 | * @param AbstractModel $model |
||
| 213 | * @return AbstractModelFile |
||
| 214 | */ |
||
| 215 | 275 | protected function defineModelAnnotationsFromWsdl(PhpAnnotationBlock $block, AbstractModel $model = null) |
|
| 220 | /** |
||
| 221 | * @return AbstractModelFile |
||
| 222 | */ |
||
| 223 | 300 | protected function addClassElement() |
|
| 234 | /** |
||
| 235 | * @return AbstractModelFile |
||
| 236 | */ |
||
| 237 | 300 | protected function defineNamespace() |
|
| 244 | /** |
||
| 245 | * @return AbstractModelFile |
||
| 246 | */ |
||
| 247 | 300 | protected function defineUseStatement() |
|
| 254 | /** |
||
| 255 | * @param PhpClass $class |
||
| 256 | * @return AbstractModelFile |
||
| 257 | */ |
||
| 258 | 300 | protected function defineConstants(PhpClass $class) |
|
| 271 | /** |
||
| 272 | * @param PhpClass $class |
||
| 273 | * @return AbstractModelFile |
||
| 274 | */ |
||
| 275 | 300 | protected function defineProperties(PhpClass $class) |
|
| 288 | /** |
||
| 289 | * @param PhpClass $class |
||
| 290 | * @return AbstractModelFile |
||
| 291 | */ |
||
| 292 | 300 | protected function defineMethods(PhpClass $class) |
|
| 305 | /** |
||
| 306 | * @param Constant $constants |
||
| 307 | */ |
||
| 308 | abstract protected function getClassConstants(Constant $constants); |
||
| 309 | /** |
||
| 310 | * @param PhpConstant $constant |
||
| 311 | * @return PhpAnnotationBlock|null |
||
| 312 | */ |
||
| 313 | abstract protected function getConstantAnnotationBlock(PhpConstant $constant); |
||
| 314 | /** |
||
| 315 | * @param Property $properties |
||
| 316 | */ |
||
| 317 | abstract protected function getClassProperties(Property $properties); |
||
| 318 | /** |
||
| 319 | * @param PhpProperty $property |
||
| 320 | * @return PhpAnnotationBlock|null |
||
| 321 | */ |
||
| 322 | abstract protected function getPropertyAnnotationBlock(PhpProperty $property); |
||
| 323 | /** |
||
| 324 | * @param Method $methods |
||
| 325 | */ |
||
| 326 | abstract protected function getClassMethods(Method $methods); |
||
| 327 | /** |
||
| 328 | * @param PhpMethod $method |
||
| 329 | * @return PhpAnnotationBlock|null |
||
| 330 | */ |
||
| 331 | abstract protected function getMethodAnnotationBlock(PhpMethod $method); |
||
| 332 | /** |
||
| 333 | * @param PhpClass $class |
||
| 334 | * @return AbstractModelFile |
||
| 335 | */ |
||
| 336 | 275 | protected function defineStringMethod(PhpClass $class) |
|
| 342 | /** |
||
| 343 | * @return PhpAnnotationBlock |
||
| 344 | */ |
||
| 345 | 275 | protected function getToStringMethodAnnotationBlock() |
|
| 352 | /** |
||
| 353 | * @return PhpMethod |
||
| 354 | */ |
||
| 355 | 275 | protected function getToStringMethod() |
|
| 361 | /** |
||
| 362 | * @param StructAttributeModel $attribute |
||
| 363 | * @return StructAttributeModel |
||
| 364 | */ |
||
| 365 | 225 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
|
| 373 | /** |
||
| 374 | * @param StructAttributeModel $attribute |
||
| 375 | * @return StructModel|null |
||
| 376 | */ |
||
| 377 | 225 | public function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 386 | /** |
||
| 387 | * @param StructAttributeModel $attribute |
||
| 388 | * @return StructModel|null |
||
| 389 | */ |
||
| 390 | 185 | public function getRestrictionFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 398 | /** |
||
| 399 | * @param StructAttributeModel $attribute |
||
| 400 | * @param bool $namespaced |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | 225 | public function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
|
| 432 | /** |
||
| 433 | * @param StructAttributeModel $attribute |
||
| 434 | * @param bool $returnArrayType |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 155 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 442 | /** |
||
| 443 | * @param StructAttributeModel $attribute |
||
| 444 | * @param bool $returnArrayType |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 155 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 452 | /** |
||
| 453 | * @param StructAttributeModel $attribute |
||
| 454 | * @param bool $returnArrayType |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | 155 | protected function useBrackets(StructAttributeModel $attribute, $returnArrayType = true) |
|
| 461 | /** |
||
| 462 | * @param StructAttributeModel $attribute |
||
| 463 | * @param bool $returnArrayType |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | 155 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 471 | /** |
||
| 472 | * @param StructAttributeModel $attribute |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 185 | public function getStructAttributeTypeAsPhpType(StructAttributeModel $attribute = null) |
|
| 484 | /** |
||
| 485 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 486 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 487 | * @param mixed $type |
||
| 488 | * @param mixed $fallback |
||
| 489 | * @return mixed |
||
| 490 | */ |
||
| 491 | 185 | public static function getValidType($type, $fallback = null) |
|
| 495 | /** |
||
| 496 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 497 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 498 | * @param mixed $type |
||
| 499 | * @param mixed $fallback |
||
| 500 | * @return mixed |
||
| 501 | */ |
||
| 502 | 180 | public static function getPhpType($type, $fallback = self::TYPE_STRING) |
|
| 506 | } |
||
| 507 |