From 641d9ffab021b0068ad8ac9cb87ccfde47180a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 29 Nov 2017 16:24:41 +0100 Subject: [PATCH 1/2] Fixing nullable with no default previous management and nullable return type --- src/ExtensionDefinition.php | 8 ++++++-- tests/Fixtures/TestServiceProvider.php | 10 ++++++++++ tests/ServiceProviderTest.php | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ExtensionDefinition.php b/src/ExtensionDefinition.php index 33251a9..c2243a2 100644 --- a/src/ExtensionDefinition.php +++ b/src/ExtensionDefinition.php @@ -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; } } @@ -67,6 +68,9 @@ 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'; } } diff --git a/tests/Fixtures/TestServiceProvider.php b/tests/Fixtures/TestServiceProvider.php index 8cf1e1e..fd8c7d0 100644 --- a/tests/Fixtures/TestServiceProvider.php +++ b/tests/Fixtures/TestServiceProvider.php @@ -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; + } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 54a9b62..ec7b6ed 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -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); + } } From 09fce5f2617bff54966ea5de853639c6cc717533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 29 Nov 2017 16:29:40 +0100 Subject: [PATCH 2/2] CS --- src/ExtensionDefinition.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ExtensionDefinition.php b/src/ExtensionDefinition.php index c2243a2..b036911 100644 --- a/src/ExtensionDefinition.php +++ b/src/ExtensionDefinition.php @@ -69,7 +69,8 @@ public function buildExtensionCode(string $functionName) : string 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. + // If a first argument has no default null value but is nullable (because of ?), + // we still put the null default value. $previousParameterCode .= ' = null'; } }