Conditions | 10 |
Paths | 292 |
Total Lines | 75 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
33 | public function run() |
||
34 | { |
||
35 | /** |
||
36 | * @var ProxyManager $proxyManager |
||
37 | */ |
||
38 | $proxyManager = Yii::createObject(ProxyManager::class); |
||
39 | /** |
||
40 | * @var AccountUpdater $accountUpdater |
||
41 | */ |
||
42 | $accountUpdater = Yii::createObject([ |
||
43 | 'class' => AccountUpdater::class, |
||
44 | 'account' => $this->account, |
||
45 | ]); |
||
46 | |||
47 | $proxy = $proxyManager->reserve($this->account); |
||
48 | try { |
||
49 | $httpClient = Client::factory($proxy, [], 3600); |
||
50 | |||
51 | $scraper = Yii::createObject(AccountScraper::class, [ |
||
52 | $httpClient, |
||
53 | ]); |
||
54 | |||
55 | $accountData = $this->fetchAccountData($scraper); |
||
56 | $posts = $scraper->fetchLastPosts($accountData->username); |
||
57 | |||
58 | $proxyManager->release($proxy); |
||
59 | unset($proxy); |
||
60 | |||
61 | |||
62 | if ($accountData->isPrivate) { |
||
63 | $accountUpdater |
||
64 | ->setIsInValid(AccountInvalidationType::IS_PRIVATE) |
||
65 | ->setNextStatsUpdate(true) |
||
66 | ->save(); |
||
67 | } else { |
||
68 | $accountUpdater |
||
69 | ->setDetails($accountData) |
||
70 | ->setIdents($accountData) |
||
71 | ->setIsValid() |
||
72 | ->setStats($accountData, $posts) |
||
73 | ->setNextStatsUpdate() |
||
74 | ->save(); |
||
75 | |||
76 | $mediaManager = Yii::createObject(MediaManager::class); |
||
77 | $mediaManager->addToAccount($this->account, $posts); |
||
78 | |||
79 | } |
||
80 | |||
81 | } catch (NotFoundHttpException $exception) { |
||
82 | $accountUpdater |
||
83 | ->setIsInValid(AccountInvalidationType::NOT_FOUND) |
||
84 | ->setNextStatsUpdate(true) |
||
85 | ->save(); |
||
86 | } catch (RestrictedProfileException $exception) { |
||
87 | $accountUpdater |
||
88 | ->setIsInValid(AccountInvalidationType::RESTRICTED_PROFILE) |
||
89 | ->setNextStatsUpdate(true) |
||
90 | ->save(); |
||
91 | } catch (RequestException $exception) { |
||
92 | if (isset($proxy) && (strpos($exception->getMessage(), "Failed to connect to " . $proxy->ip) !== false || strpos($exception->getMessage(), "malformed") !== false)) { |
||
93 | $type = strpos($exception->getMessage(), "malformed") !== false ? AccountInvalidationType::PROXY_ERROR : AccountInvalidationType::PROXY_TIMEOUT; |
||
94 | $proxyManager->invalidate($proxy); |
||
95 | $accountUpdater |
||
96 | ->setIsInvalid($type) |
||
97 | ->setNextStatsUpdate(true) |
||
98 | ->save(); |
||
99 | } else { |
||
100 | $accountUpdater |
||
101 | ->setIsInvalidUnknown($exception->getMessage()) |
||
102 | ->setNextStatsUpdate(true) |
||
103 | ->save(); |
||
104 | } |
||
105 | } finally { |
||
106 | if (isset($proxy)) { |
||
107 | $proxyManager->release($proxy); |
||
108 | } |
||
140 | } |