Conditions | 10 |
Paths | 1 |
Total Lines | 72 |
Code Lines | 42 |
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 |
||
28 | public function __construct(PromiseInterface $promise) |
||
29 | { |
||
30 | $out = $this; |
||
31 | $store =& $this->stream; |
||
32 | $buffer =& $this->buffer; |
||
33 | $ending =& $this->ending; |
||
34 | $closed =& $this->closed; |
||
35 | |||
36 | $this->promise = $promise->then( |
||
37 | function ($stream) { |
||
38 | if (!$stream instanceof WritableStreamInterface) { |
||
39 | throw new InvalidArgumentException('Not a writable stream'); |
||
40 | } |
||
41 | return $stream; |
||
42 | } |
||
43 | )->then( |
||
44 | function (WritableStreamInterface $stream) use ($out, &$store, &$buffer, &$ending, &$closed) { |
||
45 | // stream is already closed, make sure to close output stream |
||
46 | if (!$stream->isWritable()) { |
||
47 | $out->close(); |
||
48 | return $stream; |
||
49 | } |
||
50 | |||
51 | // resolves but output is already closed, make sure to close stream silently |
||
52 | if ($closed) { |
||
53 | $stream->close(); |
||
54 | return $stream; |
||
55 | } |
||
56 | |||
57 | // forward drain events for back pressure |
||
58 | $stream->on('drain', function () use ($out) { |
||
59 | $out->emit('drain', array($out)); |
||
60 | }); |
||
61 | |||
62 | // error events cancel output stream |
||
63 | $stream->on('error', function ($error) use ($out) { |
||
64 | $out->emit('error', array($error, $out)); |
||
65 | $out->close(); |
||
66 | }); |
||
67 | |||
68 | // close both streams once either side closes |
||
69 | $stream->on('close', array($out, 'close')); |
||
70 | $out->on('close', array($stream, 'close')); |
||
71 | |||
72 | if ($buffer) { |
||
73 | // flush buffer to stream and check if its buffer is not exceeded |
||
74 | $drained = true; |
||
75 | foreach ($buffer as $chunk) { |
||
76 | if (!$stream->write($chunk)) { |
||
77 | $drained = false; |
||
78 | } |
||
79 | } |
||
80 | $buffer = array(); |
||
81 | |||
82 | if ($drained) { |
||
83 | // signal drain event, because the output stream previous signalled a full buffer |
||
84 | $out->emit('drain', array($out)); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if ($ending) { |
||
89 | $stream->end(); |
||
90 | } else { |
||
91 | $store = $stream; |
||
92 | } |
||
93 | |||
94 | return $stream; |
||
95 | }, |
||
96 | function ($e) use ($out, &$closed) { |
||
97 | if (!$closed) { |
||
98 | $out->emit('error', array($e, $out)); |
||
99 | $out->close(); |
||
100 | } |
||
165 |