Failed Conditions
Pull Request — master (#7885)
by Šimon
16:21
created

IterableNewResult   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 74
ccs 20
cts 21
cp 0.9524
rs 10
wmc 8

6 Methods

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