Passed
Pull Request — master (#109)
by Marco
05:58 queued 03:28
created

AncestorRemoved   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 2
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 json_encode;
13
use function 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
Bug Best Practice introduced by
The expression $removedAncestors 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...
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