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

Workaround for non-static default values for arguments #615

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,48 @@ function it_generates_read_only_class_if_parent_class_is_read_only(ClassNode $cl
readonly class CustomClass extends \ReadOnlyClass implements \Prophecy\Doubler\Generator\MirroredInterface {


}
}
PHP;
$expected = strtr($expected, array("\r\n" => "\n", "\r" => "\n"));
$code->shouldBe($expected);
}

function it_generates_correct_code_if_argument_default_value_is_an_object(
ClassNode $class,
MethodNode $method,
ArgumentNode $argument
) {
$class->getParentClass()->willReturn('ClassWithArgument');
$class->getInterfaces()->willReturn(array('Prophecy\Doubler\Generator\MirroredInterface'));
$class->getProperties()->willReturn(array());
$class->getMethods()->willReturn(array($method));
$class->isReadOnly()->willReturn(false);

$method->getName()->willReturn('foo');
$method->getVisibility()->willReturn('public');
$method->isStatic()->willReturn(false);
$method->getArguments()->willReturn([$argument]);
$method->getReturnTypeNode()->willReturn(new ReturnTypeNode());
$method->returnsReference()->willReturn(false);
$method->getCode()->willReturn('');

$argument->getTypeNode()->willReturn(new ArgumentTypeNode(\DateTimeInterface::class));
$argument->getName()->willReturn('arg');
$argument->isPassedByReference()->willReturn(false);
$argument->isVariadic()->willReturn(false);
$argument->isOptional()->willReturn(true);
$argument->getDefault()->willReturn(new \DateTime());

$code = $this->generate('CustomClass', $class);
$expected =<<<'PHP'
namespace {
class CustomClass extends \ClassWithArgument implements \Prophecy\Doubler\Generator\MirroredInterface {

public function foo(\DateTimeInterface $arg = NULL) {

}

}
}
PHP;
Expand Down
7 changes: 6 additions & 1 deletion src/Prophecy/Doubler/Generator/ClassCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ private function generateArguments(array $arguments): array
$php .= '$'.$argument->getName();

if ($argument->isOptional() && !$argument->isVariadic()) {
$php .= ' = '.var_export($argument->getDefault(), true);
$default = $argument->getDefault();
if (is_object($argument->getDefault())) {
$default = null;
}

$php .= ' = '.var_export($default, true);
}

return $php;
Expand Down
Loading