| Conditions | 10 |
| Paths | 12 |
| Total Lines | 32 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 48 | public function onBefore(BeforeEvent $event) |
||
| 49 | { |
||
| 50 | if (!$item = array_shift($this->queue)) { |
||
| 51 | throw new \OutOfBoundsException('Mock queue is empty'); |
||
| 52 | } elseif ($item instanceof RequestException) { |
||
| 53 | throw $item; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Emulate reading a response body |
||
| 57 | $request = $event->getRequest(); |
||
| 58 | if ($this->readBodies && $request->getBody()) { |
||
| 59 | while (!$request->getBody()->eof()) { |
||
| 60 | $request->getBody()->read(8096); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $saveTo = $event->getRequest()->getConfig()->get('save_to'); |
||
| 65 | |||
| 66 | if (null !== $saveTo) { |
||
| 67 | $body = $item->getBody(); |
||
| 68 | |||
| 69 | if (is_resource($saveTo)) { |
||
| 70 | fwrite($saveTo, $body); |
||
| 71 | } elseif (is_string($saveTo)) { |
||
| 72 | file_put_contents($saveTo, $body); |
||
| 73 | } elseif ($saveTo instanceof StreamInterface) { |
||
| 74 | $saveTo->write($body); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | $event->intercept($item); |
||
| 79 | } |
||
| 80 | |||
| 148 |