Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Client |
||
| 12 | { |
||
| 13 | private $user; |
||
| 14 | private $key; |
||
| 15 | private $selectedLanguage; |
||
| 16 | |||
| 17 | private $lr; |
||
| 18 | private $l10n; |
||
| 19 | private $sortByType; |
||
| 20 | private $sortByDirection; |
||
| 21 | private $filter; |
||
| 22 | private $maxPassages; |
||
| 23 | private $groupByMode; |
||
| 24 | private $groupByGroupsOnPage; |
||
| 25 | private $groupByDocsInGroup; |
||
| 26 | private $page; |
||
| 27 | private $showMeCaptcha; |
||
| 28 | private $action; |
||
| 29 | private $query; |
||
| 30 | |||
| 31 | private $url = 'https://yandex.{lang}/search/xml'; |
||
| 32 | |||
| 33 | const AVAILABLE_REGIONS = [ |
||
| 34 | '225', '187', '149', '159', '20', '1092', '37', '30', '197', '47', '4', '65', '77', '66', '191', '10', '24', |
||
| 35 | '48', '75', '49', '33', '50', '192', '25', '38', '39', '21', '11', '193', '51', '1106', '2', '54', '42', '5', |
||
| 36 | '12', '63', '239', '41', '36', '43', '973', '22', '13', '64', '14', '7', '67', '35', '15', '62', '195', '53', |
||
| 37 | '172', '8', '76', '9', '45', '28', '56', '1', '1104', '213', '16', '23' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | const DOMAIN_RU = 'ru'; |
||
| 41 | const DOMAIN_COM_TR = 'com.tr'; |
||
| 42 | const DOMAIN_COM = 'com'; |
||
| 43 | |||
| 44 | const ACTION_LIMITS_INFO = 'limits-info'; |
||
| 45 | |||
| 46 | const L10N_RUSSIAN = 'ru'; |
||
| 47 | const L10N_UKRAINIAN = 'uk'; |
||
| 48 | const L10N_BELARUSIAN = 'be'; |
||
| 49 | const L10N_KAZAKH = 'kk'; |
||
| 50 | const L10N_TURKISH = 'tr'; |
||
| 51 | const L10N_ENGLISH = 'en'; |
||
| 52 | |||
| 53 | const SORT_BY_RLV = 'rlv'; |
||
| 54 | const SORT_BY_TM = 'tm'; |
||
| 55 | |||
| 56 | const SORTING_DIRECTION_DESC = 'descending'; |
||
| 57 | const SORTING_DIRECTION_ASC = 'ascending'; |
||
| 58 | |||
| 59 | const FILTER_STRICT = 'strict'; |
||
| 60 | const FILTER_MODERATE = 'moderate'; |
||
| 61 | const FILTER_NONE = 'none'; |
||
| 62 | |||
| 63 | const GROUP_MODE_FLAT = 'flat'; |
||
| 64 | const GROUP_MODE_DEEP = 'deep'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $user API user |
||
| 68 | * @param string $key API key |
||
| 69 | * @param string|null $domainLanguage (optional) Domain language |
||
| 70 | * @param string|null $endpointUrl |
||
| 71 | * @see https://yandex.ru/dev/xml/ |
||
| 72 | */ |
||
| 73 | 40 | public function __construct(string $user, string $key, ?string $domainLanguage = null, |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set a search region id. Region list you can find here: https://yandex.ru/dev/xml/doc/dg/reference/regions.html/ |
||
| 91 | * |
||
| 92 | * @param integer $lr region id |
||
| 93 | * @return Client $client |
||
| 94 | * @throws YandexInvalidArgumentException |
||
| 95 | * @see https://yandex.ru/dev/xml/doc/dg/reference/regions.html/ |
||
| 96 | */ |
||
| 97 | 2 | public function lr(int $lr): Client |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Set a response language |
||
| 110 | * |
||
| 111 | * @param string $l10n region id |
||
| 112 | * @return Client $client |
||
| 113 | * @throws YandexInvalidArgumentException |
||
| 114 | */ |
||
| 115 | 2 | public function l10n(string $l10n): Client |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Set a search region id. Region list you can find here: https://yandex.ru/dev/xml/doc/dg/reference/regions.html/ |
||
| 137 | * |
||
| 138 | * @param string $type sorting type |
||
| 139 | * @param string|null $direction (optional) sorting direction |
||
| 140 | * @return Client $client |
||
| 141 | * @throws YandexInvalidArgumentException |
||
| 142 | */ |
||
| 143 | 6 | public function sortBy(string $type, ?string $direction = null): Client |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Set a filtering rules |
||
| 164 | * |
||
| 165 | * @param string $type filtering type |
||
| 166 | * @return Client $client |
||
| 167 | * @throws YandexInvalidArgumentException |
||
| 168 | */ |
||
| 169 | 2 | public function filter(string $type): Client |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Set a maximum passages number |
||
| 182 | * |
||
| 183 | * @param integer $number maximum passages number |
||
| 184 | * @return Client $client |
||
| 185 | * @throws YandexInvalidArgumentException |
||
| 186 | */ |
||
| 187 | 2 | public function maxPassages(int $number): Client |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Set a grouping rules |
||
| 200 | * |
||
| 201 | * @param string $mode grouping method |
||
| 202 | * @param integer|null $groupsOnPage (optional) groups on page number |
||
| 203 | * @param integer|null $docsInGroup (optional) documents in group number |
||
| 204 | * @return Client $client |
||
| 205 | * @throws YandexInvalidArgumentException |
||
| 206 | */ |
||
| 207 | 10 | public function groupBy(string $mode, |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Set a page number |
||
| 238 | * |
||
| 239 | * @param integer $number page number. Starts from "0" |
||
| 240 | * @return Client $client |
||
| 241 | * @throws YandexInvalidArgumentException |
||
| 242 | */ |
||
| 243 | 2 | public function page(int $number): Client |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Enable Captcha on result page |
||
| 256 | * |
||
| 257 | * @return Client $client |
||
| 258 | */ |
||
| 259 | 2 | public function showMeCaptcha(): Client |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Set query string |
||
| 268 | * |
||
| 269 | * @param string $query query string |
||
| 270 | * @return Client $client |
||
| 271 | */ |
||
| 272 | 16 | public function query(string $query): Client |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Set action value (at this time uses only for getting information about day or hour limits) |
||
| 281 | * |
||
| 282 | * @return Client $client |
||
| 283 | */ |
||
| 284 | 4 | public function action(): Client |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Retrieving search results |
||
| 293 | * |
||
| 294 | * @return Client $client |
||
| 295 | * @throws YandexException|ClientException|GuzzleException |
||
| 296 | */ |
||
| 297 | public function get(): string |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Sends a request |
||
| 306 | * |
||
| 307 | * @return string Response body |
||
| 308 | * @throws YandexException|ClientException|GuzzleException |
||
| 309 | */ |
||
| 310 | protected function getData(): string |
||
| 334 | |||
| 335 | 2 | private function generateSortByAttribute(): string |
|
| 345 | |||
| 346 | 8 | private function generateGroupByAttribute(): string |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Generates end-point URL with parameters |
||
| 367 | * |
||
| 368 | * @return string Full end-point URL |
||
| 369 | * @throws YandexException |
||
| 370 | */ |
||
| 371 | 16 | public function getEndpointUrlWithParams(): string |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Generates action parameters array |
||
| 393 | * |
||
| 394 | * @return array Action parameters array |
||
| 395 | */ |
||
| 396 | 2 | public function getActionParams(): array |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Generates query parameters array |
||
| 405 | * |
||
| 406 | * @return array Query parameters array |
||
| 407 | */ |
||
| 408 | 14 | public function getQueryParams(): array |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Generates end-point URL |
||
| 426 | * |
||
| 427 | * @return string Full end-point URL |
||
| 428 | */ |
||
| 429 | 18 | public function getEndpointUrl(): string |
|
| 433 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.