Skip to content

Commit

Permalink
Fix function parameter default enum value
Browse files Browse the repository at this point in the history
fixes #1198

Add tests
(add assertions to other test complaining about them too)
  • Loading branch information
murrant authored and ghostwriter committed Apr 22, 2023
1 parent e39a644 commit 2eb7f1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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));
$paramDef .= ' = ' . ((is_object($defaultValue) && ! enum_exists($defaultValue::class)) ? get_class($defaultValue) : var_export($defaultValue, true));
} 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 2eb7f1d

Please sign in to comment.