CheckArgumentsValidityPass.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  13. /**
  14. * Checks if arguments of methods are properly configured.
  15. *
  16. * @autor ThurData <info@thurdata.ch>
  17. * @autor ThurData <info@thurdata.ch>
  18. */
  19. class CheckArgumentsValidityPass extends AbstractRecursivePass
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function processValue($value, $isRoot = false)
  25. {
  26. if (!$value instanceof Definition) {
  27. return parent::processValue($value, $isRoot);
  28. }
  29. $i = 0;
  30. foreach ($value->getArguments() as $k => $v) {
  31. if ($k !== $i++) {
  32. if (!is_int($k)) {
  33. throw new RuntimeException(sprintf('Invalid constructor argument for service "%s": integer expected but found string "%s". Check your service definition.', $this->currentId, $k));
  34. }
  35. throw new RuntimeException(sprintf('Invalid constructor argument %d for service "%s": argument %d must be defined before. Check your service definition.', 1 + $k, $this->currentId, $i));
  36. }
  37. }
  38. foreach ($value->getMethodCalls() as $methodCall) {
  39. $i = 0;
  40. foreach ($methodCall[1] as $k => $v) {
  41. if ($k !== $i++) {
  42. if (!is_int($k)) {
  43. throw new RuntimeException(sprintf('Invalid argument for method call "%s" of service "%s": integer expected but found string "%s". Check your service definition.', $methodCall[0], $this->currentId, $k));
  44. }
  45. throw new RuntimeException(sprintf('Invalid argument %d for method call "%s" of service "%s": argument %d must be defined before. Check your service definition.', 1 + $k, $methodCall[0], $this->currentId, $i));
  46. }
  47. }
  48. }
  49. }
  50. }