Conditions | 78 |
Paths | > 20000 |
Total Lines | 278 |
Code Lines | 157 |
Lines | 40 |
Ratio | 14.39 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
210 | private function parseDefinition($id, $service, $file) |
||
211 | { |
||
212 | // nette |
||
213 | if ($service instanceof Entity) { |
||
214 | $value = $service->value; |
||
215 | $service = ['arguments' => $service->attributes]; |
||
216 | if (false === strpos($value, ':')) { |
||
217 | $service['class'] = $value; |
||
218 | } else { |
||
219 | $service['factory'] = $value; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | // nette |
||
224 | if (preg_match('#^(\S+)\s+<\s+(\S+)\z#', $id, $matches)) { |
||
225 | View Code Duplication | if (isset($service['parent']) && $matches[2] !== $service['parent']) { |
|
226 | throw new InvalidArgumentException(sprintf('Two parent services "%s" and "%s" are defined for service "%s" in "%s". Check your NEON syntax.', $service['parent'], $matches[2], $matches[1], $file)); |
||
227 | } |
||
228 | |||
229 | $id = $matches[1]; |
||
230 | $parent = $matches[2]; |
||
231 | } |
||
232 | |||
233 | // nette |
||
234 | if (is_string($service) && false !== strpos($service, ':')) { |
||
235 | $service = ['factory' => $this->parseFactory($service)]; |
||
236 | } elseif (is_string($service) && 0 === strpos($service, '@')) { |
||
237 | $this->container->setAlias($id, substr($service, 1)); |
||
238 | |||
239 | return; |
||
240 | } elseif (is_string($service)) { |
||
241 | $service = ['class' => $service]; |
||
242 | } |
||
243 | |||
244 | if (!is_array($service)) { |
||
245 | throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" or a NEON entity but %s found for service "%s" in %s. Check your NEON syntax.', gettype($service), $id, $file)); |
||
246 | } |
||
247 | |||
248 | self::checkDefinition($id, $service, $file); |
||
249 | |||
250 | if (isset($service['alias'])) { |
||
251 | $public = !array_key_exists('public', $service) || (bool) $service['public']; |
||
252 | $this->container->setAlias($id, new Alias($service['alias'], $public)); |
||
253 | |||
254 | foreach ($service as $key => $value) { |
||
255 | if (!in_array($key, ['alias', 'public'])) { |
||
256 | throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias" and "public".', $key, $id, $file)); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return; |
||
261 | } |
||
262 | |||
263 | // nette |
||
264 | if (isset($parent)) { |
||
265 | $service['parent'] = $parent; |
||
266 | } |
||
267 | |||
268 | if (isset($service['parent'])) { |
||
269 | $definition = new DefinitionDecorator($service['parent']); |
||
270 | } else { |
||
271 | $definition = new Definition(); |
||
272 | } |
||
273 | |||
274 | View Code Duplication | if (isset($service['class'])) { |
|
275 | $class = $service['class']; |
||
276 | |||
277 | // nette |
||
278 | if ($class instanceof Entity) { |
||
279 | if (isset($service['arguments']) && !empty($class->attributes)) { |
||
280 | throw new InvalidArgumentException(sprintf('Duplicated definition of arguments for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
281 | } |
||
282 | |||
283 | $service['arguments'] = $class->attributes; |
||
284 | $class = $class->value; |
||
285 | } |
||
286 | |||
287 | $definition->setClass($class); |
||
288 | } |
||
289 | |||
290 | if (isset($service['shared'])) { |
||
291 | $definition->setShared($service['shared']); |
||
292 | } |
||
293 | |||
294 | if (isset($service['synthetic'])) { |
||
295 | $definition->setSynthetic($service['synthetic']); |
||
296 | } |
||
297 | |||
298 | if (isset($service['lazy'])) { |
||
299 | $definition->setLazy($service['lazy']); |
||
300 | } |
||
301 | |||
302 | if (isset($service['public'])) { |
||
303 | $definition->setPublic($service['public']); |
||
304 | } |
||
305 | |||
306 | if (isset($service['abstract'])) { |
||
307 | $definition->setAbstract($service['abstract']); |
||
308 | } |
||
309 | |||
310 | if (array_key_exists('deprecated', $service)) { |
||
311 | $definition->setDeprecated(true, $service['deprecated']); |
||
312 | } |
||
313 | |||
314 | View Code Duplication | if (isset($service['factory'])) { |
|
315 | $factory = $service['factory']; |
||
316 | |||
317 | //nette |
||
318 | if ($factory instanceof Entity) { |
||
319 | if (isset($service['arguments']) && !empty($factory->attributes)) { |
||
320 | throw new InvalidArgumentException(sprintf('Duplicated definition of arguments for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
321 | } |
||
322 | |||
323 | $service['arguments'] = $factory->attributes; |
||
324 | $factory = $factory->value; |
||
325 | } |
||
326 | |||
327 | $definition->setFactory($this->parseFactory($factory)); |
||
328 | } |
||
329 | |||
330 | if (isset($service['file'])) { |
||
331 | $definition->setFile($service['file']); |
||
332 | } |
||
333 | |||
334 | if (isset($service['arguments'])) { |
||
335 | $autowired = false; |
||
336 | array_walk($service['arguments'], function (&$value) use (&$autowired) { |
||
337 | if ('...' === $value) { |
||
338 | $value = ''; |
||
339 | $autowired = true; |
||
340 | } |
||
341 | |||
342 | return $value; |
||
343 | }); |
||
344 | |||
345 | $definition->setAutowired($autowired); |
||
346 | $definition->setArguments($this->resolveServices($service['arguments'])); |
||
347 | } |
||
348 | |||
349 | // nette |
||
350 | if (isset($service['setup'])) { |
||
351 | foreach ($service['setup'] as $setup) { |
||
352 | if ($setup instanceof Entity) { |
||
353 | $name = $setup->value; |
||
354 | $args = $setup->attributes; |
||
355 | } elseif (is_array($setup)) { |
||
356 | $name = $setup[0]; |
||
357 | $args = isset($setup[1]) ? $setup[1] : []; |
||
358 | } else { |
||
359 | $name = $setup; |
||
360 | $args = []; |
||
361 | } |
||
362 | |||
363 | if ('$' === $name[0]) { |
||
364 | $service['properties'][substr($name, 1)] = $args; |
||
365 | } else { |
||
366 | $service['calls'][] = [$name, $args]; |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | |||
371 | if (isset($service['properties'])) { |
||
372 | $definition->setProperties($this->resolveServices($service['properties'])); |
||
373 | } |
||
374 | |||
375 | if (isset($service['configurator'])) { |
||
376 | if (is_string($service['configurator'])) { |
||
377 | $definition->setConfigurator($service['configurator']); |
||
378 | } else { |
||
379 | $definition->setConfigurator([$this->resolveServices($service['configurator'][0]), $service['configurator'][1]]); |
||
380 | } |
||
381 | } |
||
382 | |||
383 | if (isset($service['calls'])) { |
||
384 | if (!is_array($service['calls'])) { |
||
385 | throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
386 | } |
||
387 | |||
388 | foreach ($service['calls'] as $call) { |
||
389 | if ($call instanceof Entity) { // nette |
||
390 | $method = $call->value; |
||
391 | $args = $this->resolveServices($call->attributes); |
||
392 | } elseif (isset($call['method'])) { |
||
393 | $method = $call['method']; |
||
394 | $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : []; |
||
395 | } elseif (is_array($call)) { |
||
396 | $method = $call[0]; |
||
397 | $args = isset($call[1]) ? $this->resolveServices($call[1]) : []; |
||
398 | } else { // nette |
||
399 | $method = $call; |
||
400 | $args = []; |
||
401 | } |
||
402 | |||
403 | $definition->addMethodCall($method, $args); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | if (isset($service['tags'])) { |
||
408 | if (!is_array($service['tags'])) { |
||
409 | throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
410 | } |
||
411 | |||
412 | foreach ($service['tags'] as $tag) { |
||
413 | if ($tag instanceof Entity) { |
||
414 | $tag = ['name' => $tag->value] + $tag->attributes; |
||
415 | } elseif (is_string($tag)) { |
||
416 | $tag = ['name' => $tag]; |
||
417 | } |
||
418 | |||
419 | if (!is_array($tag)) { |
||
420 | throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
421 | } |
||
422 | |||
423 | if (!isset($tag['name'])) { |
||
424 | throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); |
||
425 | } |
||
426 | |||
427 | if (!is_string($tag['name']) || '' === $tag['name']) { |
||
428 | throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); |
||
429 | } |
||
430 | |||
431 | $name = $tag['name']; |
||
432 | unset($tag['name']); |
||
433 | |||
434 | foreach ($tag as $attribute => $value) { |
||
435 | if (!is_scalar($value) && null !== $value) { |
||
436 | throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your NEON syntax.', $id, $name, $attribute, $file)); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | $definition->addTag($name, $tag); |
||
441 | } |
||
442 | } |
||
443 | |||
444 | if (isset($service['decorates'])) { |
||
445 | $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; |
||
446 | $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0; |
||
447 | $definition->setDecoratedService($service['decorates'], $renameId, $priority); |
||
448 | } |
||
449 | |||
450 | // nette |
||
451 | View Code Duplication | if (isset($service['autowired'])) { |
|
452 | if (isset($service['autowire']) && $service['autowire'] !== $service['autowired']) { |
||
453 | throw new InvalidArgumentException(sprintf('Contradictory definition of autowiring for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
454 | } |
||
455 | |||
456 | $service['autowire'] = $service['autowired']; |
||
457 | } |
||
458 | |||
459 | if (isset($service['autowire'])) { |
||
460 | // nette |
||
461 | if ($definition->isAutowired() && !$service['autowire']) { |
||
462 | throw new InvalidArgumentException(sprintf('Contradictory definition of autowiring for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
463 | } |
||
464 | |||
465 | $definition->setAutowired($service['autowire']); |
||
466 | } |
||
467 | |||
468 | if (isset($service['autowiring_types'])) { |
||
469 | if (is_string($service['autowiring_types'])) { |
||
470 | $definition->addAutowiringType($service['autowiring_types']); |
||
471 | } else { |
||
472 | if (!is_array($service['autowiring_types'])) { |
||
473 | throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
474 | } |
||
475 | |||
476 | foreach ($service['autowiring_types'] as $autowiringType) { |
||
477 | if (!is_string($autowiringType)) { |
||
478 | throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
479 | } |
||
480 | |||
481 | $definition->addAutowiringType($autowiringType); |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | |||
486 | $this->container->setDefinition($id, $definition); |
||
487 | } |
||
488 | |||
572 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: