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 |
||
10 | class Grid |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $template = 'laravel-grid::grid'; |
||
16 | |||
17 | /** |
||
18 | * @var GridHelper |
||
19 | */ |
||
20 | protected $gridHelper; |
||
21 | |||
22 | /** |
||
23 | * @var SourceInterface |
||
24 | */ |
||
25 | protected $source; |
||
26 | |||
27 | /** |
||
28 | * @var Column[] |
||
29 | */ |
||
30 | protected $columns = []; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $perPage = 15; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $emptyMessage = 'Brak danych do wyświetlenia.'; |
||
41 | |||
42 | /** |
||
43 | * @var Rows |
||
44 | */ |
||
45 | protected $rows; |
||
46 | |||
47 | /** |
||
48 | * Total number of records. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $total = 0; |
||
53 | |||
54 | /** |
||
55 | * @var RowAction[] |
||
56 | */ |
||
57 | protected $rowActions = []; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $defaultOrder = [ |
||
63 | 'column' => 'id', |
||
64 | 'direction' => 'desc' |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * @var Order |
||
69 | */ |
||
70 | protected $order; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $enablePagination = true; |
||
76 | |||
77 | /** |
||
78 | * @var callable |
||
79 | */ |
||
80 | protected $eachCallback; |
||
81 | |||
82 | /** |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $data = []; |
||
86 | |||
87 | /** |
||
88 | * @param GridHelper $gridHelper |
||
89 | */ |
||
90 | public function __construct(GridHelper $gridHelper) |
||
96 | |||
97 | public function buildGrid() |
||
101 | |||
102 | /** |
||
103 | * @return GridHelper |
||
104 | */ |
||
105 | public function getGridHelper() |
||
109 | |||
110 | /** |
||
111 | * @param SourceInterface $source |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setSource(SourceInterface $source) |
||
120 | |||
121 | /** |
||
122 | * @param string $name |
||
123 | * @param array $options |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function addColumn($name, array $options = []) |
||
139 | |||
140 | /** |
||
141 | * @return Column[] |
||
142 | */ |
||
143 | public function getColumns() |
||
147 | |||
148 | /** |
||
149 | * @param Order $order |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function setDefaultOrder(Order $order) |
||
158 | |||
159 | /** |
||
160 | * @return Order |
||
161 | */ |
||
162 | public function getOrder() |
||
166 | |||
167 | /** |
||
168 | * @param int $perPage |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function setPerPage($perPage) |
||
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | */ |
||
181 | public function getPerPage() |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getEmptyMessage() |
||
193 | |||
194 | /** |
||
195 | * @param string $emptyMessage |
||
196 | */ |
||
197 | public function setEmptyMessage($emptyMessage) |
||
201 | |||
202 | /** |
||
203 | * @param RowAction $rowAction |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function addRowAction(RowAction $rowAction) |
||
213 | |||
214 | /** |
||
215 | * @param bool $flag |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setEnablePagination($flag) |
||
225 | |||
226 | /** |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function isPaginationEnabled() |
||
233 | |||
234 | /** |
||
235 | * @param callable $callback |
||
236 | */ |
||
237 | public function each(callable $callback) |
||
241 | |||
242 | /** |
||
243 | * @param array $data |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function setData($data) |
||
252 | |||
253 | /** |
||
254 | * @return \Illuminate\View\View; |
||
|
|||
255 | */ |
||
256 | public function render() |
||
273 | |||
274 | /** |
||
275 | * Is table filterable? |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isFilterable() |
||
292 | |||
293 | /** |
||
294 | * @return Rows |
||
295 | */ |
||
296 | public function getRows() |
||
346 | |||
347 | /** |
||
348 | * @param Rows $rows |
||
349 | * @return LengthAwarePaginator |
||
350 | */ |
||
351 | protected function getPaginator(Rows $rows) |
||
357 | |||
358 | /** |
||
359 | * @return mixed |
||
360 | */ |
||
361 | protected function execute() |
||
371 | |||
372 | /** |
||
373 | * @return int |
||
374 | */ |
||
375 | protected function resolveCurrentPage() |
||
379 | |||
380 | /** |
||
381 | * @return string |
||
382 | */ |
||
383 | protected function resolveCurrentPath() |
||
387 | |||
388 | /** |
||
389 | * @return array |
||
390 | */ |
||
391 | protected function getValidatorRules() |
||
406 | |||
407 | protected function makeDefaultOrder() |
||
413 | |||
414 | /** |
||
415 | * @param string $name |
||
416 | * @param array $options |
||
417 | * @return Column |
||
418 | */ |
||
419 | protected function makeColumn($name, array $options = []) |
||
425 | |||
426 | /** |
||
427 | * @param string $name |
||
428 | * @param array $options |
||
429 | * @return array |
||
430 | */ |
||
431 | protected function setupColumnOptions($name, array $options) |
||
437 | |||
438 | /** |
||
439 | * @return string |
||
440 | */ |
||
441 | public function __toString() |
||
445 | } |
||
446 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.