LinkedListIterator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 9
c 0
b 0
f 0
dl 0
loc 59
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A next() 0 3 1
A valid() 0 3 1
A __construct() 0 3 1
A rewind() 0 3 1
A current() 0 3 1
1
<?php
2
3
namespace Smoren\Containers\Structs;
4
5
use Iterator;
6
7
/**
8
 * Class LinkedListIterator
9
 */
10
class LinkedListIterator implements Iterator
11
{
12
    /**
13
     * @var LinkedList iterator owner
14
     */
15
    protected LinkedList $owner;
16
    /**
17
     * @var LinkedListItem|null current position
18
     */
19
    protected ?LinkedListItem $position = null;
20
21
    /**
22
     * LinkedListIterator constructor.
23
     * @param LinkedList $owner iterator owner
24
     */
25
    public function __construct(LinkedList $owner)
26
    {
27
        $this->owner = $owner;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function current()
34
    {
35
        return $this->position->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        return $this->position->/** @scrutinizer ignore-call */ getData();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function next(): void
42
    {
43
        $this->position = $this->position->getNext();
44
    }
45
46
    /**
47
     * @inheritDoc
48
     * @return LinkedListItem
49
     */
50
    public function key(): LinkedListItem
51
    {
52
        return $this->position;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->position could return the type null which is incompatible with the type-hinted return Smoren\Containers\Structs\LinkedListItem. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function valid(): bool
59
    {
60
        return $this->position !== null;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function rewind(): void
67
    {
68
        $this->position = $this->owner->getFirst();
69
    }
70
}
71