Conditions | 12 |
Paths | 11 |
Total Lines | 52 |
Code Lines | 22 |
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 |
||
41 | public function __construct($stream, LoopInterface $loop = null, $readChunkSize = null, WritableStreamInterface $buffer = null) |
||
42 | { |
||
43 | if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") { |
||
44 | throw new InvalidArgumentException('First parameter must be a valid stream resource'); |
||
45 | } |
||
46 | |||
47 | // ensure resource is opened for reading and wrting (fopen mode must contain "+") |
||
48 | $meta = \stream_get_meta_data($stream); |
||
49 | if (isset($meta['mode']) && $meta['mode'] !== '' && \strpos($meta['mode'], '+') === false) { |
||
50 | throw new InvalidArgumentException('Given stream resource is not opened in read and write mode'); |
||
51 | } |
||
52 | |||
53 | // this class relies on non-blocking I/O in order to not interrupt the event loop |
||
54 | // e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918 |
||
55 | if (\stream_set_blocking($stream, false) !== true) { |
||
56 | throw new \RuntimeException('Unable to set stream resource to non-blocking mode'); |
||
57 | } |
||
58 | |||
59 | // Use unbuffered read operations on the underlying stream resource. |
||
60 | // Reading chunks from the stream may otherwise leave unread bytes in |
||
61 | // PHP's stream buffers which some event loop implementations do not |
||
62 | // trigger events on (edge triggered). |
||
63 | // This does not affect the default event loop implementation (level |
||
64 | // triggered), so we can ignore platforms not supporting this (HHVM). |
||
65 | // Pipe streams (such as STDIN) do not seem to require this and legacy |
||
66 | // PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this. |
||
67 | if (\function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) { |
||
68 | \stream_set_read_buffer($stream, 0); |
||
69 | } |
||
70 | |||
71 | if ($buffer === null) { |
||
72 | $buffer = new WritableResourceStream($stream, $loop); |
||
73 | } |
||
74 | |||
75 | $this->stream = $stream; |
||
76 | $this->loop = $loop ?: Loop::get(); |
||
77 | $this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize; |
||
78 | $this->buffer = $buffer; |
||
79 | |||
80 | $that = $this; |
||
81 | |||
82 | $this->buffer->on('error', function ($error) use ($that) { |
||
83 | $that->emit('error', array($error)); |
||
84 | }); |
||
85 | |||
86 | $this->buffer->on('close', array($this, 'close')); |
||
87 | |||
88 | $this->buffer->on('drain', function () use ($that) { |
||
89 | $that->emit('drain'); |
||
90 | }); |
||
91 | |||
92 | $this->resume(); |
||
93 | } |
||
228 |