Skip to content

Commit

Permalink
Fix #520: Error if an optional parameter is before required parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 5, 2017
1 parent 3d57084 commit 99de099
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Definition/Resolver/ParameterResolver.php
Expand Up @@ -59,7 +59,7 @@ public function resolveParameters(
$value = &$definitionParameters[$index];
} else {
// If the parameter is optional and wasn't specified, we take its default value
if ($parameter->isOptional()) {
if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) {
$args[] = $this->getParameterDefaultValue($parameter, $method);
continue;
}
Expand Down
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest\Definitions\AutowireDefinition;

class OptionalParameterFollowedByRequiredParameter
{
public $first;
public $second;

public function __construct($first = null, \stdClass $second)
{
$this->first = $first;
$this->second = $second;
}
}
14 changes: 14 additions & 0 deletions tests/IntegrationTest/Definitions/AutowireDefinitionTest.php
Expand Up @@ -6,6 +6,7 @@

use DI\ContainerBuilder;
use DI\Test\IntegrationTest\BaseContainerTest;
use DI\Test\IntegrationTest\Definitions\AutowireDefinition\OptionalParameterFollowedByRequiredParameter;
use DI\Test\IntegrationTest\Definitions\AutowireDefinitionTest\ConstructorInjection;
use DI\Test\IntegrationTest\Definitions\AutowireDefinitionTest\LazyService;
use DI\Test\IntegrationTest\Definitions\AutowireDefinitionTest\NullableConstructorParameter;
Expand Down Expand Up @@ -307,6 +308,19 @@ public function test_autowire_lazy_object(ContainerBuilder $builder)
self::assertEquals('bar', $object->bar);
self::assertTrue($object->isProxyInitialized());
}

/**
* @dataProvider provideContainer
*/
public function test_optional_parameter_followed_by_required_parameters(ContainerBuilder $builder)
{
$container = $builder->build();

$object = $container->get(OptionalParameterFollowedByRequiredParameter::class);

self::assertNull($object->first);
self::assertInstanceOf(\stdClass::class, $object->second);
}
}

namespace DI\Test\IntegrationTest\Definitions\AutowireDefinitionTest;
Expand Down

0 comments on commit 99de099

Please sign in to comment.