Conditions | 4 |
Paths | 1 |
Total Lines | 68 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 3 |
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 | |||
79 | if ($tranferStats->hasResponse()) { |
||
80 | $validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody()); |
||
81 | |||
82 | if (false === $validatorResult) { |
||
83 | $validatorError = $url->getValidator()->getError(); |
||
84 | } |
||
85 | |||
86 | $statusCode = $tranferStats->getResponse()->getStatusCode(); |
||
87 | $transferTime = $tranferStats->getTransferTime(); |
||
88 | } else { |
||
89 | // If we have a connection error |
||
90 | $statusCode = 400; |
||
91 | $transferTime = 0; |
||
92 | $handlerError = curl_strerror($tranferStats->getHandlerErrorData()); |
||
93 | } |
||
94 | |||
95 | $resultCollection->append( |
||
96 | (new Result( |
||
97 | $url, |
||
98 | $statusCode, |
||
99 | $transferTime, |
||
100 | $handlerError, |
||
101 | $validatorResult, |
||
|
|||
102 | $validatorError |
||
103 | )) |
||
104 | ); |
||
105 | }, |
||
106 | ] |
||
107 | ); |
||
108 | }; |
||
109 | } |
||
110 | }; |
||
111 | |||
112 | $pool = new Pool($this->client, $requests(), [ |
||
113 | 'concurrency' => 5, |
||
114 | ]); |
||
115 | |||
116 | $promise = $pool->promise(); |
||
117 | |||
118 | $promise->wait(); |
||
119 | |||
120 | return $resultCollection; |
||
121 | } |
||
122 | } |
||
123 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: