diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 634e37455..d51d703d7 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -216,7 +216,8 @@ public function getByType($type, $throw = false) { $type = Helpers::normalizeClass($type); - if ($this->currentService !== null + if ( + $this->currentService !== null && is_a($this->definitions[$this->currentService]->getType(), $type, true) ) { return $this->currentService; @@ -337,7 +338,8 @@ public function prepareClassList() } // auto-disable autowiring for aliases - if ($def->getAutowired() === true + if ( + $def->getAutowired() === true && ($alias = $this->getServiceName($def->getFactory()->getEntity())) && (!$def->getImplement() || (!Strings::contains($alias, '\\') && $this->definitions[$alias]->getImplement())) ) { @@ -438,7 +440,8 @@ private function resolveImplement(ServiceDefinition $def, $name) if (!$def->getEntity()) { $def->setFactory($def->getType(), $def->getFactory() ? $def->getFactory()->arguments : []); } - if (($class = $this->resolveEntityType($def->getFactory(), [$name => 1])) + if ( + ($class = $this->resolveEntityType($def->getFactory(), [$name => 1])) && ($ctor = (new ReflectionClass($class))->getConstructor()) ) { foreach ($ctor->getParameters() as $param) { @@ -657,9 +660,10 @@ public function completeStatement(Statement $statement) if (!$arguments && substr($entity[1], -2) === '[]') { throw new ServiceCreationException("Missing argument for $entity[1]."); } - } elseif ($type = empty($service) || $entity[1] === 'create' - ? $this->resolveEntityType($entity[0]) - : $this->definitions[$service]->getType() + } elseif ( + $type = empty($service) || $entity[1] === 'create' + ? $this->resolveEntityType($entity[0]) + : $this->definitions[$service]->getType() ) { $arguments = $this->autowireArguments($type, $entity[1], $arguments); } diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index 6a8f46f2b..56f7ba46c 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -118,7 +118,10 @@ private function generateService($name) $this->currentService = null; $code = '$service = ' . $this->formatStatement($factory) . ";\n"; - if ((PHP_VERSION_ID < 70000 || $def->getSetup()) && ($type = $def->getType()) && !$serviceRef && $type !== $entity + if ( + (PHP_VERSION_ID < 70000 || $def->getSetup()) + && ($type = $def->getType()) + && !$serviceRef && $type !== $entity && !(is_string($entity) && preg_match('#^[\w\\\\]+\z#', $entity) && is_subclass_of($entity, $type)) ) { $code .= PhpHelpers::formatArgs("if (!\$service instanceof $type) {\n" diff --git a/src/DI/Statement.php b/src/DI/Statement.php index 143e59a61..e067cab91 100644 --- a/src/DI/Statement.php +++ b/src/DI/Statement.php @@ -31,8 +31,11 @@ class Statement */ public function __construct($entity, array $arguments = []) { - if (!is_string($entity) && !(is_array($entity) && isset($entity[0], $entity[1])) - && !$entity instanceof ServiceDefinition && $entity !== null + if ( + !is_string($entity) + && !(is_array($entity) && isset($entity[0], $entity[1])) + && !$entity instanceof ServiceDefinition + && $entity !== null ) { throw new Nette\InvalidArgumentException('Argument is not valid Statement entity.'); }