Conditions | 111 |
Total Lines | 377 |
Code Lines | 217 |
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 |
||
340 | private function parseDefinition(string $id, array|string|null $service, string $file, array $defaults, bool $return = false, bool $trackBindings = true): Definition|Alias|null |
||
341 | { |
||
342 | if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) { |
||
343 | throw new InvalidArgumentException(\sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id)); |
||
344 | } |
||
345 | |||
346 | if (\is_string($service) && str_starts_with($service, '@')) { |
||
347 | $alias = new Alias(substr($service, 1)); |
||
348 | |||
349 | if (isset($defaults['public'])) { |
||
350 | $alias->setPublic($defaults['public']); |
||
351 | } |
||
352 | |||
353 | return $return ? $alias : $this->container->setAlias($id, $alias); |
||
354 | } |
||
355 | |||
356 | if (\is_array($service) && $this->isUsingShortSyntax($service)) { |
||
357 | $service = ['arguments' => $service]; |
||
358 | } |
||
359 | |||
360 | if (!\is_array($service ??= [])) { |
||
361 | throw new InvalidArgumentException(\sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file)); |
||
362 | } |
||
363 | |||
364 | if (isset($service['stack'])) { |
||
365 | if (!\is_array($service['stack'])) { |
||
366 | throw new InvalidArgumentException(\sprintf('A stack must be an array of definitions, "%s" given for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file)); |
||
367 | } |
||
368 | |||
369 | $stack = []; |
||
370 | |||
371 | foreach ($service['stack'] as $k => $frame) { |
||
372 | if (\is_array($frame) && 1 === \count($frame) && !isset(self::SERVICE_KEYWORDS[key($frame)])) { |
||
373 | $frame = [ |
||
374 | 'class' => key($frame), |
||
375 | 'arguments' => current($frame), |
||
376 | ]; |
||
377 | } |
||
378 | |||
379 | if (\is_array($frame) && isset($frame['stack'])) { |
||
380 | throw new InvalidArgumentException(\sprintf('Service stack "%s" cannot contain another stack in "%s".', $id, $file)); |
||
381 | } |
||
382 | |||
383 | $definition = $this->parseDefinition($id.'" at index "'.$k, $frame, $file, $defaults, true); |
||
384 | |||
385 | if ($definition instanceof Definition) { |
||
386 | $definition->setInstanceofConditionals($this->instanceof); |
||
387 | } |
||
388 | |||
389 | $stack[$k] = $definition; |
||
390 | } |
||
391 | |||
392 | if ($diff = array_diff(array_keys($service), ['stack', 'public', 'deprecated'])) { |
||
393 | throw new InvalidArgumentException(\sprintf('Invalid attribute "%s"; supported ones are "public" and "deprecated" for service "%s" in "%s". Check your YAML syntax.', implode('", "', $diff), $id, $file)); |
||
394 | } |
||
395 | |||
396 | $service = [ |
||
397 | 'parent' => '', |
||
398 | 'arguments' => $stack, |
||
399 | 'tags' => ['container.stack'], |
||
400 | 'public' => $service['public'] ?? null, |
||
401 | 'deprecated' => $service['deprecated'] ?? null, |
||
402 | ]; |
||
403 | } |
||
404 | |||
405 | $definition = isset($service[0]) && $service[0] instanceof Definition ? array_shift($service) : null; |
||
406 | $return = null === $definition ? $return : true; |
||
407 | |||
408 | if (isset($service['from_callable'])) { |
||
409 | foreach (['alias', 'parent', 'synthetic', 'factory', 'file', 'arguments', 'properties', 'configurator', 'calls'] as $key) { |
||
410 | if (isset($service['factory'])) { |
||
411 | throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported for the service "%s" when using "from_callable" in "%s".', $key, $id, $file)); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | if ('Closure' !== $service['class'] ??= 'Closure') { |
||
416 | $service['lazy'] = true; |
||
417 | } |
||
418 | |||
419 | $service['factory'] = ['Closure', 'fromCallable']; |
||
420 | $service['arguments'] = [$service['from_callable']]; |
||
421 | unset($service['from_callable']); |
||
422 | } |
||
423 | |||
424 | $this->checkDefinition($id, $service, $file); |
||
425 | |||
426 | if (isset($service['alias'])) { |
||
427 | $alias = new Alias($service['alias']); |
||
428 | |||
429 | if (isset($service['public'])) { |
||
430 | $alias->setPublic($service['public']); |
||
431 | } elseif (isset($defaults['public'])) { |
||
432 | $alias->setPublic($defaults['public']); |
||
433 | } |
||
434 | |||
435 | foreach ($service as $key => $value) { |
||
436 | if (!\in_array($key, ['alias', 'public', 'deprecated'])) { |
||
437 | throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias", "public" and "deprecated".', $key, $id, $file)); |
||
438 | } |
||
439 | |||
440 | if ('deprecated' === $key) { |
||
441 | $deprecation = \is_array($value) ? $value : ['message' => $value]; |
||
442 | |||
443 | if (!isset($deprecation['package'])) { |
||
444 | throw new InvalidArgumentException(\sprintf('Missing attribute "package" of the "deprecated" option in "%s".', $file)); |
||
445 | } |
||
446 | |||
447 | if (!isset($deprecation['version'])) { |
||
448 | throw new InvalidArgumentException(\sprintf('Missing attribute "version" of the "deprecated" option in "%s".', $file)); |
||
449 | } |
||
450 | |||
451 | $alias->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message'] ?? ''); |
||
452 | } |
||
453 | } |
||
454 | |||
455 | return $return ? $alias : $this->container->setAlias($id, $alias); |
||
456 | } |
||
457 | |||
458 | $changes = []; |
||
459 | if (null !== $definition) { |
||
460 | $changes = $definition->getChanges(); |
||
461 | } elseif ($this->isLoadingInstanceof) { |
||
462 | $definition = new ChildDefinition(''); |
||
463 | } elseif (isset($service['parent'])) { |
||
464 | if ('' !== $service['parent'] && '@' === $service['parent'][0]) { |
||
465 | throw new InvalidArgumentException(\sprintf('The value of the "parent" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['parent'], substr($service['parent'], 1))); |
||
466 | } |
||
467 | |||
468 | $definition = new ChildDefinition($service['parent']); |
||
469 | } else { |
||
470 | $definition = new Definition(); |
||
471 | } |
||
472 | |||
473 | if (isset($defaults['public'])) { |
||
474 | $definition->setPublic($defaults['public']); |
||
475 | } |
||
476 | if (isset($defaults['autowire'])) { |
||
477 | $definition->setAutowired($defaults['autowire']); |
||
478 | } |
||
479 | if (isset($defaults['autoconfigure'])) { |
||
480 | $definition->setAutoconfigured($defaults['autoconfigure']); |
||
481 | } |
||
482 | |||
483 | $definition->setChanges($changes); |
||
484 | |||
485 | if (isset($service['class'])) { |
||
486 | $definition->setClass($service['class']); |
||
487 | } |
||
488 | |||
489 | if (isset($service['shared'])) { |
||
490 | $definition->setShared($service['shared']); |
||
491 | } |
||
492 | |||
493 | if (isset($service['synthetic'])) { |
||
494 | $definition->setSynthetic($service['synthetic']); |
||
495 | } |
||
496 | |||
497 | if (isset($service['lazy'])) { |
||
498 | $definition->setLazy((bool) $service['lazy']); |
||
499 | if (\is_string($service['lazy'])) { |
||
500 | $definition->addTag('proxy', ['interface' => $service['lazy']]); |
||
501 | } |
||
502 | } |
||
503 | |||
504 | if (isset($service['public'])) { |
||
505 | $definition->setPublic($service['public']); |
||
506 | } |
||
507 | |||
508 | if (isset($service['abstract'])) { |
||
509 | $definition->setAbstract($service['abstract']); |
||
510 | } |
||
511 | |||
512 | if (isset($service['deprecated'])) { |
||
513 | $deprecation = \is_array($service['deprecated']) ? $service['deprecated'] : ['message' => $service['deprecated']]; |
||
514 | |||
515 | if (!isset($deprecation['package'])) { |
||
516 | throw new InvalidArgumentException(\sprintf('Missing attribute "package" of the "deprecated" option in "%s".', $file)); |
||
517 | } |
||
518 | |||
519 | if (!isset($deprecation['version'])) { |
||
520 | throw new InvalidArgumentException(\sprintf('Missing attribute "version" of the "deprecated" option in "%s".', $file)); |
||
521 | } |
||
522 | |||
523 | $definition->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message'] ?? ''); |
||
524 | } |
||
525 | |||
526 | if (isset($service['factory'])) { |
||
527 | $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file)); |
||
528 | } |
||
529 | |||
530 | if (isset($service['constructor'])) { |
||
531 | if (null !== $definition->getFactory()) { |
||
532 | throw new LogicException(\sprintf('The "%s" service cannot declare a factory as well as a constructor.', $id)); |
||
533 | } |
||
534 | |||
535 | $definition->setFactory([null, $service['constructor']]); |
||
536 | } |
||
537 | |||
538 | if (isset($service['file'])) { |
||
539 | $definition->setFile($service['file']); |
||
540 | } |
||
541 | |||
542 | if (isset($service['arguments'])) { |
||
543 | $definition->setArguments($this->resolveServices($service['arguments'], $file)); |
||
544 | } |
||
545 | |||
546 | if (isset($service['properties'])) { |
||
547 | $definition->setProperties($this->resolveServices($service['properties'], $file)); |
||
548 | } |
||
549 | |||
550 | if (isset($service['configurator'])) { |
||
551 | $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file)); |
||
552 | } |
||
553 | |||
554 | if (isset($service['calls'])) { |
||
555 | if (!\is_array($service['calls'])) { |
||
556 | throw new InvalidArgumentException(\sprintf('Parameter "calls" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
557 | } |
||
558 | |||
559 | foreach ($service['calls'] as $k => $call) { |
||
560 | if (!\is_array($call) && (!\is_string($k) || !$call instanceof TaggedValue)) { |
||
561 | throw new InvalidArgumentException(\sprintf('Invalid method call for service "%s": expected map or array, "%s" given in "%s".', $id, $call instanceof TaggedValue ? '!'.$call->getTag() : get_debug_type($call), $file)); |
||
562 | } |
||
563 | |||
564 | if (\is_string($k)) { |
||
565 | throw new InvalidArgumentException(\sprintf('Invalid method call for service "%s", did you forget a leading dash before "%s: ..." in "%s"?', $id, $k, $file)); |
||
566 | } |
||
567 | |||
568 | if (isset($call['method']) && \is_string($call['method'])) { |
||
569 | $method = $call['method']; |
||
570 | $args = $call['arguments'] ?? []; |
||
571 | $returnsClone = $call['returns_clone'] ?? false; |
||
572 | } else { |
||
573 | if (1 === \count($call) && \is_string(key($call))) { |
||
574 | $method = key($call); |
||
575 | $args = $call[$method]; |
||
576 | |||
577 | if ($args instanceof TaggedValue) { |
||
578 | if ('returns_clone' !== $args->getTag()) { |
||
579 | throw new InvalidArgumentException(\sprintf('Unsupported tag "!%s", did you mean "!returns_clone" for service "%s" in "%s"?', $args->getTag(), $id, $file)); |
||
580 | } |
||
581 | |||
582 | $returnsClone = true; |
||
583 | $args = $args->getValue(); |
||
584 | } else { |
||
585 | $returnsClone = false; |
||
586 | } |
||
587 | } elseif (empty($call[0])) { |
||
588 | throw new InvalidArgumentException(\sprintf('Invalid call for service "%s": the method must be defined as the first index of an array or as the only key of a map in "%s".', $id, $file)); |
||
589 | } else { |
||
590 | $method = $call[0]; |
||
591 | $args = $call[1] ?? []; |
||
592 | $returnsClone = $call[2] ?? false; |
||
593 | } |
||
594 | } |
||
595 | |||
596 | if (!\is_array($args)) { |
||
597 | throw new InvalidArgumentException(\sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in "%s". Check your YAML syntax.', $method, $id, $file)); |
||
598 | } |
||
599 | |||
600 | $args = $this->resolveServices($args, $file); |
||
601 | $definition->addMethodCall($method, $args, $returnsClone); |
||
602 | } |
||
603 | } |
||
604 | |||
605 | $tags = $service['tags'] ?? []; |
||
606 | if (!\is_array($tags)) { |
||
607 | throw new InvalidArgumentException(\sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
608 | } |
||
609 | |||
610 | if (isset($defaults['tags'])) { |
||
611 | $tags = array_merge($tags, $defaults['tags']); |
||
612 | } |
||
613 | |||
614 | foreach ($tags as $tag) { |
||
615 | if (!\is_array($tag)) { |
||
616 | $tag = ['name' => $tag]; |
||
617 | } |
||
618 | |||
619 | if (1 === \count($tag) && \is_array(current($tag))) { |
||
620 | $name = key($tag); |
||
621 | $tag = current($tag); |
||
622 | } else { |
||
623 | if (!isset($tag['name'])) { |
||
624 | throw new InvalidArgumentException(\sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file)); |
||
625 | } |
||
626 | $name = $tag['name']; |
||
627 | unset($tag['name']); |
||
628 | } |
||
629 | |||
630 | if (!\is_string($name) || '' === $name) { |
||
631 | throw new InvalidArgumentException(\sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $id, $file)); |
||
632 | } |
||
633 | |||
634 | $this->validateAttributes(\sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in "%s". Check your YAML syntax.', $id, $name, '%s', $file), $tag); |
||
635 | |||
636 | $definition->addTag($name, $tag); |
||
637 | } |
||
638 | |||
639 | if (null !== $decorates = $service['decorates'] ?? null) { |
||
640 | if ('' !== $decorates && '@' === $decorates[0]) { |
||
641 | throw new InvalidArgumentException(\sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($decorates, 1))); |
||
642 | } |
||
643 | |||
644 | $decorationOnInvalid = \array_key_exists('decoration_on_invalid', $service) ? $service['decoration_on_invalid'] : 'exception'; |
||
645 | if ('exception' === $decorationOnInvalid) { |
||
646 | $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
||
647 | } elseif ('ignore' === $decorationOnInvalid) { |
||
648 | $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
||
649 | } elseif (null === $decorationOnInvalid) { |
||
650 | $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE; |
||
651 | } elseif ('null' === $decorationOnInvalid) { |
||
652 | throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean null (without quotes) in "%s"?', $decorationOnInvalid, $id, $file)); |
||
653 | } else { |
||
654 | throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean "exception", "ignore" or null in "%s"?', $decorationOnInvalid, $id, $file)); |
||
655 | } |
||
656 | |||
657 | $renameId = $service['decoration_inner_name'] ?? null; |
||
658 | $priority = $service['decoration_priority'] ?? 0; |
||
659 | |||
660 | $definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior); |
||
661 | } |
||
662 | |||
663 | if (isset($service['autowire'])) { |
||
664 | $definition->setAutowired($service['autowire']); |
||
665 | } |
||
666 | |||
667 | if (isset($defaults['bind']) || isset($service['bind'])) { |
||
668 | // deep clone, to avoid multiple process of the same instance in the passes |
||
669 | $bindings = $definition->getBindings(); |
||
670 | $bindings += isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : []; |
||
671 | |||
672 | if (isset($service['bind'])) { |
||
673 | if (!\is_array($service['bind'])) { |
||
674 | throw new InvalidArgumentException(\sprintf('Parameter "bind" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
675 | } |
||
676 | |||
677 | $bindings = array_merge($bindings, $this->resolveServices($service['bind'], $file)); |
||
678 | $bindingType = $this->isLoadingInstanceof ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING; |
||
679 | foreach ($bindings as $argument => $value) { |
||
680 | if (!$value instanceof BoundArgument) { |
||
681 | $bindings[$argument] = new BoundArgument($value, $trackBindings, $bindingType, $file); |
||
682 | } |
||
683 | } |
||
684 | } |
||
685 | |||
686 | $definition->setBindings($bindings); |
||
687 | } |
||
688 | |||
689 | if (isset($service['autoconfigure'])) { |
||
690 | $definition->setAutoconfigured($service['autoconfigure']); |
||
691 | } |
||
692 | |||
693 | if (\array_key_exists('namespace', $service) && !\array_key_exists('resource', $service)) { |
||
694 | throw new InvalidArgumentException(\sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
695 | } |
||
696 | |||
697 | if ($return) { |
||
698 | if (\array_key_exists('resource', $service)) { |
||
699 | throw new InvalidArgumentException(\sprintf('Invalid "resource" attribute found for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
700 | } |
||
701 | |||
702 | return $definition; |
||
703 | } |
||
704 | |||
705 | if (\array_key_exists('resource', $service)) { |
||
706 | if (!\is_string($service['resource'])) { |
||
707 | throw new InvalidArgumentException(\sprintf('A "resource" attribute must be of type string for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
||
708 | } |
||
709 | $exclude = $service['exclude'] ?? null; |
||
710 | $namespace = $service['namespace'] ?? $id; |
||
711 | $this->registerClasses($definition, $namespace, $service['resource'], $exclude, $file); |
||
712 | } else { |
||
713 | $this->setDefinition($id, $definition); |
||
714 | } |
||
715 | |||
716 | return null; |
||
717 | } |
||
994 |