Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection\Compiler; Chris@14: Chris@14: use Symfony\Component\DependencyInjection\Definition; Chris@14: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@14: Chris@14: /** Chris@14: * Checks if arguments of methods are properly configured. Chris@14: * Chris@14: * @author Kévin Dunglas Chris@14: * @author Nicolas Grekas Chris@14: */ Chris@14: class CheckArgumentsValidityPass extends AbstractRecursivePass Chris@14: { Chris@14: private $throwExceptions; Chris@14: Chris@14: public function __construct($throwExceptions = true) Chris@14: { Chris@14: $this->throwExceptions = $throwExceptions; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: protected function processValue($value, $isRoot = false) Chris@14: { Chris@14: if (!$value instanceof Definition) { Chris@14: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: $i = 0; Chris@14: foreach ($value->getArguments() as $k => $v) { Chris@14: if ($k !== $i++) { Chris@17: if (!\is_int($k)) { Chris@14: $msg = sprintf('Invalid constructor argument for service "%s": integer expected but found string "%s". Check your service definition.', $this->currentId, $k); Chris@14: $value->addError($msg); Chris@14: if ($this->throwExceptions) { Chris@14: throw new RuntimeException($msg); Chris@14: } Chris@14: Chris@14: break; Chris@14: } Chris@14: Chris@14: $msg = sprintf('Invalid constructor argument %d for service "%s": argument %d must be defined before. Check your service definition.', 1 + $k, $this->currentId, $i); Chris@14: $value->addError($msg); Chris@14: if ($this->throwExceptions) { Chris@14: throw new RuntimeException($msg); Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: foreach ($value->getMethodCalls() as $methodCall) { Chris@14: $i = 0; Chris@14: foreach ($methodCall[1] as $k => $v) { Chris@14: if ($k !== $i++) { Chris@17: if (!\is_int($k)) { Chris@14: $msg = 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); Chris@14: $value->addError($msg); Chris@14: if ($this->throwExceptions) { Chris@14: throw new RuntimeException($msg); Chris@14: } Chris@14: Chris@14: break; Chris@14: } Chris@14: Chris@14: $msg = 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); Chris@14: $value->addError($msg); Chris@14: if ($this->throwExceptions) { Chris@14: throw new RuntimeException($msg); Chris@14: } Chris@14: } Chris@14: } Chris@14: } Chris@14: } Chris@14: }