| Conditions | 12 |
| Paths | 8 |
| Total Lines | 36 |
| Code Lines | 27 |
| 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 |
||
| 83 | function stream_for($resource = '', array $options = []) |
||
| 84 | { |
||
| 85 | if (is_scalar($resource)) { |
||
| 86 | $stream = fopen('php://temp', 'r+'); |
||
| 87 | if ($resource !== '' && $stream !== false) { |
||
| 88 | fwrite($stream, $resource); |
||
| 89 | fseek($stream, 0); |
||
| 90 | } |
||
| 91 | return new Stream($stream, $options); |
||
| 92 | } |
||
| 93 | switch (gettype($resource)) { |
||
| 94 | case 'resource': |
||
| 95 | return new Stream($resource, $options); |
||
| 96 | case 'object': |
||
| 97 | if ($resource instanceof StreamInterface) { |
||
| 98 | return $resource; |
||
| 99 | } elseif (method_exists($resource, '__toString')) { |
||
| 100 | return stream_for((string) $resource, $options); |
||
| 101 | }return new PumpStream(function () use ($resource) { |
||
|
|
|||
| 102 | if (!$resource->valid()) { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | $result = $resource->current(); |
||
| 106 | $resource->next(); |
||
| 107 | return $result; |
||
| 108 | }, $options); |
||
| 109 | break; |
||
| 110 | case 'NULL': |
||
| 111 | return new Stream(fopen('php://temp', 'r+'), $options); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (is_callable($resource) && !is_null($resource)) { |
||
| 115 | return new \One\Http\PumpStream($resource, $options); |
||
| 116 | } |
||
| 117 | |||
| 118 | throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); |
||
| 119 | } |
||
| 120 |
As per the PSR-2 coding standard, the
break(or other terminating) statement must be on a line of its own.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.