Failed Conditions
Pull Request — master (#7885)
by Šimon
10:14
created

IterableNewResult::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 Doctrine\ORM\Internal\Hydration;
6
7
use Iterator;
8
use function array_key_exists;
9
use function count;
10
use function is_array;
11
12
/**
13
 * Represents a result structure that can be iterated over, hydrating row-by-row
14
 * during the iteration. An IterableResult is obtained by AbstractHydrator#iterate().
15
 */
16
final class IterableNewResult implements Iterator
17
{
18
    /** @var AbstractHydrator */
19
    private $hydrator;
20
21
    /** @var bool */
22
    private $rewinded = false;
23
24
    /** @var int */
25
    private $key = -1;
26
27
    /** @var object|null */
28
    private $current;
29
30
    /**
31
     * @param AbstractHydrator $hydrator
32
     */
33 15
    public function __construct($hydrator)
34
    {
35 15
        $this->hydrator = $hydrator;
36 15
    }
37
38
    /**
39
     * @throws HydrationException
40
     */
41 15
    public function rewind()
42
    {
43 15
        if ($this->rewinded === true) {
44
            throw new HydrationException('Can only iterate a Result once.');
45
        }
46
47 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...
48 15
        $this->rewinded = true;
49 15
    }
50
51
    /**
52
     * Gets the next set of results.
53
     *
54
     * @return mixed|false
55
     */
56 15
    public function next()
57
    {
58 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...
59 15
        if (is_array($this->current) && array_key_exists(0, $this->current) && count($this->current) === 1) {
0 ignored issues
show
introduced by
The condition is_array($this->current) is always false.
Loading history...
60 14
            $this->current = $this->current[0];
61
        }
62
63 15
        $this->key++;
64
65 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...
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71 14
    public function current()
72
    {
73 14
        return $this->current;
74
    }
75
76
    /**
77
     * @return int
78
     */
79 14
    public function key()
80
    {
81 14
        return $this->key;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87 15
    public function valid()
88
    {
89 15
        return $this->current!==false;
90
    }
91
}
92