Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit f125de0

Browse files
committed
Fix generation of non-optional object parameters that can be null.
Example: function foo(\stdClass $a = null, $b)
1 parent ee170a8 commit f125de0

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Generator/ParameterGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
6363

6464
$param->setVariadic($variadic);
6565

66-
if (! $variadic && $reflectionParameter->isOptional()) {
66+
if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
6767
try {
6868
$param->setDefaultValue($reflectionParameter->getDefaultValue());
6969
} catch (\ReflectionException $e) {

test/Generator/ParameterGeneratorTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ public function testFromReflectionGetDefaultValue()
9898
$this->assertEquals('\'foo\'', (string) $defaultValue);
9999
}
100100

101+
public function testFromReflectionGetDefaultValueNotOptional()
102+
{
103+
$reflectionClass = new \Zend\Code\Reflection\ClassReflection(
104+
'ZendTest\Code\Generator\TestAsset\ParameterClass'
105+
);
106+
$method = $reflectionClass->getMethod('defaultObjectEqualsNullAndNotOptional');
107+
108+
$params = $method->getParameters();
109+
$this->assertCount(2, $params);
110+
111+
$firstParameter = $codeGenParam = ParameterGenerator::fromReflection($params[0]);
112+
$this->assertInstanceOf(ValueGenerator::class, $firstParameter->getDefaultValue());
113+
$this->assertNull($firstParameter->getDefaultValue()->getSourceContent());
114+
}
115+
101116
public function testFromReflectionGetArrayHint()
102117
{
103118
$reflectionParameter = $this->getFirstReflectionParameter('fromArray');

test/Generator/TestAsset/ParameterClass.php

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public function defaultConstant($con = self::FOO)
8282

8383
}
8484

85+
public function defaultObjectEqualsNullAndNotOptional(\stdClass $a = null, $b)
86+
{
87+
88+
}
89+
8590
/**
8691
* @param int $integer
8792
*/

0 commit comments

Comments
 (0)