| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased; |
||
| 6 | |||
| 7 | use Roave\BackwardCompatibility\Change; |
||
| 8 | use Roave\BackwardCompatibility\Changes; |
||
| 9 | use Roave\BetterReflection\Reflection\ReflectionClass; |
||
| 10 | use Roave\BetterReflection\Reflection\ReflectionMethod; |
||
| 11 | use function array_diff_key; |
||
| 12 | use function array_map; |
||
| 13 | use function array_values; |
||
| 14 | use function Safe\array_combine; |
||
| 15 | use function Safe\sprintf; |
||
| 16 | use function strtolower; |
||
| 17 | |||
| 18 | final class MethodAdded implements InterfaceBased |
||
| 19 | { |
||
| 20 | public function __invoke(ReflectionClass $fromInterface, ReflectionClass $toInterface) : Changes |
||
| 21 | { |
||
| 22 | $fromMethods = $this->methods($fromInterface); |
||
| 23 | $toMethods = $this->methods($toInterface); |
||
| 24 | $newMethods = array_diff_key($toMethods, $fromMethods); |
||
| 25 | |||
| 26 | if (! $newMethods) { |
||
|
0 ignored issues
–
show
|
|||
| 27 | return Changes::empty(); |
||
| 28 | } |
||
| 29 | |||
| 30 | return Changes::fromList(...array_values(array_map(static function (ReflectionMethod $method) use ( |
||
| 31 | $fromInterface |
||
| 32 | ) : Change { |
||
| 33 | return Change::added( |
||
| 34 | sprintf( |
||
| 35 | 'Method %s() was added to interface %s', |
||
| 36 | $method->getName(), |
||
| 37 | $fromInterface->getName() |
||
| 38 | ), |
||
| 39 | true |
||
| 40 | ); |
||
| 41 | }, $newMethods))); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** @return ReflectionMethod[] indexed by lowercase method name */ |
||
| 45 | private function methods(ReflectionClass $interface) : array |
||
| 46 | { |
||
| 47 | $methods = $interface->getMethods(); |
||
| 48 | |||
| 49 | return array_combine( |
||
| 50 | array_map(static function (ReflectionMethod $method) : string { |
||
| 51 | return strtolower($method->getName()); |
||
| 52 | }, $methods), |
||
| 53 | $methods |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | } |
||
| 57 |
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.