Complex classes like Grid 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 Grid, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Grid |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $template = 'laravel-grid::grid'; |
||
17 | |||
18 | /** |
||
19 | * @var GridHelper |
||
20 | */ |
||
21 | protected $gridHelper; |
||
22 | |||
23 | /** |
||
24 | * @var SourceInterface |
||
25 | */ |
||
26 | protected $source; |
||
27 | |||
28 | /** |
||
29 | * @var Column[] |
||
30 | */ |
||
31 | protected $columns = []; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $perPage = 15; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $emptyMessage = 'Brak danych do wyświetlenia.'; |
||
42 | |||
43 | /** |
||
44 | * @var Rows |
||
45 | */ |
||
46 | protected $rows; |
||
47 | |||
48 | /** |
||
49 | * Total number of records. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $total = 0; |
||
54 | |||
55 | /** |
||
56 | * @var RowAction[] |
||
57 | */ |
||
58 | protected $rowActions = []; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $defaultOrder = [ |
||
64 | 'column' => 'id', |
||
65 | 'direction' => 'desc' |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * @var Order |
||
70 | */ |
||
71 | protected $order; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $enablePagination = true; |
||
77 | |||
78 | /** |
||
79 | * @var callable |
||
80 | */ |
||
81 | protected $afterCallback; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $viewData = []; |
||
87 | |||
88 | /** |
||
89 | * @param GridHelper $gridHelper |
||
90 | */ |
||
91 | public function __construct(GridHelper $gridHelper) |
||
97 | |||
98 | public function buildGrid() |
||
102 | |||
103 | /** |
||
104 | * @return GridHelper |
||
105 | */ |
||
106 | public function getGridHelper() |
||
110 | |||
111 | /** |
||
112 | * @param SourceInterface $source |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function setSource(SourceInterface $source) |
||
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param array $options |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function addColumn($name, array $options = []) |
||
140 | |||
141 | /** |
||
142 | * @param string $name |
||
143 | * @return Column|null |
||
144 | */ |
||
145 | public function getColumn($name) |
||
149 | |||
150 | /** |
||
151 | * @return Column[] |
||
152 | */ |
||
153 | public function getColumns() |
||
157 | |||
158 | /** |
||
159 | * @param Order $order |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function setDefaultOrder(Order $order) |
||
168 | |||
169 | /** |
||
170 | * @return Order |
||
171 | */ |
||
172 | public function getOrder() |
||
176 | |||
177 | /** |
||
178 | * @param int $perPage |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setPerPage($perPage) |
||
187 | |||
188 | /** |
||
189 | * @return int |
||
190 | */ |
||
191 | public function getPerPage() |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getEmptyMessage() |
||
203 | |||
204 | /** |
||
205 | * @param string $emptyMessage |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setEmptyMessage($emptyMessage) |
||
214 | |||
215 | /** |
||
216 | * @param RowAction $rowAction |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function addRowAction(RowAction $rowAction) |
||
226 | |||
227 | /** |
||
228 | * @param bool $flag |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function setEnablePagination($flag) |
||
238 | |||
239 | /** |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function isPaginationEnabled() |
||
246 | |||
247 | /** |
||
248 | * @param callable $callback |
||
249 | * @deprecated |
||
250 | */ |
||
251 | public function each(callable $callback) |
||
255 | |||
256 | /** |
||
257 | * @param callable $callback |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function after(callable $callback) |
||
266 | |||
267 | /** |
||
268 | * @param Component $component |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function addComponent(Component $component) |
||
278 | |||
279 | /** |
||
280 | * @param array $viewData |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setViewData($viewData) |
||
289 | |||
290 | /** |
||
291 | * @return \Illuminate\View\View |
||
292 | */ |
||
293 | public function render() |
||
310 | |||
311 | /** |
||
312 | * Is table filterable? |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function isFilterable() |
||
329 | |||
330 | /** |
||
331 | * @return Rows |
||
332 | */ |
||
333 | public function getRows() |
||
386 | |||
387 | /** |
||
388 | * @param Rows $rows |
||
389 | * @return LengthAwarePaginator |
||
390 | */ |
||
391 | protected function getPaginator(Rows $rows) |
||
397 | |||
398 | /** |
||
399 | * @return mixed |
||
400 | */ |
||
401 | protected function execute() |
||
413 | |||
414 | /** |
||
415 | * @return int |
||
416 | */ |
||
417 | protected function resolveCurrentPage() |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | protected function resolveCurrentPath() |
||
429 | |||
430 | /** |
||
431 | * @return array |
||
432 | */ |
||
433 | protected function getValidatorRules() |
||
448 | |||
449 | protected function makeDefaultOrder() |
||
455 | |||
456 | /** |
||
457 | * @param string $name |
||
458 | * @param array $options |
||
459 | * @return Column |
||
460 | */ |
||
461 | protected function makeColumn($name, array $options = []) |
||
467 | |||
468 | /** |
||
469 | * @param string $name |
||
470 | * @param array $options |
||
471 | * @return array |
||
472 | */ |
||
473 | protected function setupColumnOptions($name, array $options) |
||
479 | |||
480 | /** |
||
481 | * @return string |
||
482 | */ |
||
483 | public function __toString() |
||
487 | } |
||
488 |