| Conditions | 15 |
| Paths | 10 |
| Total Lines | 56 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 3 |
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 |
||
| 109 | protected function postprocessTokens() { |
||
| 110 | // we need to manually iterate and manage a count because we'll change |
||
| 111 | // the tokens array on the way |
||
| 112 | for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) { |
||
| 113 | // first check that the following tokens are of form ~LABEL~, |
||
| 114 | // then match the __EMU__... sequence. |
||
| 115 | if ('~' === $this->tokens[$i] |
||
| 116 | && isset($this->tokens[$i + 2]) |
||
| 117 | && '~' === $this->tokens[$i + 2] |
||
| 118 | && T_STRING === $this->tokens[$i + 1][0] |
||
| 119 | && preg_match('(^__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?$)', $this->tokens[$i + 1][1], $matches) |
||
| 120 | ) { |
||
| 121 | if ('ELLIPSIS' === $matches[1]) { |
||
| 122 | $replace = array( |
||
| 123 | array(self::T_ELLIPSIS, '...', $this->tokens[$i + 1][2]) |
||
| 124 | ); |
||
| 125 | } else if ('POW' === $matches[1]) { |
||
| 126 | $replace = array( |
||
| 127 | array(self::T_POW, '**', $this->tokens[$i + 1][2]) |
||
| 128 | ); |
||
| 129 | } else if ('POWEQUAL' === $matches[1]) { |
||
| 130 | $replace = array( |
||
| 131 | array(self::T_POW_EQUAL, '**=', $this->tokens[$i + 1][2]) |
||
| 132 | ); |
||
| 133 | } else if ('COALESCE' === $matches[1]) { |
||
| 134 | $replace = array( |
||
| 135 | array(self::T_COALESCE, '??', $this->tokens[$i + 1][2]) |
||
| 136 | ); |
||
| 137 | } else if ('SPACESHIP' === $matches[1]) { |
||
| 138 | $replace = array( |
||
| 139 | array(self::T_SPACESHIP, '<=>', $this->tokens[$i + 1][2]), |
||
| 140 | ); |
||
| 141 | } else if ('YIELDFROM' === $matches[1]) { |
||
| 142 | $content = hex2bin($matches[2]); |
||
| 143 | $replace = array( |
||
| 144 | array(self::T_YIELD_FROM, $content, $this->tokens[$i + 1][2] - substr_count($content, "\n")) |
||
| 145 | ); |
||
| 146 | } else { |
||
| 147 | throw new \RuntimeException('Invalid __EMU__ sequence'); |
||
| 148 | } |
||
| 149 | |||
| 150 | array_splice($this->tokens, $i, 3, $replace); |
||
| 151 | $c -= 3 - count($replace); |
||
| 152 | // for multichar tokens (e.g. strings) replace any ~__EMU__...~ sequences |
||
| 153 | // in their content with the original character sequence |
||
| 154 | } elseif (is_array($this->tokens[$i]) |
||
| 155 | && 0 !== strpos($this->tokens[$i][1], '__EMU__') |
||
| 156 | ) { |
||
| 157 | $this->tokens[$i][1] = preg_replace_callback( |
||
| 158 | '(~__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?~)', |
||
| 159 | array($this, 'restoreContentCallback'), |
||
| 160 | $this->tokens[$i][1] |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 206 |