Skip to content

Commit

Permalink
Merge pull request #651 from kukulich/optional
Browse files Browse the repository at this point in the history
Parameter with default value before variadic parameter should be optional
  • Loading branch information
Ocramius committed Jun 9, 2020
2 parents 9982305 + ee2c824 commit 4cda6ae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ private function setNodeOptionalFlag() : void
$overallOptionalFlag = true;
$lastParamIndex = count($this->getNode()->params) - 1;
for ($i = $lastParamIndex; $i >= 0; $i--) {
$hasDefault = ($this->getNode()->params[$i]->default !== null);
$param = $this->getNode()->params[$i];

// When we find the first parameter that does not have a default,
// When we find the first parameter that does not have a default or is not variadic,
// flip the flag as all params for this are no longer optional
// EVEN if they have a default value
if (! $hasDefault) {
if ($param->default === null && ! $param->variadic) {
$overallOptionalFlag = false;
}

$this->getNode()->params[$i]->isOptional = $overallOptionalFlag;
$param->isOptional = $overallOptionalFlag;
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/unit/Fixture/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function methodWithVariadic($nonVariadicParameter, ...$variadicParameter)
{
}

public function methodWithFirstParameterWithDefaultValueAndSecondParameterIsVariadic($parameterWithDefaultValue = null, ...$variadicParameter)
{
}

public function methodWithReference($nonRefParameter, &$refParameter)
{
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function getMethodsWithFilterDataProvider() : array
[CoreReflectionMethod::IS_STATIC, 1],
[CoreReflectionMethod::IS_ABSTRACT, 1],
[CoreReflectionMethod::IS_FINAL, 1],
[CoreReflectionMethod::IS_PUBLIC, 16],
[CoreReflectionMethod::IS_PUBLIC, 17],
[CoreReflectionMethod::IS_PROTECTED, 1],
[CoreReflectionMethod::IS_PRIVATE, 1],
[
Expand All @@ -183,7 +183,7 @@ public function getMethodsWithFilterDataProvider() : array
CoreReflectionMethod::IS_PUBLIC |
CoreReflectionMethod::IS_PROTECTED |
CoreReflectionMethod::IS_PRIVATE,
18,
19,
],
];
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/Reflection/ReflectionParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ public function testGetDefaultValueAndIsOptional() : void
self::assertFalse($secondParam->isDefaultValueAvailable());
}

public function testParameterWithDefaultValueBeforeVariadicParameterShouldBeOptional() : void
{
$classInfo = $this->reflector->reflect(Methods::class);
$method = $classInfo->getMethod('methodWithFirstParameterWithDefaultValueAndSecondParameterIsVariadic');

$firstParam = $method->getParameter('parameterWithDefaultValue');
self::assertTrue($firstParam->isOptional());
self::assertTrue($firstParam->isDefaultValueAvailable());

$secondParam = $method->getParameter('variadicParameter');
self::assertTrue($secondParam->isOptional());
self::assertTrue($secondParam->isVariadic());
}

/**
* @group 109
*/
Expand Down

0 comments on commit 4cda6ae

Please sign in to comment.