Total Complexity | 81 |
Total Lines | 468 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
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.
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 int Meta annotation length |
||
25 | */ |
||
26 | const ANNOTATION_META_LENGTH = 250; |
||
27 | /** |
||
28 | * @var int 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 TYPE_STRING = 'string'; |
||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | const TYPE_ARRAY = 'array'; |
||
75 | /** |
||
76 | * @var AbstractModel |
||
77 | */ |
||
78 | private $model; |
||
79 | /** |
||
80 | * @var Method |
||
81 | */ |
||
82 | protected $methods; |
||
83 | /** |
||
84 | * @param bool $withSrc |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getFileDestination($withSrc = true) |
||
88 | { |
||
89 | 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 | public function getDestinationFolder($withSrc = true) |
||
96 | { |
||
97 | $src = rtrim($this->generator->getOptionSrcDirname(), DIRECTORY_SEPARATOR); |
||
98 | return sprintf('%s%s', $this->getGenerator()->getOptionDestination(), (bool) $withSrc && !empty($src) ? $src . DIRECTORY_SEPARATOR : ''); |
||
99 | } |
||
100 | /** |
||
101 | * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile() |
||
102 | * @param bool $withSrc |
||
103 | * @return void |
||
104 | */ |
||
105 | public function writeFile($withSrc = true) |
||
106 | { |
||
107 | if (!$this->getModel()) { |
||
108 | throw new \InvalidArgumentException('You MUST define the model before being able to generate the file', __LINE__); |
||
109 | } |
||
110 | GeneratorUtils::createDirectory($this->getFileDestination($withSrc)); |
||
111 | $this->defineNamespace()->defineUseStatement()->addAnnotationBlock()->addClassElement(); |
||
112 | parent::writeFile(); |
||
113 | } |
||
114 | /** |
||
115 | * @return AbstractModelFile |
||
116 | */ |
||
117 | protected function addAnnotationBlock() |
||
118 | { |
||
119 | $this->getFile()->addAnnotationBlockElement($this->getClassAnnotationBlock()); |
||
120 | return $this; |
||
121 | } |
||
122 | /** |
||
123 | * @param AbstractModel $model |
||
124 | * @return AbstractModelFile |
||
125 | */ |
||
126 | public function setModel(AbstractModel $model) |
||
127 | { |
||
128 | $this->model = $model; |
||
129 | $this->getFile()->getMainElement()->setName($model->getPackagedName()); |
||
130 | return $this; |
||
131 | } |
||
132 | /** |
||
133 | * @return AbstractModel |
||
134 | */ |
||
135 | public function getModel() |
||
136 | { |
||
137 | return $this->model; |
||
138 | } |
||
139 | /** |
||
140 | * @param string $name |
||
141 | * @return StructModel|null |
||
142 | */ |
||
143 | protected function getModelByName($name) |
||
144 | { |
||
145 | return $this->getGenerator()->getStructByName($name); |
||
146 | } |
||
147 | /** |
||
148 | * @param PhpAnnotationBlock $block |
||
149 | * @return AbstractModelFile |
||
150 | */ |
||
151 | protected function definePackageAnnotations(PhpAnnotationBlock $block) |
||
152 | { |
||
153 | $packageName = $this->getPackageName(); |
||
154 | if (!empty($packageName)) { |
||
155 | $block->addChild(new PhpAnnotation(self::ANNOTATION_PACKAGE, $packageName)); |
||
156 | } |
||
157 | if (count($this->getModel()->getDocSubPackages()) > 0) { |
||
158 | $block->addChild(new PhpAnnotation(self::ANNOTATION_SUB_PACKAGE, implode(',', $this->getModel()->getDocSubPackages()))); |
||
159 | } |
||
160 | return $this; |
||
161 | } |
||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function getPackageName() |
||
166 | { |
||
167 | $packageName = ''; |
||
168 | if ($this->getGenerator()->getOptionPrefix() !== '') { |
||
169 | $packageName = $this->getGenerator()->getOptionPrefix(); |
||
170 | } elseif ($this->getGenerator()->getOptionSuffix() !== '') { |
||
171 | $packageName = $this->getGenerator()->getOptionSuffix(); |
||
172 | } |
||
173 | return $packageName; |
||
174 | } |
||
175 | /** |
||
176 | * @param PhpAnnotationBlock $block |
||
177 | * @return AbstractModelFile |
||
178 | */ |
||
179 | protected function defineGeneralAnnotations(PhpAnnotationBlock $block) |
||
180 | { |
||
181 | foreach ($this->getGenerator()->getOptionAddComments() as $tagName => $tagValue) { |
||
182 | $block->addChild(new PhpAnnotation($tagName, $tagValue)); |
||
183 | } |
||
184 | return $this; |
||
185 | } |
||
186 | /** |
||
187 | * @return PhpAnnotationBlock |
||
188 | */ |
||
189 | protected function getClassAnnotationBlock() |
||
190 | { |
||
191 | $block = new PhpAnnotationBlock(); |
||
192 | $block->addChild($this->getClassDeclarationLine()); |
||
193 | $this->defineModelAnnotationsFromWsdl($block)->definePackageAnnotations($block)->defineGeneralAnnotations($block); |
||
194 | return $block; |
||
195 | } |
||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getClassDeclarationLine() |
||
200 | { |
||
201 | return sprintf($this->getClassDeclarationLineText(), $this->getModel()->getName(), $this->getModel()->getContextualPart()); |
||
202 | } |
||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | protected function getClassDeclarationLineText() |
||
207 | { |
||
208 | return 'This class stands for %s %s'; |
||
209 | } |
||
210 | /** |
||
211 | * @param PhpAnnotationBlock $block |
||
212 | * @param AbstractModel $model |
||
213 | * @return AbstractModelFile |
||
214 | */ |
||
215 | protected function defineModelAnnotationsFromWsdl(PhpAnnotationBlock $block, AbstractModel $model = null) |
||
216 | { |
||
217 | FileUtils::defineModelAnnotationsFromWsdl($block, $model instanceof AbstractModel ? $model : $this->getModel()); |
||
218 | return $this; |
||
219 | } |
||
220 | /** |
||
221 | * @return AbstractModelFile |
||
222 | */ |
||
223 | protected function addClassElement() |
||
224 | { |
||
225 | $class = new PhpClass($this->getModel()->getPackagedName(), $this->getModel()->isAbstract(), $this->getModel()->getExtendsClassName() === '' ? null : $this->getModel()->getExtendsClassName()); |
||
226 | $this->defineConstants($class) |
||
227 | ->defineProperties($class) |
||
228 | ->defineMethods($class) |
||
229 | ->getFile() |
||
230 | ->addClassComponent($class); |
||
231 | return $this; |
||
232 | } |
||
233 | /** |
||
234 | * @return AbstractModelFile |
||
235 | */ |
||
236 | protected function defineNamespace() |
||
237 | { |
||
238 | if ($this->getModel()->getNamespace() !== '') { |
||
239 | $this->getFile()->setNamespace($this->getModel()->getNamespace()); |
||
240 | } |
||
241 | return $this; |
||
242 | } |
||
243 | /** |
||
244 | * @return AbstractModelFile |
||
245 | */ |
||
246 | protected function defineUseStatement() |
||
247 | { |
||
248 | if ($this->getModel()->getExtends() !== '') { |
||
249 | $this->getFile()->addUse($this->getModel()->getExtends(), null, true); |
||
250 | } |
||
251 | return $this; |
||
252 | } |
||
253 | /** |
||
254 | * @param PhpClass $class |
||
255 | * @return AbstractModelFile |
||
256 | */ |
||
257 | protected function defineConstants(PhpClass $class) |
||
258 | { |
||
259 | $constants = new Constant($this->getGenerator()); |
||
260 | $this->getClassConstants($constants); |
||
261 | foreach ($constants as $constant) { |
||
262 | $annotationBlock = $this->getConstantAnnotationBlock($constant); |
||
263 | if (!empty($annotationBlock)) { |
||
264 | $class->addAnnotationBlockElement($annotationBlock); |
||
265 | } |
||
266 | $class->addConstantElement($constant); |
||
267 | } |
||
268 | return $this; |
||
269 | } |
||
270 | /** |
||
271 | * @param PhpClass $class |
||
272 | * @return AbstractModelFile |
||
273 | */ |
||
274 | protected function defineProperties(PhpClass $class) |
||
275 | { |
||
276 | $properties = new Property($this->getGenerator()); |
||
277 | $this->getClassProperties($properties); |
||
278 | foreach ($properties as $property) { |
||
279 | $annotationBlock = $this->getPropertyAnnotationBlock($property); |
||
280 | if (!empty($annotationBlock)) { |
||
281 | $class->addAnnotationBlockElement($annotationBlock); |
||
282 | } |
||
283 | $class->addPropertyElement($property); |
||
284 | } |
||
285 | return $this; |
||
286 | } |
||
287 | /** |
||
288 | * @param PhpClass $class |
||
289 | * @return AbstractModelFile |
||
290 | */ |
||
291 | protected function defineMethods(PhpClass $class) |
||
292 | { |
||
293 | $this->methods = new Method($this->getGenerator()); |
||
294 | $this->fillClassMethods(); |
||
295 | foreach ($this->methods as $method) { |
||
296 | $annotationBlock = $this->getMethodAnnotationBlock($method); |
||
297 | if (!empty($annotationBlock)) { |
||
298 | $class->addAnnotationBlockElement($annotationBlock); |
||
299 | } |
||
300 | $class->addMethodElement($method); |
||
301 | } |
||
302 | return $this; |
||
303 | } |
||
304 | /** |
||
305 | * @param Constant $constants |
||
306 | */ |
||
307 | abstract protected function getClassConstants(Constant $constants); |
||
308 | /** |
||
309 | * @param PhpConstant $constant |
||
310 | * @return PhpAnnotationBlock|null |
||
311 | */ |
||
312 | abstract protected function getConstantAnnotationBlock(PhpConstant $constant); |
||
313 | /** |
||
314 | * @param Property $properties |
||
315 | */ |
||
316 | abstract protected function getClassProperties(Property $properties); |
||
317 | /** |
||
318 | * @param PhpProperty $property |
||
319 | * @return PhpAnnotationBlock|null |
||
320 | */ |
||
321 | abstract protected function getPropertyAnnotationBlock(PhpProperty $property); |
||
322 | /** |
||
323 | * This method is responsible for filling in the $methods property with appropriate methods for the current model |
||
324 | * @return void |
||
325 | */ |
||
326 | abstract protected function fillClassMethods(); |
||
327 | /** |
||
328 | * @param PhpMethod $method |
||
329 | * @return PhpAnnotationBlock|null |
||
330 | */ |
||
331 | abstract protected function getMethodAnnotationBlock(PhpMethod $method); |
||
332 | /** |
||
333 | * @param StructAttributeModel|null $attribute |
||
334 | * @return StructAttributeModel |
||
335 | */ |
||
336 | protected function getStructAttribute(StructAttributeModel $attribute = null) |
||
337 | { |
||
338 | $struct = $this->getModel(); |
||
339 | if (empty($attribute) && $struct instanceof StructModel && $struct->getAttributes()->count() === 1) { |
||
340 | $attribute = $struct->getAttributes()->offsetGet(0); |
||
341 | } |
||
342 | return $attribute; |
||
343 | } |
||
344 | /** |
||
345 | * @param StructAttributeModel|null $attribute |
||
346 | * @return StructModel|null |
||
347 | */ |
||
348 | public function getModelFromStructAttribute(StructAttributeModel $attribute = null) |
||
349 | { |
||
350 | return $this->getStructAttribute($attribute)->getTypeStruct(); |
||
351 | } |
||
352 | /** |
||
353 | * @param StructAttributeModel|null $attribute |
||
354 | * @return StructModel|null |
||
355 | */ |
||
356 | public function getRestrictionFromStructAttribute(StructAttributeModel $attribute = null) |
||
373 | } |
||
374 | /** |
||
375 | * @param StructAttributeModel|null $attribute |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function isAttributeAList(StructAttributeModel $attribute = null) |
||
381 | } |
||
382 | /** |
||
383 | * @param StructAttributeModel|null $attribute |
||
384 | * @param bool $namespaced |
||
385 | * @return string |
||
386 | */ |
||
387 | public function getStructAttributeType(StructAttributeModel $attribute = null, $namespaced = false) |
||
415 | } |
||
416 | /** |
||
417 | * @param StructAttributeModel|null $attribute |
||
418 | * @param bool $returnArrayType |
||
419 | * @return string |
||
420 | */ |
||
421 | protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
||
422 | { |
||
423 | $attribute = $this->getStructAttribute($attribute); |
||
424 | return sprintf('%s%s%s', $this->getStructAttributeTypeAsPhpType($attribute), $this->useBrackets($attribute, $returnArrayType) ? '[]' : '', $attribute->isRequired() ? '' : '|null'); |
||
425 | } |
||
426 | /** |
||
427 | * @param StructAttributeModel|null $attribute |
||
428 | * @param bool $returnArrayType |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute = null, $returnArrayType = true) |
||
432 | { |
||
433 | $attribute = $this->getStructAttribute($attribute); |
||
434 | return sprintf('%s%s', $this->getStructAttributeTypeAsPhpType($attribute), $this->useBrackets($attribute, $returnArrayType) ? '[]' : ''); |
||
435 | } |
||
436 | /** |
||
437 | * @param StructAttributeModel $attribute |
||
438 | * @param bool $returnArrayType |
||
439 | * @return bool |
||
440 | */ |
||
441 | protected function useBrackets(StructAttributeModel $attribute, $returnArrayType = true) |
||
442 | { |
||
443 | return $returnArrayType && ($attribute->isArray() || $this->isAttributeAList($attribute)); |
||
444 | } |
||
445 | /** |
||
446 | * @param StructAttributeModel|null $attribute |
||
447 | * @param bool $returnArrayType |
||
448 | * @return string |
||
449 | */ |
||
450 | protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, $returnArrayType = true) |
||
451 | { |
||
452 | $attribute = $this->getStructAttribute($attribute); |
||
453 | return ($returnArrayType && ($attribute->isArray() || $this->isAttributeAList($attribute))) ? self::TYPE_ARRAY : $this->getStructAttributeType($attribute, true); |
||
454 | } |
||
455 | /** |
||
456 | * @param StructAttributeModel|null $attribute |
||
457 | * @return string |
||
458 | */ |
||
459 | public function getStructAttributeTypeAsPhpType(StructAttributeModel $attribute = null) |
||
467 | } |
||
468 | /** |
||
469 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
470 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
471 | * @param mixed $type |
||
472 | * @param mixed $fallback |
||
473 | * @return mixed |
||
474 | */ |
||
475 | public static function getValidType($type, $xsdTypesPath = null, $fallback = null) |
||
478 | } |
||
479 | /** |
||
480 | * See http://php.net/manual/fr/language.oop5.typehinting.php for these cases |
||
481 | * Also see http://www.w3schools.com/schema/schema_dtypes_numeric.asp |
||
482 | * @param mixed $type |
||
483 | * @param mixed $fallback |
||
484 | * @return mixed |
||
485 | */ |
||
486 | public static function getPhpType($type, $xsdTypesPath = null, $fallback = self::TYPE_STRING) |
||
487 | { |
||
488 | return XsdTypes::instance($xsdTypesPath)->isXsd(str_replace('[]', '', $type)) ? XsdTypes::instance($xsdTypesPath)->phpType($type) : $fallback; |
||
489 | } |
||
490 | } |
||
491 |