Passed
Push — master ( b3b3ee...9aa525 )
by Pol
03:15
created

IterableIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\collection\Iterator;
6
7
use Generator;
8
use Iterator;
9
10
/**
11
 * Class IterableIterator.
12
 *
13
 * @implements Iterator<Iterator>
14
 */
15
final class IterableIterator implements Iterator
16
{
17
    /**
18
     * @var \drupol\collection\Iterator\ClosureIterator
19
     */
20
    private $iterator;
21
22
    /**
23
     * IterableIterator constructor.
24
     *
25
     * @param iterable<mixed> $iterable
26
     */
27 7
    public function __construct(iterable $iterable)
28
    {
29
        $callable = static function () use ($iterable): Generator {
30 7
            foreach ($iterable as $key => $value) {
31 7
                yield $key => $value;
32
            }
33 7
        };
34
35 7
        $this->iterator = new ClosureIterator($callable);
36 7
    }
37
38
    /**
39
     * @return Iterator<mixed>
40
     */
41 6
    public function current()
42
    {
43 6
        return $this->iterator->current();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @return int|string
50
     */
51 2
    public function key()
52
    {
53 2
        return $this->iterator->key();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @return $this
60
     */
61 5
    public function next()
62
    {
63 5
        $this->iterator->next();
64
65 5
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type drupol\collection\Iterator\IterableIterator which is incompatible with the return type mandated by Iterator::next() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
66
    }
67
68
    /**
69
     * @return $this|void
70
     */
71 3
    public function rewind()
72
    {
73 3
        $this->iterator->rewind();
74
75 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type drupol\collection\Iterator\IterableIterator which is incompatible with the return type mandated by Iterator::rewind() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @return bool
82
     */
83 3
    public function valid()
84
    {
85 3
        return $this->iterator->valid();
86
    }
87
}
88