From 3e7dd4bf234772285f698eade7179846a7565bee Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 24 Jan 2024 18:16:38 +0100 Subject: [PATCH 1/2] Add regression test for #568 --- .../Generator/ClassCodeGeneratorSpec.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php b/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php index 50fb1704c..dad9423b1 100644 --- a/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php +++ b/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php @@ -422,6 +422,46 @@ 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; From 7d6edbe71db7d50cc473b4f2a13d059a6593a711 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 24 Jan 2024 18:19:52 +0100 Subject: [PATCH 2/2] Fallback to null when argument default value is an object --- spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php | 4 +++- src/Prophecy/Doubler/Generator/ClassCodeGenerator.php | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php b/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php index dad9423b1..46c68cfde 100644 --- a/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php +++ b/spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php @@ -459,9 +459,11 @@ function it_generates_correct_code_if_argument_default_value_is_an_object( $expected =<<<'PHP' namespace { class CustomClass extends \ClassWithArgument implements \Prophecy\Doubler\Generator\MirroredInterface { -public function foo(\DateTimeInterface $arg = NULL) { + +public function foo(\DateTimeInterface $arg = NULL) { } + } } PHP; diff --git a/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php b/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php index 2d30a3f2e..489117b5f 100644 --- a/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php +++ b/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php @@ -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;