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