From 5f9d171d6ab052d8e22120d2bcd7a4aa99fe7e7e Mon Sep 17 00:00:00 2001 From: Max Stegmeyer Date: Fri, 13 Feb 2026 10:55:27 +0100 Subject: [PATCH 1/5] fix: adding optional parameters to contructor --- .../BCBreak/MethodBased/MethodParameterAdded.php | 11 +++++++++++ .../BCBreak/MethodBased/MethodParameterAddedTest.php | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index 432f08a6..caab0c94 100644 --- a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php +++ b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php @@ -30,6 +30,17 @@ public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMetho array_map(static fn (ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()), ); + // new optional parameters of constructors are not BC breaks, + // as the method signature of child classes does not need to match the parent + if ($fromMethod->isConstructor()) { + foreach ($added as $key => $paramName) { + $parameter = $toMethod->getParameter($paramName); + if ($parameter->isOptional()) { + unset($added[$key]); + } + } + } + return Changes::fromList( ...array_map( static fn (string $paramName): Change => Change::added( diff --git a/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php b/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php index 0ef64e08..85e123e3 100644 --- a/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php +++ b/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php @@ -109,6 +109,7 @@ public function noParameters() {} public function twoParams(int $one, int $two) {} private function privateMethod() {} public function removedParameter(int $one, array $options = []) {} + public function __construct(int $one) {} } PHP , @@ -127,6 +128,7 @@ public function noParameters() {} public function twoParams(int $one, int $two) {} private function privateMethod(array $options = []) {} public function removedParameter(int $one) {} + public function __construct(int $one, int $two, array $options = []) {} } PHP , @@ -149,6 +151,7 @@ public function removedParameter(int $one) {} 'privateMethod' => [], 'removedParameter' => [], 'twoParams' => [], + '__construct' => ['[BC] ADDED: Parameter two was added to Method __construct() of class TheClass',], ]; return array_combine( From 327814136d4acde4ab181f6b4cc722bfba4ce51b Mon Sep 17 00:00:00 2001 From: Max Stegmeyer Date: Fri, 13 Feb 2026 14:31:04 +0100 Subject: [PATCH 2/5] refactor: foreach to array_filter --- .../BCBreak/MethodBased/MethodParameterAdded.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index caab0c94..a1b78b22 100644 --- a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php +++ b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php @@ -33,12 +33,7 @@ public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMetho // new optional parameters of constructors are not BC breaks, // as the method signature of child classes does not need to match the parent if ($fromMethod->isConstructor()) { - foreach ($added as $key => $paramName) { - $parameter = $toMethod->getParameter($paramName); - if ($parameter->isOptional()) { - unset($added[$key]); - } - } + $added = array_filter($added, static fn (string $paramName) => !($toMethod->getParameter($paramName)?->isOptional())); } return Changes::fromList( From 66cab698c36f71dfdc50c70a196bd42009691c3e Mon Sep 17 00:00:00 2001 From: Max Stegmeyer Date: Fri, 13 Feb 2026 15:12:36 +0100 Subject: [PATCH 3/5] refactor: move filtering up --- .../BCBreak/MethodBased/MethodParameterAdded.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index a1b78b22..a2e6521c 100644 --- a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php +++ b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php @@ -25,17 +25,18 @@ public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMetho return Changes::empty(); } + $toParameters = $toMethod->getParameters(); + if ($fromMethod->isConstructor()) { + // new optional parameters of constructors are not BC breaks, + // as the method signature of child classes does not need to match the parent + $toParameters = array_filter($toParameters, static fn (ReflectionParameter $param) => !$param->isOptional()); + } + $added = array_diff( - array_map(static fn (ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), + array_map(static fn (ReflectionParameter $param) => $param->getName(), $toParameters), array_map(static fn (ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()), ); - // new optional parameters of constructors are not BC breaks, - // as the method signature of child classes does not need to match the parent - if ($fromMethod->isConstructor()) { - $added = array_filter($added, static fn (string $paramName) => !($toMethod->getParameter($paramName)?->isOptional())); - } - return Changes::fromList( ...array_map( static fn (string $paramName): Change => Change::added( From b05448ba5d20313f5e59afb6b7a85bea7added31 Mon Sep 17 00:00:00 2001 From: Max Stegmeyer Date: Fri, 13 Feb 2026 17:07:02 +0100 Subject: [PATCH 4/5] fix: add use statement --- src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index a2e6521c..b17dec6a 100644 --- a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php +++ b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php @@ -11,6 +11,7 @@ use Roave\BetterReflection\Reflection\ReflectionParameter; use function array_diff; +use function array_filter; use function array_map; /** From 72de41e9e562f9a4fcd9899685aca588256fdc5b Mon Sep 17 00:00:00 2001 From: Max Stegmeyer Date: Fri, 13 Feb 2026 17:38:06 +0100 Subject: [PATCH 5/5] fix: codestyle --- src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php | 2 +- .../BCBreak/MethodBased/MethodParameterAddedTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index b17dec6a..f3422441 100644 --- a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php +++ b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php @@ -30,7 +30,7 @@ public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMetho if ($fromMethod->isConstructor()) { // new optional parameters of constructors are not BC breaks, // as the method signature of child classes does not need to match the parent - $toParameters = array_filter($toParameters, static fn (ReflectionParameter $param) => !$param->isOptional()); + $toParameters = array_filter($toParameters, static fn (ReflectionParameter $param) => ! $param->isOptional()); } $added = array_diff( diff --git a/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php b/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php index 85e123e3..93aaf250 100644 --- a/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php +++ b/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php @@ -151,7 +151,7 @@ public function __construct(int $one, int $two, array $options = []) {} 'privateMethod' => [], 'removedParameter' => [], 'twoParams' => [], - '__construct' => ['[BC] ADDED: Parameter two was added to Method __construct() of class TheClass',], + '__construct' => ['[BC] ADDED: Parameter two was added to Method __construct() of class TheClass'], ]; return array_combine(