-
Notifications
You must be signed in to change notification settings - Fork 61
Add check for adding param to method #801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased; | ||
|
||
use Psl\Str; | ||
use Roave\BackwardCompatibility\Change; | ||
use Roave\BackwardCompatibility\Changes; | ||
use Roave\BetterReflection\Reflection\ReflectionMethod; | ||
use Roave\BetterReflection\Reflection\ReflectionParameter; | ||
|
||
use function array_diff; | ||
use function array_map; | ||
|
||
/** | ||
* Adding a parameter to a method on a non-final class is a BC break. | ||
* Any child classes which extend public and protected methods will be incompatible with the new method signature. | ||
*/ | ||
final class MethodParameterAdded implements MethodBased | ||
{ | ||
public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMethod): Changes | ||
{ | ||
if ($fromMethod->getDeclaringClass()->isFinal() || $fromMethod->isPrivate()) { | ||
return Changes::empty(); | ||
} | ||
|
||
$added = array_diff( | ||
array_map(static fn (ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), | ||
array_map(static fn (ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()), | ||
); | ||
|
||
return Changes::fromList( | ||
...array_map( | ||
static fn (string $paramName): Change => Change::added( | ||
Str\format( | ||
'Parameter %s was added to Method %s() of class %s', | ||
$paramName, | ||
$fromMethod->getName(), | ||
$fromMethod->getDeclaringClass()->getName(), | ||
), | ||
), | ||
$added, | ||
), | ||
); | ||
} | ||
} |
178 changes: 178 additions & 0 deletions
178
test/unit/DetectChanges/BCBreak/MethodBased/MethodParameterAddedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\MethodBased; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Roave\BackwardCompatibility\Change; | ||
use Roave\BackwardCompatibility\Changes; | ||
use Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodBased; | ||
use Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodParameterAdded; | ||
use Roave\BetterReflection\BetterReflection; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
use Roave\BetterReflection\Reflection\ReflectionMethod; | ||
use Roave\BetterReflection\Reflector\DefaultReflector; | ||
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; | ||
|
||
use function array_combine; | ||
use function array_keys; | ||
use function array_map; | ||
use function assert; | ||
use function iterator_to_array; | ||
|
||
/** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodParameterAdded */ | ||
final class MethodParameterAddedTest extends TestCase | ||
{ | ||
private MethodBased $methodCheck; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->methodCheck = new MethodParameterAdded(); | ||
} | ||
|
||
public function testWillSkipCheckingPrivateMethods(): void | ||
{ | ||
$from = $this->createMock(ReflectionMethod::class); | ||
$to = $this->createMock(ReflectionMethod::class); | ||
|
||
$from | ||
->method('isPrivate') | ||
->willReturn(true); | ||
|
||
self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); | ||
} | ||
|
||
public function testWillSkipCheckingMethodsOnFinalClasses(): void | ||
{ | ||
$from = $this->createMock(ReflectionMethod::class); | ||
$to = $this->createMock(ReflectionMethod::class); | ||
|
||
$from | ||
->method('isPrivate') | ||
->willReturn(false); | ||
|
||
self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); | ||
} | ||
|
||
public function testWillSkipCheckingPrivateMethodsOnFinalClasses(): void | ||
{ | ||
$from = $this->createMock(ReflectionMethod::class); | ||
$to = $this->createMock(ReflectionMethod::class); | ||
|
||
$from | ||
->method('isPrivate') | ||
->willReturn(true); | ||
|
||
$from | ||
->method('isFinal') | ||
->willReturn(true); | ||
|
||
self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); | ||
} | ||
|
||
/** | ||
* @param string[] $expectedMessages | ||
* | ||
* @dataProvider methodsToBeTested | ||
*/ | ||
public function testDiffs( | ||
ReflectionMethod $fromMethod, | ||
ReflectionMethod $toMethod, | ||
array $expectedMessages, | ||
): void { | ||
$changes = (new MethodParameterAdded())($fromMethod, $toMethod); | ||
|
||
self::assertSame( | ||
$expectedMessages, | ||
array_map(static function (Change $change): string { | ||
return $change->__toString(); | ||
}, iterator_to_array($changes)), | ||
); | ||
} | ||
|
||
/** @return array<string, array{0: ReflectionMethod, 1: ReflectionMethod, 2: list<string>}> */ | ||
public function methodsToBeTested(): array | ||
{ | ||
$astLocator = (new BetterReflection())->astLocator(); | ||
|
||
$fromLocator = new StringSourceLocator( | ||
<<<'PHP' | ||
<?php | ||
|
||
class TheClass { | ||
public function addedParameter() {} | ||
public function addedTwoParameters() {} | ||
public function addedAnotherParameter(int $one) {} | ||
public function noParameters() {} | ||
public function twoParams(int $one, int $two) {} | ||
private function privateMethod() {} | ||
public function removedParameter(int $one, array $options = []) {} | ||
} | ||
PHP | ||
, | ||
$astLocator, | ||
); | ||
|
||
$toLocator = new StringSourceLocator( | ||
<<<'PHP' | ||
<?php | ||
|
||
class TheClass { | ||
public function addedParameter(array $options = []) {} | ||
public function addedTwoParameters(int $one, array $options = []) {} | ||
public function addedAnotherParameter(int $one, array $options = []) {} | ||
public function noParameters() {} | ||
public function twoParams(int $one, int $two) {} | ||
private function privateMethod(array $options = []) {} | ||
public function removedParameter(int $one) {} | ||
} | ||
PHP | ||
, | ||
$astLocator, | ||
); | ||
|
||
$fromClassReflector = new DefaultReflector($fromLocator); | ||
$toClassReflector = new DefaultReflector($toLocator); | ||
$fromClass = $fromClassReflector->reflectClass('TheClass'); | ||
$toClass = $toClassReflector->reflectClass('TheClass'); | ||
|
||
$methods = [ | ||
'addedParameter' => ['[BC] ADDED: Parameter options was added to Method addedParameter() of class TheClass'], | ||
'addedTwoParameters' => [ | ||
'[BC] ADDED: Parameter one was added to Method addedTwoParameters() of class TheClass', | ||
'[BC] ADDED: Parameter options was added to Method addedTwoParameters() of class TheClass', | ||
], | ||
'addedAnotherParameter' => ['[BC] ADDED: Parameter options was added to Method addedAnotherParameter() of class TheClass'], | ||
'noParameters' => [], | ||
'privateMethod' => [], | ||
'removedParameter' => [], | ||
'twoParams' => [], | ||
]; | ||
|
||
return array_combine( | ||
array_keys($methods), | ||
array_map( | ||
static fn (string $methodName, array $errors): array => [ | ||
self::getMethod($fromClass, $methodName), | ||
self::getMethod($toClass, $methodName), | ||
$errors, | ||
], | ||
array_keys($methods), | ||
$methods, | ||
), | ||
); | ||
} | ||
|
||
/** @param non-empty-string $name */ | ||
private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod | ||
{ | ||
$method = $class->getMethod($name); | ||
|
||
assert($method !== null); | ||
|
||
return $method; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.