Complex classes like DataGrid 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 DataGrid, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class DataGrid implements \Countable, \IteratorAggregate, \ArrayAccess |
||
13 | { |
||
14 | use EventManagerAwareTrait; |
||
15 | |||
16 | const EVENT_GRID_INSERT_PRE = 'at-datagrid.grid.insert.pre'; |
||
17 | const EVENT_GRID_INSERT_POST = 'at-datagrid.grid.insert.post'; |
||
18 | const EVENT_GRID_UPDATE_PRE = 'at-datagrid.grid.update.pre'; |
||
19 | const EVENT_GRID_UPDATE_POST = 'at-datagrid.grid.update.post'; |
||
20 | const EVENT_GRID_PERSIST_PRE = 'at-datagrid.grid.persist.pre'; |
||
21 | const EVENT_GRID_PERSIST_POST = 'at-datagrid.grid.persist.post'; |
||
22 | const EVENT_GRID_DELETE_PRE = 'at-datagrid.grid.delete.pre'; |
||
23 | const EVENT_GRID_DELETE_POST = 'at-datagrid.grid.delete.post'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $title; |
||
29 | |||
30 | /** |
||
31 | * @var DataSource\AbstractDataSource |
||
32 | */ |
||
33 | private $dataSource; |
||
34 | |||
35 | /** |
||
36 | * Data grid columns |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $columns = []; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $identifierColumnName = 'id'; |
||
46 | |||
47 | /** |
||
48 | * @var Paginator |
||
49 | */ |
||
50 | private $paginator; |
||
51 | |||
52 | /** |
||
53 | * Order in format ['id' => 'desc'] |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $order; |
||
58 | |||
59 | /** |
||
60 | * Current page |
||
61 | * |
||
62 | * @var integer |
||
63 | */ |
||
64 | private $currentPage = 1; |
||
65 | |||
66 | /** |
||
67 | * Items per page |
||
68 | * |
||
69 | * @var integer |
||
70 | */ |
||
71 | private $itemsPerPage = 20; |
||
72 | |||
73 | /** |
||
74 | * Page range |
||
75 | * |
||
76 | * @var integer |
||
77 | */ |
||
78 | private $pageRange = 10; |
||
79 | |||
80 | /** |
||
81 | * Array of column filters |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | private $filters = []; |
||
86 | |||
87 | /** |
||
88 | * Array of rows from data source |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | private $data = []; |
||
93 | |||
94 | /** |
||
95 | * @param $dataSource |
||
96 | * @param string $title |
||
97 | */ |
||
98 | public function __construct($dataSource, $title = '') |
||
106 | |||
107 | // METADATA |
||
108 | |||
109 | /** |
||
110 | * @param string $title |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setTitle($title) |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getTitle() |
||
126 | |||
127 | // COLUMNS |
||
128 | |||
129 | /** |
||
130 | * @param $name |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setIdentifierColumnName($name) |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getIdentifierColumnName() |
||
148 | |||
149 | /** |
||
150 | * Check if grid has column by the given name |
||
151 | * |
||
152 | * @param $name |
||
153 | * @return bool |
||
154 | */ |
||
155 | protected function hasColumn($name) |
||
159 | |||
160 | /** |
||
161 | * Add a column to data grid |
||
162 | * |
||
163 | * @param Column $column |
||
164 | * @return $this |
||
165 | * @throws \Exception |
||
166 | */ |
||
167 | public function addColumn(Column $column) |
||
177 | |||
178 | /** |
||
179 | * Set column by given name with overwriting. |
||
180 | * |
||
181 | * @param Column $column |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setColumn(Column $column) |
||
189 | |||
190 | /** |
||
191 | * Return column object specified by it name |
||
192 | * |
||
193 | * @param $name |
||
194 | * @return Column |
||
195 | * @throws \Exception |
||
196 | */ |
||
197 | public function getColumn($name) |
||
205 | |||
206 | /** |
||
207 | * Return all column objects |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function getColumns() |
||
215 | |||
216 | /** |
||
217 | * Remove column specified by it name |
||
218 | * |
||
219 | * @param $name |
||
220 | * @return DataGrid |
||
221 | */ |
||
222 | public function removeColumn($name) |
||
230 | |||
231 | /** |
||
232 | * Remove columns specified by its names |
||
233 | * |
||
234 | * @param array $names |
||
235 | * @return DataGrid |
||
236 | */ |
||
237 | public function removeColumns(array $names) |
||
245 | |||
246 | /** |
||
247 | * Set columns invisible in grid |
||
248 | * |
||
249 | * @param array $names |
||
250 | * @return DataGrid |
||
251 | */ |
||
252 | public function hideColumns(array $names) |
||
260 | |||
261 | /** |
||
262 | * Set columns invisible in form |
||
263 | * |
||
264 | * @param $names |
||
265 | * @return DataGrid |
||
266 | */ |
||
267 | public function hideColumnsInForm(array $names) |
||
275 | |||
276 | /** |
||
277 | * Set columns visible in grid |
||
278 | * |
||
279 | * @param array $names |
||
280 | * @return DataGrid |
||
281 | */ |
||
282 | public function showColumns(array $names) |
||
290 | |||
291 | /** |
||
292 | * Set columns visible in form |
||
293 | * |
||
294 | * @param $names |
||
295 | * @return DataGrid |
||
296 | */ |
||
297 | public function showColumnsInForm(array $names) |
||
305 | |||
306 | // SORTING |
||
307 | |||
308 | /** |
||
309 | * @param array $order |
||
310 | */ |
||
311 | public function setOrder(array $order) |
||
319 | |||
320 | /** |
||
321 | * @return array |
||
322 | */ |
||
323 | public function getOrder() |
||
327 | |||
328 | /** |
||
329 | * @return mixed |
||
330 | */ |
||
331 | public function getOrderColumn() |
||
339 | |||
340 | /** |
||
341 | * @return string|null |
||
342 | */ |
||
343 | public function getOrderDirection() |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getRevertOrderDirection() |
||
359 | |||
360 | // DATA SOURCE |
||
361 | |||
362 | /** |
||
363 | * @param DataSource\AbstractDataSource $dataSource |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setDataSource(DataSource\AbstractDataSource $dataSource) |
||
371 | |||
372 | /** |
||
373 | * @return DataSource\AbstractDataSource |
||
374 | */ |
||
375 | public function getDataSource() |
||
379 | |||
380 | /** |
||
381 | * Find row by primary key |
||
382 | * |
||
383 | * @param $key |
||
384 | * @return mixed |
||
385 | */ |
||
386 | public function getRow($key) |
||
390 | |||
391 | /** |
||
392 | * @return mixed |
||
393 | * @throws \Exception |
||
394 | */ |
||
395 | public function getData() |
||
421 | |||
422 | /** |
||
423 | * @return Paginator |
||
424 | */ |
||
425 | public function getPaginator() |
||
429 | |||
430 | // CRUD |
||
431 | |||
432 | /** |
||
433 | * @param $data |
||
434 | * @return mixed |
||
435 | */ |
||
436 | public function save($data, $identifier = null) |
||
455 | |||
456 | /** |
||
457 | * @param $data |
||
458 | * @return mixed |
||
459 | */ |
||
460 | public function insert($data) |
||
474 | |||
475 | /** |
||
476 | * @param $data |
||
477 | * @param $primary |
||
478 | * @return mixed |
||
479 | */ |
||
480 | public function update($data, $primary) |
||
494 | |||
495 | /** |
||
496 | * @param $id |
||
497 | */ |
||
498 | public function delete($id) |
||
506 | |||
507 | // FILTERS |
||
508 | |||
509 | /** |
||
510 | * @param FilterInterface $filter |
||
511 | * @param string|Column $column |
||
512 | * @return $this |
||
513 | */ |
||
514 | public function addFilter(FilterInterface $filter, $column) |
||
532 | |||
533 | /** |
||
534 | * @param $columnName |
||
535 | * @return bool |
||
536 | */ |
||
537 | public function hasFilter($columnName) |
||
541 | |||
542 | /** |
||
543 | * @param $columnName |
||
544 | * @return null |
||
545 | */ |
||
546 | public function getFilter($columnName) |
||
554 | |||
555 | /** |
||
556 | * @return array |
||
557 | */ |
||
558 | public function getFilters() |
||
562 | |||
563 | /** |
||
564 | * @return bool |
||
565 | */ |
||
566 | public function hasFilters() |
||
570 | |||
571 | /** |
||
572 | * @param $values |
||
573 | */ |
||
574 | public function setFiltersData($values) |
||
581 | |||
582 | // PAGINATOR |
||
583 | |||
584 | /** |
||
585 | * @param $number |
||
586 | * @return $this |
||
587 | */ |
||
588 | public function setCurrentPage($number) |
||
593 | |||
594 | /** |
||
595 | * @return int |
||
596 | */ |
||
597 | public function getCurrentPage() |
||
601 | |||
602 | |||
603 | /** |
||
604 | * @param $count |
||
605 | * @return $this |
||
606 | */ |
||
607 | public function setItemsPerPage($count) |
||
612 | |||
613 | /** |
||
614 | * @return int |
||
615 | */ |
||
616 | public function getItemsPerPage() |
||
620 | |||
621 | /** |
||
622 | * @param $count |
||
623 | * @return $this |
||
624 | */ |
||
625 | public function setPageRange($count) |
||
630 | |||
631 | // INTERFACES IMPLEMENTATION |
||
632 | |||
633 | /** |
||
634 | * @return mixed |
||
635 | */ |
||
636 | public function current() |
||
640 | |||
641 | /** |
||
642 | * @return bool |
||
643 | */ |
||
644 | public function valid() |
||
648 | |||
649 | /** |
||
650 | * @return mixed |
||
651 | */ |
||
652 | public function next() |
||
656 | |||
657 | /** |
||
658 | * @return mixed |
||
659 | */ |
||
660 | public function key() |
||
664 | |||
665 | /** |
||
666 | * @return mixed |
||
667 | */ |
||
668 | public function rewind() |
||
672 | |||
673 | /** |
||
674 | * @return int |
||
675 | */ |
||
676 | public function count() |
||
680 | |||
681 | /** |
||
682 | * @param mixed $offset |
||
683 | * @return bool |
||
684 | */ |
||
685 | public function offsetExists($offset) |
||
689 | |||
690 | /** |
||
691 | * @param mixed $offset |
||
692 | * @return bool|mixed |
||
693 | */ |
||
694 | public function offsetGet($offset) |
||
702 | |||
703 | /** |
||
704 | * @param mixed $offset |
||
705 | * @param mixed $column |
||
706 | */ |
||
707 | public function offsetSet($offset, $column) |
||
715 | |||
716 | /** |
||
717 | * @param mixed $offset |
||
718 | */ |
||
719 | public function offsetUnset($offset) |
||
725 | |||
726 | /** |
||
727 | * Get an iterator for iterating over the elements in the collection. |
||
728 | * |
||
729 | * @return \ArrayIterator|\Traversable |
||
730 | */ |
||
731 | public function getIterator() |
||
735 | } |
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..