Total Complexity | 43 |
Total Lines | 613 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractSchemaReader 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 AbstractSchemaReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class AbstractSchemaReader |
||
39 | { |
||
40 | |||
41 | const XSD_NS = "http://www.w3.org/2001/XMLSchema"; |
||
42 | |||
43 | const XML_NS = "http://www.w3.org/XML/1998/namespace"; |
||
44 | |||
45 | /** |
||
46 | * @var string[] |
||
47 | */ |
||
48 | protected $knownLocationSchemas = [ |
||
49 | 'http://www.w3.org/2001/xml.xsd' => ( |
||
50 | __DIR__ . '/Resources/xml.xsd' |
||
51 | ), |
||
52 | 'http://www.w3.org/2001/XMLSchema.xsd' => ( |
||
53 | __DIR__ . '/Resources/XMLSchema.xsd' |
||
54 | ), |
||
55 | 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' => ( |
||
56 | __DIR__ . '/Resources/oasis-200401-wss-wssecurity-secext-1.0.xsd' |
||
57 | ), |
||
58 | 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' => ( |
||
59 | __DIR__ . '/Resources/oasis-200401-wss-wssecurity-utility-1.0.xsd' |
||
60 | ), |
||
61 | 'https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => ( |
||
62 | __DIR__ . '/Resources/xmldsig-core-schema.xsd' |
||
63 | ), |
||
64 | 'http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => ( |
||
65 | __DIR__ . '/Resources/xmldsig-core-schema.xsd' |
||
66 | ), |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @var string[] |
||
71 | */ |
||
72 | protected static $globalSchemaInfo = array( |
||
73 | self::XML_NS => 'http://www.w3.org/2001/xml.xsd', |
||
74 | self::XSD_NS => 'http://www.w3.org/2001/XMLSchema.xsd' |
||
75 | ); |
||
76 | |||
77 | /** |
||
78 | * @param string $remote |
||
79 | * @param string $local |
||
80 | */ |
||
81 | public function addKnownSchemaLocation($remote, $local) |
||
82 | { |
||
83 | $this->knownLocationSchemas[$remote] = $local; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $remote |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function hasKnownSchemaLocation($remote) |
||
92 | { |
||
93 | return isset($this->knownLocationSchemas[$remote]); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param string $remote |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getKnownSchemaLocation($remote) |
||
102 | { |
||
103 | return $this->knownLocationSchemas[$remote]; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return Closure |
||
108 | */ |
||
109 | abstract protected function loadAttributeGroup(Schema $schema, DOMElement $node); |
||
110 | |||
111 | /** |
||
112 | * @param bool $attributeDef |
||
113 | * |
||
114 | * @return Closure |
||
115 | */ |
||
116 | abstract protected function loadAttributeOrElementDef( |
||
117 | Schema $schema, |
||
118 | DOMElement $node, |
||
119 | $attributeDef |
||
120 | ); |
||
121 | |||
122 | /** |
||
123 | * @return Closure |
||
124 | */ |
||
125 | abstract protected function loadAttributeDef(Schema $schema, DOMElement $node); |
||
126 | |||
127 | /** |
||
128 | * @param DOMElement $node |
||
129 | * @return string |
||
130 | */ |
||
131 | public static function getDocumentation(DOMElement $node) |
||
132 | { |
||
133 | $doc = ''; |
||
134 | foreach ($node->childNodes as $childNode) { |
||
135 | if ($childNode->localName == "annotation") { |
||
136 | $doc .= static::getDocumentation($childNode); |
||
137 | } elseif ($childNode->localName == 'documentation') { |
||
138 | $doc .= ($childNode->nodeValue); |
||
139 | } |
||
140 | } |
||
141 | $doc = preg_replace('/[\t ]+/', ' ', $doc); |
||
142 | return trim($doc); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $key |
||
147 | * |
||
148 | * @return Closure|null |
||
149 | */ |
||
150 | public function maybeCallMethod( |
||
151 | array $methods, |
||
152 | $key, |
||
153 | DOMNode $childNode, |
||
154 | ...$args |
||
155 | ) { |
||
156 | if ($childNode instanceof DOMElement && isset($methods[$key])) { |
||
157 | $method = $methods[$key]; |
||
158 | |||
159 | $append = $this->$method(...$args); |
||
160 | |||
161 | if ($append instanceof Closure) { |
||
162 | return $append; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * |
||
169 | * @param Schema $schema |
||
170 | * @param DOMElement $node |
||
171 | * @param Schema $parent |
||
172 | * @return Closure[] |
||
173 | */ |
||
174 | public function schemaNode(Schema $schema, DOMElement $node, Schema $parent = null) |
||
175 | { |
||
176 | $schema->setSchemaThingsFromNode($node, $parent); |
||
177 | $functions = array(); |
||
178 | |||
179 | $schemaReaderMethods = [ |
||
180 | 'include' => (Schema::class . '::loadImport'), |
||
181 | 'import' => (Schema::class . '::loadImport'), |
||
182 | ]; |
||
183 | |||
184 | $thisMethods = [ |
||
185 | 'element' => [$this, 'loadElementDef'], |
||
186 | 'attribute' => [$this, 'loadAttributeDef'], |
||
187 | 'attributeGroup' => [$this, 'loadAttributeGroup'], |
||
188 | 'group' => [$this, 'loadGroup'], |
||
189 | 'complexType' => [$this, 'loadComplexType'], |
||
190 | 'simpleType' => [$this, 'loadSimpleType'], |
||
191 | ]; |
||
192 | |||
193 | foreach ($node->childNodes as $childNode) { |
||
194 | if ($childNode instanceof DOMElement) { |
||
195 | $callback = $this->maybeCallCallableWithArgs( |
||
196 | $childNode, |
||
197 | [], |
||
198 | [], |
||
199 | [ |
||
200 | [ |
||
201 | $schemaReaderMethods, |
||
202 | [ |
||
203 | $this, |
||
204 | $schema, |
||
205 | $childNode, |
||
206 | ] |
||
207 | ], |
||
208 | [ |
||
209 | $thisMethods, |
||
210 | [ |
||
211 | $schema, |
||
212 | $childNode |
||
213 | ], |
||
214 | ], |
||
215 | ] |
||
216 | ); |
||
217 | |||
218 | if ($callback instanceof Closure) { |
||
219 | $functions[] = $callback; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | return $functions; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return InterfaceSetMinMax |
||
229 | */ |
||
230 | public static function maybeSetMax(InterfaceSetMinMax $ref, DOMElement $node) |
||
231 | { |
||
232 | if ( |
||
233 | $node->hasAttribute("maxOccurs") |
||
234 | ) { |
||
235 | $ref->setMax($node->getAttribute("maxOccurs") == "unbounded" ? -1 : (int)$node->getAttribute("maxOccurs")); |
||
236 | } |
||
237 | |||
238 | return $ref; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return InterfaceSetMinMax |
||
243 | */ |
||
244 | public static function maybeSetMin(InterfaceSetMinMax $ref, DOMElement $node) |
||
245 | { |
||
246 | if ($node->hasAttribute("minOccurs")) { |
||
247 | $ref->setMin((int) $node->getAttribute("minOccurs")); |
||
248 | } |
||
249 | |||
250 | return $ref; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param int|null $max |
||
255 | * |
||
256 | * @return int|null |
||
257 | */ |
||
258 | abstract protected static function loadSequenceNormaliseMax(DOMElement $node, $max); |
||
259 | |||
260 | /** |
||
261 | * @param int|null $max |
||
262 | */ |
||
263 | abstract protected function loadSequence(ElementContainer $elementContainer, DOMElement $node, $max = null); |
||
264 | |||
265 | /** |
||
266 | * @param int|null $max |
||
267 | */ |
||
268 | abstract protected function loadSequenceChildNode( |
||
269 | ElementContainer $elementContainer, |
||
270 | DOMElement $node, |
||
271 | DOMElement $childNode, |
||
272 | $max |
||
273 | ); |
||
274 | |||
275 | /** |
||
276 | * @param mixed[][] $methods |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | abstract protected function maybeCallCallableWithArgs( |
||
281 | DOMElement $childNode, |
||
282 | array $commonMethods = [], |
||
283 | array $methods = [], |
||
284 | array $commonArguments = [] |
||
285 | ); |
||
286 | |||
287 | /** |
||
288 | * @param int|null $max |
||
289 | */ |
||
290 | abstract protected function loadSequenceChildNodeLoadSequence( |
||
291 | ElementContainer $elementContainer, |
||
292 | DOMElement $childNode, |
||
293 | $max |
||
294 | ); |
||
295 | |||
296 | /** |
||
297 | * @param int|null $max |
||
298 | */ |
||
299 | abstract protected function loadSequenceChildNodeLoadElement( |
||
300 | ElementContainer $elementContainer, |
||
301 | DOMElement $node, |
||
302 | DOMElement $childNode, |
||
303 | $max |
||
304 | ); |
||
305 | |||
306 | abstract protected function loadSequenceChildNodeLoadGroup( |
||
307 | ElementContainer $elementContainer, |
||
308 | DOMElement $node, |
||
309 | DOMElement $childNode |
||
310 | ); |
||
311 | |||
312 | abstract protected function addGroupAsElement( |
||
313 | Schema $schema, |
||
314 | DOMElement $node, |
||
315 | DOMElement $childNode, |
||
316 | ElementContainer $elementContainer |
||
317 | ); |
||
318 | |||
319 | abstract protected function maybeLoadSequenceFromElementContainer( |
||
320 | BaseComplexType $type, |
||
321 | DOMElement $childNode |
||
322 | ); |
||
323 | |||
324 | /** |
||
325 | * @return Closure |
||
326 | */ |
||
327 | abstract protected function loadGroup(Schema $schema, DOMElement $node); |
||
328 | |||
329 | /** |
||
330 | * @return BaseComplexType |
||
331 | */ |
||
332 | abstract protected function loadComplexTypeBeforeCallbackCallback( |
||
333 | Schema $schema, |
||
334 | DOMElement $node |
||
335 | ); |
||
336 | |||
337 | /** |
||
338 | * @param Closure|null $callback |
||
339 | * |
||
340 | * @return Closure |
||
341 | */ |
||
342 | abstract protected function loadComplexType(Schema $schema, DOMElement $node, $callback = null); |
||
343 | |||
344 | /** |
||
345 | * @param Closure|null $callback |
||
346 | * |
||
347 | * @return Closure |
||
348 | */ |
||
349 | abstract protected function makeCallbackCallback( |
||
350 | Type $type, |
||
351 | DOMElement $node, |
||
352 | Closure $callbackCallback, |
||
353 | $callback = null |
||
354 | ); |
||
355 | |||
356 | /** |
||
357 | * @param Closure|null $callback |
||
358 | */ |
||
359 | abstract protected function runCallbackAgainstDOMNodeList( |
||
360 | Type $type, |
||
361 | DOMElement $node, |
||
362 | Closure $againstNodeList, |
||
363 | $callback = null |
||
364 | ); |
||
365 | |||
366 | abstract protected function loadComplexTypeFromChildNode( |
||
367 | BaseComplexType $type, |
||
368 | DOMElement $node, |
||
369 | DOMElement $childNode, |
||
370 | Schema $schema |
||
371 | ); |
||
372 | |||
373 | /** |
||
374 | * @param Closure|null $callback |
||
375 | * |
||
376 | * @return Closure |
||
377 | */ |
||
378 | abstract protected function loadSimpleType(Schema $schema, DOMElement $node, $callback = null); |
||
379 | |||
380 | abstract protected function loadList(SimpleType $type, DOMElement $node); |
||
381 | |||
382 | /** |
||
383 | * @param string $attributeName |
||
384 | * |
||
385 | * @return SchemaItem |
||
386 | */ |
||
387 | abstract protected function findSomeType( |
||
388 | SchemaItem $fromThis, |
||
389 | DOMElement $node, |
||
390 | $attributeName |
||
391 | ); |
||
392 | |||
393 | /** |
||
394 | * @param string $attributeName |
||
395 | * |
||
396 | * @return SchemaItem |
||
397 | */ |
||
398 | abstract protected function findSomeTypeFromAttribute( |
||
399 | SchemaItem $fromThis, |
||
400 | DOMElement $node, |
||
401 | $attributeName |
||
402 | ); |
||
403 | |||
404 | abstract protected function loadUnion(SimpleType $type, DOMElement $node); |
||
405 | |||
406 | /** |
||
407 | * @param bool $checkAbstract |
||
408 | */ |
||
409 | abstract protected function fillTypeNode(Type $type, DOMElement $node, $checkAbstract = false); |
||
410 | |||
411 | abstract protected function loadExtensionChildNode( |
||
412 | BaseComplexType $type, |
||
413 | DOMElement $node, |
||
414 | DOMElement $childNode |
||
415 | ); |
||
416 | |||
417 | abstract protected function loadExtension(BaseComplexType $type, DOMElement $node); |
||
418 | |||
419 | abstract protected function loadExtensionChildNodes( |
||
420 | BaseComplexType $type, |
||
421 | DOMNodeList $childNodes, |
||
422 | DOMElement $node |
||
423 | ); |
||
424 | |||
425 | public function findAndSetSomeBase( |
||
426 | Type $type, |
||
427 | Base $setBaseOnThis, |
||
428 | DOMElement $node |
||
429 | ) { |
||
430 | /** |
||
431 | * @var Type $parent |
||
432 | */ |
||
433 | $parent = $this->findSomeType($type, $node, 'base'); |
||
434 | $setBaseOnThis->setBase($parent); |
||
435 | } |
||
436 | |||
437 | abstract protected function maybeLoadExtensionFromBaseComplexType( |
||
438 | Type $type, |
||
439 | DOMElement $childNode |
||
440 | ); |
||
441 | |||
442 | abstract protected function loadRestriction(Type $type, DOMElement $node); |
||
443 | |||
444 | /** |
||
445 | * @param string $typeName |
||
446 | * |
||
447 | * @return mixed[] |
||
448 | */ |
||
449 | abstract protected static function splitParts(DOMElement $node, $typeName); |
||
450 | |||
451 | /** |
||
452 | * |
||
453 | * @param string $finder |
||
454 | * @param Schema $schema |
||
455 | * @param DOMElement $node |
||
456 | * @param string $typeName |
||
457 | * @throws TypeException |
||
458 | * @return ElementItem|Group|AttributeItem|AttributeGroup|Type |
||
459 | */ |
||
460 | public function findSomething($finder, Schema $schema, DOMElement $node, $typeName) |
||
461 | { |
||
462 | list ($name, $namespace) = static::splitParts($node, $typeName); |
||
463 | |||
464 | $namespace = $namespace ?: $schema->getTargetNamespace(); |
||
465 | |||
466 | try { |
||
467 | return $schema->$finder($name, $namespace); |
||
468 | } catch (TypeNotFoundException $e) { |
||
469 | throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", strtolower(substr($finder, 4)), $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e); |
||
470 | } |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * @return Closure |
||
475 | */ |
||
476 | abstract protected function loadElementDef(Schema $schema, DOMElement $node); |
||
477 | |||
478 | public function fillItem(Item $element, DOMElement $node) |
||
503 | } |
||
504 | |||
505 | abstract protected function fillItemNonLocalType(Item $element, DOMElement $node); |
||
506 | |||
507 | /** |
||
508 | * @var Schema|null |
||
509 | */ |
||
510 | protected $globalSchema; |
||
511 | |||
512 | /** |
||
513 | * @return Schema[] |
||
514 | */ |
||
515 | protected function setupGlobalSchemas(array & $callbacks) |
||
516 | { |
||
517 | $globalSchemas = array(); |
||
518 | foreach (self::$globalSchemaInfo as $namespace => $uri) { |
||
519 | Schema::setLoadedFile( |
||
520 | $uri, |
||
521 | $globalSchemas[$namespace] = $schema = new Schema() |
||
522 | ); |
||
523 | if ($namespace === self::XSD_NS) { |
||
524 | $this->globalSchema = $schema; |
||
525 | } |
||
526 | $xml = $this->getDOM($this->knownLocationSchemas[$uri]); |
||
527 | $callbacks = array_merge($callbacks, $this->schemaNode($schema, $xml->documentElement)); |
||
528 | } |
||
529 | |||
530 | return $globalSchemas; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @return string[] |
||
535 | */ |
||
536 | public function getGlobalSchemaInfo() |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * |
||
543 | * @return Schema |
||
544 | */ |
||
545 | public function getGlobalSchema() |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * @param DOMElement $node |
||
572 | * @param string $file |
||
573 | * |
||
574 | * @return Schema |
||
575 | */ |
||
576 | public function readNode(DOMElement $node, $file = 'schema.xsd') |
||
577 | { |
||
578 | $fileKey = $node->hasAttribute('targetNamespace') ? $this->getNamespaceSpecificFileIndex($file, $node->getAttribute('targetNamespace')) : $file; |
||
579 | Schema::setLoadedFile($fileKey, $rootSchema = new Schema()); |
||
580 | |||
581 | $rootSchema->addSchema($this->getGlobalSchema()); |
||
582 | $callbacks = $this->schemaNode($rootSchema, $node); |
||
583 | |||
584 | foreach ($callbacks as $callback) { |
||
585 | call_user_func($callback); |
||
586 | } |
||
587 | |||
588 | return $rootSchema; |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * It is possible that a single file contains multiple <xsd:schema/> nodes, for instance in a WSDL file. |
||
593 | * |
||
594 | * Each of these <xsd:schema/> nodes typically target a specific namespace. Append the target namespace to the |
||
595 | * file to distinguish between multiple schemas in a single file. |
||
596 | * |
||
597 | * @param string $file |
||
598 | * @param string $targetNamespace |
||
599 | * |
||
600 | * @return string |
||
601 | */ |
||
602 | public function getNamespaceSpecificFileIndex($file, $targetNamespace) |
||
603 | { |
||
604 | return $file . '#' . $targetNamespace; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @param string $content |
||
609 | * @param string $file |
||
610 | * |
||
611 | * @return Schema |
||
612 | * |
||
613 | * @throws IOException |
||
614 | */ |
||
615 | public function readString($content, $file = 'schema.xsd') |
||
616 | { |
||
617 | $xml = new DOMDocument('1.0', 'UTF-8'); |
||
618 | if (!$xml->loadXML($content)) { |
||
619 | throw new IOException("Can't load the schema"); |
||
620 | } |
||
621 | $xml->documentURI = $file; |
||
622 | |||
623 | return $this->readNode($xml->documentElement, $file); |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * @param string $file |
||
628 | * |
||
629 | * @return Schema |
||
630 | */ |
||
631 | public function readFile($file) |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * @param string $file |
||
639 | * |
||
640 | * @return DOMDocument |
||
641 | * |
||
642 | * @throws IOException |
||
643 | */ |
||
644 | public function getDOM($file) |
||
651 | } |
||
652 | } |
||
653 |