XmlReader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 93
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getFields() 0 4 1
A current() 0 8 2
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 12 3
A count() 0 8 2
1
<?php
2
3
namespace Mathielen\DataImport\Reader;
4
5
use Ddeboer\DataImport\Reader\CountableReaderInterface;
6
7
/**
8
 * Reads data from a xml file.
9
 */
10
class XmlReader implements CountableReaderInterface
11
{
12
    /**
13
     * @var \Iterator
14
     */
15
    protected $iterableResult;
16
17
    private $filename;
18
    private $xpath;
19
20 3
    public function __construct(\SplFileObject $file, $xpath = null)
21
    {
22 3
        $this->filename = $file->getPathname();
23
24 3
        if (!is_null($xpath) && !is_string($xpath)) {
25
            throw new \InvalidArgumentException('xpath must be null or a string');
26
        }
27
28 3
        $this->xpath = $xpath;
29 3
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function getFields()
35
    {
36 2
        return array_keys($this->current()['@attributes']); //TODO
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    public function current()
43
    {
44 3
        if (!$this->iterableResult) {
45
            $this->rewind();
46
        }
47
48 3
        return (array) $this->iterableResult->current();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function next()
55
    {
56 3
        $this->iterableResult->next();
57 3
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function key()
63
    {
64 2
        return $this->iterableResult->key();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function valid()
71
    {
72 3
        return $this->iterableResult->valid();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 3
    public function rewind()
79
    {
80 3
        if (!$this->iterableResult) {
81 3
            $this->iterableResult = new \SimpleXMLIterator(file_get_contents($this->filename));
82
83 3
            if ($this->xpath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->xpath of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84 1
                $this->iterableResult = new \ArrayIterator($this->iterableResult->xpath($this->xpath));
85
            }
86
        }
87
88 3
        $this->iterableResult->rewind();
89 3
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function count()
95
    {
96 2
        if (!$this->iterableResult) {
97 2
            $this->rewind();
98
        }
99
100 2
        return count($this->iterableResult);
101
    }
102
}
103