Completed
Push — master ( 99f7bd...3359a6 )
by Robbert
02:10
created

Parser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 61.82%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 17
c 6
b 1
f 2
lcom 1
cbo 2
dl 9
loc 81
ccs 34
cts 55
cp 0.6182
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 3
A parse() 0 15 4
A parsePartial() 0 19 3
C parseFull() 0 29 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace arc\xml;
4
5
class Parser
6
{
7
8
    public $namespaces = array();
9
10 2 View Code Duplication
    public function __construct( $options = array() )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    {
12 2
        $optionList = array( 'namespaces' );
13 2
        foreach( $options as $option => $optionValue) {
14
            if (in_array( $option, $optionList )) {
15
                $this->{$option} = $optionValue;
16
            }
17 2
        }
18 2
    }
19
20 2
    public function parse( $xml, $encoding = null )
21
    {
22 2
        if (!$xml) {
23
            return Proxy( null );
24
        }
25 2
        if ($xml instanceof Proxy) { // already parsed
26
            return $xml;
27
        }
28 2
        $xml = (string) $xml;
29
        try {
30 2
            return $this->parseFull( $xml, $encoding );
31 1
        } catch( \arc\Exception $e) {
32 1
            return $this->parsePartial( $xml, $encoding );
33
        }
34
    }
35
36 1
    private function parsePartial( $xml, $encoding )
37
    {
38
        // add a known (single) root element with all declared namespaces
39
        // libxml will barf on multiple root elements
40
        // and it will silently drop namespace prefixes not defined in the document
41 1
        $root = '<arcxmlroot';
42 1
        foreach ($this->namespaces as $name => $uri) {
43
            if ($name === 0) {
44
                $root .= ' xmlns="';
45
            } else {
46
                $root .= ' xmlns:'.$name.'="';
47
            }
48
            $root .= htmlspecialchars( $uri ) . '"';
49 1
        }
50 1
        $root .= '>';
51 1
        $result = $this->parseFull( $root.$xml.'</arcxmlroot>', $encoding );
52
        $result = $result->firstChild->childNodes;
0 ignored issues
show
Documentation introduced by
The property firstChild does not exist on object<arc\xml\Proxy>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
        return $result;
54
    }
55
56 2
    private function parseFull( $xml, $encoding = null )
57
    {
58 2
        $dom = new \DomDocument();
59 2
        if ($encoding) {
60
            $xml = '<?xml encoding="' . $encoding . '">' . $xml;
61
        }
62 2
        libxml_disable_entity_loader(); // prevents XXE attacks
63 2
        $prevErrorSetting = libxml_use_internal_errors(true);
64 2
        if ($dom->loadXML( $xml )) {
65 2
            if ($encoding) {
66
                foreach( $dom->childNodes as $item) {
67
                    if ($item->nodeType == XML_PI_NODE) {
68
                        $dom->removeChild( $item );
69
                        break;
70
                    }
71
                }
72
                $dom->encoding = $encoding;
73
            }
74 2
            return new Proxy( simplexml_import_dom( $dom ), $this );
75
        }
76 1
        $errors = libxml_get_errors();
77 1
        libxml_clear_errors();
78 1
        libxml_use_internal_errors( $prevErrorSetting );
79 1
        $message = 'Incorrect xml passed.';
80 1
        foreach ($errors as $error) {
81 1
            $message .= '\nline: '.$error->line.'; column: '.$error->column.'; '.$error->message;
82 1
        }
83 1
        throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT );
84
    }
85
}
86