| Conditions | 14 |
| Paths | 27 |
| Total Lines | 64 |
| 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 |
||
| 63 | public function __invoke(RequestInterface $request, array $options) |
||
| 64 | { |
||
| 65 | if (!$this->queue) { |
||
|
|
|||
| 66 | throw new \OutOfBoundsException('Mock queue is empty'); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (isset($options['delay'])) { |
||
| 70 | usleep($options['delay'] * 1000); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->lastRequest = $request; |
||
| 74 | $this->lastOptions = $options; |
||
| 75 | $response = array_shift($this->queue); |
||
| 76 | |||
| 77 | if (isset($options['on_headers'])) { |
||
| 78 | if (!is_callable($options['on_headers'])) { |
||
| 79 | throw new \InvalidArgumentException('on_headers must be callable'); |
||
| 80 | } |
||
| 81 | try { |
||
| 82 | $options['on_headers']($response); |
||
| 83 | } catch (\Exception $e) { |
||
| 84 | $msg = 'An error was encountered during the on_headers event'; |
||
| 85 | $response = new RequestException($msg, $request, $response, $e); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if (is_callable($response)) { |
||
| 90 | $response = call_user_func($response, $request, $options); |
||
| 91 | } |
||
| 92 | |||
| 93 | $response = $response instanceof \Exception |
||
| 94 | ? \GuzzleHttp\Promise\rejection_for($response) |
||
| 95 | : \GuzzleHttp\Promise\promise_for($response); |
||
| 96 | |||
| 97 | return $response->then( |
||
| 98 | function ($value) use ($request, $options) { |
||
| 99 | $this->invokeStats($request, $options, $value); |
||
| 100 | if ($this->onFulfilled) { |
||
| 101 | call_user_func($this->onFulfilled, $value); |
||
| 102 | } |
||
| 103 | if (isset($options['sink'])) { |
||
| 104 | $contents = (string) $value->getBody(); |
||
| 105 | $sink = $options['sink']; |
||
| 106 | |||
| 107 | if (is_resource($sink)) { |
||
| 108 | fwrite($sink, $contents); |
||
| 109 | } elseif (is_string($sink)) { |
||
| 110 | file_put_contents($sink, $contents); |
||
| 111 | } elseif ($sink instanceof \Psr\Http\Message\StreamInterface) { |
||
| 112 | $sink->write($contents); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $value; |
||
| 117 | }, |
||
| 118 | function ($reason) use ($request, $options) { |
||
| 119 | $this->invokeStats($request, $options, null, $reason); |
||
| 120 | if ($this->onRejected) { |
||
| 121 | call_user_func($this->onRejected, $reason); |
||
| 122 | } |
||
| 123 | return \GuzzleHttp\Promise\rejection_for($reason); |
||
| 124 | } |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 190 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.