Skip to content

Commit

Permalink
uses nette/utils 3.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 19, 2021
1 parent 720e989 commit cb3f8e3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"nette/php-generator": "^3.3.3",
"nette/robot-loader": "^3.2",
"nette/schema": "^1.1",
"nette/utils": "^3.1.4"
"nette/utils": "^3.1.5"
},
"require-dev": {
"nette/tester": "^2.2",
Expand Down
14 changes: 5 additions & 9 deletions src/DI/Definitions/FactoryDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette;
use Nette\DI\ServiceCreationException;
use Nette\Utils\Reflection;
use Nette\Utils\Type;


/**
Expand Down Expand Up @@ -242,13 +243,11 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
}

foreach ($method->getParameters() as $param) {
$methodHint = Reflection::getParameterTypes($param);
$methodType = Type::fromReflection($param);
if (isset($ctorParams[$param->name])) {
$ctorParam = $ctorParams[$param->name];
$ctorHint = Reflection::getParameterTypes($ctorParam);
if ($methodHint !== $ctorHint
&& !is_a((string) reset($methodHint), (string) reset($ctorHint), true)
) {
$ctorType = Type::fromReflection($ctorParam);
if ($ctorType && !$ctorType->allows((string) $methodType)) {
throw new ServiceCreationException(sprintf(
"Type of \$%s in %s::create() doesn't match type in %s constructor.",
$param->name,
Expand All @@ -267,10 +266,7 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
) . ($hint ? ", did you mean \${$hint}?" : '.'));
}

$paramDef = PHP_VERSION_ID < 80000
? ($methodHint && $param->allowsNull() ? '?' : '') . reset($methodHint)
: implode('|', $methodHint);
$paramDef .= ' ' . $param->name;
$paramDef = $methodType . ' ' . $param->name;
if ($param->isDefaultValueAvailable()) {
$this->parameters[$paramDef] = Reflection::getParameterDefaultValue($param);
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/DI/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Utils\Reflection;
use Nette\Utils\Type;
use ReflectionClass;
use ReflectionMethod;

Expand Down Expand Up @@ -121,7 +122,7 @@ class_uses($name),
$name,
$prop->name,
$prop->getDocComment(),
Reflection::getPropertyTypes($prop),
(string) Type::fromReflection($prop),
PHP_VERSION_ID >= 80000 ? count($prop->getAttributes(Attributes\Inject::class)) : null,
];
}
Expand All @@ -133,7 +134,7 @@ class_uses($name),
$method->name,
$method->getDocComment(),
self::hashParameters($method),
Reflection::getReturnTypes($method),
(string) Type::fromReflection($method),
];
}
}
Expand All @@ -157,7 +158,7 @@ class_uses($name),
$uses,
$method->getDocComment(),
self::hashParameters($method),
Reflection::getReturnTypes($method),
(string) Type::fromReflection($method),
];
}

Expand All @@ -171,7 +172,7 @@ private static function hashParameters(\ReflectionFunctionAbstract $method): arr
foreach ($method->getParameters() as $param) {
$res[] = [
$param->name,
Reflection::getParameterTypes($param),
(string) Type::fromReflection($param),
$param->isVariadic(),
$param->isDefaultValueAvailable()
? [Reflection::getParameterDefaultValue($param)]
Expand Down
10 changes: 5 additions & 5 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
));
}

$types = array_diff(Reflection::getParameterTypes($parameter), ['null']);
$type = count($types) === 1 ? reset($types) : null;
$rtype = Nette\Utils\Type::fromReflection($parameter);
$type = $rtype ? $rtype->getSingleName() : null;
$method = $parameter->getDeclaringFunction();

if ($type && !Reflection::isBuiltinType($type)) {
if ($type && !$rtype->isBuiltin()) {
try {
$res = $getter($type, true);
} catch (MissingServiceException $e) {
Expand Down Expand Up @@ -607,7 +607,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
return $getter($itemType, false);

} elseif (
($types && $parameter->allowsNull())
($rtype && $parameter->allowsNull())
|| $parameter->isOptional()
|| $parameter->isDefaultValueAvailable()
) {
Expand All @@ -621,7 +621,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
throw new ServiceCreationException(sprintf(
'Parameter %s has %s, so its value must be specified.',
$desc,
count($types) > 1 ? 'union type and no default value' : 'no class type or default value'
$rtype && $rtype->isUnion() ? 'union type and no default value' : 'no class type or default value'
));
}
}
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, 'The {closure}%a?% is not expected to have a union type.');
}, Nette\InvalidStateException::class, 'The {closure}%a?% is not expected to have a union%a?% type.');
2 changes: 1 addition & 1 deletion tests/DI/InjectExtension.getInjectProperties().php80.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EClass

Assert::exception(function () {
InjectExtension::getInjectProperties(AClass::class);
}, Nette\InvalidStateException::class, 'The AClass::$var is not expected to have a union type.');
}, Nette\InvalidStateException::class, 'The AClass::$var is not expected to have a union%a?% type.');

Assert::same([
'varA' => 'EInjected',
Expand Down

0 comments on commit cb3f8e3

Please sign in to comment.