Conditions | 25 |
Paths | 215 |
Total Lines | 174 |
Code Lines | 86 |
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 |
||
158 | public function processFile($file, $contents) |
||
159 | { |
||
160 | $this->getLogger()->debug("Processing `$file`"); |
||
161 | $file = realpath($file); |
||
162 | |||
163 | self::$file = $file; |
||
164 | |||
165 | $ignoreFile = sprintf('%s/.signalignore', dirname($file)); |
||
166 | |||
167 | if (file_exists($ignoreFile)) |
||
168 | { |
||
169 | $this->getLogger()->notice("Skipping `$file` because of `$ignoreFile`" . PHP_EOL); |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | $this->paths[] = $file; |
||
174 | // Remove initial `\` from namespace |
||
175 | try |
||
176 | { |
||
177 | $annotated = AnnotationUtility::rawAnnotate($file); |
||
178 | } |
||
179 | catch (ClassNotFoundException $e) |
||
180 | { |
||
181 | $this->log($e, $file); |
||
182 | return; |
||
183 | } |
||
184 | catch (ParseException $e) |
||
185 | { |
||
186 | $this->err($e, $file); |
||
187 | return; |
||
188 | } |
||
189 | catch (UnexpectedValueException $e) |
||
190 | { |
||
191 | $this->err($e, $file); |
||
192 | return; |
||
193 | } |
||
194 | catch (Exception $e) |
||
195 | { |
||
196 | $this->err($e, $file); |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | $namespace = preg_replace('~^\\\\+~', '', $annotated['namespace']); |
||
201 | $className = $annotated['className']; |
||
202 | |||
203 | |||
204 | // Use fully qualified name, class must autoload |
||
205 | $fqn = $namespace . '\\' . $className; |
||
206 | NameNormalizer::normalize($fqn); |
||
207 | |||
208 | try |
||
209 | { |
||
210 | // NOTE: This autloader must be registered on ReflectionClass |
||
211 | // creation ONLY! That's why register/unregister. |
||
212 | // This will detect not found depending classes |
||
213 | // (base classes,interfaces,traits etc.) |
||
214 | $autoload = static::class . '::autoloadHandler'; |
||
215 | spl_autoload_register($autoload); |
||
216 | eval('$info = new ReflectionClass($fqn);'); |
||
1 ignored issue
–
show
|
|||
217 | spl_autoload_unregister($autoload); |
||
218 | } |
||
219 | catch (ParseError $e) |
||
220 | { |
||
221 | $this->err($e, $file); |
||
222 | return; |
||
223 | } |
||
224 | catch (ClassNotFoundException $e) |
||
225 | { |
||
226 | $this->log($e, $file); |
||
227 | return; |
||
228 | } |
||
229 | catch (ReflectionException $e) |
||
230 | { |
||
231 | $this->err($e, $file); |
||
232 | return; |
||
233 | } |
||
234 | $isAnnotated = $info->implementsInterface(AnnotatedInterface::class); |
||
1 ignored issue
–
show
|
|||
235 | $hasSignals = $this->hasSignals($contents); |
||
236 | $isAbstract = $info->isAbstract() || $info->isInterface(); |
||
237 | |||
238 | if ($isAnnotated) |
||
239 | { |
||
240 | $this->getLogger()->debug("Annotated: $info->name"); |
||
241 | } |
||
242 | else |
||
243 | { |
||
244 | $this->getLogger()->debug("Not annotated: $info->name"); |
||
245 | } |
||
246 | |||
247 | // Old classes must now implement interface |
||
248 | // Brake BC! |
||
249 | if ($hasSignals && !$isAnnotated && !$isAbstract) |
||
250 | { |
||
251 | throw new UnexpectedValueException(sprintf('Class %s must implement %s to use signals', $fqn, AnnotatedInterface::class)); |
||
252 | } |
||
253 | |||
254 | // Skip not annotated class |
||
255 | if (!$isAnnotated) |
||
256 | { |
||
257 | return; |
||
258 | } |
||
259 | |||
260 | // Skip abstract classes |
||
261 | if ($isAbstract) |
||
262 | { |
||
263 | return; |
||
264 | } |
||
265 | try |
||
266 | { |
||
267 | // Discard notices (might be the case when outdated cache?) |
||
268 | $level = error_reporting(); |
||
269 | error_reporting(E_WARNING); |
||
270 | $meta = SignalsMeta::create($fqn); |
||
271 | error_reporting($level); |
||
272 | } |
||
273 | catch (ParseException $e) |
||
274 | { |
||
275 | $this->err($e, $file); |
||
276 | return; |
||
277 | } |
||
278 | catch (ClassNotFoundException $e) |
||
279 | { |
||
280 | $this->log($e, $file); |
||
281 | return; |
||
282 | } |
||
283 | catch (UnexpectedValueException $e) |
||
284 | { |
||
285 | $this->err($e, $file); |
||
286 | return; |
||
287 | } |
||
288 | |||
289 | /* @var $typeMeta DocumentTypeMeta */ |
||
290 | $typeMeta = $meta->type(); |
||
291 | |||
292 | // Signals |
||
293 | foreach ($typeMeta->signalFor as $slot) |
||
294 | { |
||
295 | $this->getLogger()->debug("Signal: $slot:$fqn"); |
||
296 | $this->data[Signal::Slots][$slot][$fqn] = true; |
||
297 | } |
||
298 | |||
299 | // Slots |
||
300 | // For constructor injection |
||
301 | foreach ($typeMeta->slotFor as $slot) |
||
302 | { |
||
303 | $key = implode('::', [$fqn, '__construct']) . '()'; |
||
304 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
305 | $this->data[Signal::Signals][$slot][$fqn][$key] = true; |
||
306 | } |
||
307 | |||
308 | // For method injection |
||
309 | foreach ($meta->methods() as $methodName => $method) |
||
310 | { |
||
311 | /* @var $method DocumentMethodMeta */ |
||
312 | foreach ($method->slotFor as $slot) |
||
313 | { |
||
314 | $key = implode('::', [$fqn, $methodName]) . '()'; |
||
315 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
316 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s()', $methodName); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | // For property injection |
||
321 | foreach ($meta->fields() as $fieldName => $field) |
||
322 | { |
||
323 | /* @var $field DocumentPropertyMeta */ |
||
324 | foreach ($field->slotFor as $slot) |
||
325 | { |
||
326 | $key = implode('::$', [$fqn, $fieldName]); |
||
327 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
328 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s', $fieldName); |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | |||
360 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.