Total Complexity | 44 |
Total Lines | 488 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SchemaReaderLoadAbstraction 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 SchemaReaderLoadAbstraction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class SchemaReaderLoadAbstraction extends SchemaReaderFillAbstraction |
||
39 | { |
||
40 | /** |
||
41 | * @return Closure |
||
42 | */ |
||
43 | protected function loadAttributeGroup(Schema $schema, DOMElement $node) |
||
44 | { |
||
45 | return AttributeGroup::loadAttributeGroup($this, $schema, $node); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param bool $attributeDef |
||
50 | * |
||
51 | * @return Closure |
||
52 | */ |
||
53 | protected function loadAttributeOrElementDef( |
||
54 | Schema $schema, |
||
55 | DOMElement $node, |
||
56 | $attributeDef |
||
57 | ) { |
||
58 | $name = $node->getAttribute('name'); |
||
59 | if ($attributeDef) { |
||
60 | $attribute = new AttributeDef($schema, $name); |
||
61 | $schema->addAttribute($attribute); |
||
62 | } else { |
||
63 | $attribute = new ElementDef($schema, $name); |
||
64 | $schema->addElement($attribute); |
||
65 | } |
||
66 | |||
67 | |||
68 | return function () use ($attribute, $node) { |
||
69 | $this->fillItem($attribute, $node); |
||
70 | }; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return Closure |
||
75 | */ |
||
76 | protected function loadAttributeDef(Schema $schema, DOMElement $node) |
||
77 | { |
||
78 | return $this->loadAttributeOrElementDef($schema, $node, true); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param int|null $max |
||
83 | * |
||
84 | * @return int|null |
||
85 | */ |
||
86 | protected static function loadSequenceNormaliseMax(DOMElement $node, $max) |
||
87 | { |
||
88 | return |
||
89 | ( |
||
90 | (is_int($max) && (bool) $max) || |
||
91 | $node->getAttribute("maxOccurs") == "unbounded" || |
||
92 | $node->getAttribute("maxOccurs") > 1 |
||
93 | ) |
||
94 | ? 2 |
||
95 | : null; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param int|null $max |
||
100 | */ |
||
101 | protected function loadSequence(ElementContainer $elementContainer, DOMElement $node, $max = null) |
||
102 | { |
||
103 | $max = static::loadSequenceNormaliseMax($node, $max); |
||
104 | |||
105 | foreach ($node->childNodes as $childNode) { |
||
106 | if ($childNode instanceof DOMElement) { |
||
107 | $this->loadSequenceChildNode( |
||
108 | $elementContainer, |
||
109 | $node, |
||
110 | $childNode, |
||
111 | $max |
||
112 | ); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param int|null $max |
||
119 | */ |
||
120 | protected function loadSequenceChildNode( |
||
121 | ElementContainer $elementContainer, |
||
122 | DOMElement $node, |
||
123 | DOMElement $childNode, |
||
124 | $max |
||
125 | ) { |
||
126 | $commonMethods = [ |
||
127 | [ |
||
128 | ['sequence', 'choice', 'all'], |
||
129 | [$this, 'loadSequenceChildNodeLoadSequence'], |
||
130 | [ |
||
131 | $elementContainer, |
||
132 | $childNode, |
||
133 | $max, |
||
134 | ], |
||
135 | ], |
||
136 | ]; |
||
137 | $methods = [ |
||
138 | 'element' => [ |
||
139 | [$this, 'loadSequenceChildNodeLoadElement'], |
||
140 | [ |
||
141 | $elementContainer, |
||
142 | $node, |
||
143 | $childNode, |
||
144 | $max |
||
145 | ] |
||
146 | ], |
||
147 | 'group' => [ |
||
148 | [$this, 'loadSequenceChildNodeLoadGroup'], |
||
149 | [ |
||
150 | $elementContainer, |
||
151 | $node, |
||
152 | $childNode |
||
153 | ] |
||
154 | ], |
||
155 | ]; |
||
156 | |||
157 | $this->maybeCallCallableWithArgs($childNode, $commonMethods, $methods); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param int|null $max |
||
162 | */ |
||
163 | protected function loadSequenceChildNodeLoadSequence( |
||
164 | ElementContainer $elementContainer, |
||
165 | DOMElement $childNode, |
||
166 | $max |
||
167 | ) { |
||
168 | $this->loadSequence($elementContainer, $childNode, $max); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param int|null $max |
||
173 | */ |
||
174 | protected function loadSequenceChildNodeLoadElement( |
||
175 | ElementContainer $elementContainer, |
||
176 | DOMElement $node, |
||
177 | DOMElement $childNode, |
||
178 | $max |
||
179 | ) { |
||
180 | if ($childNode->hasAttribute("ref")) { |
||
181 | /** |
||
182 | * @var ElementDef $referencedElement |
||
183 | */ |
||
184 | $referencedElement = $this->findSomething('findElement', $elementContainer->getSchema(), $node, $childNode->getAttribute("ref")); |
||
185 | $element = ElementRef::loadElementRef( |
||
186 | $referencedElement, |
||
187 | $childNode |
||
188 | ); |
||
189 | } else { |
||
190 | $element = Element::loadElement( |
||
191 | $this, |
||
192 | $elementContainer->getSchema(), |
||
193 | $childNode |
||
194 | ); |
||
195 | } |
||
196 | if (is_int($max) && (bool) $max) { |
||
197 | $element->setMax($max); |
||
198 | } |
||
199 | $elementContainer->addElement($element); |
||
200 | } |
||
201 | |||
202 | protected function loadSequenceChildNodeLoadGroup( |
||
203 | ElementContainer $elementContainer, |
||
204 | DOMElement $node, |
||
205 | DOMElement $childNode |
||
206 | ) { |
||
207 | $this->addGroupAsElement( |
||
208 | $elementContainer->getSchema(), |
||
209 | $node, |
||
210 | $childNode, |
||
211 | $elementContainer |
||
212 | ); |
||
213 | } |
||
214 | |||
215 | protected function addGroupAsElement( |
||
216 | Schema $schema, |
||
217 | DOMElement $node, |
||
218 | DOMElement $childNode, |
||
219 | ElementContainer $elementContainer |
||
220 | ) { |
||
221 | /** |
||
222 | * @var Group $referencedGroup |
||
223 | */ |
||
224 | $referencedGroup = $this->findSomething( |
||
225 | 'findGroup', |
||
226 | $schema, |
||
227 | $node, |
||
228 | $childNode->getAttribute("ref") |
||
229 | ); |
||
230 | |||
231 | $group = GroupRef::loadGroupRef($referencedGroup, $childNode); |
||
232 | $elementContainer->addElement($group); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return Closure |
||
237 | */ |
||
238 | protected function loadGroup(Schema $schema, DOMElement $node) |
||
239 | { |
||
240 | return Group::loadGroup($this, $schema, $node); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return BaseComplexType |
||
245 | */ |
||
246 | protected function loadComplexTypeBeforeCallbackCallback( |
||
247 | Schema $schema, |
||
248 | DOMElement $node |
||
249 | ) { |
||
250 | $isSimple = false; |
||
251 | |||
252 | foreach ($node->childNodes as $childNode) { |
||
253 | if ($childNode->localName === "simpleContent") { |
||
254 | $isSimple = true; |
||
255 | break; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | $type = $isSimple ? new ComplexTypeSimpleContent($schema, $node->getAttribute("name")) : new ComplexType($schema, $node->getAttribute("name")); |
||
260 | |||
261 | $type->setDoc(static::getDocumentation($node)); |
||
262 | if ($node->getAttribute("name")) { |
||
263 | $schema->addType($type); |
||
264 | } |
||
265 | |||
266 | return $type; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param Closure|null $callback |
||
271 | * |
||
272 | * @return Closure |
||
273 | */ |
||
274 | protected function loadComplexType(Schema $schema, DOMElement $node, $callback = null) |
||
275 | { |
||
276 | $type = $this->loadComplexTypeBeforeCallbackCallback($schema, $node); |
||
277 | |||
278 | return $this->makeCallbackCallback( |
||
279 | $type, |
||
280 | $node, |
||
281 | function ( |
||
282 | DOMElement $node, |
||
283 | DOMElement $childNode |
||
284 | ) use( |
||
285 | $schema, |
||
286 | $type |
||
287 | ) { |
||
288 | $this->loadComplexTypeFromChildNode( |
||
289 | $type, |
||
290 | $node, |
||
291 | $childNode, |
||
292 | $schema |
||
293 | ); |
||
294 | }, |
||
295 | $callback |
||
296 | ); |
||
297 | } |
||
298 | |||
299 | protected function loadComplexTypeFromChildNode( |
||
300 | BaseComplexType $type, |
||
301 | DOMElement $node, |
||
302 | DOMElement $childNode, |
||
303 | Schema $schema |
||
304 | ) { |
||
305 | $commonMethods = [ |
||
306 | [ |
||
307 | ['sequence', 'choice', 'all'], |
||
308 | [$this, 'maybeLoadSequenceFromElementContainer'], |
||
309 | [ |
||
310 | $type, |
||
311 | $childNode, |
||
312 | ], |
||
313 | ], |
||
314 | ]; |
||
315 | $methods = [ |
||
316 | 'attribute' => [ |
||
317 | [$type, 'addAttributeFromAttributeOrRef'], |
||
318 | [ |
||
319 | $this, |
||
320 | $childNode, |
||
321 | $schema, |
||
322 | $node |
||
323 | ] |
||
324 | ], |
||
325 | 'attributeGroup' => [ |
||
326 | (AttributeGroup::class . '::findSomethingLikeThis'), |
||
327 | [ |
||
328 | $this, |
||
329 | $schema, |
||
330 | $node, |
||
331 | $childNode, |
||
332 | $type |
||
333 | ] |
||
334 | ], |
||
335 | ]; |
||
336 | if ( |
||
337 | $type instanceof ComplexType |
||
338 | ) { |
||
339 | $methods['group'] = [ |
||
340 | [$this, 'addGroupAsElement'], |
||
341 | [ |
||
342 | $schema, |
||
343 | $node, |
||
344 | $childNode, |
||
345 | $type |
||
346 | ] |
||
347 | ]; |
||
348 | } |
||
349 | |||
350 | $this->maybeCallCallableWithArgs($childNode, $commonMethods, $methods); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @param Closure|null $callback |
||
355 | * |
||
356 | * @return Closure |
||
357 | */ |
||
358 | protected function loadSimpleType(Schema $schema, DOMElement $node, $callback = null) |
||
359 | { |
||
360 | $type = new SimpleType($schema, $node->getAttribute("name")); |
||
361 | $type->setDoc(static::getDocumentation($node)); |
||
362 | if ($node->getAttribute("name")) { |
||
363 | $schema->addType($type); |
||
364 | } |
||
365 | |||
366 | static $methods = [ |
||
367 | 'union' => 'loadUnion', |
||
368 | 'list' => 'loadList', |
||
369 | ]; |
||
370 | |||
371 | return $this->makeCallbackCallback( |
||
372 | $type, |
||
373 | $node, |
||
374 | function ( |
||
375 | DOMElement $node, |
||
376 | DOMElement $childNode |
||
377 | ) use ( |
||
378 | $methods, |
||
379 | $type |
||
380 | ) { |
||
381 | $this->maybeCallMethod( |
||
382 | $methods, |
||
383 | $childNode->localName, |
||
384 | $childNode, |
||
385 | $type, |
||
386 | $childNode |
||
387 | ); |
||
388 | }, |
||
389 | $callback |
||
390 | ); |
||
391 | } |
||
392 | |||
393 | protected function loadList(SimpleType $type, DOMElement $node) |
||
394 | { |
||
395 | if ($node->hasAttribute("itemType")) { |
||
396 | /** |
||
397 | * @var SimpleType $listType |
||
398 | */ |
||
399 | $listType = $this->findSomeType($type, $node, 'itemType'); |
||
400 | $type->setList($listType); |
||
401 | } else { |
||
402 | $addCallback = function (SimpleType $list) use ($type) { |
||
403 | $type->setList($list); |
||
404 | }; |
||
405 | |||
406 | Type::loadTypeWithCallbackOnChildNodes( |
||
407 | $this, |
||
408 | $type->getSchema(), |
||
409 | $node, |
||
410 | $addCallback |
||
411 | ); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | protected function loadUnion(SimpleType $type, DOMElement $node) |
||
416 | { |
||
417 | if ($node->hasAttribute("memberTypes")) { |
||
418 | $types = preg_split('/\s+/', $node->getAttribute("memberTypes")); |
||
419 | foreach ($types as $typeName) { |
||
420 | /** |
||
421 | * @var SimpleType $unionType |
||
422 | */ |
||
423 | $unionType = $this->findSomeTypeFromAttribute( |
||
424 | $type, |
||
425 | $node, |
||
426 | $typeName |
||
427 | ); |
||
428 | $type->addUnion($unionType); |
||
429 | } |
||
430 | } |
||
431 | $addCallback = function (SimpleType $unType) use ($type) { |
||
432 | $type->addUnion($unType); |
||
433 | }; |
||
434 | |||
435 | Type::loadTypeWithCallbackOnChildNodes( |
||
436 | $this, |
||
437 | $type->getSchema(), |
||
438 | $node, |
||
439 | $addCallback |
||
440 | ); |
||
441 | } |
||
442 | |||
443 | protected function loadExtensionChildNode( |
||
444 | BaseComplexType $type, |
||
445 | DOMElement $node, |
||
446 | DOMElement $childNode |
||
447 | ) { |
||
448 | $commonMethods = [ |
||
449 | [ |
||
450 | ['sequence', 'choice', 'all'], |
||
451 | [$this, 'maybeLoadSequenceFromElementContainer'], |
||
452 | [ |
||
453 | $type, |
||
454 | $childNode, |
||
455 | ], |
||
456 | ], |
||
457 | ]; |
||
458 | $methods = [ |
||
459 | 'attribute' => [ |
||
460 | [$type, 'addAttributeFromAttributeOrRef'], |
||
461 | [ |
||
462 | $this, |
||
463 | $childNode, |
||
464 | $type->getSchema(), |
||
465 | $node |
||
466 | ] |
||
467 | ], |
||
468 | 'attributeGroup' => [ |
||
469 | (AttributeGroup::class . '::findSomethingLikeThis'), |
||
470 | [ |
||
471 | $this, |
||
472 | $type->getSchema(), |
||
473 | $node, |
||
474 | $childNode, |
||
475 | $type |
||
476 | ] |
||
477 | ], |
||
478 | ]; |
||
479 | |||
480 | $this->maybeCallCallableWithArgs($childNode, $commonMethods, $methods); |
||
481 | } |
||
482 | |||
483 | protected function loadExtension(BaseComplexType $type, DOMElement $node) |
||
484 | { |
||
485 | $extension = new Extension(); |
||
486 | $type->setExtension($extension); |
||
487 | |||
488 | if ($node->hasAttribute("base")) { |
||
489 | $this->findAndSetSomeBase( |
||
490 | $type, |
||
491 | $extension, |
||
492 | $node |
||
493 | ); |
||
494 | } |
||
495 | |||
496 | $this->loadExtensionChildNodes($type, $node->childNodes, $node); |
||
497 | } |
||
498 | |||
499 | protected function loadExtensionChildNodes( |
||
510 | ); |
||
511 | } |
||
512 | } |
||
513 | } |
||
514 | |||
515 | protected function loadRestriction(Type $type, DOMElement $node) |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * @return Closure |
||
522 | */ |
||
523 | protected function loadElementDef(Schema $schema, DOMElement $node) |
||
524 | { |
||
525 | return $this->loadAttributeOrElementDef($schema, $node, false); |
||
526 | } |
||
527 | } |
||
528 |