| Conditions | 13 |
| Paths | 120 |
| Total Lines | 70 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 147 | public function exec() : int |
||
| 148 | { |
||
| 149 | // check if signal is presented |
||
| 150 | $this->signal && $this->stage = $this->result; |
||
| 151 | |||
| 152 | // co interaction |
||
| 153 | try { |
||
| 154 | if ($this->started) { |
||
| 155 | if ($this->stage instanceof Throwable) { |
||
| 156 | $this->stage = $this->co->throw($this->stage); |
||
| 157 | } else { |
||
| 158 | $this->stage = $this->co->send($this->stage); |
||
| 159 | } |
||
| 160 | } else { |
||
| 161 | $this->started = true; |
||
| 162 | $this->stage = $this->co->current(); |
||
| 163 | } |
||
| 164 | } catch (Throwable $e) { |
||
| 165 | $this->stage = $e; |
||
| 166 | } |
||
| 167 | |||
| 168 | // state switcher |
||
| 169 | if ($this->stage instanceof Generator) { |
||
| 170 | // jump next co |
||
| 171 | $this->chain->push($this->co); |
||
| 172 | $this->co = $this->stage; |
||
| 173 | $this->started = false; |
||
| 174 | // job switch |
||
| 175 | return Signal::ROLL; |
||
| 176 | } |
||
| 177 | |||
| 178 | // valid checker |
||
| 179 | if ($this->co->valid()) { |
||
| 180 | // sleeping ? |
||
| 181 | if ($this->stage instanceof Promised) { |
||
| 182 | // wait promise |
||
| 183 | return $this->sleep($this->stage); |
||
| 184 | } elseif ($this->stage instanceof Syscall) { |
||
| 185 | // exec syscall |
||
| 186 | $this->stage = $this->stage->exec($this); |
||
| 187 | } |
||
| 188 | // keep running |
||
| 189 | return Signal::KEEP; |
||
| 190 | } |
||
| 191 | |||
| 192 | // co rolling for some features such as "defer" |
||
| 193 | $this->rolling(); |
||
| 194 | |||
| 195 | // check if co/chain is finished |
||
| 196 | if ($this->chain->isEmpty()) { |
||
| 197 | // result detector |
||
| 198 | $this->result = $this->stage instanceof Throwable ? $this->stage : $this->co->getReturn(); |
||
| 199 | // job done |
||
| 200 | return $this->signal ?? Signal::FIN; |
||
| 201 | } |
||
| 202 | |||
| 203 | // trying to get returned value if no exception happens |
||
| 204 | $this->stage instanceof Throwable || $this->stage = $this->co->getReturn(); |
||
| 205 | |||
| 206 | // jump prev co |
||
| 207 | $this->co = $this->chain->pop(); |
||
| 208 | |||
| 209 | // sleeping ? |
||
| 210 | if ($this->stage instanceof Promised) { |
||
| 211 | // wait promise |
||
| 212 | return $this->sleep($this->stage); |
||
| 213 | } |
||
| 214 | |||
| 215 | // job switch |
||
| 216 | return Signal::ROLL; |
||
| 217 | } |
||
| 315 |