Conditions | 5 |
Paths | 104 |
Total Lines | 55 |
Code Lines | 41 |
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 |
||
32 | public function run() |
||
33 | { |
||
34 | $proxyManager = Yii::createObject(ProxyManager::class); |
||
35 | $accountUpdater = Yii::createObject([ |
||
36 | 'class' => AccountUpdater::class, |
||
37 | 'account' => $this->account, |
||
38 | ]); |
||
39 | |||
40 | try { |
||
41 | $proxy = $proxyManager->reserve($this->account); |
||
42 | $httpClient = Client::factory($proxy, [], 3600); |
||
43 | |||
44 | $scraper = Yii::createObject(AccountScraper::class, [ |
||
45 | $httpClient, |
||
46 | ]); |
||
47 | |||
48 | $accountData = $this->fetchAccountData($scraper); |
||
49 | $posts = $scraper->fetchLastPosts($accountData->username); |
||
50 | |||
51 | $proxyManager->release($proxy); |
||
52 | unset($proxy); |
||
53 | |||
54 | |||
55 | if ($accountData->isPrivate) { |
||
56 | $accountUpdater |
||
57 | ->setIsInValid(AccountInvalidationType::IS_PRIVATE) |
||
58 | ->setNextStatsUpdate(true) |
||
59 | ->save(); |
||
60 | } else { |
||
61 | $accountUpdater |
||
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 | $accountUpdater |
||
76 | ->setIsInValid(AccountInvalidationType::NOT_FOUND) |
||
77 | ->setNextStatsUpdate(true) |
||
78 | ->save(); |
||
79 | } catch (RequestException $exception) { |
||
80 | $accountUpdater |
||
81 | ->setIsInValid() |
||
82 | ->setNextStatsUpdate(true) |
||
83 | ->save(); |
||
84 | } finally { |
||
85 | if (isset($proxy)) { |
||
86 | $proxyManager->release($proxy); |
||
87 | } |
||
119 | } |