| Conditions | 13 |
| Paths | 30 |
| Total Lines | 44 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 28 |
| CRAP Score | 13 |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 25 | 8 | protected function readFrame() |
|
| 26 | { |
||
| 27 | 8 | $jsonFrameEnd = false; |
|
| 28 | 8 | $lastJsonChar = ''; |
|
| 29 | 8 | $inquote = false; |
|
| 30 | 8 | $jsonFrame = ""; |
|
| 31 | 8 | $level = 0; |
|
| 32 | |||
| 33 | 8 | while (!$jsonFrameEnd && !$this->stream->eof()) { |
|
| 34 | 8 | $jsonChar = $this->stream->read(1); |
|
| 35 | |||
| 36 | 8 | if ((boolean)($jsonChar == '"' && $lastJsonChar != '\\')) { |
|
| 37 | 8 | $inquote = !$inquote; |
|
| 38 | 8 | } |
|
| 39 | |||
| 40 | 8 | if (!$inquote && in_array($jsonChar, [" ", "\r", "\n", "\t"])) { |
|
| 41 | 8 | continue; |
|
| 42 | } |
||
| 43 | |||
| 44 | 8 | if (!$inquote && in_array($jsonChar, ['{', '['])) { |
|
| 45 | 8 | $level++; |
|
| 46 | 8 | } |
|
| 47 | |||
| 48 | 8 | if (!$inquote && in_array($jsonChar, ['}', ']'])) { |
|
| 49 | 8 | $level--; |
|
| 50 | |||
| 51 | 8 | if ($level == 0) { |
|
| 52 | 8 | $jsonFrameEnd = true; |
|
| 53 | 8 | $jsonFrame .= $jsonChar; |
|
| 54 | |||
| 55 | 8 | continue; |
|
| 56 | } |
||
| 57 | 6 | } |
|
| 58 | |||
| 59 | 8 | $jsonFrame .= $jsonChar; |
|
| 60 | 8 | } |
|
| 61 | |||
| 62 | // Invalid last json, or timeout, or connection close before receiving |
||
| 63 | 8 | if (!$jsonFrameEnd) { |
|
| 64 | 8 | return null; |
|
| 65 | } |
||
| 66 | |||
| 67 | 8 | return $this->serializer->deserialize($jsonFrame, $this->getDecodeClass(), 'json'); |
|
| 68 | } |
||
| 69 | |||
| 77 |