Skip to content

Commit 2e0f1c9

Browse files
committed
Make optional constructor parameters nullable explicitly
Implicitly nullable types are deprecated in PHP 8.4
1 parent e3810e5 commit 2e0f1c9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spec/Prophecy/Doubler/ClassPatch/DisableConstructorPatchSpec.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
77
use Prophecy\Doubler\Generator\Node\ArgumentNode;
8+
use Prophecy\Doubler\Generator\Node\ArgumentTypeNode;
89
use Prophecy\Doubler\Generator\Node\ClassNode;
910
use Prophecy\Doubler\Generator\Node\MethodNode;
1011

@@ -31,6 +32,9 @@ function it_makes_all_constructor_arguments_optional(
3132
ArgumentNode $arg1,
3233
ArgumentNode $arg2
3334
) {
35+
$arg1->getTypeNode()->willReturn(new ArgumentTypeNode('string', 'null'));
36+
$arg2->getTypeNode()->willReturn(new ArgumentTypeNode('mixed'));
37+
3438
$class->isExtendable('__construct')->willReturn(true);
3539
$class->hasMethod('__construct')->willReturn(true);
3640
$class->getMethod('__construct')->willReturn($method);
@@ -39,6 +43,14 @@ function it_makes_all_constructor_arguments_optional(
3943
$arg1->setDefault(null)->shouldBeCalled();
4044
$arg2->setDefault(null)->shouldBeCalled();
4145

46+
if (\PHP_VERSION_ID >= 80400) {
47+
$arg1->setTypeNode(new ArgumentTypeNode('null', 'string'))->shouldBeCalled();
48+
$arg2->setTypeNode()->shouldNotBeCalled();
49+
} else {
50+
$arg1->setTypeNode()->shouldNotBeCalled();
51+
$arg2->setTypeNode()->shouldNotBeCalled();
52+
}
53+
4254
$method->setCode(Argument::type('string'))->shouldBeCalled();
4355

4456
$this->apply($class);

src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Prophecy\Doubler\ClassPatch;
1313

14+
use Prophecy\Doubler\Generator\Node\ArgumentTypeNode;
1415
use Prophecy\Doubler\Generator\Node\ClassNode;
1516
use Prophecy\Doubler\Generator\Node\MethodNode;
1617

@@ -55,6 +56,16 @@ public function apply(ClassNode $node)
5556
\assert($constructor !== null);
5657
foreach ($constructor->getArguments() as $argument) {
5758
$argument->setDefault(null);
59+
if (\PHP_VERSION_ID < 80400) {
60+
continue;
61+
}
62+
63+
$types = $argument->getTypeNode()->getNonNullTypes();
64+
if ([] == $types || ['mixed'] == $types) {
65+
continue;
66+
}
67+
68+
$argument->setTypeNode(new ArgumentTypeNode('null', ...$types));
5869
}
5970

6071
$constructor->setCode(<<<PHP

0 commit comments

Comments
 (0)