Conditions | 54 |
Paths | 230 |
Total Lines | 191 |
Code Lines | 142 |
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 |
||
82 | public function render(CompressResult $result, $resultFile, $headerFile = null) |
||
83 | { |
||
84 | $headerFile = $headerFile ?: \fopen('php://memory', 'rw'); |
||
85 | |||
86 | $this->language->begin($resultFile, $headerFile); |
||
87 | |||
88 | $this->compress = $result; |
||
89 | unset($result); |
||
90 | |||
91 | $skipMode = false; |
||
92 | $lineChanged = false; |
||
93 | $tailCode = false; |
||
94 | $reduceMode = [ |
||
95 | 'enabled' => false, |
||
96 | 'm' => -1, |
||
97 | 'n' => 0, |
||
98 | 'mac' => [], |
||
99 | ]; |
||
100 | $tokenMode = [ |
||
101 | 'enabled' => false, |
||
102 | 'mac' => [], |
||
103 | ]; |
||
104 | $buffer = ''; |
||
105 | |||
106 | foreach ($this->template as $line) { |
||
107 | $line .= "\n"; |
||
108 | if ($tailCode) { |
||
109 | $this->language->write($buffer.$line); |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | if ($skipMode) { |
||
114 | if ($this->metaMatch(\ltrim($line), 'endif')) { |
||
115 | $skipMode = false; |
||
116 | } |
||
117 | continue; |
||
118 | } |
||
119 | |||
120 | if ($reduceMode['enabled']) { |
||
121 | if ($this->metaMatch(\trim($line), 'endreduce')) { |
||
122 | $reduceMode['enabled'] = false; |
||
123 | $this->lineNumber++; |
||
124 | if ($reduceMode['m'] < 0) { |
||
125 | $reduceMode['m'] = $reduceMode['n']; |
||
126 | } |
||
127 | |||
128 | foreach ($this->context->grams as $gram) { |
||
129 | if ($gram->action) { |
||
130 | for ($j = 0; $j < $reduceMode['m']; $j++) { |
||
131 | $this->expandMacro($reduceMode['mac'][$j], $gram->num, null); |
||
132 | } |
||
133 | } else { |
||
134 | for ($j = $reduceMode['m']; $j < $reduceMode['n']; $j++) { |
||
135 | $this->expandMacro($reduceMode['mac'][$j], $gram->num, null); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | continue; |
||
140 | } elseif ($this->metaMatch(\trim($line), 'noact')) { |
||
141 | $reduceMode['m'] = $reduceMode['n']; |
||
142 | continue; |
||
143 | } |
||
144 | $reduceMode['mac'][$reduceMode['n']++] = $line; |
||
145 | continue; |
||
146 | } |
||
147 | |||
148 | if ($tokenMode['enabled']) { |
||
149 | if ($this->metaMatch(\trim($line), 'endtokenval')) { |
||
150 | $tokenMode['enabled'] = false; |
||
151 | $this->lineNumber++; |
||
152 | for ($i = 1; $i < $this->context->countTerminals; $i++) { |
||
153 | $symbol = $this->context->symbol($i); |
||
154 | if ($symbol->name[0] != '\'') { |
||
155 | $str = $symbol->name; |
||
156 | if ($i === 1) { |
||
157 | $str = 'YYERRTOK'; |
||
158 | } |
||
159 | foreach ($tokenMode['mac'] as $mac) { |
||
160 | $this->expandMacro($mac, $symbol->value, $str); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | $tokenMode['mac'][] = $line; |
||
166 | } |
||
167 | continue; |
||
168 | } |
||
169 | $p = $line; |
||
170 | $buffer = ''; |
||
171 | for ($i = 0; $i < \mb_strlen($line); $i++) { |
||
172 | $p = \mb_substr($line, $i); |
||
173 | if ($p[0] !== $this->metaChar) { |
||
174 | $buffer .= $line[$i]; |
||
175 | } elseif ($i + 1 < \mb_strlen($line) && $p[1] === $this->metaChar) { |
||
176 | $i++; |
||
177 | $buffer .= $this->metaChar; |
||
178 | } elseif ($this->metaMatch($p, '(')) { |
||
179 | $start = $i + 2; |
||
180 | $val = \mb_substr($p, 2); |
||
181 | while ($i < \mb_strlen($line) && $line[$i] !== ')') { |
||
182 | $i++; |
||
183 | } |
||
184 | if (!isset($line[$i])) { |
||
185 | throw new TemplateException('$(: missing ")"'); |
||
186 | } |
||
187 | $length = $i - $start; |
||
188 | |||
189 | $buffer .= $this->genValueOf(\mb_substr($val, 0, $length)); |
||
190 | } elseif ($this->metaMatch($p, 'TYPEOF(')) { |
||
191 | throw new LogicException('TYPEOF is not implemented'); |
||
192 | } else { |
||
193 | break; |
||
194 | } |
||
195 | } |
||
196 | if (isset($p[0]) && $p[0] === $this->metaChar) { |
||
197 | if (\trim($buffer) !== '') { |
||
198 | throw new TemplateException('Non-blank character before $-keyword'); |
||
199 | } |
||
200 | if ($this->metaMatch($p, 'header')) { |
||
201 | $this->copyHeader = true; |
||
202 | } elseif ($this->metaMatch($p, 'endheader')) { |
||
203 | $this->copyHeader = false; |
||
204 | } elseif ($this->metaMatch($p, 'tailcode')) { |
||
205 | //$this->printLine(); |
||
|
|||
206 | $tailCode = true; |
||
207 | continue; |
||
208 | } elseif ($this->metaMatch($p, 'verification-table')) { |
||
209 | throw new TemplateException('verification-table is not implemented'); |
||
210 | } elseif ($this->metaMatch($p, 'union')) { |
||
211 | throw new TemplateException('union is not implemented'); |
||
212 | } elseif ($this->metaMatch($p, 'tokenval')) { |
||
213 | $tokenMode = [ |
||
214 | 'enabled' => true, |
||
215 | 'mac' => [], |
||
216 | ]; |
||
217 | } elseif ($this->metaMatch($p, 'reduce')) { |
||
218 | $reduceMode = [ |
||
219 | 'enabled' => true, |
||
220 | 'm' => -1, |
||
221 | 'n' => 0, |
||
222 | 'mac' => [], |
||
223 | ]; |
||
224 | } elseif ($this->metaMatch($p, 'switch-for-token-name')) { |
||
225 | for ($i = 0; $i < $this->context->countTerminals; $i++) { |
||
226 | if ($this->context->cTermIndex[$i] >= 0) { |
||
227 | $symbol = $this->context->symbol($i); |
||
228 | $this->language->caseBlock($buffer, $symbol->value, $symbol->name); |
||
229 | } |
||
230 | } |
||
231 | } elseif ($this->metaMatch($p, 'production-strings')) { |
||
232 | foreach ($this->context->grams as $gram) { |
||
233 | $info = \array_slice($gram->body, 0); |
||
234 | $this->language->write($buffer.'"'); |
||
235 | $this->language->writeQuoted($info[0]->name); |
||
236 | $this->language->writeQuoted(' :'); |
||
237 | if (\count($info) === 1) { |
||
238 | $this->language->writeQuoted(' /* empty */'); |
||
239 | } |
||
240 | for ($i = 1; $i < \count($info); $i++) { |
||
241 | $this->language->writeQuoted(' '.$info[$i]->name); |
||
242 | } |
||
243 | if ($gram->num + 1 === $this->context->countGrams) { |
||
244 | $this->language->write("\"\n"); |
||
245 | } else { |
||
246 | $this->language->write("\",\n"); |
||
247 | } |
||
248 | } |
||
249 | } elseif ($this->metaMatch($p, 'listvar')) { |
||
250 | $var = \trim(\mb_substr($p, 9)); |
||
251 | $this->genListVar($buffer, $var); |
||
252 | } elseif ($this->metaMatch($p, 'ifnot')) { |
||
253 | $skipMode = $skipMode || !$this->skipIf($p); |
||
254 | } elseif ($this->metaMatch($p, 'if')) { |
||
255 | $skipMode = $skipMode || $this->skipIf($p); |
||
256 | } elseif ($this->metaMatch($p, 'endif')) { |
||
257 | $skipMode = false; |
||
258 | } else { |
||
259 | throw new TemplateException("Unknown \$: $line"); |
||
260 | } |
||
261 | $lineChanged = true; |
||
262 | } else { |
||
263 | if ($lineChanged) { |
||
264 | //$this->printLine(); |
||
265 | $lineChanged = false; |
||
266 | } |
||
267 | $this->language->write($buffer, $this->copyHeader); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | $this->language->commit(); |
||
272 | } |
||
273 | |||
536 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.