Complex classes like Listing 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 Listing, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Listing implements \IteratorAggregate |
||
21 | { |
||
22 | use SelectorValidationTrait; |
||
23 | |||
24 | /** |
||
25 | * @var RecordSelector|DocumentSelector |
||
26 | */ |
||
27 | private $selector = null; |
||
28 | |||
29 | /** |
||
30 | * List of added filters. |
||
31 | * |
||
32 | * @var FilterInterface[] |
||
33 | */ |
||
34 | private $filters = []; |
||
35 | |||
36 | /** |
||
37 | * List of added sorters. |
||
38 | * |
||
39 | * @var SorterInterface[] |
||
40 | */ |
||
41 | private $sorters = []; |
||
42 | |||
43 | /** |
||
44 | * Available selection limits |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | private $limits = [25, 50, 100]; |
||
49 | |||
50 | /** |
||
51 | * Stores current listing state. |
||
52 | * |
||
53 | * @var StateInterface|null |
||
54 | */ |
||
55 | private $state = null; |
||
56 | |||
57 | /** |
||
58 | * Default state to be used if primary state is not active |
||
59 | * |
||
60 | * @var StateInterface|null |
||
61 | */ |
||
62 | private $defaultState = null; |
||
63 | |||
64 | /** |
||
65 | * @param PaginatorAwareInterface|RecordSelector|DocumentSelector $selector |
||
66 | * @param StateInterface $state |
||
67 | * |
||
68 | * @throws InvalidSelectorException |
||
69 | */ |
||
70 | public function __construct( |
||
83 | |||
84 | /** |
||
85 | * Set active listing selector. |
||
86 | * |
||
87 | * @param RecordSelector|DocumentSelector|PaginatorAwareInterface $selector |
||
88 | * |
||
89 | * @return $this |
||
90 | * |
||
91 | * @throws InvalidSelectorException |
||
92 | */ |
||
93 | public function setSelector(PaginatorAwareInterface $selector) |
||
100 | |||
101 | /** |
||
102 | * Get configured listing selector |
||
103 | * |
||
104 | * @return DocumentSelector|RecordSelector |
||
105 | * |
||
106 | * @throws ListingException |
||
107 | */ |
||
108 | public function getSelector() |
||
116 | |||
117 | /** |
||
118 | * @return DocumentSelector|RecordSelector |
||
119 | * |
||
120 | * @throws ListingException |
||
121 | */ |
||
122 | public function getIterator() |
||
126 | |||
127 | /** |
||
128 | * State responsible for listing settings management. |
||
129 | * |
||
130 | * @param StateInterface $state |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setState(StateInterface $state) |
||
139 | |||
140 | /** |
||
141 | * Listing state. |
||
142 | * |
||
143 | * @return StateInterface |
||
144 | */ |
||
145 | public function getState() |
||
149 | |||
150 | /** |
||
151 | * Default state to be used if no active state can be found |
||
152 | * |
||
153 | * @param StateInterface $state |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setDefaultState(StateInterface $state) |
||
162 | |||
163 | /** |
||
164 | * Default state, if any were set. |
||
165 | * |
||
166 | * @return null|StateInterface |
||
167 | */ |
||
168 | public function getDefaultState() |
||
172 | |||
173 | /** |
||
174 | * Active listing state (with fallback to default state) |
||
175 | * |
||
176 | * @return StateInterface |
||
177 | * @throws ListingException |
||
178 | */ |
||
179 | public function activeState() |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getNamespace() |
||
205 | |||
206 | /** |
||
207 | * Set listing isolation namespace |
||
208 | * |
||
209 | * @param string $namespace |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function setNamespace($namespace) |
||
219 | |||
220 | /** |
||
221 | * Set allowed pagination limits |
||
222 | * |
||
223 | * @param array $limits |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setLimits(array $limits = []) |
||
237 | |||
238 | /** |
||
239 | * Available limits. |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | public function getLimits() |
||
247 | |||
248 | /** |
||
249 | * Modify selector |
||
250 | * |
||
251 | * @param PaginatorAwareInterface|RecordSelector|DocumentSelector $selector |
||
252 | * |
||
253 | * @return RecordSelector|DocumentSelector Modified selector |
||
254 | * |
||
255 | * @throws InvalidSelectorException |
||
256 | */ |
||
257 | public function configureSelector(PaginatorAwareInterface $selector) |
||
278 | |||
279 | /** |
||
280 | * Add new selection filter |
||
281 | * |
||
282 | * @param string $name |
||
283 | * @param FilterInterface $filter |
||
284 | * |
||
285 | * @return self |
||
286 | */ |
||
287 | final public function addFilter($name, FilterInterface $filter) |
||
293 | |||
294 | /** |
||
295 | * List of every associated filter. |
||
296 | * |
||
297 | * @return FilterInterface[] |
||
298 | */ |
||
299 | public function getFilters() |
||
303 | |||
304 | /** |
||
305 | * Attach new sorter to listing |
||
306 | * |
||
307 | * @param string $name |
||
308 | * @param SorterInterface $sorter |
||
309 | * |
||
310 | * @return self |
||
311 | */ |
||
312 | final public function addSorter($name, SorterInterface $sorter) |
||
318 | |||
319 | /** |
||
320 | * Every available sorter |
||
321 | * |
||
322 | * @return SorterInterface[] |
||
323 | */ |
||
324 | public function getSorters() |
||
328 | |||
329 | /** |
||
330 | * List of filters to be applied to current selection (named list) |
||
331 | * |
||
332 | * @return FilterInterface[] |
||
333 | * |
||
334 | * @throws ListingException |
||
335 | */ |
||
336 | public function activeFilters() |
||
357 | |||
358 | /** |
||
359 | * Looking for active sorter name (normalized). |
||
360 | * |
||
361 | * @return string|null |
||
362 | */ |
||
363 | public function getSorter() |
||
374 | |||
375 | /** |
||
376 | * Active sorter instance (if any) |
||
377 | * |
||
378 | * @return SorterInterface|null |
||
379 | * |
||
380 | * @throws SorterException |
||
381 | */ |
||
382 | public function activeSorter() |
||
396 | |||
397 | /** |
||
398 | * Get paginator associated with current listing |
||
399 | * |
||
400 | * @return Paginator |
||
401 | */ |
||
402 | protected function createPaginator() |
||
409 | |||
410 | /** |
||
411 | * Get active pagination limit |
||
412 | * |
||
413 | * @return int |
||
414 | */ |
||
415 | protected function getLimit() |
||
425 | |||
426 | /** |
||
427 | * Get current page. |
||
428 | * |
||
429 | * @return int |
||
430 | */ |
||
431 | protected function getPage() |
||
435 | } |
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..