Complex classes like ResultSet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResultSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class ResultSet implements \Countable, \IteratorAggregate |
||
47 | { |
||
48 | |||
49 | use \Kdyby\StrictObjects\Scream; |
||
50 | |||
51 | /** |
||
52 | * @var int|NULL |
||
53 | */ |
||
54 | private $totalCount; |
||
55 | |||
56 | /** |
||
57 | * @var \Doctrine\ORM\AbstractQuery|\Doctrine\ORM\Query|\Doctrine\ORM\NativeQuery |
||
58 | */ |
||
59 | private $query; |
||
60 | |||
61 | /** |
||
62 | * @var \Kdyby\Doctrine\QueryObject|NULL |
||
63 | */ |
||
64 | private $queryObject; |
||
65 | |||
66 | /** |
||
67 | * @var \Kdyby\Persistence\Queryable|NULL |
||
68 | */ |
||
69 | private $repository; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $fetchJoinCollection = TRUE; |
||
75 | |||
76 | /** |
||
77 | * @var bool|NULL |
||
78 | */ |
||
79 | private $useOutputWalkers; |
||
80 | |||
81 | /** |
||
82 | * @var \ArrayIterator|NULL |
||
83 | */ |
||
84 | private $iterator; |
||
85 | |||
86 | /** |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $frozen = FALSE; |
||
90 | |||
91 | |||
92 | |||
93 | /** |
||
94 | * @param ORM\AbstractQuery $query |
||
95 | * @param \Kdyby\Doctrine\QueryObject $queryObject |
||
96 | * @param \Kdyby\Persistence\Queryable $repository |
||
97 | */ |
||
98 | public function __construct(ORM\AbstractQuery $query, QueryObject $queryObject = NULL, Queryable $repository = NULL) |
||
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * @param bool $fetchJoinCollection |
||
113 | * @throws InvalidStateException |
||
114 | * @return ResultSet |
||
115 | */ |
||
116 | public function setFetchJoinCollection($fetchJoinCollection) |
||
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @param bool|null $useOutputWalkers |
||
130 | * @throws InvalidStateException |
||
131 | * @return ResultSet |
||
132 | */ |
||
133 | public function setUseOutputWalkers($useOutputWalkers) |
||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * @return bool|null |
||
147 | */ |
||
148 | public function getUseOutputWalkers() |
||
152 | |||
153 | |||
154 | |||
155 | /** |
||
156 | * @return boolean |
||
157 | */ |
||
158 | public function getFetchJoinCollection() |
||
162 | |||
163 | |||
164 | |||
165 | /** |
||
166 | * Removes ORDER BY clause that is not inside subquery. |
||
167 | * |
||
168 | * @throws InvalidStateException |
||
169 | * @return ResultSet |
||
170 | */ |
||
171 | public function clearSorting() |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * @param string|array $columns |
||
190 | * @throws InvalidStateException |
||
191 | * @return ResultSet |
||
192 | */ |
||
193 | public function applySorting($columns) |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * @param int|NULL $offset |
||
230 | * @param int|NULL $limit |
||
231 | * |
||
232 | * @throws InvalidStateException |
||
233 | * @return ResultSet |
||
234 | */ |
||
235 | public function applyPaging($offset, $limit) |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * @param \Nette\Utils\Paginator $paginator |
||
250 | * @param int $itemsPerPage |
||
251 | * @return ResultSet |
||
252 | */ |
||
253 | public function applyPaginator(UIPaginator $paginator, $itemsPerPage = NULL) |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function isEmpty() |
||
277 | |||
278 | |||
279 | |||
280 | /** |
||
281 | * @throws \Kdyby\Doctrine\QueryException |
||
282 | * @return int |
||
283 | */ |
||
284 | public function getTotalCount() |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * @param int $hydrationMode |
||
312 | * @throws QueryException |
||
313 | * @return \ArrayIterator |
||
314 | */ |
||
315 | public function getIterator($hydrationMode = ORM\AbstractQuery::HYDRATE_OBJECT) |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * @param int $hydrationMode |
||
347 | * @return array |
||
348 | */ |
||
349 | public function toArray($hydrationMode = ORM\AbstractQuery::HYDRATE_OBJECT) |
||
353 | |||
354 | |||
355 | |||
356 | /** |
||
357 | * @return int |
||
358 | */ |
||
359 | public function count() |
||
363 | |||
364 | |||
365 | |||
366 | /** |
||
367 | * @param \Doctrine\ORM\AbstractQuery|\Doctrine\ORM\Query|\Doctrine\ORM\NativeQuery $query |
||
368 | * @return \Doctrine\ORM\Tools\Pagination\Paginator |
||
369 | */ |
||
370 | private function createPaginatedQuery(ORM\AbstractQuery $query) |
||
381 | |||
382 | |||
383 | |||
384 | private function updating() |
||
390 | |||
391 | } |
||
392 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.