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 | 196 | public function getFileDestination($withSrc = true) |
|
| 88 | { |
||
| 89 | 196 | 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 | 196 | public function getDestinationFolder($withSrc = true) |
|
| 96 | { |
||
| 97 | 196 | 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 | 176 | public function writeFile($withSrc = true) |
|
| 105 | { |
||
| 106 | 176 | 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 | 172 | GeneratorUtils::createDirectory($this->getFileDestination($withSrc)); |
|
| 110 | 129 | $this |
|
| 111 | 172 | ->defineNamespace() |
|
| 112 | 172 | ->defineUseStatement() |
|
| 113 | 172 | ->addAnnotationBlock() |
|
| 114 | 172 | ->addClassElement(); |
|
| 115 | 172 | return parent::writeFile(); |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * @return AbstractModelFile |
||
| 119 | */ |
||
| 120 | 172 | protected function addAnnotationBlock() |
|
| 127 | /** |
||
| 128 | * @param AbstractModel $model |
||
| 129 | * @return AbstractModelFile |
||
| 130 | */ |
||
| 131 | 220 | public function setModel(AbstractModel $model) |
|
| 137 | /** |
||
| 138 | * @return AbstractModel |
||
| 139 | */ |
||
| 140 | 224 | 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 | 160 | protected function definePackageAnnotations(PhpAnnotationBlock $block) |
|
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 160 | protected function getPackageName() |
|
| 180 | /** |
||
| 181 | * @param PhpAnnotationBlock $block |
||
| 182 | * @return AbstractModelFile |
||
| 183 | */ |
||
| 184 | 160 | protected function defineGeneralAnnotations(PhpAnnotationBlock $block) |
|
| 191 | /** |
||
| 192 | * @return PhpAnnotationBlock |
||
| 193 | */ |
||
| 194 | 160 | protected function getClassAnnotationBlock() |
|
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 160 | protected function getClassDeclarationLine() |
|
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 144 | protected function getClassDeclarationLineText() |
|
| 218 | /** |
||
| 219 | * @param PhpAnnotationBlock $block |
||
| 220 | * @param AbstractModel $model |
||
| 221 | * @return AbstractModelFile |
||
| 222 | */ |
||
| 223 | 160 | protected function defineModelAnnotationsFromWsdl(PhpAnnotationBlock $block, AbstractModel $model = null) |
|
| 228 | /** |
||
| 229 | * @param AbstractModel $model |
||
| 230 | * @return string[] |
||
| 231 | */ |
||
| 232 | protected function getValidMetaValues(AbstractModel $model) |
||
| 236 | /** |
||
| 237 | * @return AbstractModelFile |
||
| 238 | */ |
||
| 239 | 172 | protected function addClassElement() |
|
| 251 | /** |
||
| 252 | * @return AbstractModelFile |
||
| 253 | */ |
||
| 254 | 172 | protected function defineNamespace() |
|
| 263 | /** |
||
| 264 | * @return AbstractModelFile |
||
| 265 | */ |
||
| 266 | 172 | protected function defineUseStatement() |
|
| 275 | /** |
||
| 276 | * @param PhpClass $class |
||
| 277 | * @return AbstractModelFile |
||
| 278 | */ |
||
| 279 | 172 | protected function defineConstants(PhpClass $class) |
|
| 292 | /** |
||
| 293 | * @param PhpClass $class |
||
| 294 | * @return AbstractModelFile |
||
| 295 | */ |
||
| 296 | 172 | protected function defineProperties(PhpClass $class) |
|
| 309 | /** |
||
| 310 | * @param PhpClass $class |
||
| 311 | * @return AbstractModelFile |
||
| 312 | */ |
||
| 313 | 172 | protected function defineMethods(PhpClass $class) |
|
| 326 | /** |
||
| 327 | * @param Constant $constants |
||
| 328 | */ |
||
| 329 | abstract protected function getClassConstants(Constant $constants); |
||
| 330 | /** |
||
| 331 | * @param PhpConstant $constant |
||
| 332 | * @return PhpAnnotationBlock|null |
||
| 333 | */ |
||
| 334 | abstract protected function getConstantAnnotationBlock(PhpConstant $constant); |
||
| 335 | /** |
||
| 336 | * @param Property $properties |
||
| 337 | */ |
||
| 338 | abstract protected function getClassProperties(Property $properties); |
||
| 339 | /** |
||
| 340 | * @param PhpProperty $property |
||
| 341 | * @return PhpAnnotationBlock|null |
||
| 342 | */ |
||
| 343 | abstract protected function getPropertyAnnotationBlock(PhpProperty $property); |
||
| 344 | /** |
||
| 345 | * @param Method $methods |
||
| 346 | */ |
||
| 347 | abstract protected function getClassMethods(Method $methods); |
||
| 348 | /** |
||
| 349 | * @param PhpMethod $method |
||
| 350 | * @return PhpAnnotationBlock|null |
||
| 351 | */ |
||
| 352 | abstract protected function getMethodAnnotationBlock(PhpMethod $method); |
||
| 353 | /** |
||
| 354 | * @param PhpClass $class |
||
| 355 | */ |
||
| 356 | 160 | protected function defineStringMethod(PhpClass $class) |
|
| 362 | /** |
||
| 363 | * @return PhpAnnotationBlock |
||
| 364 | */ |
||
| 365 | 160 | protected function getToStringMethodAnnotationBlock() |
|
| 372 | /** |
||
| 373 | * @return PhpMethod |
||
| 374 | */ |
||
| 375 | 160 | protected function getToStringMethod() |
|
| 381 | /** |
||
| 382 | * @param StructAttributeModel $attribute |
||
| 383 | * @return StructAttributeModel |
||
| 384 | */ |
||
| 385 | 80 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
|
| 393 | /** |
||
| 394 | * @param StructAttributeModel $attribute |
||
| 395 | * @return StructModel|null |
||
| 396 | */ |
||
| 397 | 80 | protected function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 406 | /** |
||
| 407 | * @param StructAttributeModel $attribute |
||
| 408 | * @return StructModel|null |
||
| 409 | */ |
||
| 410 | 80 | protected function getRestrictionFromStructAttribute(StructAttributeModel $attribute = null) |
|
| 418 | /** |
||
| 419 | * @param StructAttributeModel $attribute |
||
| 420 | * @param bool $namespaced |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 80 | protected function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
|
| 441 | /** |
||
| 442 | * @param StructAttributeModel $attribute |
||
| 443 | * @param bool $returnArrayType |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | 80 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 451 | /** |
||
| 452 | * @param StructAttributeModel $attribute |
||
| 453 | * @param bool $returnArrayType |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 80 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 461 | /** |
||
| 462 | * @param StructAttributeModel $attribute |
||
| 463 | * @param string $returnArrayType |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | 80 | protected function useBrackets(StructAttributeModel $attribute, $returnArrayType = true) |
|
| 470 | /** |
||
| 471 | * @param StructAttributeModel $attribute |
||
| 472 | * @param bool $returnArrayType |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 80 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
| 480 | /** |
||
| 481 | * @param StructAttributeModel $attribute |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | 80 | protected function getStructAttributeTypeAsPhpType(StructAttributeModel $attribute = null) |
|
| 493 | /** |
||
| 494 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 495 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 496 | * @param mixed $type |
||
| 497 | * @param mixed $fallback |
||
| 498 | * @return mixed |
||
| 499 | */ |
||
| 500 | 80 | public static function getValidType($type, $fallback = null) |
|
| 504 | /** |
||
| 505 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
| 506 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
| 507 | * @param mixed $type |
||
| 508 | * @param mixed $fallback |
||
| 509 | * @return mixed |
||
| 510 | */ |
||
| 511 | 28 | public static function getPhpType($type, $fallback = self::TYPE_STRING) |
|
| 515 | } |
||
| 516 |