Conditions | 54 |
Paths | 230 |
Total Lines | 195 |
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 | |||
235 | $this->language->write($buffer.'"'); |
||
236 | $this->language->writeQuoted($info[0]->name); |
||
237 | $this->language->writeQuoted(' :'); |
||
238 | |||
239 | if (\count($info) === 1) { |
||
240 | $this->language->writeQuoted(' /* empty */'); |
||
241 | } |
||
242 | |||
243 | for ($i = 1, $l = \count($info); $i < $l; $i++) { |
||
244 | $this->language->writeQuoted(' '.$info[$i]->name); |
||
245 | } |
||
246 | |||
247 | if ($gram->num + 1 === $this->context->countGrams) { |
||
248 | $this->language->write("\"\n"); |
||
249 | } else { |
||
250 | $this->language->write("\",\n"); |
||
251 | } |
||
252 | } |
||
253 | } elseif ($this->metaMatch($p, 'listvar')) { |
||
254 | $var = \trim(\mb_substr($p, 9)); |
||
255 | $this->genListVar($buffer, $var); |
||
256 | } elseif ($this->metaMatch($p, 'ifnot')) { |
||
257 | $skipMode = $skipMode || !$this->skipIf($p); |
||
258 | } elseif ($this->metaMatch($p, 'if')) { |
||
259 | $skipMode = $skipMode || $this->skipIf($p); |
||
260 | } elseif ($this->metaMatch($p, 'endif')) { |
||
261 | $skipMode = false; |
||
262 | } else { |
||
263 | throw new TemplateException("Unknown \$: $line"); |
||
264 | } |
||
265 | $lineChanged = true; |
||
266 | } else { |
||
267 | if ($lineChanged) { |
||
268 | $this->printLine(); |
||
269 | $lineChanged = false; |
||
270 | } |
||
271 | $this->language->write($buffer, $this->copyHeader); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | $this->language->commit(); |
||
276 | } |
||
277 | |||
540 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.