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 |
||
14 | class Grid |
||
15 | { |
||
16 | const DEFAULT_TEMPLATE = 'laravel-grid::grid'; |
||
17 | |||
18 | /** |
||
19 | * @var Request |
||
20 | */ |
||
21 | protected $request; |
||
22 | |||
23 | /** |
||
24 | * @var ValidationFactory |
||
25 | */ |
||
26 | protected $validator; |
||
27 | |||
28 | /** |
||
29 | * @var HtmlBuilder |
||
30 | */ |
||
31 | protected $htmlBuilder; |
||
32 | |||
33 | /** |
||
34 | * @var FormBuilder |
||
35 | */ |
||
36 | protected $formBuilder; |
||
37 | |||
38 | /** |
||
39 | * @var SourceInterface |
||
40 | */ |
||
41 | protected $source; |
||
42 | |||
43 | /** |
||
44 | * @var Column[] |
||
45 | */ |
||
46 | protected $columns = []; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $perPage = 15; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $emptyMessage = 'Brak danych do wyświetlenia.'; |
||
57 | |||
58 | /** |
||
59 | * @var Rows |
||
60 | */ |
||
61 | protected $rows; |
||
62 | |||
63 | /** |
||
64 | * Total number of records. |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $total = 0; |
||
69 | |||
70 | /** |
||
71 | * @var RowAction[] |
||
72 | */ |
||
73 | protected $rowActions = []; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $defaultOrder = [ |
||
79 | 'column' => 'id', |
||
80 | 'direction' => 'desc' |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * @var Order |
||
85 | */ |
||
86 | protected $order; |
||
87 | |||
88 | /** |
||
89 | * @var bool |
||
90 | */ |
||
91 | protected $enablePagination = true; |
||
92 | |||
93 | /** |
||
94 | * @var callable |
||
95 | */ |
||
96 | protected $eachCallback; |
||
97 | |||
98 | /** |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $data = []; |
||
102 | |||
103 | /** |
||
104 | * @param Request $request |
||
105 | * @param ValidationFactory $validator |
||
106 | * @param HtmlBuilder $htmlBuilder |
||
107 | * @param FormBuilder $formBuilder |
||
108 | */ |
||
109 | public function __construct( |
||
122 | |||
123 | public function buildGrid() |
||
127 | |||
128 | /** |
||
129 | * @return Request |
||
130 | */ |
||
131 | public function getRequest() |
||
135 | |||
136 | /** |
||
137 | * @return HtmlBuilder |
||
138 | */ |
||
139 | public function getHtmlBuilder() |
||
143 | |||
144 | /** |
||
145 | * @return FormBuilder |
||
146 | */ |
||
147 | public function getFormBuilder() |
||
151 | |||
152 | /** |
||
153 | * @param SourceInterface $source |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setSource(SourceInterface $source) |
||
162 | |||
163 | /** |
||
164 | * @param string $name |
||
165 | * @param array $options |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function addColumn($name, array $options = []) |
||
181 | |||
182 | /** |
||
183 | * @return Column[] |
||
184 | */ |
||
185 | public function getColumns() |
||
189 | |||
190 | /** |
||
191 | * @param Order $order |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function setDefaultOrder(Order $order) |
||
200 | |||
201 | /** |
||
202 | * @return Order |
||
203 | */ |
||
204 | public function getOrder() |
||
208 | |||
209 | /** |
||
210 | * @param int $perPage |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function setPerPage($perPage) |
||
219 | |||
220 | /** |
||
221 | * @return int |
||
222 | */ |
||
223 | public function getPerPage() |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getEmptyMessage() |
||
232 | { |
||
233 | return $this->emptyMessage; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $emptyMessage |
||
238 | */ |
||
239 | public function setEmptyMessage($emptyMessage) |
||
240 | { |
||
241 | $this->emptyMessage = $emptyMessage; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param RowAction $rowAction |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function addRowAction(RowAction $rowAction) |
||
255 | |||
256 | /** |
||
257 | * @param bool $flag |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function setEnablePagination($flag) |
||
267 | |||
268 | /** |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function isPaginationEnabled() |
||
275 | |||
276 | /** |
||
277 | * @param callable $callback |
||
278 | */ |
||
279 | public function each(callable $callback) |
||
283 | |||
284 | /** |
||
285 | * @param array $data |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function setData($data) |
||
294 | |||
295 | /** |
||
296 | * @return string |
||
297 | */ |
||
298 | public function render() |
||
315 | |||
316 | /** |
||
317 | * @return bool |
||
318 | */ |
||
319 | public function hasFilters() |
||
332 | |||
333 | /** |
||
334 | * @return Rows |
||
335 | */ |
||
336 | 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() |
||
411 | |||
412 | /** |
||
413 | * @return int |
||
414 | */ |
||
415 | protected function resolveCurrentPage() |
||
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | protected function resolveCurrentPath() |
||
427 | |||
428 | /** |
||
429 | * @return \Illuminate\Contracts\Validation\Validator |
||
430 | */ |
||
431 | protected function getValidatorInstance() |
||
435 | |||
436 | /** |
||
437 | * @return array |
||
438 | */ |
||
439 | protected function getValidatorRules() |
||
454 | |||
455 | protected function makeDefaultOrder() |
||
461 | |||
462 | /** |
||
463 | * @param string $name |
||
464 | * @param array $options |
||
465 | * @return Column |
||
466 | */ |
||
467 | protected function makeColumn($name, array $options = []) |
||
473 | |||
474 | /** |
||
475 | * @param string $name |
||
476 | * @param array $options |
||
477 | * @return array |
||
478 | */ |
||
479 | protected function setupColumnOptions($name, array $options) |
||
485 | |||
486 | /** |
||
487 | * @return string |
||
488 | */ |
||
489 | public function __toString() |
||
493 | } |
||
494 |