diff --git a/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php b/src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php index 432f08a6..f3422441 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; /** @@ -25,8 +26,15 @@ 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()), ); diff --git a/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php b/test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php index 0ef64e08..93aaf250 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(