| Conditions | 17 |
| Paths | 15 |
| Total Lines | 92 |
| Code Lines | 68 |
| 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 |
||
| 26 | public function read(int $code, int $pos, int $line, int $col, Token $prev): Token |
||
| 27 | { |
||
| 28 | $body = $this->lexer->getBody(); |
||
| 29 | $bodyLength = mb_strlen($body); |
||
| 30 | $start = $pos; |
||
| 31 | $pos = $start + 1; |
||
| 32 | $chunkStart = $pos; |
||
| 33 | $value = ''; |
||
| 34 | |||
| 35 | while ($pos < $bodyLength && ($code = charCodeAt($body, $pos)) !== null && !$this->isLineTerminator($code)) { |
||
| 36 | // Closing Quote (") |
||
| 37 | if ($code === 34) { |
||
| 38 | $value .= sliceString($body, $chunkStart, $pos); |
||
| 39 | |||
| 40 | return new Token(TokenKindEnum::STRING, $start, $pos + 1, $line, $col, $prev, $value); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($this->isSourceCharacter($code)) { |
||
| 44 | throw new SyntaxErrorException( |
||
| 45 | $this->lexer->getSource(), |
||
| 46 | $pos, |
||
| 47 | sprintf('Invalid character within String: %s', printCharCode($code)) |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | ++$pos; |
||
| 52 | |||
| 53 | if ($code === 92) { |
||
| 54 | // \ |
||
| 55 | $value .= sliceString($body, $chunkStart, $pos + 1); |
||
| 56 | $code = charCodeAt($body, $pos); |
||
| 57 | |||
| 58 | switch ($code) { |
||
| 59 | case 34: |
||
| 60 | $value .= '"'; |
||
| 61 | break; |
||
| 62 | case 47: |
||
| 63 | $value .= '/'; |
||
| 64 | break; |
||
| 65 | case 92: |
||
| 66 | $value .= '\\'; |
||
| 67 | break; |
||
| 68 | case 98: |
||
| 69 | $value .= '\b'; |
||
| 70 | break; |
||
| 71 | case 102: |
||
| 72 | $value .= '\f'; |
||
| 73 | break; |
||
| 74 | case 110: |
||
| 75 | $value .= '\n'; |
||
| 76 | break; |
||
| 77 | case 114: |
||
| 78 | $value .= '\r'; |
||
| 79 | break; |
||
| 80 | case 116: |
||
| 81 | $value .= '\t'; |
||
| 82 | break; |
||
| 83 | case 117: |
||
| 84 | // u |
||
| 85 | $charCode = uniCharCode( |
||
| 86 | charCodeAt($body, $pos + 1), |
||
| 87 | charCodeAt($body, $pos + 2), |
||
| 88 | charCodeAt($body, $pos + 3), |
||
| 89 | charCodeAt($body, $pos + 4) |
||
| 90 | ); |
||
| 91 | if ($charCode < 0) { |
||
| 92 | throw new SyntaxErrorException( |
||
| 93 | $this->lexer->getSource(), |
||
| 94 | $pos, |
||
| 95 | sprintf( |
||
| 96 | 'Invalid character escape sequence: \\u%s', |
||
| 97 | sliceString($body, $pos + 1, $pos + 5) |
||
| 98 | ) |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | $value .= chr($charCode); |
||
|
|
|||
| 102 | $pos += 4; |
||
| 103 | break; |
||
| 104 | default: |
||
| 105 | throw new SyntaxErrorException( |
||
| 106 | $this->lexer->getSource(), |
||
| 107 | $pos, |
||
| 108 | sprintf('Invalid character escape sequence: \\%s', chr($code)) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | ++$pos; |
||
| 113 | $chunkStart = $pos; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | throw new SyntaxErrorException($this->lexer->getSource(), $pos, 'Unterminated string.'); |
||
| 118 | } |
||
| 148 |