Completed
Pull Request — master (#38)
by Marco
03:32
created

MethodAdded::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\InterfaceBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionClass;
10
use Roave\BetterReflection\Reflection\ReflectionMethod;
11
12
final class MethodAdded implements InterfaceBased
13
{
14
    public function compare(ReflectionClass $fromInterface, ReflectionClass $toInterface) : Changes
15
    {
16
        $fromMethods = $this->methods($fromInterface);
17
        $toMethods   = $this->methods($toInterface);
18
        $newMethods  = array_diff_key($toMethods, $fromMethods);
19
20
        if (! $newMethods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $newMethods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
21
            return Changes::new();
22
        }
23
24
        return Changes::fromArray(array_values(array_map(function (ReflectionMethod $method) use (
25
            $fromInterface
26
        ) : Change {
27
            return Change::added(
28
                sprintf(
29
                    'Method %s() was added to interface %s',
30
                    $method->getName(),
31
                    $fromInterface->getName()
32
                ),
33
                true
34
            );
35
        }, $newMethods)));
36
    }
37
38
    /** @return ReflectionMethod[] indexed by lowercase method name */
39
    private function methods(ReflectionClass $interface) : array
40
    {
41
        $methods = $interface->getMethods();
42
43
        return array_combine(
44
            array_map(function (ReflectionMethod $method) : string {
45
                return strtolower($method->getName());
46
            }, $methods),
47
            $methods
48
        );
49
    }
50
}
51