Skip to content

Commit

Permalink
more readable exception strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 11, 2014
1 parent bbec33d commit 0ac6d3b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static function parseService(ServiceDefinition $definition, $config)

$known = array('class', 'create', 'arguments', 'setup', 'autowired', 'inject', 'parameters', 'implement', 'run', 'tags');
if ($error = array_diff(array_keys($config), $known)) {
throw new Nette\InvalidStateException("Unknown or deprecated key '" . implode("', '", $error) . "' in definition of service.");
throw new Nette\InvalidStateException(sprintf("Unknown or deprecated key '%s' in definition of service.", implode("', '", $error)));
}

$arguments = array();
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Config/Adapters/IniAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static function build($input, & $output, $prefix)
$output[] = "$prefix$key = \"$val\"";

} else {
throw new Nette\InvalidArgumentException("The '$prefix$key' item must be scalar or array, " . gettype($val) ." given.");
throw new Nette\InvalidArgumentException(sprintf("The '%s' item must be scalar or array, %s given.", $prefix . $key, gettype($val)));
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public function addService($name, $service)
throw new Nette\DeprecatedException('Parameter $meta has been removed.');

} elseif (!is_string($name) || !$name) {
throw new Nette\InvalidArgumentException('Service name must be a non-empty string, ' . gettype($name) . ' given.');
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));

} elseif (isset($this->registry[$name])) {
throw new Nette\InvalidStateException("Service '$name' already exists.");

} elseif (is_string($service) || is_array($service) || $service instanceof \Closure || $service instanceof Nette\Callback) {
trigger_error('Passing factories to ' . __METHOD__ . '() is deprecated; pass the object itself.', E_USER_DEPRECATED);
trigger_error(sprintf('Passing factories to %s() is deprecated; pass the object itself.', __METHOD__), E_USER_DEPRECATED);
$service = is_string($service) && !preg_match('#\x00|:#', $service) ? new $service : call_user_func($service, $this);
}

if (!is_object($service)) {
throw new Nette\InvalidArgumentException('Service must be a object, ' . gettype($service) . ' given.');
throw new Nette\InvalidArgumentException(sprintf('Service must be a object, %s given.', gettype($service)));
}

$this->registry[$name] = $service;
Expand Down Expand Up @@ -141,8 +141,7 @@ public function createService($name, array $args = array())
{
$method = Container::getMethodName($name);
if (isset($this->creating[$name])) {
throw new Nette\InvalidStateException("Circular reference detected for services: "
. implode(', ', array_keys($this->creating)) . ".");
throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));

} elseif (!method_exists($this, $method) || $this->getReflection()->getMethod($method)->getName() !== $method) {
throw new MissingServiceException("Service '$name' not found.");
Expand Down Expand Up @@ -244,7 +243,7 @@ public function createInstance($class, array $args = array())
public function callInjects($service)
{
if (!is_object($service)) {
throw new Nette\InvalidArgumentException('Service must be object, ' . gettype($service) . ' given.');
throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
}

foreach (array_reverse(get_class_methods($service)) as $method) {
Expand Down
12 changes: 6 additions & 6 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ContainerBuilder extends Nette\Object
public function addDefinition($name, ServiceDefinition $definition = NULL)
{
if (!is_string($name) || !$name) { // builder is not ready for falsy names such as '0'
throw new Nette\InvalidArgumentException("Service name must be a non-empty string, " . gettype($name) . " given.");
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));

} elseif (isset($this->definitions[$name])) {
throw new Nette\InvalidStateException("Service '$name' has already been added.");
Expand Down Expand Up @@ -295,7 +295,7 @@ public function prepareClassList()
private function resolveClass($name, $recursive = array())
{
if (isset($recursive[$name])) {
throw new ServiceCreationException('Circular reference detected for services: ' . implode(', ', array_keys($recursive)) . '.');
throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($recursive))));
}
$recursive[$name] = TRUE;

Expand All @@ -320,12 +320,12 @@ private function resolveClass($name, $recursive = array())
}
}
if (!is_callable($factory)) {
throw new ServiceCreationException("Factory '" . Nette\Utils\Callback::toString($factory) . "' is not callable.");
throw new ServiceCreationException(sprintf("Factory '%s' is not callable.", Nette\Utils\Callback::toString($factory)));
}
try {
$reflection = Nette\Utils\Callback::toReflection($factory);
} catch (\ReflectionException $e) {
throw new ServiceCreationException("Missing factory '" . Nette\Utils\Callback::toString($factory) . "'.");
throw new ServiceCreationException(sprintf("Missing factory '%s'.", Nette\Utils\Callback::toString($factory)));
}
$def->class = preg_replace('#[|\s].*#', '', $reflection->getAnnotation('return'));
if ($def->class && $reflection instanceof \ReflectionMethod) {
Expand Down Expand Up @@ -413,7 +413,7 @@ public function generateClasses($className = 'Container', $parentName = 'Nette\D
throw new ServiceCreationException('Name contains invalid characters.');
}
$containerClass->addMethod($methodName)
->addDocument("@return " . ($def->implement ?: $def->class))
->addDocument('@return ' . ($def->implement ?: $def->class))
->setBody($name === self::THIS_CONTAINER ? 'return $this;' : $this->generateService($name))
->setParameters($def->implement ? array() : $this->convertParameters($def->parameters));
} catch (\Exception $e) {
Expand Down Expand Up @@ -564,7 +564,7 @@ public function formatStatement(Statement $statement)
return $this->formatPhp("new $entity" . ($arguments ? '(?*)' : ''), array($arguments));

} elseif (!Nette\Utils\Arrays::isList($entity) || count($entity) !== 2) {
throw new ServiceCreationException("Expected class, method or property, " . PhpHelpers::dump($entity) . " given.");
throw new ServiceCreationException(sprintf('Expected class, method or property, %s given.', PhpHelpers::dump($entity)));

} elseif ($entity[0] === '') { // globalFunc
return $this->formatPhp("$entity[1](?*)", array($arguments));
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function expand($var, array $params, $recursive = FALSE)
$res .= '%';

} elseif (isset($recursive[$part])) {
throw new Nette\InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($recursive)) . '.');
throw new Nette\InvalidArgumentException(sprintf('Circular reference detected for variables: %s.', implode(', ', array_keys($recursive))));

} else {
try {
Expand Down

0 comments on commit 0ac6d3b

Please sign in to comment.