Issues (590)

src/Sharding/MultiResult.php (7 issues)

1
<?php
2
3
namespace Bdf\Prime\Sharding;
4
5
use ArrayIterator;
6
use Doctrine\DBAL\Driver\Result as DriverResult;
7
use Doctrine\DBAL\Result;
8
use IteratorAggregate;
9
10
/**
11
 * Aggregation of query results
12
 * This class handle results of multiple shards query
13
 */
14
final class MultiResult implements IteratorAggregate, DriverResult
15
{
16
    /**
17
     * @var list<Result>
0 ignored issues
show
The type Bdf\Prime\Sharding\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
     */
19
    private array $results;
20
21
    /**
22
     * @var int
23
     */
24
    private int $current = 0;
25
26
    /**
27
     * @param list<Result> $results The connection query Result instances
28
     */
29 90
    public function __construct(array $results = [])
30
    {
31 90
        $this->results = $results;
0 ignored issues
show
Documentation Bug introduced by
It seems like $results of type array is incompatible with the declared type Bdf\Prime\Sharding\list of property $results.

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...
32
    }
33
34
    /**
35
     * Add a query Result into the sharding result
36
     */
37 84
    public function add(Result $result): void
38
    {
39 84
        $this->results[] = $result;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 17
    public function free(): void
46
    {
47 17
        foreach ($this->results as $result) {
48 17
            $result->free();
49
        }
50
51 17
        unset($this->results);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3
    public function columnCount(): int
58
    {
59 3
        if (!isset($this->results[0])) {
60 1
            return 0;
61
        }
62
63 2
        return $this->results[0]->columnCount();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 3
    public function getIterator(): \Iterator
70
    {
71 3
        return new ArrayIterator($this->fetchAllAssociative());
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 3
    public function fetchNumeric()
78
    {
79 3
        return $this->fetch(__FUNCTION__);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetch(__FUNCTION__) also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Result::fetchNumeric() of Doctrine\DBAL\Driver\list.
Loading history...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 12
    public function fetchAssociative()
86
    {
87 12
        return $this->fetch(__FUNCTION__);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 3
    public function fetchOne()
94
    {
95 3
        return $this->fetch(__FUNCTION__);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 3
    public function fetchAllNumeric(): array
102
    {
103 3
        return $this->fetchAll(__FUNCTION__);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetchAll(__FUNCTION__) returns the type Bdf\Prime\Sharding\list which is incompatible with the type-hinted return array.
Loading history...
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 84
    public function fetchAllAssociative(): array
110
    {
111 84
        return $this->fetchAll(__FUNCTION__);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetchAll(__FUNCTION__) returns the type Bdf\Prime\Sharding\list which is incompatible with the type-hinted return array.
Loading history...
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 4
    public function fetchFirstColumn(): array
118
    {
119 4
        return $this->fetchAll(__FUNCTION__);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetchAll(__FUNCTION__) returns the type Bdf\Prime\Sharding\list which is incompatible with the type-hinted return array.
Loading history...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 3
    public function rowCount(): int
126
    {
127 3
        $count = 0;
128
129 3
        foreach ($this->results as $statement) {
130 2
            $count += $statement->rowCount();
131
        }
132
133 3
        return $count;
134
    }
135
136
    /**
137
     * @param string $method
138
     *
139
     * @return false|mixed False if there is no more results, or the current row value
140
     */
141 12
    private function fetch(string $method)
142
    {
143 12
        for (;; ++$this->current) {
144
            // Stop the fetch if there is no statement
145 12
            if (!isset($this->results[$this->current])) {
146 12
                return false;
147
            }
148
149 11
            $result = $this->results[$this->current]->$method();
150
151 11
            if ($result) {
152 11
                return $result;
153
            }
154
        }
155
    }
156
157
    /**
158
     * @param string $method
159
     * @return list<mixed>
160
     */
161 88
    private function fetchAll(string $method)
162
    {
163 88
        $result = [];
164
165 88
        foreach ($this->results as $statement) {
166 87
            $result = array_merge($result, $statement->$method());
167
        }
168
169 88
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type array which is incompatible with the documented return type Bdf\Prime\Sharding\list.
Loading history...
170
    }
171
}
172