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) |
|
88 | { |
||
89 | 192 | 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 | 192 | public function getDestinationFolder($withSrc = true) |
|
96 | { |
||
97 | 192 | 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 | 172 | public function writeFile($withSrc = true) |
|
105 | { |
||
106 | 172 | 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 | 168 | GeneratorUtils::createDirectory($this->getFileDestination($withSrc)); |
|
110 | 126 | $this |
|
111 | 168 | ->defineNamespace() |
|
112 | 168 | ->defineUseStatement() |
|
113 | 168 | ->addAnnotationBlock() |
|
114 | 168 | ->addClassElement(); |
|
115 | 168 | return parent::writeFile(); |
|
116 | } |
||
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() |
|
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | 156 | protected function getClassDeclarationLine() |
|
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | 140 | protected function getClassDeclarationLineText() |
|
218 | /** |
||
219 | * @param PhpAnnotationBlock $block |
||
220 | * @param AbstractModel $model |
||
221 | * @return AbstractModelFile |
||
222 | */ |
||
223 | 156 | 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 | 168 | protected function addClassElement() |
|
251 | /** |
||
252 | * @return AbstractModelFile |
||
253 | */ |
||
254 | 168 | protected function defineNamespace() |
|
263 | /** |
||
264 | * @return AbstractModelFile |
||
265 | */ |
||
266 | 168 | protected function defineUseStatement() |
|
275 | /** |
||
276 | * @param PhpClass $class |
||
277 | * @return AbstractModelFile |
||
278 | */ |
||
279 | 168 | protected function defineConstants(PhpClass $class) |
|
292 | /** |
||
293 | * @param PhpClass $class |
||
294 | * @return AbstractModelFile |
||
295 | */ |
||
296 | 168 | protected function defineProperties(PhpClass $class) |
|
309 | /** |
||
310 | * @param PhpClass $class |
||
311 | * @return AbstractModelFile |
||
312 | */ |
||
313 | 168 | 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 | 156 | protected function defineStringMethod(PhpClass $class) |
|
362 | /** |
||
363 | * @return PhpAnnotationBlock |
||
364 | */ |
||
365 | 156 | protected function getToStringMethodAnnotationBlock() |
|
372 | /** |
||
373 | * @return PhpMethod |
||
374 | */ |
||
375 | 156 | protected function getToStringMethod() |
|
381 | /** |
||
382 | * @param StructAttributeModel $attribute |
||
383 | * @return StructAttributeModel |
||
384 | */ |
||
385 | 76 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
|
393 | /** |
||
394 | * @param StructAttributeModel $attribute |
||
395 | * @return StructModel|null |
||
396 | */ |
||
397 | 76 | protected function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
|
406 | /** |
||
407 | * @param StructAttributeModel $attribute |
||
408 | * @return StructModel|null |
||
409 | */ |
||
410 | 76 | protected function getRestrictionFromStructAttribute(StructAttributeModel $attribute = null) |
|
418 | /** |
||
419 | * @param StructAttributeModel $attribute |
||
420 | * @param bool $namespaced |
||
421 | * @return string |
||
422 | */ |
||
423 | 76 | protected function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
|
441 | /** |
||
442 | * @param StructAttributeModel $attribute |
||
443 | * @param bool $returnArrayType |
||
444 | * @return string |
||
445 | */ |
||
446 | 76 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
451 | /** |
||
452 | * @param StructAttributeModel $attribute |
||
453 | * @param bool $returnArrayType |
||
454 | * @return string |
||
455 | */ |
||
456 | 76 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
461 | /** |
||
462 | * @param StructAttributeModel $attribute |
||
463 | * @param string $returnArrayType |
||
464 | * @return bool |
||
465 | */ |
||
466 | 76 | protected function useBrackets(StructAttributeModel $attribute, $returnArrayType = true) |
|
470 | /** |
||
471 | * @param StructAttributeModel $attribute |
||
472 | * @param bool $returnArrayType |
||
473 | * @return string |
||
474 | */ |
||
475 | 76 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, $returnArrayType = true) |
|
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 | 76 | public static function getValidType($type, $fallback = null) |
|
491 | /** |
||
492 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
493 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
494 | * @param mixed $type |
||
495 | * @param mixed $fallback |
||
496 | * @return mixed |
||
497 | */ |
||
498 | 24 | public static function getPhpType($type, $fallback = self::TYPE_STRING) |
|
502 | } |
||
503 |