ParserTrait::parserFromFile()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 24
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of gpupo/pipe2
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * For more information, see
12
 * <https://opensource.gpupo.com/pipe2/>.
13
 */
14
15
namespace Gpupo\Pipe2\Traits;
16
17
trait ParserTrait
18
{
19
    protected function fieldReduce(Array $item)
20
    {
21
        return $item;
22
    }
23
24
    protected function parserItems($data)
25
    {
26
        $item = [];
27
        foreach ($data['item'] as $product) {
28
            if (array_key_exists('tag', $product) && array_key_exists('value', $product)) {
29
                $item[$product['tag']] = $product['value'];
30
            }
31
        }
32
33
        return $item;
34
    }
35
36
    protected function parserFromFile($filePath, $key = 'item')
37
    {
38
        $list = [];
39
40
        $doc = new \DOMDocument();
41
        if (@$doc->load($filePath)) {
42
            $xml = $doc->saveXML();
43
            $values = $index = [];
44
            $parser = xml_parser_create();
45
            xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
46
            xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
47
            xml_parse_into_struct($parser, $xml, $values, $index);
48
            xml_parser_free($parser);
49
50
            foreach ($index as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $index of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
51
                if ($k === $key) {
52
                    for ($i = 0; $i < count($v); $i += 2) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
53
                        $count = (empty($count) ? $v[$i] : ++$count);
54
                        $offset = $v[$i] + 1;
55
                        $len = $v[$i + 1] - $offset;
56
                        $list[$count] = $values[$v[0]];
57
                        $item = $this->fieldReduce(array_slice($values, $offset, $len));
58
59
                        if (!empty($item)) {
60
                            $list[$count][$key] = $item;
61
                        }
62
                    }
63
64
                    break;
65
                }
66
67
                $list[] = $values[$v[0]];
68
            }
69
        }
70
71
        return $list;
72
    }
73
}
74