| Conditions | 8 |
| Paths | 224 |
| Total Lines | 65 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | public function run() |
||
| 35 | { |
||
| 36 | $proxyManager = Yii::createObject(ProxyManager::class); |
||
| 37 | $accountBuilder = Yii::createObject([ |
||
| 38 | 'class' => AccountBuilder::class, |
||
| 39 | 'account' => $this->account, |
||
| 40 | ]); |
||
| 41 | |||
| 42 | try { |
||
| 43 | $proxy = $proxyManager->reserve(); |
||
| 44 | $httpClient = Client::factory($proxy); |
||
| 45 | $scraper = Yii::createObject(AccountScraper::class, [ |
||
| 46 | $httpClient, |
||
| 47 | ]); |
||
| 48 | |||
| 49 | $accountData = $this->fetchAccountData($scraper); |
||
| 50 | $posts = $scraper->fetchLastPosts($accountData->username); |
||
| 51 | |||
| 52 | $proxyManager->release($proxy, false); |
||
| 53 | unset($proxy); |
||
| 54 | |||
| 55 | if ($accountData->isPrivate) { |
||
| 56 | $accountBuilder |
||
| 57 | ->setIsInValid(AccountInvalidationType::IS_PRIVATE) |
||
| 58 | ->setNextStatsUpdate(true) |
||
| 59 | ->save(); |
||
| 60 | } else { |
||
| 61 | $accountBuilder |
||
| 62 | ->setDetails($accountData) |
||
| 63 | ->setIdents($accountData) |
||
| 64 | ->setIsValid() |
||
| 65 | ->setStats($accountData, $posts) |
||
| 66 | ->setNextStatsUpdate() |
||
| 67 | ->save(); |
||
| 68 | |||
| 69 | $mediaManager = Yii::createObject(MediaManager::class); |
||
| 70 | $mediaManager->addToAccount($this->account, $posts); |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | } catch (NotFoundHttpException $exception) { |
||
| 75 | $accountBuilder |
||
| 76 | ->setIsInValid(AccountInvalidationType::NOT_FOUND) |
||
| 77 | ->setNextStatsUpdate(true) |
||
| 78 | ->save(); |
||
| 79 | } catch (RestrictedProfileException $exception) { |
||
| 80 | $accountBuilder |
||
| 81 | ->setIsInValid(AccountInvalidationType::RESTRICTED_PROFILE) |
||
| 82 | ->setNextStatsUpdate(true) |
||
| 83 | ->save(); |
||
| 84 | } catch (LoginAndSignupPageException $exception) { |
||
| 85 | $accountBuilder |
||
| 86 | ->setNextStatsUpdate(1) |
||
| 87 | ->save(); |
||
| 88 | if (isset($proxy)) { // must be |
||
| 89 | $proxyManager->release($proxy, true); |
||
| 90 | } |
||
| 91 | } catch (RequestException $exception) { |
||
| 92 | $accountBuilder |
||
| 93 | ->setIsInValid() |
||
| 94 | ->setNextStatsUpdate(true) |
||
| 95 | ->save(); |
||
| 96 | } finally { |
||
| 97 | if (isset($proxy)) { |
||
| 98 | $proxyManager->release($proxy); |
||
| 99 | } |
||
| 130 | } |