Skip to content

Commit

Permalink
improved exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 14, 2021
1 parent f21f128 commit f575f06
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function addService(string $name, $service)

if ($service instanceof \Closure) {
$rt = Nette\Utils\Type::fromReflection(new \ReflectionFunction($service));
$type = $rt ? Helpers::ensureClassType($rt, 'return type of factory') : '';
$type = $rt ? Helpers::ensureClassType($rt, 'return type of closure') : '';
} else {
$type = get_class($service);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Definitions/ServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function resolveType(Nette\DI\Resolver $resolver): void
} elseif (!$this->getType()) {
$type = $resolver->resolveEntityType($this->factory);
if (!$type) {
throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory.');
throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.');
}

$this->setType($type);
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Container.dynamic.php80.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ $container = new Container;
Assert::exception(function () use ($container) {
@$container->addService('six', function (): stdClass|Closure {}); // @ triggers service should be defined as "imported"
$container->getService('six');
}, Nette\InvalidStateException::class, "Return type of factory is not expected to be nullable/union/intersection/built-in, 'stdClass|Closure' given.");
}, Nette\InvalidStateException::class, "Return type of closure is not expected to be nullable/union/intersection/built-in, 'stdClass|Closure' given.");
2 changes: 1 addition & 1 deletion tests/DI/ContainerBuilder.factory.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('one')->setFactory('Bad7::create');
$builder->complete();
}, Nette\DI\ServiceCreationException::class, "Service 'one': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'one': Unknown service type, specify it or declare return type of factory method.");


class Bad8
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ namespace
$builder->addDefinition('f')
->setFactory('@factory::createObject');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory method.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
Expand All @@ -145,6 +145,6 @@ namespace
$builder->addDefinition('f')
->setFactory('@factory::createMixed');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory method.");

}
8 changes: 4 additions & 4 deletions tests/DI/ContainerBuilder.resolveTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ Assert::exception(function () {
$builder->addDefinition('a')
->setFactory([Factory::class, 'createObjectPhpDoc']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setFactory([Factory::class, 'createObject']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
Expand All @@ -161,14 +161,14 @@ Assert::exception(function () {
$builder->addDefinition('a')
->setFactory([Factory::class, 'createMixedPhpDoc']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setFactory([Factory::class, 'createMixed']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
Expand Down

0 comments on commit f575f06

Please sign in to comment.