Passed
Pull Request — master (#38)
by Marco
02:40
created

MethodAdded   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 22 2
A methods() 0 9 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
use function array_combine;
12
use function array_diff_key;
13
use function array_map;
14
use function array_values;
15
use function sprintf;
16
use function strtolower;
17
18
final class MethodAdded implements InterfaceBased
19
{
20
    public function compare(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
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...
27
            return Changes::new();
28
        }
29
30
        return Changes::fromArray(array_values(array_map(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(function (ReflectionMethod $method) : string {
51
                return strtolower($method->getName());
52
            }, $methods),
53
            $methods
54
        );
55
    }
56
}
57