Skip to content

Commit

Permalink
Merge pull request #5 from moufmouf/fix_nullable_previous
Browse files Browse the repository at this point in the history
Extending a service that does not exist always fails
  • Loading branch information
moufmouf committed Nov 29, 2017
2 parents 00ecab4 + 09fce5f commit 842a244
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ExtensionDefinition.php
Expand Up @@ -40,10 +40,11 @@ public function buildExtensionCode(string $functionName) : string
$returnTypeCode = '';
$returnType = $this->reflectionMethod->getReturnType();
if ($returnType) {
$allowsNull = $returnType->allowsNull() ? '?':'';
if ($returnType->isBuiltin()) {
$returnTypeCode = ': '.$this->reflectionMethod->getReturnType();
$returnTypeCode = ': '.$allowsNull.$returnType;
} else {
$returnTypeCode = ': \\'.$this->reflectionMethod->getReturnType();
$returnTypeCode = ': '.$allowsNull.'\\'.$returnType;
}
}

Expand All @@ -67,6 +68,10 @@ public function buildExtensionCode(string $functionName) : string
$previousParameterCode = ', '.$previousTypeCode.'$previous';
if ($previousParameter->isDefaultValueAvailable()) {
$previousParameterCode .= ' = '.var_export($previousParameter->getDefaultValue(), true);
} elseif ($previousParameter->allowsNull()) {
// If a first argument has no default null value but is nullable (because of ?),
// we still put the null default value.
$previousParameterCode .= ' = null';
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/TestServiceProvider.php
Expand Up @@ -79,4 +79,14 @@ public static function extendNotExistent(string $value = 'foo') : string
{
return $value;
}

/**
* @Extension(
* name="extendNonExistentNullable"
* )
*/
public static function extendNotExistentNullable(?string $value) : ?string
{
return $value;
}
}
10 changes: 10 additions & 0 deletions tests/ServiceProviderTest.php
Expand Up @@ -169,4 +169,14 @@ public function testExtendNonExistentService()
$value = $simplex->get('extendNonExistent');
$this->assertSame('foo', $value);
}

public function testExtendNonExistentNullableService()
{
$sp = new TestServiceProvider();

$simplex = new Container([$sp]);

$value = $simplex->get('extendNonExistentNullable');
$this->assertNull($value);
}
}

0 comments on commit 842a244

Please sign in to comment.