| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; |
||
| 6 | |||
| 7 | use Roave\BackwardCompatibility\Change; |
||
| 8 | use Roave\BackwardCompatibility\Changes; |
||
| 9 | use Roave\BetterReflection\Reflection\ReflectionClass; |
||
| 10 | use function array_diff; |
||
| 11 | use function array_merge; |
||
| 12 | use function Safe\json_encode; |
||
| 13 | use function Safe\sprintf; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * A class ancestor (interface or class) cannot be removed, as that breaks type |
||
| 17 | * checking in consumers. |
||
| 18 | */ |
||
| 19 | final class AncestorRemoved implements ClassBased |
||
| 20 | { |
||
| 21 | public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes |
||
| 22 | { |
||
| 23 | $removedAncestors = array_merge( |
||
| 24 | array_diff($fromClass->getParentClassNames(), $toClass->getParentClassNames()), |
||
| 25 | array_diff($fromClass->getInterfaceNames(), $toClass->getInterfaceNames()) |
||
| 26 | ); |
||
| 27 | |||
| 28 | if (! $removedAncestors) { |
||
|
0 ignored issues
–
show
|
|||
| 29 | return Changes::empty(); |
||
| 30 | } |
||
| 31 | |||
| 32 | return Changes::fromList(Change::removed( |
||
| 33 | sprintf( |
||
| 34 | 'These ancestors of %s have been removed: %s', |
||
| 35 | $fromClass->getName(), |
||
| 36 | json_encode($removedAncestors) |
||
| 37 | ), |
||
| 38 | true |
||
| 39 | )); |
||
| 40 | } |
||
| 41 | } |
||
| 42 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.