Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function parameter default enum value #1199

Merged
merged 2 commits into from
Apr 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}