8000 Fix generation of non-optional object parameters that can be null. by digilist · Pull Request #95 · zendframework/zend-code · GitHub
[go: up one dir, main page]

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

Fix generation of non-optional object parameters that can be null. #95

Merged
merged 1 commit into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
2 changes: 1 addition & 1 deletion src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function fromReflection(ParameterReflection $reflectionParameter)

$param->setVariadic($variadic);

if (! $variadic && $reflectionParameter->isOptional()) {
if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
try {
$param->setDefaultValue($reflectionParameter->getDefaultValue());
} catch (\ReflectionException $e) {
Expand Down
15 changes: 15 additions & 0 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public function testFromReflectionGetDefaultValue()
$this->assertEquals('\'foo\'', (string) $defaultValue);
}

public function testFromReflectionGetDefaultValueNotOptional()
{
$reflectionClass = new \Zend\Code\Reflection\ClassReflection(
'ZendTest\Code\Generator\TestAsset\ParameterClass'
);
$method = $reflectionClass->getMethod('defaultObjectEqualsNullAndNotOptional');

$params = $method->getParameters();
$this->assertCount(2, $params);

$firstParameter = $codeGenParam = ParameterGenerator::fromReflection($params[0]);
$this->assertInstanceOf(ValueGenerator::class, $firstParameter->getDefaultValue());
$this->assertNull($firstParameter->getDefaultValue()->getSourceContent());
}

public function testFromReflectionGetArrayHint()
{
$reflectionParameter = $this->getFirstReflectionParameter('fromArray');
Expand Down
5 changes: 5 additions & 0 deletions test/Generator/TestAsset/ParameterClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function defaultConstant($con = self::FOO)

}

public function defaultObjectE 5046 qualsNullAndNotOptional(\stdClass $a = null, $b)
{

}

/**
* @param int $integer
*/
Expand Down
0