Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 196d98c

Browse files
committed
Dont duplicate the ? before nullable params in certain cases
1 parent e66f55a commit 196d98c

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/Mock.hh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ final class Mock<TC> implements MockInterface {
5757
);
5858

5959
foreach ($method->getParameters() as $parameter) {
60+
$return_type_hint = Str\trim((string) $parameter->getType());
61+
6062
if ($parameter->isDefaultValueAvailable() === true) {
6163
if ($parameter->isDefaultValueConstant()) {
6264
$gen_method->addParameterf(
@@ -79,12 +81,12 @@ final class Mock<TC> implements MockInterface {
7981
if (is_array($default_value)) {
8082
$default_value = '[]';
8183
}
82-
if ($parameter->allowsNull()) {
84+
if ($parameter->allowsNull() && !Str\starts_with($return_type_hint, '?')) {
8385
$nullable = '?';
8486
}
8587
$gen_method->addParameterf(
8688
'%s%s $%s = %s',
87-
(string) $parameter->getType(),
89+
$return_type_hint,
8890
$nullable,
8991
$parameter->getName(),
9092
$default_value
@@ -94,15 +96,16 @@ final class Mock<TC> implements MockInterface {
9496
} else {
9597
$nullable = '';
9698
$default = '';
97-
$type = Str\trim((string) $parameter->getType());
9899
if ($parameter->allowsNull()) {
99-
$nullable = '?';
100+
if (!Str\starts_with($return_type_hint, '?')) {
101+
$nullable = '?';
102+
}
100103
$default = ' = null';
101104
}
102105
$gen_method->addParameterf(
103106
'%s%s $%s%s',
104107
$nullable,
105-
$type,
108+
$return_type_hint,
106109
$parameter->getName(),
107110
$default
108111
);

tests/SampleInterface.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ interface SampleInterface {
1919
public function hasDefaultForParameterBuildsCorrectMock(string $text = 'something'): void;
2020

2121
public function hasNullAsDefaultForParemterBuildsCorrectMock(?string $text = null): void;
22+
23+
public function isNullableWithoutDefaultsBuildsCorrectMock(?string $text): void;
2224
}

0 commit comments

Comments
 (0)