Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Roave\BetterReflection\Reflection\ReflectionParameter;

use function array_diff;
use function array_filter;
use function array_map;

/**
Expand All @@ -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()),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
,
Expand All @@ -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
,
Expand All @@ -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(
Expand Down
Loading