Conditions | 16 |
Paths | 97 |
Total Lines | 72 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public static function load( $content, Config $config = null ) { |
||
37 | if ( !extension_loaded( 'dom' ) ) { |
||
38 | throw new RuntimeException( 'Extension DOM is required.' ); |
||
39 | } |
||
40 | |||
41 | if ( is_null( $config ) ) { |
||
42 | $config = new Config(); |
||
43 | } |
||
44 | |||
45 | $internalErrors = libxml_use_internal_errors( true ); |
||
46 | $disableEntities = libxml_disable_entity_loader(); |
||
47 | libxml_clear_errors(); |
||
48 | |||
49 | $dom = new DOMDocument( '1.0', 'UTF-8' ); |
||
50 | $dom->validateOnParse = true; |
||
51 | |||
52 | if ( is_string( $config->getSetRootElement() ) && !empty( $config->getSetRootElement() ) ) { |
||
53 | $content = "<{$config->getSetRootElement()}>$content</{$config->getSetRootElement()}>"; |
||
54 | } |
||
55 | |||
56 | $res = $dom->loadXML( $content, $config->getXML_OPTIONS() ); |
||
57 | |||
58 | if ( !$res ) { |
||
59 | libxml_disable_entity_loader( $disableEntities ); |
||
60 | |||
61 | throw new XmlParsingException( implode( "\n", static::getXmlErrors( $internalErrors ) ) ); |
||
62 | } |
||
63 | |||
64 | $dom->normalizeDocument(); |
||
65 | |||
66 | libxml_use_internal_errors( $internalErrors ); |
||
67 | libxml_disable_entity_loader( $disableEntities ); |
||
68 | |||
69 | foreach ( $dom->childNodes as $child ) { |
||
70 | if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType && !$config->getAllowDocumentType() ) { |
||
71 | throw new XmlParsingException( 'Document types are not allowed.' ); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if ( null !== $config->getSchemaOrCallable() ) { |
||
76 | $internalErrors = libxml_use_internal_errors( true ); |
||
77 | libxml_clear_errors(); |
||
78 | |||
79 | $e = null; |
||
80 | if ( is_callable( $config->getSchemaOrCallable() ) ) { |
||
81 | try { |
||
82 | $valid = call_user_func( $config->getSchemaOrCallable(), $dom, $internalErrors ); |
||
83 | } catch ( Exception $e ) { |
||
84 | $valid = false; |
||
85 | } |
||
86 | } elseif ( !is_array( $config->getSchemaOrCallable() ) && is_file( (string)$config->getSchemaOrCallable() ) ) { |
||
87 | $schemaSource = file_get_contents( (string)$config->getSchemaOrCallable() ); |
||
88 | $valid = @$dom->schemaValidateSource( $schemaSource ); |
||
89 | } else { |
||
90 | libxml_use_internal_errors( $internalErrors ); |
||
91 | |||
92 | throw new XmlParsingException( 'The schemaOrCallable argument has to be a valid path to XSD file or callable.' ); |
||
93 | } |
||
94 | |||
95 | if ( !$valid ) { |
||
96 | $messages = static::getXmlErrors( $internalErrors ); |
||
97 | if ( empty( $messages ) ) { |
||
98 | throw new InvalidXmlException( 'The XML is not valid.', 0, $e ); |
||
99 | } |
||
100 | throw new XmlParsingException( implode( "\n", $messages ), 0, $e ); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | libxml_clear_errors(); |
||
105 | libxml_use_internal_errors( $internalErrors ); |
||
106 | |||
107 | return $dom; |
||
108 | } |
||
134 | } |