Skip to content

Commit 810eb88

Browse files
authored
Merge pull request #987 from mstegmeyer/fix/adding-optional-parameters-to-constructor
fix: adding optional parameters to contructor
2 parents 68937e2 + 72de41e commit 810eb88

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Roave\BetterReflection\Reflection\ReflectionParameter;
1212

1313
use function array_diff;
14+
use function array_filter;
1415
use function array_map;
1516

1617
/**
@@ -25,8 +26,15 @@ public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMetho
2526
return Changes::empty();
2627
}
2728

29+
$toParameters = $toMethod->getParameters();
30+
if ($fromMethod->isConstructor()) {
31+
// new optional parameters of constructors are not BC breaks,
32+
// as the method signature of child classes does not need to match the parent
33+
$toParameters = array_filter($toParameters, static fn (ReflectionParameter $param) => ! $param->isOptional());
34+
}
35+
2836
$added = array_diff(
29-
array_map(static fn (ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()),
37+
array_map(static fn (ReflectionParameter $param) => $param->getName(), $toParameters),
3038
array_map(static fn (ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()),
3139
);
3240

test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function noParameters() {}
109109
public function twoParams(int $one, int $two) {}
110110
private function privateMethod() {}
111111
public function removedParameter(int $one, array $options = []) {}
112+
public function __construct(int $one) {}
112113
}
113114
PHP
114115
,
@@ -127,6 +128,7 @@ public function noParameters() {}
127128
public function twoParams(int $one, int $two) {}
128129
private function privateMethod(array $options = []) {}
129130
public function removedParameter(int $one) {}
131+
public function __construct(int $one, int $two, array $options = []) {}
130132
}
131133
PHP
132134
,
@@ -149,6 +151,7 @@ public function removedParameter(int $one) {}
149151
'privateMethod' => [],
150152
'removedParameter' => [],
151153
'twoParams' => [],
154+
'__construct' => ['[BC] ADDED: Parameter two was added to Method __construct() of class TheClass'],
152155
];
153156

154157
return array_combine(

0 commit comments

Comments
 (0)