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