Skip to content

Commit

Permalink
Resolver: tolerates nullable types when resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 14, 2023
1 parent 355cefb commit 3bbcba7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function resolveEntityType(Statement $statement): ?string
trigger_error('Annotation @return should be replaced with native return type at ' . Callback::toString($entity), E_USER_DEPRECATED);
}

return Helpers::ensureClassType($type, sprintf('return type of %s()', Callback::toString($entity)));
return Helpers::ensureClassType($type, sprintf('return type of %s()', Callback::toString($entity)), true);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/ContainerBuilder.resolveTypes.next.php80.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Assert::exception(function () {
$builder->addDefinition('a')
->setFactory([new Statement([Factory::class, 'createUnion']), 'next']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be nullable/built-in/complex, 'stdClass|array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be built-in/complex, 'stdClass|array' given.");
8 changes: 4 additions & 4 deletions tests/DI/ContainerBuilder.resolveTypes.next.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,26 @@ Assert::noError(function () {
$container = @createContainer($builder); // @return is deprecated
});

Assert::exception(function () {
Assert::noError(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setFactory([new Statement([Factory::class, 'createNullableClass']), 'next']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createNullableClass() is expected to not be nullable/built-in/complex, '?Lorem' given.");
});

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setFactory([new Statement([Factory::class, 'createScalarPhpDoc']), 'next']);
$container = @createContainer($builder); // @return is deprecated
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be nullable/built-in/complex, 'array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be built-in/complex, 'array' given.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setFactory([new Statement([Factory::class, 'createScalar']), 'next']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be nullable/built-in/complex, 'array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be built-in/complex, 'array' given.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/ContainerBuilder.resolveTypes.php80.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Assert::exception(function () {
$builder->addDefinition('a')
->setCreator([Factory::class, 'createUnion']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be nullable/built-in/complex, 'stdClass|array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be built-in/complex, 'stdClass|array' given.");
8 changes: 4 additions & 4 deletions tests/DI/ContainerBuilder.resolveTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,26 @@ Assert::noError(function () {
$container = @createContainer($builder); // @return is deprecated
});

Assert::exception(function () {
Assert::noError(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setCreator([Factory::class, 'createNullableClass']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createNullableClass() is expected to not be nullable/built-in/complex, '?stdClass' given.");
});

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setCreator([Factory::class, 'createScalarPhpDoc']);
$container = @createContainer($builder); // @return is deprecated
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be nullable/built-in/complex, 'array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be built-in/complex, 'array' given.");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('a')
->setCreator([Factory::class, 'createScalar']);
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be nullable/built-in/complex, 'array' given.");
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be built-in/complex, 'array' given.");

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

0 comments on commit 3bbcba7

Please sign in to comment.