| Conditions | 11 |
| Paths | 11 |
| Total Lines | 39 |
| Code Lines | 28 |
| 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 |
||
| 66 | function stream_for($resource = '', array $options = []) |
||
| 67 | { |
||
| 68 | if (is_scalar($resource)) { |
||
| 69 | $stream = fopen('php://temp', 'r+'); |
||
| 70 | if ($resource !== '') { |
||
| 71 | fwrite($stream, $resource); |
||
|
|
|||
| 72 | fseek($stream, 0); |
||
| 73 | } |
||
| 74 | return new \One\Http\Stream($stream, $options); |
||
| 75 | } |
||
| 76 | |||
| 77 | switch (gettype($resource)) { |
||
| 78 | case 'resource': |
||
| 79 | return new \One\Http\Stream($resource, $options); |
||
| 80 | case 'object': |
||
| 81 | if ($resource instanceof StreamInterface) { |
||
| 82 | return $resource; |
||
| 83 | } elseif ($resource instanceof \Iterator) { |
||
| 84 | return new \One\Http\PumpStream(function () use ($resource) { |
||
| 85 | if (!$resource->valid()) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | $result = $resource->current(); |
||
| 89 | $resource->next(); |
||
| 90 | return $result; |
||
| 91 | }, $options); |
||
| 92 | } elseif (method_exists($resource, '__toString')) { |
||
| 93 | return stream_for((string) $resource, $options); |
||
| 94 | } |
||
| 95 | break; |
||
| 96 | case 'NULL': |
||
| 97 | return new \One\Http\Stream(fopen('php://temp', 'r+'), $options); |
||
| 98 | } |
||
| 99 | |||
| 100 | if (is_callable($resource)) { |
||
| 101 | return new \One\Http\PumpStream($resource, $options); |
||
| 102 | } |
||
| 103 | |||
| 104 | throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); |
||
| 105 | } |
||
| 106 |