| Conditions | 11 |
| Paths | 7 |
| Total Lines | 42 |
| 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 |
||
| 18 | public function run() |
||
| 19 | { |
||
| 20 | if ($this->stage++ > 0) { |
||
| 21 | $this->header('500 Too Busy'); |
||
| 22 | return; |
||
| 23 | } |
||
| 24 | $this->noncache(); |
||
| 25 | if (isset($_SERVER['HTTP_CONTENT_TYPE']) && $_SERVER['HTTP_CONTENT_TYPE'] === 'application/x-www-form-urlencoded') { |
||
| 26 | if (!isset($_POST['d']) || !is_string($_POST['d']) || !mb_orig_strlen($_POST['d'])) { |
||
| 27 | $this->header('500 Internal Server Error'); |
||
| 28 | echo 'Payload expected.'; |
||
| 29 | return; |
||
| 30 | } |
||
| 31 | if (!json_decode($_POST['d'], true)) { |
||
| 32 | $this->header('500 Internal Server Error'); |
||
| 33 | echo 'Broken JSON encoding.'; |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | $payload = $_POST['d']; |
||
| 37 | } else { |
||
| 38 | if ($this->attrs->raw === '') { |
||
| 39 | $this->header('500 Internal Server Error'); |
||
| 40 | echo 'Payload expected.'; |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | if (!json_decode($this->attrs->raw, true)) { |
||
| 44 | $this->header('500 Internal Server Error'); |
||
| 45 | echo 'Broken JSON encoding.'; |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | $payload = $this->attrs->raw; |
||
| 49 | } |
||
| 50 | $this->appInstance->publish('c2s:' . $this->sessId, $payload, function ($redis) { |
||
|
|
|||
| 51 | if ($redis->result === 0) { |
||
| 52 | $this->header('404 Not Found'); |
||
| 53 | } else { |
||
| 54 | echo 'ok'; |
||
| 55 | } |
||
| 56 | $this->finish(); |
||
| 57 | }); |
||
| 58 | $this->sleep(30); |
||
| 59 | } |
||
| 60 | } |
||
| 61 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: