Conditions | 4 |
Paths | 1 |
Total Lines | 70 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 4 |
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 |
||
54 | public function run() |
||
55 | { |
||
56 | $urls = $this->urlProvider->getUrls(); |
||
57 | $client = $this->client; |
||
58 | |||
59 | //This is a bit messie, need a refacto |
||
60 | $resultCollection = new ResultCollection(); |
||
61 | |||
62 | $requests = function () use ($urls, $client, $resultCollection) { |
||
63 | foreach ($urls as $url) { |
||
64 | yield function () use ($client, $url, $resultCollection) { |
||
65 | return $client->sendAsync( |
||
66 | new Request( |
||
67 | $url->getMethod(), |
||
68 | $url->getUrl(), |
||
69 | $url->getHeaders() |
||
70 | ), |
||
71 | [ |
||
72 | 'timeout' => $url->getTimeout(), |
||
73 | 'connect_timeout' => $url->getTimeout(), |
||
74 | 'on_stats' => function (TransferStats $tranferStats) use ($url, $resultCollection) { |
||
75 | |||
76 | $handlerError = null; |
||
77 | $validatorError = null; |
||
78 | $validatorResult = null; |
||
79 | |||
80 | if ($tranferStats->hasResponse()) { |
||
81 | $validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody()); |
||
82 | |||
83 | if (false === $validatorResult) { |
||
84 | $validatorError = $url->getValidator()->getError(); |
||
85 | } |
||
86 | |||
87 | $statusCode = $tranferStats->getResponse()->getStatusCode(); |
||
88 | $transferTime = $tranferStats->getTransferTime(); |
||
89 | } else { |
||
90 | // If we have a connection error |
||
91 | $statusCode = 400; |
||
92 | $transferTime = 0; |
||
93 | $handlerError = curl_strerror($tranferStats->getHandlerErrorData()); |
||
94 | } |
||
95 | |||
96 | $resultCollection->offsetSet( |
||
97 | $url->getName(), |
||
98 | (new Result( |
||
99 | $url, |
||
100 | $statusCode, |
||
101 | $transferTime, |
||
102 | $handlerError, |
||
103 | $validatorResult, |
||
104 | $validatorError |
||
105 | )) |
||
106 | ); |
||
107 | }, |
||
108 | ] |
||
109 | ); |
||
110 | }; |
||
111 | } |
||
112 | }; |
||
113 | |||
114 | $pool = new Pool($this->client, $requests(), [ |
||
115 | 'concurrency' => 5, |
||
116 | ]); |
||
117 | |||
118 | $promise = $pool->promise(); |
||
119 | |||
120 | $promise->wait(); |
||
121 | |||
122 | return $resultCollection; |
||
123 | } |
||
124 | } |
||
125 |