Skip to content

Commit

Permalink
Helpers::autowireArguments() can handle PHP 7.1 nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 21, 2016
1 parent 7d1385a commit aa40af0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function autowireArguments(\ReflectionFunctionAbstract $method, ar
$optCount = 0;
}

} elseif ($parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
} elseif (($class && $parameter->allowsNull()) || $parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
// !optional + defaultAvailable = func($a = NULL, $b) since 5.4.7
// optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
$res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
Expand Down
42 changes: 42 additions & 0 deletions tests/DI/Helpers.autowireArguments.php71.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Test: Nette\DI\Config\Helpers::autowireArguments()
* @phpversion 7.1
*/

use Nette\DI\Helpers;
use Tester\Assert;


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


class Container
{
function getByType($type)
{
return $type === 'Test' ? new Test : NULL;
}
}

class Test
{
function method(Test $class, self $self, Undefined $nullable1 = NULL, int $nullable2 = NULL)
{}

function methodNullable(?Test $class, ?self $self, ?Undefined $nullable1, ?int $nullable2)
{}
}

$container = new Container;

Assert::equal(
[new Test, new Test],
Helpers::autowireArguments(new ReflectionMethod('Test', 'method'), [], $container)
);

Assert::equal(
[new Test, new Test],
Helpers::autowireArguments(new ReflectionMethod('Test', 'methodNullable'), [], $container)
);

0 comments on commit aa40af0

Please sign in to comment.