Passed
Push — master ( 4dc916...6fdd76 )
by Thomas
02:09
created

src/FluentDOM/XMLReader/Iterator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace FluentDOM\XMLReader {
3
4
  use FluentDOM\XMLReader;
5
6
  /**
7
   * Class Iterator
8
   *
9
   * It will use XMLReader::read() to iterate the nodes and use expand to return each found node as DOM node.
10
   */
11
  class Iterator implements \Iterator {
12
13
    /**
14
     * @var XMLReader
15
     */
16
    private $_reader;
17
    /**
18
     * @var string|NULL
19
     */
20
    private $_name;
21
    /**
22
     * @var callable
23
     */
24
    private $_filter;
25
26
    /**
27
     * @var int
28
     */
29
    private $_key = -1;
30
31
    /**
32
     * @var NULL|\DOMNode
33
     */
34
    private $_current = NULL;
35
36
    /**
37
     * Iterator constructor.
38
     *
39
     * @param XMLReader $reader
40
     * @param NULL|string $name tag name filter
41
     * @param callable|NULL $filter
42
     */
43 2
    public function __construct(
44
      XMLReader $reader, $name = NULL, callable $filter = NULL
45
    ) {
46 2
      $this->_reader = $reader;
47 2
      $this->_name = $name;
48 2
      $this->_filter = $filter;
49 2
    }
50
51
    /**
52
     * Throw an exception if rewind() is called after next()
53
     */
54 2
    public function rewind() {
55 2
      if ($this->_key >= 0) {
56 1
        throw new \LogicException(sprintf('%s is not a seekable iterator', __CLASS__));
57
      }
58 2
      $this->next();
59 2
    }
60
61 2
    public function next() {
62 2
      if ($this->move($this->_reader, $this->_name, $this->_filter)) {
63 2
        $this->_current = (NULL === $this->_current)
1 ignored issue
show
Documentation Bug introduced by
It seems like NULL === $this->_current...current->ownerDocument) can also be of type object<FluentDOM\DOM\Node>. However, the property $_current is declared as type null|object<DOMNode>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64 2
          ? $this->_reader->expand()
65 2
          : $this->_reader->expand($this->_current->ownerDocument);
66 2
        $this->_key++;
67
      } else {
68 2
        $this->_current = NULL;
69
      }
70 2
    }
71
72
    /**
73
     * @param XMLReader $reader
74
     * @param string|NULL $name
75
     * @param callable|NULL $filter
76
     * @return bool
77
     */
78 2
    protected function move(XMLReader $reader, $name, $filter): bool {
79 2
      return $reader->read($name, NULL, $filter);
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 2
    public function valid(): bool {
86 2
      return NULL !== $this->_current;
87
    }
88
89
    /**
90
     * @return \DOMNode|NULL
91
     */
92 2
    public function current() {
93 2
      return $this->_current;
94
    }
95
96
    /**
97
     * @return int
98
     */
99 2
    public function key(): int {
100 2
      return $this->_key;
101
    }
102
  }
103
}
104