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 | protected $actionName = 'RecordsList'; |
||
| 52 | |||
| 53 | /** @var array Custom views */ |
||
| 54 | protected $customViews; |
||
| 55 | |||
| 56 | /** @var int Custom view ID */ |
||
| 57 | protected $cvId; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get instance. |
||
| 61 | * |
||
| 62 | * @param string $moduleName |
||
| 63 | * @param string $viewName |
||
| 64 | * |
||
| 65 | * @return self |
||
|
|
|||
| 66 | */ |
||
| 67 | public static function getInstance(string $moduleName, string $viewName = 'ListView'): self |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Function to get the Module Model. |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getModuleName(): string |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Function to set raw data. |
||
| 88 | * |
||
| 89 | * @param bool $rawData |
||
| 90 | * |
||
| 91 | * @return self |
||
| 92 | */ |
||
| 93 | public function setRawData(bool $rawData): self |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set custom fields. |
||
| 101 | * |
||
| 102 | * @param array $fields |
||
| 103 | * |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function setFields(array $fields): self |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set limit. |
||
| 114 | * |
||
| 115 | * @param int $limit |
||
| 116 | * |
||
| 117 | * @return self |
||
| 118 | */ |
||
| 119 | public function setLimit(int $limit): self |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set offset. |
||
| 127 | * |
||
| 128 | * @param int $offset |
||
| 129 | * |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | public function setOffset(int $offset): self |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set order. |
||
| 140 | * |
||
| 141 | * @param string $field |
||
| 142 | * @param string $direction |
||
| 143 | * |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | public function setOrder(string $field, string $direction): self |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set conditions. |
||
| 155 | * |
||
| 156 | * @param array $conditions |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function setConditions(array $conditions) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Undocumented function. |
||
| 167 | * |
||
| 168 | * @param int $cvId |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setCvId(int $cvId): self |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Load a list of records from the API. |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function loadRecordsList(): self |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Gets headers for api. |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function getApiHeaders(): array |
||
| 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 |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get headers of list. |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getHeaders(): array |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Gets custom views. |
||
| 280 | * |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function getCustomViews(): array |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get default custom view ID. |
||
| 293 | * |
||
| 294 | * @return int|null |
||
| 295 | */ |
||
| 296 | public function getDefaultCustomView(): ?int |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get all rows count. |
||
| 311 | * |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public function getCount(): int |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get current page. |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | public function getPage(): int |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Sets page number. |
||
| 334 | * |
||
| 335 | * @param int $page |
||
| 336 | * |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function setPage(int $page) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Is there more pages. |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public function isMorePages(): bool |
||
| 354 | } |
||
| 355 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.