Skip to content

Commit

Permalink
Container: added typehint object
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 16, 2023
1 parent c900421 commit 8257f49
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
22 changes: 7 additions & 15 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ public function getParameters(): array
* @param object $service service or its factory
* @return static
*/
public function addService(string $name, $service)
public function addService(string $name, object $service)
{
$name = $this->aliases[$name] ?? $name;
if (isset($this->instances[$name])) {
throw new Nette\InvalidStateException(sprintf("Service '%s' already exists.", $name));

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

if ($service instanceof \Closure) {
Expand Down Expand Up @@ -117,10 +114,9 @@ public function removeService(string $name): void

/**
* Gets the service object by name.
* @return object
* @throws MissingServiceException
*/
public function getService(string $name)
public function getService(string $name): object
{
if (!isset($this->instances[$name])) {
if (isset($this->aliases[$name])) {
Expand All @@ -136,10 +132,9 @@ public function getService(string $name)

/**
* Gets the service object by name.
* @return object
* @throws MissingServiceException
*/
public function getByName(string $name)
public function getByName(string $name): object
{
return $this->getService($name);
}
Expand Down Expand Up @@ -194,10 +189,9 @@ public function isCreated(string $name): bool

/**
* Creates new instance of the service.
* @return object
* @throws MissingServiceException
*/
public function createService(string $name, array $args = [])
public function createService(string $name, array $args = []): object
{
$name = $this->aliases[$name] ?? $name;
$method = self::getMethodName($name);
Expand Down Expand Up @@ -237,7 +231,7 @@ public function createService(string $name, array $args = [])
* @return ?T
* @throws MissingServiceException
*/
public function getByType(string $type, bool $throw = true)
public function getByType(string $type, bool $throw = true): ?object
{
$type = Helpers::normalizeClass($type);
if (!empty($this->wiring[$type][0])) {
Expand Down Expand Up @@ -313,10 +307,9 @@ public function findByTag(string $tag): array

/**
* Creates new instance using autowiring.
* @return object
* @throws Nette\InvalidArgumentException
*/
public function createInstance(string $class, array $args = [])
public function createInstance(string $class, array $args = []): object
{
$rc = new \ReflectionClass($class);
if (!$rc->isInstantiable()) {
Expand All @@ -335,9 +328,8 @@ public function createInstance(string $class, array $args = [])

/**
* Calls all methods starting with with "inject" using autowiring.
* @param object $service
*/
public function callInjects($service): void
public function callInjects(object $service): void
{
Extensions\InjectExtension::callInjects($this, $service);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function resolveEntityType(Statement $statement): ?string
$this->addDependency($reflection);

$type = Nette\Utils\Type::fromReflection($reflection) ?? ($annotation = Helpers::getReturnTypeAnnotation($reflection));
if ($type && !in_array((string) $type, ['object', 'mixed'], true)) {
if ($type && !in_array($type->getSingleName(), ['object', 'mixed'], true)) {
if (isset($annotation)) {
trigger_error('Annotation @return should be replaced with native return type at ' . Callback::toString($entity), E_USER_DEPRECATED);
}
Expand Down
4 changes: 0 additions & 4 deletions tests/DI/Container.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ Assert::exception(function () use ($container, $service) {
$container->addService('', $service);
}, Nette\InvalidArgumentException::class, 'Service name must be a non-empty string.');

Assert::exception(function () use ($container) {
$container->addService('one', null);
}, Nette\InvalidArgumentException::class, "Service 'one' must be a object, NULL given.");

Assert::exception(function () use ($container) {
$container->getService('one');
}, Nette\DI\MissingServiceException::class, "Service 'one' not found.");
Expand Down
34 changes: 34 additions & 0 deletions tests/DI/ContainerBuilder.resolve.Container.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Test: Nette\DI\Container::getByType() can be resolved
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class Lorem
{
public function next(): stdClass
{
return new stdClass;
}
}


$container = createContainer(new DI\Compiler, '
services:
lorem: Lorem
next:
factory: @container::getByType(Lorem)::next()
type: stdClass
');


Assert::type(stdClass::class, $container->getService('next'));
2 changes: 1 addition & 1 deletion tests/DI/ContainerBuilder.resolveTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Assert::exception(function () {
$builder->addDefinition('a')
->setCreator([Factory::class, 'createObjectNullable']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createObjectNullable() is expected to not be nullable/built-in/complex, '?object' given.");
}, 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 8257f49

Please sign in to comment.