Conditions | 22 |
Paths | 296 |
Total Lines | 78 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
89 | protected function write(): ?array |
||
90 | { |
||
91 | if (!isset($this->pipes[0])) { |
||
92 | return null; |
||
93 | } |
||
94 | $input = $this->input; |
||
95 | |||
96 | if ($input instanceof \Iterator) { |
||
97 | if (!$input->valid()) { |
||
98 | $input = null; |
||
99 | } elseif (\is_resource($input = $input->current())) { |
||
100 | stream_set_blocking($input, 0); |
||
101 | } elseif (!isset($this->inputBuffer[0])) { |
||
102 | if (!\is_string($input)) { |
||
103 | if (!\is_scalar($input)) { |
||
104 | throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input))); |
||
105 | } |
||
106 | $input = (string) $input; |
||
107 | } |
||
108 | $this->inputBuffer = $input; |
||
109 | $this->input->next(); |
||
110 | $input = null; |
||
111 | } else { |
||
112 | $input = null; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $r = $e = []; |
||
117 | $w = [$this->pipes[0]]; |
||
118 | |||
119 | // let's have a look if something changed in streams |
||
120 | if (false === @stream_select($r, $w, $e, 0, 0)) { |
||
121 | return null; |
||
122 | } |
||
123 | |||
124 | foreach ($w as $stdin) { |
||
125 | if (isset($this->inputBuffer[0])) { |
||
126 | $written = fwrite($stdin, $this->inputBuffer); |
||
127 | $this->inputBuffer = substr($this->inputBuffer, $written); |
||
128 | if (isset($this->inputBuffer[0])) { |
||
129 | return [$this->pipes[0]]; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | if ($input) { |
||
134 | while (true) { |
||
135 | $data = fread($input, self::CHUNK_SIZE); |
||
|
|||
136 | if (!isset($data[0])) { |
||
137 | break; |
||
138 | } |
||
139 | $written = fwrite($stdin, $data); |
||
140 | $data = substr($data, $written); |
||
141 | if (isset($data[0])) { |
||
142 | $this->inputBuffer = $data; |
||
143 | |||
144 | return [$this->pipes[0]]; |
||
145 | } |
||
146 | } |
||
147 | if (feof($input)) { |
||
148 | if ($this->input instanceof \Iterator) { |
||
149 | $this->input->next(); |
||
150 | } else { |
||
151 | $this->input = null; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // no input to read on resource, buffer is empty |
||
158 | if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { |
||
159 | $this->input = null; |
||
160 | fclose($this->pipes[0]); |
||
161 | unset($this->pipes[0]); |
||
162 | } elseif (!$w) { |
||
163 | return [$this->pipes[0]]; |
||
164 | } |
||
165 | |||
166 | return null; |
||
167 | } |
||
177 |