Skip to content

Commit d63ceaf

Browse files
committed
PhpGenerator: improved optional parameter detection (PHP is buggy until version 5.4.7)
1 parent 1acc593 commit d63ceaf

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

src/PhpGenerator/Parameter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public static function from(\ReflectionParameter $from)
5050
$param = new static;
5151
$param->name = $from->getName();
5252
$param->reference = $from->isPassedByReference();
53-
$param->optional = $from->isOptional() || $from->allowsNull();
54-
$param->defaultValue = $from->isOptional() ? $from->getDefaultValue() : NULL; // PHP bug #62988
5553
try {
5654
$param->typeHint = $from->isArray() ? 'array' : ($from->getClass() ? '\\' . $from->getClass()->getName() : '');
5755
} catch (\ReflectionException $e) {
@@ -61,6 +59,9 @@ public static function from(\ReflectionParameter $from)
6159
throw $e;
6260
}
6361
}
62+
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable();
63+
$param->defaultValue = (PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable()) ? $from->getDefaultValue() : NULL;
64+
6465
$namespace = /*5.2*PHP_VERSION_ID < 50300 ? '' : */$from->getDeclaringClass()->getNamespaceName();
6566
$namespace = $namespace ? "\\$namespace\\" : "\\";
6667
if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {

tests/PhpGenerator/PhpGenerator.reflection.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Class2 extends Class1 implements Interface2
5757
* @return Class1
5858
*
5959
*/
60-
private function & func3(array $a, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d)
60+
private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, $e)
6161
{
6262

6363
}

tests/PhpGenerator/PhpGenerator.reflection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Class2 extends Class1 implements Interface2
6161
* Func3
6262
* @return Class1
6363
*/
64-
private function & func3(array $a = array(), Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d)
64+
private function & func3(array $a = array(), Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, $e)
6565
{}
6666

6767
final function func2()

0 commit comments

Comments
 (0)