Complex classes like CollectionDataTable 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 CollectionDataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class CollectionDataTable extends DataTableAbstract |
||
11 | { |
||
12 | /** |
||
13 | * Collection object. |
||
14 | * |
||
15 | * @var \Illuminate\Support\Collection |
||
16 | */ |
||
17 | public $collection; |
||
18 | |||
19 | /** |
||
20 | * Collection object. |
||
21 | * |
||
22 | * @var \Illuminate\Support\Collection |
||
23 | */ |
||
24 | public $original; |
||
25 | |||
26 | /** |
||
27 | * The offset of the first record in the full dataset. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | private $offset = 0; |
||
32 | |||
33 | /** |
||
34 | * Can the DataTable engine be created with these parameters. |
||
35 | * |
||
36 | * @param mixed $source |
||
37 | * @return bool |
||
38 | */ |
||
39 | public static function canCreate($source) |
||
43 | |||
44 | /** |
||
45 | * Factory method, create and return an instance for the DataTable engine. |
||
46 | * |
||
47 | * @param array|\Illuminate\Support\Collection $source |
||
48 | * @return CollectionDataTable|DataTableAbstract |
||
49 | */ |
||
50 | public static function create($source) |
||
58 | |||
59 | /** |
||
60 | * CollectionEngine constructor. |
||
61 | * |
||
62 | * @param \Illuminate\Support\Collection $collection |
||
63 | */ |
||
64 | public function __construct(Collection $collection) |
||
72 | |||
73 | /** |
||
74 | * Serialize collection. |
||
75 | * |
||
76 | * @param mixed $collection |
||
77 | * @return mixed|null |
||
78 | */ |
||
79 | protected function serialize($collection) |
||
83 | |||
84 | /** |
||
85 | * Count results. |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | public function count() |
||
93 | |||
94 | /** |
||
95 | * Perform column search. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function columnSearch() |
||
137 | |||
138 | /** |
||
139 | * Perform pagination. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function paging() |
||
150 | |||
151 | /** |
||
152 | * Organizes works. |
||
153 | * |
||
154 | * @param bool $mDataSupport |
||
155 | * @return \Illuminate\Http\JsonResponse |
||
156 | */ |
||
157 | public function make($mDataSupport = true) |
||
180 | |||
181 | /** |
||
182 | * Count total items. |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | public function totalCount() |
||
190 | |||
191 | /** |
||
192 | * Get results. |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function results() |
||
200 | |||
201 | /** |
||
202 | * Revert transformed DT_RowIndex back to it's original values. |
||
203 | * |
||
204 | * @param bool $mDataSupport |
||
205 | */ |
||
206 | private function revertIndexColumn($mDataSupport) |
||
218 | |||
219 | /** |
||
220 | * Perform global search for the given keyword. |
||
221 | * |
||
222 | * @param string $keyword |
||
223 | */ |
||
224 | protected function globalSearch($keyword) |
||
252 | |||
253 | /** |
||
254 | * Perform default query orderBy clause. |
||
255 | */ |
||
256 | protected function defaultOrdering() |
||
277 | |||
278 | /** |
||
279 | * Get array sorter closure. |
||
280 | * |
||
281 | * @param array $criteria |
||
282 | * @return \Closure |
||
283 | */ |
||
284 | protected function getSorter(array $criteria) |
||
313 | |||
314 | /** |
||
315 | * Resolve callback parameter instance. |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | protected function resolveCallbackParameter() |
||
323 | |||
324 | /** |
||
325 | * Define the offset of the first item of the collection with respect to |
||
326 | * the FULL dataset the collection was sliced from. It effectively allows the |
||
327 | * collection to be "pre-sliced". |
||
328 | * |
||
329 | * @param int $offset |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setOffset(int $offset) |
||
338 | } |
||
339 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.