Complex classes like AbstractListView 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 AbstractListView, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class AbstractListView |
||
20 | { |
||
21 | /** @var string Module name. */ |
||
22 | protected $moduleName; |
||
23 | |||
24 | /** @var string[] Column fields */ |
||
25 | protected $fields = []; |
||
26 | |||
27 | /** @var array Records list from api. */ |
||
28 | protected $recordsList = []; |
||
29 | |||
30 | /** @var int Current page. */ |
||
31 | private $page = 1; |
||
32 | |||
33 | /** @var int The number of items on the page. */ |
||
34 | protected $limit = 0; |
||
35 | |||
36 | /** @var int Offset. */ |
||
37 | protected $offset = 0; |
||
38 | |||
39 | /** @var string Sorting direction. */ |
||
40 | protected $order; |
||
41 | |||
42 | /** @var string Sets the ORDER BY part of the query record list. */ |
||
43 | protected $orderField; |
||
44 | |||
45 | /** @var array Conditions. */ |
||
46 | protected $conditions = []; |
||
47 | |||
48 | /** @var bool Use raw data. */ |
||
49 | protected $rawData = false; |
||
50 | |||
51 | /** @var string Action name */ |
||
52 | protected $actionName = 'RecordsList'; |
||
53 | |||
54 | /** @var array Custom views */ |
||
55 | protected $customViews; |
||
56 | |||
57 | /** @var int Custom view ID */ |
||
58 | protected $cvId; |
||
59 | |||
60 | /** |
||
61 | * Get instance. |
||
62 | * |
||
63 | * @param string $moduleName |
||
64 | * @param string $viewName |
||
65 | * |
||
66 | * @return self |
||
|
|||
67 | */ |
||
68 | public static function getInstance(string $moduleName, string $viewName = 'ListView'): self |
||
69 | { |
||
70 | $handlerModule = \App\Loader::getModuleClassName($moduleName, 'Model', $viewName); |
||
71 | $self = new $handlerModule(); |
||
72 | $self->moduleName = $moduleName; |
||
73 | $self->limit = \App\Config::$itemsPrePage ?: 15; |
||
74 | return $self; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Function to get the Module Model. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getModuleName(): string |
||
83 | { |
||
84 | return $this->moduleName; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Function to set raw data. |
||
89 | * |
||
90 | * @param bool $rawData |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | public function setRawData(bool $rawData): self |
||
95 | { |
||
96 | $this->rawData = $rawData; |
||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set custom fields. |
||
102 | * |
||
103 | * @param array $fields |
||
104 | * |
||
105 | * @return self |
||
106 | */ |
||
107 | public function setFields(array $fields): self |
||
108 | { |
||
109 | $this->fields = $fields; |
||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set limit. |
||
115 | * |
||
116 | * @param int $limit |
||
117 | * |
||
118 | * @return self |
||
119 | */ |
||
120 | public function setLimit(int $limit): self |
||
121 | { |
||
122 | $this->limit = $limit; |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Set offset. |
||
128 | * |
||
129 | * @param int $offset |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | public function setOffset(int $offset): self |
||
134 | { |
||
135 | $this->offset = $offset; |
||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Set order. |
||
141 | * |
||
142 | * @param string $field |
||
143 | * @param string $direction |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | public function setOrder(string $field, string $direction): self |
||
148 | { |
||
149 | $this->orderField = $field; |
||
150 | $this->order = $direction; |
||
151 | return $this; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set conditions. |
||
156 | * |
||
157 | * @param array $conditions |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | public function setConditions(array $conditions) |
||
162 | { |
||
163 | $this->conditions = $conditions; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Set custom view ID. |
||
168 | * |
||
169 | * @param int $cvId |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setCvId(int $cvId): self |
||
174 | { |
||
175 | $this->cvId = $cvId; |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Load a list of records from the API. |
||
181 | * |
||
182 | * @return self |
||
183 | */ |
||
184 | public function loadRecordsList(): self |
||
185 | { |
||
186 | $this->recordsList = $this->getFromApi($this->getApiHeaders()); |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Gets headers for api. |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getApiHeaders(): array |
||
196 | { |
||
197 | $headers = [ |
||
198 | 'x-row-count' => 1, |
||
199 | 'x-row-limit' => $this->limit, |
||
200 | 'x-row-offset' => $this->offset, |
||
201 | ]; |
||
202 | if (!empty($this->fields)) { |
||
203 | $headers['x-fields'] = \App\Json::encode($this->fields); |
||
204 | } |
||
205 | if (!empty($this->conditions)) { |
||
206 | $headers['x-condition'] = \App\Json::encode($this->conditions); |
||
207 | } |
||
208 | if ($this->rawData) { |
||
209 | $headers['x-raw-data'] = 1; |
||
210 | } |
||
211 | if (!empty($this->order) && $this->orderField) { |
||
212 | $headers['x-order-by'] = \App\Json::encode([$this->orderField => $this->order]); |
||
213 | } |
||
214 | if ($cvId = $this->getDefaultCustomView()) { |
||
215 | $headers['x-cv-id'] = $cvId; |
||
216 | } |
||
217 | return $headers; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get data from api. |
||
222 | * |
||
223 | * @param array $headers |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function getFromApi(array $headers): array |
||
233 | |||
234 | /** |
||
235 | * Get records list model. |
||
236 | * |
||
237 | * @return Record[] |
||
238 | */ |
||
239 | public function getRecordsListModel(): array |
||
240 | { |
||
241 | $recordsModel = []; |
||
242 | if (!empty($this->recordsList['records'])) { |
||
243 | foreach ($this->recordsList['records'] as $id => $value) { |
||
244 | $recordModel = Record::getInstance($this->getModuleName()); |
||
258 | |||
259 | /** |
||
260 | * Get headers of list. |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function getHeaders(): array |
||
275 | |||
276 | /** |
||
277 | * Gets custom views. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getCustomViews(): array |
||
293 | |||
294 | /** |
||
295 | * Get default custom view ID. |
||
296 | * |
||
297 | * @return int|null |
||
298 | */ |
||
299 | public function getDefaultCustomView(): ?int |
||
311 | |||
312 | /** |
||
313 | * Get all rows count. |
||
314 | * |
||
315 | * @return int |
||
316 | */ |
||
317 | public function getCount(): int |
||
321 | |||
322 | /** |
||
323 | * Get current page. |
||
324 | * |
||
325 | * @return int |
||
326 | */ |
||
327 | public function getPage(): int |
||
334 | |||
335 | /** |
||
336 | * Sets page number. |
||
337 | * |
||
338 | * @param int $page |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function setPage(int $page) |
||
347 | |||
348 | /** |
||
349 | * Is there more pages. |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function isMorePages(): bool |
||
357 | } |
||
358 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.