| Conditions | 3 |
| Paths | 1 |
| Total Lines | 55 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 55 | public function run() |
||
| 56 | { |
||
| 57 | $stats = []; |
||
|
|
|||
| 58 | |||
| 59 | $urls = $this->urlProvider->getUrls(); |
||
| 60 | $client = $this->client; |
||
| 61 | |||
| 62 | $resultCollection = new ResultCollection(); |
||
| 63 | |||
| 64 | $requests = function () use ($urls, $client, $resultCollection) { |
||
| 65 | foreach ($urls as $url) { |
||
| 66 | yield function () use ($client, $url, $resultCollection) { |
||
| 67 | return $client->sendAsync( |
||
| 68 | new Request( |
||
| 69 | $url->getMethod(), |
||
| 70 | $url->getUrl(), |
||
| 71 | $url->getHeaders() |
||
| 72 | ), |
||
| 73 | [ |
||
| 74 | 'timeout' => $url->getTimeout(), |
||
| 75 | 'on_stats' => function (TransferStats $tranferStats) use ($url, $resultCollection) { |
||
| 76 | |||
| 77 | if ($tranferStats->hasResponse()) { |
||
| 78 | $statusCode = $tranferStats->getResponse()->getStatusCode(); |
||
| 79 | $transferTime = $tranferStats->getTransferTime(); |
||
| 80 | } else { |
||
| 81 | // If we have a connection error |
||
| 82 | $statusCode = 400; |
||
| 83 | $transferTime = 0; |
||
| 84 | } |
||
| 85 | |||
| 86 | $resultCollection->append( |
||
| 87 | (new Result( |
||
| 88 | $url, |
||
| 89 | $statusCode, |
||
| 90 | $transferTime |
||
| 91 | )) |
||
| 92 | ); |
||
| 93 | }, |
||
| 94 | ] |
||
| 95 | ); |
||
| 96 | }; |
||
| 97 | } |
||
| 98 | }; |
||
| 99 | |||
| 100 | $pool = new Pool($this->client, $requests(), [ |
||
| 101 | 'concurrency' => 5, |
||
| 102 | ]); |
||
| 103 | |||
| 104 | $promise = $pool->promise(); |
||
| 105 | |||
| 106 | $promise->wait(); |
||
| 107 | |||
| 108 | return $resultCollection; |
||
| 109 | } |
||
| 110 | } |
||
| 111 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.