| Conditions | 48 |
| Paths | 253 |
| Total Lines | 170 |
| Lines | 32 |
| Ratio | 18.82 % |
| 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 |
||
| 24 | public function getFunctions(array $constants = []) |
||
| 25 | { |
||
| 26 | $length = strlen($this->code); |
||
| 27 | $line = 1; |
||
| 28 | $buffer = ''; |
||
| 29 | $functions = []; |
||
| 30 | $bufferFunctions = []; |
||
| 31 | $char = null; |
||
| 32 | |||
| 33 | for ($pos = 0; $pos < $length; ++$pos) { |
||
| 34 | $prev = $char; |
||
| 35 | $char = $this->code[$pos]; |
||
| 36 | $next = isset($this->code[$pos + 1]) ? $this->code[$pos + 1] : null; |
||
| 37 | |||
| 38 | switch ($char) { |
||
| 39 | case '\\': |
||
| 40 | $pos++; |
||
| 41 | |||
| 42 | if ($next !== 'n') { |
||
| 43 | $prev = $char; |
||
|
|
|||
| 44 | $char = $next; |
||
| 45 | $next = isset($this->code[$pos]) ? $this->code[$pos] : null; |
||
| 46 | |||
| 47 | break; |
||
| 48 | } |
||
| 49 | |||
| 50 | $char = "\n"; |
||
| 51 | $next = isset($this->code[$pos]) ? $this->code[$pos] : null; |
||
| 52 | |||
| 53 | case "\n": |
||
| 54 | ++$line; |
||
| 55 | |||
| 56 | if ($this->status('line-comment')) { |
||
| 57 | $this->upStatus(); |
||
| 58 | } |
||
| 59 | break; |
||
| 60 | |||
| 61 | case '/': |
||
| 62 | switch ($this->status()) { |
||
| 63 | case 'simple-quote': |
||
| 64 | case 'double-quote': |
||
| 65 | case 'line-comment': |
||
| 66 | break; |
||
| 67 | |||
| 68 | case 'block-comment': |
||
| 69 | if ($prev === '*') { |
||
| 70 | $this->upStatus(); |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | |||
| 74 | default: |
||
| 75 | if ($next === '/') { |
||
| 76 | $this->downStatus('line-comment'); |
||
| 77 | } elseif ($next === '*') { |
||
| 78 | $this->downStatus('block-comment'); |
||
| 79 | } |
||
| 80 | break; |
||
| 81 | } |
||
| 82 | break; |
||
| 83 | |||
| 84 | View Code Duplication | case "'": |
|
| 85 | switch ($this->status()) { |
||
| 86 | case 'simple-quote': |
||
| 87 | $this->upStatus(); |
||
| 88 | break; |
||
| 89 | |||
| 90 | case 'line-comment': |
||
| 91 | case 'block-comment': |
||
| 92 | case 'double-quote': |
||
| 93 | break; |
||
| 94 | |||
| 95 | default: |
||
| 96 | $this->downStatus('simple-quote'); |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | break; |
||
| 100 | |||
| 101 | View Code Duplication | case '"': |
|
| 102 | switch ($this->status()) { |
||
| 103 | case 'double-quote': |
||
| 104 | $this->upStatus(); |
||
| 105 | break; |
||
| 106 | |||
| 107 | case 'line-comment': |
||
| 108 | case 'block-comment': |
||
| 109 | case 'simple-quote': |
||
| 110 | break; |
||
| 111 | |||
| 112 | default: |
||
| 113 | $this->downStatus('double-quote'); |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | break; |
||
| 117 | |||
| 118 | case '(': |
||
| 119 | switch ($this->status()) { |
||
| 120 | case 'simple-quote': |
||
| 121 | case 'double-quote': |
||
| 122 | case 'line-comment': |
||
| 123 | case 'block-comment': |
||
| 124 | case 'line-comment': |
||
| 125 | break; |
||
| 126 | |||
| 127 | default: |
||
| 128 | if ($buffer && preg_match('/(\w+)$/', $buffer, $matches)) { |
||
| 129 | $this->downStatus('function'); |
||
| 130 | array_unshift($bufferFunctions, [$matches[1], $line, []]); |
||
| 131 | $buffer = ''; |
||
| 132 | continue 3; |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | break; |
||
| 137 | |||
| 138 | case ')': |
||
| 139 | switch ($this->status()) { |
||
| 140 | case 'function': |
||
| 141 | if (($argument = self::prepareArgument($buffer))) { |
||
| 142 | $bufferFunctions[0][2][] = $argument; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!empty($bufferFunctions)) { |
||
| 146 | $functions[] = array_shift($bufferFunctions); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->upStatus(); |
||
| 150 | $buffer = ''; |
||
| 151 | continue 3; |
||
| 152 | } |
||
| 153 | break; |
||
| 154 | |||
| 155 | case ',': |
||
| 156 | switch ($this->status()) { |
||
| 157 | case 'function': |
||
| 158 | if (($argument = self::prepareArgument($buffer))) { |
||
| 159 | $bufferFunctions[0][2][] = $argument; |
||
| 160 | } |
||
| 161 | |||
| 162 | $buffer = ''; |
||
| 163 | continue 3; |
||
| 164 | } |
||
| 165 | break; |
||
| 166 | |||
| 167 | case ' ': |
||
| 168 | case '\t': |
||
| 169 | switch ($this->status()) { |
||
| 170 | case 'double-quote': |
||
| 171 | case 'simple-quote': |
||
| 172 | break; |
||
| 173 | |||
| 174 | default: |
||
| 175 | $buffer = ''; |
||
| 176 | continue 3; |
||
| 177 | } |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | |||
| 181 | switch ($this->status()) { |
||
| 182 | case 'line-comment': |
||
| 183 | case 'block-comment': |
||
| 184 | break; |
||
| 185 | |||
| 186 | default: |
||
| 187 | $buffer .= $char; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | return $functions; |
||
| 193 | } |
||
| 194 | |||
| 247 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.