Complex classes like AbstractTable 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 AbstractTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class AbstractTable extends AbstractElement implements TableInterface |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Collection on headers objects |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $headersObjects; |
||
28 | |||
29 | /** |
||
30 | * List of headers with title and width option |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $headers; |
||
34 | |||
35 | /** |
||
36 | * Database adapter |
||
37 | * @var \Zend\Db\Adapter\Adapter |
||
38 | */ |
||
39 | protected $adapter; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var Source\SourceInterface |
||
44 | */ |
||
45 | protected $source; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * @var Row |
||
50 | */ |
||
51 | protected $row; |
||
52 | |||
53 | /** |
||
54 | * Data after execute of query |
||
55 | * @var array | \Zend\Paginator\Paginator |
||
56 | */ |
||
57 | protected $data; |
||
58 | |||
59 | /** |
||
60 | * Render object responsible for rendering |
||
61 | * @var Render |
||
62 | */ |
||
63 | protected $render; |
||
64 | |||
65 | /** |
||
66 | * Params adapter which responsible for universal mapping parameters from diffrent |
||
67 | * source (default params, Data Table params, JGrid params) |
||
68 | * @var ParamAdapterInterface |
||
69 | */ |
||
70 | protected $paramAdapter; |
||
71 | |||
72 | /** |
||
73 | * Flag to know if table has been initializable |
||
74 | * @var boolean |
||
75 | */ |
||
76 | private $tableInit = false; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Default classes for table |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $class = array('table', 'table-bordered', 'table-condensed', 'table-hover', 'table-striped', 'dataTable'); |
||
84 | |||
85 | /** |
||
86 | * Array configuration of table |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $config; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Options base ond ModuleOptions and config array |
||
94 | * @var Options\ModuleOptions |
||
95 | */ |
||
96 | protected $options = null; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * Check if table has benn initializable |
||
101 | * @return boolean |
||
102 | */ |
||
103 | public function isTableInit() |
||
107 | |||
108 | /** |
||
109 | * Set module options |
||
110 | * |
||
111 | * @param array|\Traversable|ModuleOptions $options |
||
112 | * @return AbstractTable |
||
113 | */ |
||
114 | public function setOptions($options) |
||
123 | |||
124 | /** |
||
125 | * Return Params adapter |
||
126 | * |
||
127 | * which responsible for universal mapping parameters from different |
||
128 | * source (default params, Data Table params, JGrid params) |
||
129 | * |
||
130 | * @return ParamAdapterInterface |
||
131 | */ |
||
132 | public function getParamAdapter() |
||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * |
||
141 | * @param $params |
||
142 | * @throws Exception\InvalidArgumentException |
||
143 | */ |
||
144 | public function setParamAdapter($params) |
||
158 | |||
159 | /** |
||
160 | * |
||
161 | * @return array | \Zend\Paginator\Paginator |
||
162 | * @throws Exception\LogicException |
||
163 | */ |
||
164 | public function getData() |
||
175 | |||
176 | /** |
||
177 | * |
||
178 | * @return Source\SourceInterface |
||
179 | */ |
||
180 | public function getSource() |
||
184 | |||
185 | /** |
||
186 | * |
||
187 | * @param \Zend\Db\Sql\Select | $source |
||
188 | * @return AbstractTable |
||
189 | * @throws Exception\LogicException |
||
190 | */ |
||
191 | public function setSource($source) |
||
208 | |||
209 | /** |
||
210 | * Get database adapter |
||
211 | * |
||
212 | * @return \Zend\Db\Adapter\Adapter |
||
213 | */ |
||
214 | public function getAdapter() |
||
218 | |||
219 | /** |
||
220 | * Set database adapter |
||
221 | * |
||
222 | * @param \Zend\Db\Adapter\Adapter $adapter |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function setAdapter($adapter) |
||
230 | |||
231 | /** |
||
232 | * Rendering table |
||
233 | * |
||
234 | * @param string $type (html | dataTableAjaxInit | dataTableJson) |
||
235 | * @param null $template |
||
236 | * @throws Exception\InvalidArgumentException |
||
237 | * @return string |
||
238 | */ |
||
239 | public function render($type = 'html', $template = null) |
||
260 | |||
261 | /** |
||
262 | * Init configuration like setting header, decorators, filters and others |
||
263 | * |
||
264 | * (call in render method as well) |
||
265 | */ |
||
266 | protected function initializable() |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @deprecated since version 2.0 |
||
290 | * |
||
291 | * Function replace by initFilters |
||
292 | */ |
||
293 | protected function initQuickSearch() |
||
297 | |||
298 | /** |
||
299 | * Init filters for quick search or filters for each column |
||
300 | * @param \Zend\Db\Sql\Select $query |
||
301 | */ |
||
302 | protected function initFilters($query) |
||
306 | |||
307 | /** |
||
308 | * |
||
309 | * @param array $headers |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setHeaders(array $headers) |
||
321 | |||
322 | /** |
||
323 | * Return array of headers |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getHeaders() |
||
331 | |||
332 | /** |
||
333 | * |
||
334 | * @param string $name type |
||
335 | * @return Header | boolean |
||
336 | * @throws Exception\LogicException |
||
337 | */ |
||
338 | public function getHeader($name) |
||
349 | |||
350 | /** |
||
351 | * Add new header |
||
352 | * |
||
353 | * @param string $name |
||
354 | * @param array $options |
||
355 | */ |
||
356 | public function addHeader($name, $options) |
||
362 | |||
363 | /** |
||
364 | * Get Row object |
||
365 | * |
||
366 | * @return Row |
||
367 | */ |
||
368 | public function getRow() |
||
375 | |||
376 | /** |
||
377 | * Set row object |
||
378 | * |
||
379 | * @param $row Row |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setRow($row) |
||
387 | |||
388 | /** |
||
389 | * Get Render object |
||
390 | * |
||
391 | * @return Render |
||
392 | */ |
||
393 | public function getRender() |
||
400 | |||
401 | /** |
||
402 | * Get render object |
||
403 | * @param \ZfTable\Render $render |
||
404 | */ |
||
405 | public function setRender(Render $render) |
||
409 | |||
410 | /** |
||
411 | * Rendering table |
||
412 | */ |
||
413 | public function __toString() |
||
417 | |||
418 | /** |
||
419 | * |
||
420 | * @return ModuleOptions |
||
421 | * @throws \Exception |
||
422 | */ |
||
423 | public function getOptions() |
||
432 | |||
433 | /** |
||
434 | * |
||
435 | * @return TableForm |
||
436 | */ |
||
437 | public function getForm() |
||
441 | |||
442 | /** |
||
443 | * |
||
444 | * @return TableFilter |
||
445 | */ |
||
446 | public function getFilter() |
||
450 | } |
||
451 |
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.