| Conditions | 62 |
| Paths | 349 |
| Total Lines | 205 |
| Lines | 51 |
| Ratio | 24.88 % |
| 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 | switch ($this->status()) { |
||
| 41 | case 'simple-quote': |
||
| 42 | if ($next !== "'") { |
||
| 43 | break 2; |
||
| 44 | } |
||
| 45 | break; |
||
| 46 | |||
| 47 | case 'double-quote': |
||
| 48 | if ($next !== '"') { |
||
| 49 | break 2; |
||
| 50 | } |
||
| 51 | break; |
||
| 52 | |||
| 53 | case 'back-tick': |
||
| 54 | if ($next !== '`') { |
||
| 55 | break 2; |
||
| 56 | } |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | |||
| 60 | $prev = $char; |
||
|
|
|||
| 61 | $char = $next; |
||
| 62 | $pos++; |
||
| 63 | $next = isset($this->code[$pos]) ? $this->code[$pos] : null; |
||
| 64 | break; |
||
| 65 | |||
| 66 | case "\n": |
||
| 67 | ++$line; |
||
| 68 | |||
| 69 | if ($this->status('line-comment')) { |
||
| 70 | $this->upStatus(); |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | |||
| 74 | case '/': |
||
| 75 | switch ($this->status()) { |
||
| 76 | case 'simple-quote': |
||
| 77 | case 'double-quote': |
||
| 78 | case 'back-tick': |
||
| 79 | case 'line-comment': |
||
| 80 | break; |
||
| 81 | |||
| 82 | case 'block-comment': |
||
| 83 | if ($prev === '*') { |
||
| 84 | $this->upStatus(); |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | |||
| 88 | default: |
||
| 89 | if ($next === '/') { |
||
| 90 | $this->downStatus('line-comment'); |
||
| 91 | } elseif ($next === '*') { |
||
| 92 | $this->downStatus('block-comment'); |
||
| 93 | } |
||
| 94 | break; |
||
| 95 | } |
||
| 96 | break; |
||
| 97 | |||
| 98 | View Code Duplication | case "'": |
|
| 99 | switch ($this->status()) { |
||
| 100 | case 'simple-quote': |
||
| 101 | $this->upStatus(); |
||
| 102 | break; |
||
| 103 | |||
| 104 | case 'line-comment': |
||
| 105 | case 'block-comment': |
||
| 106 | case 'double-quote': |
||
| 107 | case 'back-tick': |
||
| 108 | break; |
||
| 109 | |||
| 110 | default: |
||
| 111 | $this->downStatus('simple-quote'); |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | break; |
||
| 115 | |||
| 116 | View Code Duplication | case '"': |
|
| 117 | switch ($this->status()) { |
||
| 118 | case 'double-quote': |
||
| 119 | $this->upStatus(); |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'line-comment': |
||
| 123 | case 'block-comment': |
||
| 124 | case 'simple-quote': |
||
| 125 | case 'back-tick': |
||
| 126 | break; |
||
| 127 | |||
| 128 | default: |
||
| 129 | $this->downStatus('double-quote'); |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | break; |
||
| 133 | |||
| 134 | View Code Duplication | case '`': |
|
| 135 | switch ($this->status()) { |
||
| 136 | case 'back-tick': |
||
| 137 | $this->upStatus(); |
||
| 138 | break; |
||
| 139 | |||
| 140 | case 'line-comment': |
||
| 141 | case 'block-comment': |
||
| 142 | case 'simple-quote': |
||
| 143 | case 'double-quote': |
||
| 144 | break; |
||
| 145 | |||
| 146 | default: |
||
| 147 | $this->downStatus('back-tick'); |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | break; |
||
| 151 | |||
| 152 | case '(': |
||
| 153 | switch ($this->status()) { |
||
| 154 | case 'simple-quote': |
||
| 155 | case 'double-quote': |
||
| 156 | case 'back-tick': |
||
| 157 | case 'line-comment': |
||
| 158 | case 'block-comment': |
||
| 159 | break; |
||
| 160 | |||
| 161 | default: |
||
| 162 | if ($buffer && preg_match('/(\w+)$/', $buffer, $matches)) { |
||
| 163 | $this->downStatus('function'); |
||
| 164 | array_unshift($bufferFunctions, [$matches[1], $line, []]); |
||
| 165 | $buffer = ''; |
||
| 166 | continue 3; |
||
| 167 | } |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | break; |
||
| 171 | |||
| 172 | case ')': |
||
| 173 | switch ($this->status()) { |
||
| 174 | case 'function': |
||
| 175 | if (($argument = static::prepareArgument($buffer))) { |
||
| 176 | $bufferFunctions[0][2][] = $argument; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!empty($bufferFunctions)) { |
||
| 180 | $functions[] = array_shift($bufferFunctions); |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->upStatus(); |
||
| 184 | $buffer = ''; |
||
| 185 | continue 3; |
||
| 186 | } |
||
| 187 | break; |
||
| 188 | |||
| 189 | case ',': |
||
| 190 | switch ($this->status()) { |
||
| 191 | case 'function': |
||
| 192 | if (($argument = static::prepareArgument($buffer))) { |
||
| 193 | $bufferFunctions[0][2][] = $argument; |
||
| 194 | } |
||
| 195 | |||
| 196 | $buffer = ''; |
||
| 197 | continue 3; |
||
| 198 | } |
||
| 199 | break; |
||
| 200 | |||
| 201 | case ' ': |
||
| 202 | case '\t': |
||
| 203 | switch ($this->status()) { |
||
| 204 | case 'double-quote': |
||
| 205 | case 'simple-quote': |
||
| 206 | case 'back-tick': |
||
| 207 | break; |
||
| 208 | |||
| 209 | default: |
||
| 210 | $buffer = ''; |
||
| 211 | continue 3; |
||
| 212 | } |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | switch ($this->status()) { |
||
| 217 | case 'line-comment': |
||
| 218 | case 'block-comment': |
||
| 219 | break; |
||
| 220 | |||
| 221 | default: |
||
| 222 | $buffer .= $char; |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | return $functions; |
||
| 228 | } |
||
| 229 | |||
| 321 |
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.