| Conditions | 16 |
| Paths | 27 |
| Total Lines | 62 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 80 | public function __invoke(RequestInterface $request, array $options): PromiseInterface |
||
| 81 | { |
||
| 82 | if (!$this->queue) { |
||
|
|
|||
| 83 | throw new \OutOfBoundsException('Mock queue is empty'); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (isset($options['delay']) && \is_numeric($options['delay'])) { |
||
| 87 | \usleep((int) $options['delay'] * 1000); |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->lastRequest = $request; |
||
| 91 | $this->lastOptions = $options; |
||
| 92 | $response = \array_shift($this->queue); |
||
| 93 | |||
| 94 | if (isset($options['on_headers'])) { |
||
| 95 | if (!\is_callable($options['on_headers'])) { |
||
| 96 | throw new \InvalidArgumentException('on_headers must be callable'); |
||
| 97 | } |
||
| 98 | try { |
||
| 99 | $options['on_headers']($response); |
||
| 100 | } catch (\Exception $e) { |
||
| 101 | $msg = 'An error was encountered during the on_headers event'; |
||
| 102 | $response = new RequestException($msg, $request, $response, $e); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if (\is_callable($response)) { |
||
| 107 | $response = $response($request, $options); |
||
| 108 | } |
||
| 109 | |||
| 110 | $response = $response instanceof \Throwable |
||
| 111 | ? P\Create::rejectionFor($response) |
||
| 112 | : P\Create::promiseFor($response); |
||
| 113 | |||
| 114 | return $response->then( |
||
| 115 | function (?ResponseInterface $value) use ($request, $options) { |
||
| 116 | $this->invokeStats($request, $options, $value); |
||
| 117 | if ($this->onFulfilled) { |
||
| 118 | ($this->onFulfilled)($value); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($value !== null && isset($options['sink'])) { |
||
| 122 | $contents = (string) $value->getBody(); |
||
| 123 | $sink = $options['sink']; |
||
| 124 | |||
| 125 | if (\is_resource($sink)) { |
||
| 126 | \fwrite($sink, $contents); |
||
| 127 | } elseif (\is_string($sink)) { |
||
| 128 | \file_put_contents($sink, $contents); |
||
| 129 | } elseif ($sink instanceof StreamInterface) { |
||
| 130 | $sink->write($contents); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return $value; |
||
| 135 | }, |
||
| 136 | function ($reason) use ($request, $options) { |
||
| 137 | $this->invokeStats($request, $options, null, $reason); |
||
| 138 | if ($this->onRejected) { |
||
| 139 | ($this->onRejected)($reason); |
||
| 140 | } |
||
| 141 | return P\Create::rejectionFor($reason); |
||
| 142 | } |
||
| 212 |
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.