Skip to content

Commit

Permalink
Fix function parameter default enum value (#1199)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Apr 22, 2023
2 parents e39a644 + b2d3e3b commit b64176d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function renderParams(Method $method, $config)

$methodParams = array();
$params = $method->getParameters();
$isPhp81 = \PHP_VERSION_ID >= 80100;
foreach ($params as $param) {
$paramDef = $this->renderTypeHint($param);
$paramDef .= $param->isPassedByReference() ? '&' : '';
Expand All @@ -76,7 +77,17 @@ protected function renderParams(Method $method, $config)
if (!$param->isVariadic()) {
if (false !== $param->isDefaultValueAvailable()) {
$defaultValue = $param->getDefaultValue();
$paramDef .= ' = ' . (is_object($defaultValue) ? get_class($defaultValue) : var_export($defaultValue, true));

if (is_object($defaultValue)){
$prefix = get_class($defaultValue);
if ($isPhp81 && enum_exists($prefix)) {
$prefix = var_export($defaultValue, true);
}
}else{
$prefix = var_export($defaultValue, true);
}

$paramDef .= ' = ' . $prefix;
} elseif ($param->isOptional()) {
$paramDef .= ' = null';
}
Expand Down
25 changes: 24 additions & 1 deletion tests/PHP81/Php81LanguageFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ReturnTypeWillChange;
use RuntimeException;
use Serializable;

use function pcntl_fork;
use function pcntl_waitpid;
use function pcntl_wexitstatus;
Expand Down Expand Up @@ -123,6 +122,15 @@ public function it_can_mock_a_class_with_a_never_returning_type_hint_with_exit()

$mock->exits();
}

/** @test */
public function it_can_parse_enum_as_default_value_correctly()
{
$mock = Mockery::mock(UsesEnums::class);
$mock->shouldReceive('set')->once();
$mock->set();
$this->assertEquals(SimpleEnum::first, $mock->enum); // check that mock did not set internal variable
}
}

interface LoggerInterface
Expand Down Expand Up @@ -214,3 +222,18 @@ public function foo(IntersectionTypeHelper1Interface&IntersectionTypeHelper2Inte
{
}
}

enum SimpleEnum
{
case first;
case second;
}

class UsesEnums
{
public SimpleEnum $enum = SimpleEnum::first;
public function set(SimpleEnum $enum = SimpleEnum::second)
{
$this->enum = $enum;
}
}

0 comments on commit b64176d

Please sign in to comment.