1 | <?php |
||
35 | class Paginator implements \Countable, \IteratorAggregate |
||
36 | { |
||
37 | /** |
||
38 | * @var Query |
||
39 | */ |
||
40 | private $query; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $fetchJoinCollection; |
||
46 | |||
47 | /** |
||
48 | * @var bool|null |
||
49 | */ |
||
50 | private $useOutputWalkers; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | private $count; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * @param Query|QueryBuilder $query A Doctrine ORM query or query builder. |
||
61 | * @param boolean $fetchJoinCollection Whether the query joins a collection (true by default). |
||
62 | */ |
||
63 | 105 | public function __construct($query, $fetchJoinCollection = true) |
|
72 | |||
73 | /** |
||
74 | * Returns the query. |
||
75 | * |
||
76 | * @return Query |
||
77 | */ |
||
78 | public function getQuery() |
||
82 | |||
83 | /** |
||
84 | * Returns whether the query joins a collection. |
||
85 | * |
||
86 | * @return boolean Whether the query joins a collection. |
||
87 | */ |
||
88 | public function getFetchJoinCollection() |
||
92 | |||
93 | /** |
||
94 | * Returns whether the paginator will use an output walker. |
||
95 | * |
||
96 | * @return bool|null |
||
97 | */ |
||
98 | public function getUseOutputWalkers() |
||
102 | |||
103 | /** |
||
104 | * Sets whether the paginator will use an output walker. |
||
105 | * |
||
106 | * @param bool|null $useOutputWalkers |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | 94 | public function setUseOutputWalkers($useOutputWalkers) |
|
111 | { |
||
112 | 94 | $this->useOutputWalkers = $useOutputWalkers; |
|
113 | |||
114 | 94 | return $this; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 15 | public function count() |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 92 | public function getIterator() |
|
137 | { |
||
138 | 92 | $offset = $this->query->getFirstResult(); |
|
139 | 92 | $length = $this->query->getMaxResults(); |
|
140 | |||
141 | 92 | if ($this->fetchJoinCollection) { |
|
142 | 65 | $subQuery = $this->cloneQuery($this->query); |
|
143 | |||
144 | 65 | if ($this->useOutputWalker($subQuery)) { |
|
145 | 41 | $subQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class); |
|
146 | } else { |
||
147 | 24 | $this->appendTreeWalker($subQuery, LimitSubqueryWalker::class); |
|
148 | } |
||
149 | |||
150 | 65 | $subQuery->setFirstResult($offset)->setMaxResults($length); |
|
151 | |||
152 | 65 | $ids = array_map('current', $subQuery->getScalarResult()); |
|
153 | |||
154 | 62 | $whereInQuery = $this->cloneQuery($this->query); |
|
155 | // don't do this for an empty id array |
||
156 | 62 | if (count($ids) === 0) { |
|
157 | return new \ArrayIterator([]); |
||
158 | } |
||
159 | |||
160 | 62 | $this->appendTreeWalker($whereInQuery, WhereInWalker::class); |
|
161 | 62 | $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids)); |
|
162 | 62 | $whereInQuery->setFirstResult(null)->setMaxResults(null); |
|
163 | 62 | $whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids); |
|
164 | 62 | $whereInQuery->setCacheable($this->query->isCacheable()); |
|
165 | |||
166 | 62 | $result = $whereInQuery->getResult($this->query->getHydrationMode()); |
|
167 | } else { |
||
168 | 27 | $result = $this->cloneQuery($this->query) |
|
169 | 27 | ->setMaxResults($length) |
|
170 | 27 | ->setFirstResult($offset) |
|
171 | 27 | ->setCacheable($this->query->isCacheable()) |
|
172 | 27 | ->getResult($this->query->getHydrationMode()) |
|
173 | ; |
||
174 | } |
||
175 | |||
176 | 27 | return new \ArrayIterator($result); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Clones a query. |
||
181 | * |
||
182 | * @param Query $query The query. |
||
183 | * |
||
184 | * @return Query The cloned query. |
||
185 | */ |
||
186 | 105 | private function cloneQuery(Query $query) |
|
200 | |||
201 | /** |
||
202 | * Determines whether to use an output walker for the query. |
||
203 | * |
||
204 | * @param Query $query The query. |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 79 | private function useOutputWalker(Query $query) |
|
216 | |||
217 | /** |
||
218 | * Appends a custom tree walker to the tree walkers hint. |
||
219 | * |
||
220 | * @param Query $query |
||
221 | * @param string $walkerClass |
||
222 | */ |
||
223 | 72 | private function appendTreeWalker(Query $query, $walkerClass) |
|
234 | |||
235 | /** |
||
236 | * Returns Query prepared to count. |
||
237 | * |
||
238 | * @return Query |
||
239 | */ |
||
240 | 15 | private function getCountQuery() |
|
280 | } |
||
281 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.