| Conditions | 9 |
| Paths | 2 |
| Total Lines | 52 |
| 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 |
||
| 23 | private function compileTree(Board $board, array $tree): array |
||
| 24 | { |
||
| 25 | $code = []; |
||
| 26 | while(true) { |
||
| 27 | if(!$tree) { |
||
| 28 | break; |
||
| 29 | } |
||
| 30 | $line = array_shift($tree); |
||
| 31 | $tokens = explode(' ', $line['line']); |
||
| 32 | if(!$tokens || !$line['line']) { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | switch($tokens[0]) { |
||
| 36 | case 'for': { |
||
| 37 | $code[] = [ |
||
| 38 | 'action' => 'for', |
||
| 39 | 'iterations' => $tokens[1], |
||
| 40 | 'program' => $this->compileTree($board, $line['sub']), |
||
| 41 | ]; |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | case 'none': { break; } |
||
| 45 | case 'if': { |
||
| 46 | $else = array_shift($tree); |
||
| 47 | $code[] = [ |
||
| 48 | 'action' => 'if', |
||
| 49 | 'left' => $tokens[1], |
||
| 50 | 'operator' => $tokens[2], |
||
| 51 | 'right' => $tokens[3], |
||
| 52 | 'true' => $this->compileTree($board, $line['sub']), |
||
| 53 | 'false' => $this->compileTree($board, $else['sub']), |
||
| 54 | ]; |
||
| 55 | break; |
||
| 56 | } |
||
| 57 | case 'while': { |
||
| 58 | $code[] = [ |
||
| 59 | 'action' => 'while', |
||
| 60 | 'left' => $tokens[1], |
||
| 61 | 'operator' => $tokens[2], |
||
| 62 | 'right' => $tokens[3], |
||
| 63 | 'program' => $this->compileTree($board, $line['sub']), |
||
| 64 | ]; |
||
| 65 | break; |
||
| 66 | } |
||
| 67 | default: { |
||
| 68 | $code = array_merge($code, $this->compileSimple($board, $tokens)); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return $code; |
||
| 74 | } |
||
| 75 | |||
| 122 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.