Skip to content

Commit

Permalink
InjectExtension: updated exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2020
1 parent 78f5212 commit ad1d29c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public static function getInjectProperties(string $class): array
if (DI\Helpers::parseAnnotation($rp, 'inject') !== null) {
if ($type = Reflection::getPropertyType($rp)) {
} elseif ($type = DI\Helpers::parseAnnotation($rp, 'var')) {
if (strpos($type, '|') !== false) {
throw new Nette\InvalidStateException('The ' . Reflection::toString($rp) . ' is not expected to have a union type.');
}
$type = Reflection::expandClassName($type, Reflection::getPropertyDeclaringClass($rp));
}
$res[$name] = $type;
Expand Down Expand Up @@ -154,11 +157,11 @@ private static function checkType($class, string $name, ?string $type, $containe
{
$propName = Reflection::toString(new \ReflectionProperty($class, $name));
if (!$type) {
throw new Nette\InvalidStateException("Property $propName has no @var annotation.");
throw new Nette\InvalidStateException("Property $propName has no type hint.");
} elseif (!class_exists($type) && !interface_exists($type)) {
throw new Nette\InvalidStateException("Class or interface '$type' used in @var annotation at $propName not found. Check annotation and 'use' statements.");
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found. Check type and 'use' statements.");
} elseif ($container && !$container->getByType($type, false)) {
throw new Nette\DI\MissingServiceException("Service of type $type used in @var annotation at $propName not found. Did you add it to configuration file?");
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found. Did you add it to configuration file?");
}
}
}
6 changes: 3 additions & 3 deletions tests/DI/InjectExtension.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:
factory: ServiceA
inject: yes
');
}, InvalidStateException::class, 'Service of type DateTimeImmutable used in @var annotation at ServiceA::$a not found. Did you add it to configuration file?');
}, InvalidStateException::class, 'Service of type DateTimeImmutable used in type hint at ServiceA::$a not found. Did you add it to configuration file?');


Assert::exception(function () use ($compiler) {
Expand All @@ -56,7 +56,7 @@ services:
factory: ServiceB
inject: yes
');
}, InvalidStateException::class, "Class or interface 'Unknown' used in @var annotation at ServiceB::\$a not found. Check annotation and 'use' statements.");
}, InvalidStateException::class, "Class or interface 'Unknown' used in type hint at ServiceB::\$a not found. Check type and 'use' statements.");


Assert::exception(function () use ($compiler) {
Expand All @@ -66,4 +66,4 @@ services:
factory: ServiceC
inject: yes
');
}, InvalidStateException::class, 'Property ServiceC::$a has no @var annotation.');
}, InvalidStateException::class, 'Property ServiceC::$a has no type hint.');

0 comments on commit ad1d29c

Please sign in to comment.