diff --git a/spec/Prophecy/Doubler/ClassPatch/DisableConstructorPatchSpec.php b/spec/Prophecy/Doubler/ClassPatch/DisableConstructorPatchSpec.php index 2ffb5cc8..0479aa29 100644 --- a/spec/Prophecy/Doubler/ClassPatch/DisableConstructorPatchSpec.php +++ b/spec/Prophecy/Doubler/ClassPatch/DisableConstructorPatchSpec.php @@ -5,6 +5,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Prophecy\Doubler\Generator\Node\ArgumentNode; +use Prophecy\Doubler\Generator\Node\ArgumentTypeNode; use Prophecy\Doubler\Generator\Node\ClassNode; use Prophecy\Doubler\Generator\Node\MethodNode; @@ -31,6 +32,9 @@ function it_makes_all_constructor_arguments_optional( ArgumentNode $arg1, ArgumentNode $arg2 ) { + $arg1->getTypeNode()->willReturn(new ArgumentTypeNode('string', 'null')); + $arg2->getTypeNode()->willReturn(new ArgumentTypeNode('mixed')); + $class->isExtendable('__construct')->willReturn(true); $class->hasMethod('__construct')->willReturn(true); $class->getMethod('__construct')->willReturn($method); @@ -39,6 +43,8 @@ function it_makes_all_constructor_arguments_optional( $arg1->setDefault(null)->shouldBeCalled(); $arg2->setDefault(null)->shouldBeCalled(); + $arg1->setTypeNode(new ArgumentTypeNode('null', 'string'))->shouldBeCalled(); + $method->setCode(Argument::type('string'))->shouldBeCalled(); $this->apply($class); diff --git a/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php b/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php index 624d94ee..c14b8296 100644 --- a/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php +++ b/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php @@ -11,6 +11,7 @@ namespace Prophecy\Doubler\ClassPatch; +use Prophecy\Doubler\Generator\Node\ArgumentTypeNode; use Prophecy\Doubler\Generator\Node\ClassNode; use Prophecy\Doubler\Generator\Node\MethodNode; @@ -55,6 +56,13 @@ public function apply(ClassNode $node) \assert($constructor !== null); foreach ($constructor->getArguments() as $argument) { $argument->setDefault(null); + + $types = $argument->getTypeNode()->getNonNullTypes(); + if ([] === $types || ['mixed'] === $types || ['\mixed'] === $types) { + continue; + } + + $argument->setTypeNode(new ArgumentTypeNode('null', ...$types)); } $constructor->setCode(<<