Failed Conditions
Pull Request — master (#7885)
by Šimon
09:39
created

IterableNewResult::current()   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 Doctrine\ORM\Internal\Hydration;
6
7
use Iterator;
8
use function array_key_exists;
0 ignored issues
show
introduced by
Type array_key_exists is not used in this file.
Loading history...
9
use function count;
0 ignored issues
show
introduced by
Type count is not used in this file.
Loading history...
10
use function is_array;
11
12
/**
13
 * TODO: temp name
14
 *
15
 * Represents a result structure that can be iterated over, hydrating row-by-row
16
 * during the iteration. An IterableResult is obtained by AbstractHydrator#getIterable().
17
 */
18
final class IterableNewResult implements Iterator
19
{
20
    /** @var AbstractHydrator */
21
    private $hydrator;
22
23
    /** @var bool */
24
    private $rewinded = false;
25
26
    /** @var int */
27
    private $key = -1;
28
29
    /** @var object|null */
30
    private $current;
31
32
    /**
33
     * @param AbstractHydrator $hydrator
34
     */
35 15
    public function __construct($hydrator)
36
    {
37 15
        $this->hydrator = $hydrator;
38 15
    }
39
40
    /**
41
     * @throws HydrationException
42
     */
43 15
    public function rewind()
44
    {
45 15
        if ($this->rewinded === true) {
46
            throw new HydrationException('Can only iterate a Result once.');
47
        }
48
49 15
        $this->current  = $this->next();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->next() of type false is incompatible with the declared type null|object of property $current.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50 15
        $this->rewinded = true;
51 15
    }
52
53
    /**
54
     * Gets the next set of results.
55
     *
56
     * @return mixed|false
57
     */
58 15
    public function next()
59
    {
60 15
        $this->current = $this->hydrator->hydrateRow();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->hydrator->hydrateRow() of type false is incompatible with the declared type null|object of property $current.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61 15
        if (is_array($this->current)) {
0 ignored issues
show
introduced by
The condition is_array($this->current) is always false.
Loading history...
62 14
            $this->current = array_values($this->current)[0];
0 ignored issues
show
introduced by
Function array_values() should not be referenced via a fallback global name, but via a use statement.
Loading history...
63
        }
64
65 15
        $this->key++;
66
67 15
        return $this->current;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->current returns the type false 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...
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73 14
    public function current()
74
    {
75 14
        return $this->current;
76
    }
77
78
    /**
79
     * @return int
80
     */
81 14
    public function key()
82
    {
83 14
        return $this->key;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89 15
    public function valid()
90
    {
91 15
        return $this->current!==false;
92
    }
93
}
94