XMLReader   B
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 207
Duplicated Lines 29.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 13
Bugs 3 Features 2
Metric Value
c 13
b 3
f 2
dl 61
loc 207
wmc 41
lcom 1
cbo 3
rs 8.2769

7 Methods

Rating   Name   Duplication   Size   Complexity  
D getCategories() 30 30 9
D getCurrencies() 31 31 9
D getOffers() 0 48 16
A getElementAttributes() 0 11 3
A open() 0 6 1
A countOffers() 0 9 2
A moveToStart() 0 5 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like XMLReader 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 XMLReader, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @author Serkin Alexander <[email protected]>
5
 * @license https://github.com/serkin/ymlparser/LICENSE MIT
6
 */
7
8
namespace YMLParser\Driver;
9
10
class XMLReader implements DriverInterface {
11
12
    /**
13
     * @var \XMLReader
14
     */
15
    private $xml;
16
17
    /**
18
     * Link to stored xml file.
19
     *
20
     * @var string
21
     */
22
    private $filename;
23
24
    /**
25
     * Gets categories.
26
     *
27
     * @return arry Array of \YMLParser\Node\Category instances or empty array
28
     */
29 View Code Duplication
    public function getCategories() {
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...
30
        $returnArr = [];
31
        $this->moveToStart();
32
        $xml = $this->xml;
33
34
        while ($xml->read()) {
35
            if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'categories') {
36
                while ($xml->read() && $xml->name != 'categories') {
37
                    if ($xml->nodeType == \XMLReader::ELEMENT) {
38
                        $arr = [];
39
40
                        if ($xml->hasAttributes) {
41
42
                            while ($xml->moveToNextAttribute()) {
43
                                $arr[strtolower($xml->name)] = $xml->value;
44
                            }
45
                        }
46
47
                        $xml->read();
48
                        $arr['value'] = $xml->value;
49
                        $returnArr[] = new \YMLParser\Node\Category($arr);
50
51
                        unset($arr);
52
                    }
53
                }
54
            }
55
        }
56
57
        return $returnArr;
58
    }
59
60
    /**
61
     * Gets currencies.
62
     *
63
     * @return array
64
     */
65 View Code Duplication
    public function getCurrencies() {
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...
66
67
        $returnArr = [];
68
        $this->moveToStart();
69
        $xml = $this->xml;
70
71
        while ($xml->read()) {
72
            if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'currencies') {
73
                while ($xml->read() && $xml->name != 'currencies') {
74
                    if ($xml->nodeType == \XMLReader::ELEMENT) {
75
                        $arr = [];
76
77
                        if ($xml->hasAttributes) {
78
79
                            while ($xml->moveToNextAttribute()) {
80
                                $arr[strtolower($xml->name)] = $xml->value;
81
                            }
82
                        }
83
84
                        $xml->read();
85
                        $arr['value'] = $xml->value;
86
                        $returnArr[] = new \YMLParser\Node\Currency($arr);
87
88
                        unset($arr);
89
                    }
90
                }
91
            }
92
        }
93
94
        return $returnArr;
95
    }
96
97
    /**
98
     * Gets offers.
99
     *
100
     * @param \Closure $filter
101
     *
102
     * @return \Iterator Array of \YMLParser\Node\Offer instances or empty array
103
     */
104
    public function getOffers(\Closure $filter = null) {
105
        $this->moveToStart();
106
        $xml = $this->xml;
107
108
        while ($xml->read()) {
109
            if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'offers') {
110
                while ($xml->read() && $xml->name != 'offers') {
111
                    if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'offer') {
112
                        $arr = $this->getElementAttributes($xml);
113
114
                        while ($xml->read() && $xml->name != 'offer') {
115
116
                            if ($xml->nodeType == \XMLReader::ELEMENT) {
117
118
                                $name = mb_strtolower($xml->name);
119
120
                                if ($name == 'param') {
121
                                    $tmpArr = ['name' => $xml->getAttribute('name')];
122
                                }
123
124
                                $xml->read();
125
126
                                if ($name == 'param') {
127
                                    $arr['params'][] = array_merge(['value' => $xml->value], $tmpArr);
0 ignored issues
show
Bug introduced by
The variable $tmpArr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
128
                                } else {
129
                                    if ($name == 'picture') {
130
                                        $arr['pictures'][] = $xml->value;
131
                                    }
132
133
                                    $arr[$name] = $xml->value;
134
                                }
135
                            }
136
                        }
137
138
                        $returnValue = new \YMLParser\Node\Offer($arr);
139
140
                        if (!is_null($filter)) {
141
                            if ($filter($returnValue)) {
142
                                yield $returnValue;
143
                            }
144
                        } else {
145
                            yield $returnValue;
146
                        }
147
                    }
148
                }
149
            }
150
        }
151
    }
152
153
    /**
154
     * Gets attributes from element.
155
     *
156
     * @param \XMLReader $element
157
     *
158
     * @return array
159
     */
160
    private function getElementAttributes(\XMLReader $element) {
161
        $returnArr = [];
162
163
        if ($element->hasAttributes) {
164
            while ($element->moveToNextAttribute()) {
165
                $returnArr[mb_strtolower($element->name)] = $element->value;
166
            }
167
        }
168
169
        return $returnArr;
170
    }
171
172
    /**
173
     * Opens file.
174
     *
175
     * @param type $filename
176
     *
177
     * @throws \Exception
178
     *
179
     * @return bool
180
     */
181
    public function open($filename) {
182
        $this->filename = $filename;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filename of type object<YMLParser\Driver\type> is incompatible with the declared type string of property $filename.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
        $this->xml = new \XMLReader();
184
185
        return $this->xml->open($filename);
186
    }
187
188
    /**
189
     * Gets amount of offers.
190
     *
191
     * @param \Closure $filter
192
     *
193
     * @return int
194
     */
195
    public function countOffers(\Closure $filter = null) {
196
        $returnValue = 0;
197
198
        foreach ($this->getOffers($filter) as $el):
199
            $returnValue++;
200
        endforeach;
201
202
        return $returnValue;
203
    }
204
205
    /**
206
     * Rewinds cursor to the first element.
207
     *
208
     * @return bool
209
     */
210
    private function moveToStart() {
211
        $this->xml->close();
212
213
        return $this->xml->open($this->filename);
214
    }
215
216
}
217