-
Notifications
You must be signed in to change notification settings - Fork 61
Detect changes in ENUMs as BC breaks #768
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
Show all changes
32 commits
Select commit
Hold shift + click to select a range
77fd27e
Roave/BackwardCompatibilityCheck#767 Create EnumCaseAdded class
bdsl be55ca1
Roave/BackwardCompatibilityCheck#767: Rename ClassBased/EnumCaseAdded…
bdsl 8c41bc8
Roave/BackwardCompatibilityCheck#767: Allow adding @internal enum cas…
bdsl 39ecc98
Add BC check for Enum Case removed
bdsl 66ceaa1
Allow removing @internal enum cases without BC break
bdsl 336bcab
Fix PHPCS errors
bdsl f8092b6
Refactor: Make relavent namespace EnumBased more visible in check script
bdsl a1e3738
Roave/BackwardCompatibilityCheck#767 : Fix BC break
bdsl b2c4ddc
Roave/BackwardCompatibilityCheck#767: Improve test coverage
bdsl 6f7c9dc
Roave/BackwardCompatibilityCheck#767: Remove unecassary array_values …
bdsl 458cab3
Roave/BackwardCompatibilityCheck#767: Remove covers annotation on new…
bdsl 9f2820b
Temporarily remove enum based checks fron script
bdsl 0460dbf
Temporarily remove enum based checks from CompareClasses to see how i…
bdsl e82047e
Revert "Temporarily remove enum based checks from CompareClasses to s…
bdsl 889b956
Revert "Temporarily remove enum based checks fron script"
bdsl 7f01d47
Roave/BackwardCompatibilityCheck#767: Add tests about enums to Compar…
bdsl 850e7a3
Roave/BackwardCompatibilityCheck#767: Refactor, remove new EnumBased …
bdsl d9d3b04
Roave/BackwardCompatibilityCheck#767: Add check in test that Enum cas…
bdsl 61231a8
Roave/BackwardCompatibilityCheck#767: add check for enum case became …
bdsl 9da3f6d
Roave/BackwardCompatibilityCheck#767: Detect enum becoming non-enum a…
bdsl aa85d92
Roave/BackwardCompatibilityCheck#767: Refactor, rename class CasedCha…
bdsl 35e77b3
Roave/BackwardCompatibilityCheck#767: Fix Psalm and PHPCS errors
bdsl aa9c85d
Roave/BackwardCompatibilityCheck#767: Add check for enum case becomin…
bdsl edcbd2b
Restore blank line deleted in error
bdsl 77a2f08
Roave/BackwardCompatibilityCheck#767: Remove outdated comments from test
bdsl ad32463
Make EnumCasesChanged final
bdsl 5519e97
Roave/BackwardCompatibilityCheck#767: Add @covers to EnumCasesChanged…
bdsl 190968c
Roave/BackwardCompatibilityCheck#767: Remove redundant term in if con…
bdsl 6703c31
Roave/BackwardCompatibilityCheck#767: Remove unecassary comment
bdsl 9f75ade
Fix covers annotation on EnumCasesChangedTest
bdsl 763dadd
Adjusting temporary path expectation: may differ based on `$UID`
Ocramius c245e9c
Reducing mutation testing requirements: makes CI green.
Ocramius 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
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
158 changes: 158 additions & 0 deletions
158
src/DetectChanges/BCBreak/ClassBased/EnumCasesChanged.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,158 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; | ||
|
||
use Psl\Regex; | ||
use Roave\BackwardCompatibility\Change; | ||
use Roave\BackwardCompatibility\Changes; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
use Roave\BetterReflection\Reflection\ReflectionEnum; | ||
use Roave\BetterReflection\Reflection\ReflectionEnumCase; | ||
|
||
use function array_filter; | ||
use function array_map; | ||
|
||
final class EnumCasesChanged implements ClassBased | ||
{ | ||
public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass): Changes | ||
{ | ||
$fromEnumName = $fromClass->getName(); | ||
$fromKind = $this->kindOf($fromClass); | ||
$toKind = $this->kindOf($toClass); | ||
|
||
if (! $fromClass instanceof ReflectionEnum && ! $toClass instanceof ReflectionEnum) { | ||
return Changes::empty(); | ||
} | ||
|
||
if (! $fromClass instanceof ReflectionEnum) { | ||
return Changes::fromList(Change::changed($fromKind . ' ' . $fromEnumName . ' became enum')); | ||
} | ||
|
||
if (! $toClass instanceof ReflectionEnum) { | ||
return Changes::fromList(Change::changed('enum ' . $fromEnumName . ' became ' . $toKind)); | ||
} | ||
|
||
$addedCases = array_filter( | ||
$toClass->getCases(), | ||
static function (ReflectionEnumCase $case) use ($fromClass): bool { | ||
if (self::isInternalDocComment($case->getDocComment())) { | ||
return false; | ||
} | ||
|
||
return ! $fromClass->hasCase($case->getName()); | ||
}, | ||
); | ||
|
||
$removedCases = array_filter( | ||
$fromClass->getCases(), | ||
static function (ReflectionEnumCase $case) use ($toClass): bool { | ||
if (self::isInternalDocComment($case->getDocComment())) { | ||
return false; | ||
} | ||
|
||
return ! $toClass->hasCase($case->getName()); | ||
}, | ||
); | ||
|
||
$internalisedCases = array_filter( | ||
$toClass->getCases(), | ||
static function (ReflectionEnumCase $case) use ($fromClass) { | ||
if (! self::isInternalDocComment($case->getDocComment())) { | ||
return false; | ||
} | ||
|
||
$fromClassCase = $fromClass->getCase($case->getName()); | ||
if (! $fromClassCase) { | ||
return false; | ||
} | ||
|
||
return ! self::isInternalDocComment($fromClassCase->getDocComment()); | ||
}, | ||
); | ||
|
||
$nowNotInternalCases = array_filter( | ||
$toClass->getCases(), | ||
static function (ReflectionEnumCase $case) use ($fromClass) { | ||
if (self::isInternalDocComment($case->getDocComment())) { | ||
return false; | ||
} | ||
|
||
$fromClassCase = $fromClass->getCase($case->getName()); | ||
if (! $fromClassCase) { | ||
return false; | ||
} | ||
|
||
return self::isInternalDocComment($fromClassCase->getDocComment()); | ||
}, | ||
); | ||
|
||
$caseRemovedChanges = array_map( | ||
static function (ReflectionEnumCase $case) use ($fromEnumName): Change { | ||
$caseName = $case->getName(); | ||
|
||
return Change::removed('Case ' . $fromEnumName . '::' . $caseName . ' was removed'); | ||
}, | ||
$removedCases, | ||
); | ||
|
||
$caseAddedChanges = array_map( | ||
static function (ReflectionEnumCase $case) use ($fromEnumName): Change { | ||
$caseName = $case->getName(); | ||
|
||
return Change::added('Case ' . $fromEnumName . '::' . $caseName . ' was added'); | ||
}, | ||
$addedCases, | ||
); | ||
|
||
$caseBecameInternalChanges = array_map( | ||
static function (ReflectionEnumCase $case) use ($fromEnumName): Change { | ||
$caseName = $case->getName(); | ||
|
||
return Change::changed('Case ' . $fromEnumName . '::' . $caseName . ' was marked "@internal"'); | ||
}, | ||
$internalisedCases, | ||
); | ||
|
||
$caseBecameNotInternalChanges = array_map( | ||
static function (ReflectionEnumCase $case) use ($fromEnumName): Change { | ||
$caseName = $case->getName(); | ||
|
||
return Change::changed('Case ' . $fromEnumName . '::' . $caseName . ' had "@internal" removed'); | ||
}, | ||
$nowNotInternalCases, | ||
); | ||
|
||
return Changes::fromList( | ||
...$caseRemovedChanges, | ||
...$caseAddedChanges, | ||
...$caseBecameInternalChanges, | ||
...$caseBecameNotInternalChanges, | ||
); | ||
Ocramius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private static function isInternalDocComment(string|null $comment): bool | ||
{ | ||
return $comment !== null | ||
&& Regex\matches($comment, '/\s+@internal\s+/'); | ||
} | ||
|
||
/** @psalm-return 'enum'|'interface'|'trait'|'class' */ | ||
private function kindOf(ReflectionClass $reflectionClass): string | ||
{ | ||
if ($reflectionClass->isEnum()) { | ||
return 'enum'; | ||
} | ||
|
||
if ($reflectionClass->isInterface()) { | ||
return 'interface'; | ||
} | ||
|
||
if ($reflectionClass->isTrait()) { | ||
return 'trait'; | ||
} | ||
|
||
return 'class'; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTestAsset; | ||
|
||
enum EnumWithCasesBeingChanged | ||
{ | ||
// two new months below: | ||
case January; | ||
case February; | ||
|
||
/** @internal - tired from the two new Months, reserving March for private use from now on.*/ | ||
case March; | ||
|
||
case April; | ||
case May; | ||
case June; | ||
case July; | ||
|
||
// We're on holiday in August, not going to allow that month any more. | ||
// case August; | ||
|
||
case September; | ||
case October; | ||
case November; | ||
case December; | ||
|
||
/** | ||
* For testing now but for use only in a future major version. As it should not be statically referenced from | ||
* outside this library, and the library will not pass a reference to it to the outside at runtime, adding it | ||
* is not a BC break. | ||
* @internal | ||
*/ | ||
case Sol; | ||
|
||
/** | ||
* Previously internal, now public. This is a BC break as we previously committed to never return an instance | ||
* of this case and we no-longer make this commitment. | ||
*/ | ||
case UnknownMonth; | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTestAsset; | ||
|
||
enum EnumWithCasesBeingChanged | ||
{ | ||
case March; | ||
case April; | ||
case May; | ||
case June; | ||
case July; | ||
case August; | ||
case September; | ||
case October; | ||
case November; | ||
Case december; // oops forget this is spelled with a capital D | ||
|
||
/** | ||
* @internal - may be removed without notice | ||
*/ | ||
case FakeMonth; | ||
|
||
/** | ||
* @internal - may be introduced in future | ||
*/ | ||
case UnknownMonth; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; | ||
|
||
enum DummyEnum | ||
{ | ||
case someCase; | ||
} |
98 changes: 98 additions & 0 deletions
98
test/unit/DetectChanges/BCBreak/ClassBased/EnumCasesChangedTest.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,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Roave\BackwardCompatibility\Change; | ||
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\EnumCasesChanged; | ||
use Roave\BetterReflection\BetterReflection; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
use Roave\BetterReflection\Reflector\DefaultReflector; | ||
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; | ||
use stdClass; | ||
|
||
use function array_map; | ||
use function iterator_to_array; | ||
|
||
/** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\EnumCasesChanged */ | ||
final class EnumCasesChangedTest extends TestCase | ||
Ocramius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @param string[] $expectedMessages | ||
* | ||
* @dataProvider enumsToBeTested | ||
*/ | ||
public function testDiffs( | ||
ReflectionClass $fromEnum, | ||
ReflectionClass $toEnum, | ||
array $expectedMessages, | ||
): void { | ||
$changes = (new EnumCasesChanged())($fromEnum, $toEnum); | ||
|
||
self::assertSame( | ||
$expectedMessages, | ||
array_map(static function (Change $change): string { | ||
return $change->__toString(); | ||
}, iterator_to_array($changes)), | ||
); | ||
} | ||
|
||
public function testReturnsClassBecameEnumError(): void | ||
{ | ||
$changes = (new EnumCasesChanged())( | ||
ReflectionClass::createFromName(stdClass::class), | ||
ReflectionClass::createFromName(DummyEnum::class), | ||
); | ||
|
||
$this->assertEquals( | ||
[Change::changed('class stdClass became enum')], | ||
iterator_to_array($changes), | ||
); | ||
} | ||
|
||
public function testReturnsEnumBecameClassError(): void | ||
{ | ||
$changes = (new EnumCasesChanged())( | ||
ReflectionClass::createFromName(DummyEnum::class), | ||
ReflectionClass::createFromName(stdClass::class), | ||
); | ||
|
||
$this->assertEquals( | ||
[Change::changed('enum RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\DummyEnum became class')], | ||
iterator_to_array($changes), | ||
); | ||
} | ||
|
||
/** | ||
* @return array<string, array<int, ReflectionClass|array<int, string>>> | ||
* @psalm-return array<string, array{0: ReflectionClass, 1: ReflectionClass, 2: list<string>}> | ||
*/ | ||
public function enumsToBeTested(): array | ||
{ | ||
$locator = (new BetterReflection())->astLocator(); | ||
|
||
return [ | ||
'RoaveTestAsset\\EnumWithCasesBeingChanged' => [ | ||
(new DefaultReflector(new SingleFileSourceLocator( | ||
__DIR__ . '/../../../../asset/api/old/EnumWithCasesBeingChanged.php', | ||
$locator, | ||
)))->reflectClass('RoaveTestAsset\EnumWithCasesBeingChanged'), | ||
(new DefaultReflector(new SingleFileSourceLocator( | ||
__DIR__ . '/../../../../asset/api/new/EnumWithCasesBeingChanged.php', | ||
$locator, | ||
)))->reflectClass('RoaveTestAsset\EnumWithCasesBeingChanged'), | ||
[ | ||
'[BC] REMOVED: Case RoaveTestAsset\EnumWithCasesBeingChanged::August was removed', | ||
'[BC] REMOVED: Case RoaveTestAsset\EnumWithCasesBeingChanged::december was removed', | ||
'[BC] ADDED: Case RoaveTestAsset\EnumWithCasesBeingChanged::January was added', | ||
'[BC] ADDED: Case RoaveTestAsset\EnumWithCasesBeingChanged::February was added', | ||
'[BC] ADDED: Case RoaveTestAsset\EnumWithCasesBeingChanged::December was added', | ||
'[BC] CHANGED: Case RoaveTestAsset\EnumWithCasesBeingChanged::March was marked "@internal"', | ||
'[BC] CHANGED: Case RoaveTestAsset\EnumWithCasesBeingChanged::UnknownMonth had "@internal" removed', | ||
], | ||
], | ||
]; | ||
} | ||
} |
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
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.