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 | 200 | public function getFileDestination($withSrc = true) |
|
| 88 | { |
||
| 89 | 200 | return sprintf('%s%s%s', $this->getDestinationFolder($withSrc), $this->getModel()->getSubDirectory(), $this->getModel()->getSubDirectory() !== '' ? '/' : ''); |
|
| 90 | } |
||
| 91 | /** |
||
| 92 | * @param bool $withSrc |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 200 | public function getDestinationFolder($withSrc = true) |
|
| 96 | { |
||
| 97 | 200 | return sprintf('%s%s', $this->getGenerator()->getOptionDestination(), $withSrc === true ? self::SRC_FOLDER : ''); |
|
| 98 | } |
||
| 99 | /** |
||
| 100 | * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile() |
||
| 101 | * @param bool $withSrc |
||
| 102 | * @return int|bool |
||
| 103 | */ |
||
| 104 | 180 | public function writeFile($withSrc = true) |
|
| 105 | { |
||
| 106 | 180 | if (!$this->getModel() instanceof AbstractModel) { |
|
| 107 | 4 | throw new \InvalidArgumentException('You MUST define the model before begin able to generate the file', __LINE__); |
|
| 108 | } |
||
| 109 | 176 | GeneratorUtils::createDirectory($this->getFileDestination($withSrc)); |
|
| 110 | 132 | $this |
|
| 111 | 176 | ->defineNamespace() |
|
| 112 | 176 | ->defineUseStatement() |
|
| 113 | 176 | ->addAnnotationBlock() |
|
| 114 | 176 | ->addClassElement(); |
|
| 115 | 176 | return parent::writeFile(); |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * @return AbstractModelFile |
||
| 119 | */ |
||
| 120 | 176 | protected function addAnnotationBlock() |
|
| 127 | /** |
||
| 128 | * @param AbstractModel $model |
||
| 129 | * @return AbstractModelFile |
||
| 130 | */ |
||
| 131 | 224 | public function setModel(AbstractModel $model) |
|
| 137 | /** |
||
| 138 | * @return AbstractModel |
||
| 139 | */ |
||
| 140 | 228 | 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 | 164 | protected function definePackageAnnotations(PhpAnnotationBlock $block) |
|
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 164 | protected function getPackageName() |
|
| 180 | /** |
||
| 181 | * @param PhpAnnotationBlock $block |
||
| 182 | * @return AbstractModelFile |
||
| 183 | */ |
||
| 184 | 164 | protected function defineGeneralAnnotations(PhpAnnotationBlock $block) |
|
| 191 | /** |
||
| 192 | * @return PhpAnnotationBlock |
||
| 193 | */ |
||
| 194 | 164 | protected function getClassAnnotationBlock() |
|
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 164 | protected function getClassDeclarationLine() |
|
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 148 | protected function getClassDeclarationLineText() |
|
| 218 | /** |
||
| 219 | * @param PhpAnnotationBlock $block |
||
| 220 | * @param AbstractModel $model |
||
| 221 | * @return AbstractModelFile |
||
| 222 | */ |
||
| 223 | 164 | protected function defineModelAnnotationsFromWsdl(PhpAnnotationBlock $block, AbstractModel $model = null) |
|
| 228 | /** |
||
| 229 | * @return AbstractModelFile |
||
| 230 | */ |
||
| 231 | 176 | protected function addClassElement() |
|
| 243 | /** |
||
| 244 | * @return AbstractModelFile |
||
| 245 | */ |
||
| 246 | 176 | protected function defineNamespace() |
|
| 255 | /** |
||
| 256 | * @return AbstractModelFile |
||
| 257 | */ |
||
| 258 | 176 | protected function defineUseStatement() |
|
| 267 | /** |
||
| 268 | * @param PhpClass $class |
||
| 269 | * @return AbstractModelFile |
||
| 270 | */ |
||
| 271 | 176 | protected function defineConstants(PhpClass $class) |
|
| 284 | /** |
||
| 285 | * @param PhpClass $class |
||
| 286 | * @return AbstractModelFile |
||
| 287 | */ |
||
| 288 | 176 | protected function defineProperties(PhpClass $class) |
|
| 301 | /** |
||
| 302 | * @param PhpClass $class |
||
| 303 | * @return AbstractModelFile |
||
| 304 | */ |
||
| 305 | 176 | protected function defineMethods(PhpClass $class) |
|
| 318 | /** |
||
| 319 | * @param Constant $constants |
||
| 320 | */ |
||
| 321 | abstract protected function getClassConstants(Constant $constants); |
||
| 322 | /** |
||
| 323 | * @param PhpConstant $constant |
||
| 324 | * @return PhpAnnotationBlock|null |
||
| 325 | */ |
||
| 326 | abstract protected function getConstantAnnotationBlock(PhpConstant $constant); |
||
| 327 | /** |
||
| 328 | * @param Property $properties |
||
| 329 | */ |
||
| 330 | abstract protected function getClassProperties(Property $properties); |
||
| 331 | /** |
||
| 332 | * @param PhpProperty $property |
||
| 333 | * @return PhpAnnotationBlock|null |
||
| 334 | */ |
||
| 335 | abstract protected function getPropertyAnnotationBlock(PhpProperty $property); |
||
| 336 | /** |
||
| 337 | * @param Method $methods |
||
| 338 | */ |
||
| 339 | abstract protected function getClassMethods(Method $methods); |
||
| 340 | /** |
||
| 341 | * @param PhpMethod $method |
||
| 342 | * @return PhpAnnotationBlock|null |
||
| 343 | */ |
||
| 344 | abstract protected function getMethodAnnotationBlock(PhpMethod $method); |
||
| 345 | /** |
||
| 346 | * @param PhpClass $class |
||
| 347 | */ |
||
| 348 | 164 | protected function defineStringMethod(PhpClass $class) |
|
| 354 | /** |
||
| 355 | * @return PhpAnnotationBlock |
||
| 356 | */ |
||
| 357 | 164 | protected function getToStringMethodAnnotationBlock() |
|
| 364 | /** |
||
| 365 | * @return PhpMethod |
||
| 366 | */ |
||
| 367 | 164 | protected function getToStringMethod() |
|
| 373 | /** |
||
| 374 | * @param StructAttributeModel $attribute |
||
| 375 | * @return StructAttributeModel |
||
| 376 | */ |
||
| 377 | 84 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
|
| 385 | /** |
||
| 386 | * @param StructAttributeModel $attribute |
||
| 387 | * @return StructModel|null |
||
| 388 | */ |
||
| 389 | 84 | protected function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 398 | /** |
||
| 399 | * @param StructAttributeModel $attribute |
||
| 400 | * @return StructModel|null |
||
| 401 | */ |
||
| 402 | 84 | protected function getRestrictionFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 410 | /** |
||
| 411 | * @param StructAttributeModel $attribute |
||
| 412 | * @param bool $namespaced |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 84 | protected function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
|
| 440 | /** |
||
| 441 | * @param StructAttributeModel $attribute |
||
| 442 | * @param bool $returnArrayType |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 84 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 450 | /** |
||
| 451 | * @param StructAttributeModel $attribute |
||
| 452 | * @param bool $returnArrayType |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | 84 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 460 | /** |
||
| 461 | * @param StructAttributeModel $attribute |
||
| 462 | * @param string $returnArrayType |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | 84 | protected function useBrackets(StructAttributeModel $attribute, $returnArrayType = true) |
|
| 469 | /** |
||
| 470 | * @param StructAttributeModel $attribute |
||
| 471 | * @param bool $returnArrayType |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 84 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 479 | /** |
||
| 480 | * @param StructAttributeModel $attribute |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | 84 | protected function getStructAttributeTypeAsPhpType(StructAttributeModel $attribute = null) |
|
| 492 | /** |
||
| 493 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 494 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 495 | * @param mixed $type |
||
| 496 | * @param mixed $fallback |
||
| 497 | * @return mixed |
||
| 498 | */ |
||
| 499 | 84 | public static function getValidType($type, $fallback = null) |
|
| 503 | /** |
||
| 504 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 505 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 506 | * @param mixed $type |
||
| 507 | * @param mixed $fallback |
||
| 508 | * @return mixed |
||
| 509 | */ |
||
| 510 | 68 | public static function getPhpType($type, $fallback = self::TYPE_STRING) |
|
| 514 | } |
||
| 515 |