Completed
Pull Request — master (#33)
by Marco
01:51
created

ConstantRemoved::accessibleConstants()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased;
6
7
use Assert\Assert;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Changes;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
12
13
final class ConstantRemoved implements ClassBased
14
{
15
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
16
    {
17
        Assert::that($fromClass->getName())->same($toClass->getName());
18
19
        $removedMethods = array_diff_key(
20
            $this->accessibleConstants($fromClass),
21
            $this->accessibleConstants($toClass)
22
        );
23
24
        return Changes::fromArray(array_values(array_map(function (ReflectionClassConstant $constant) use ($fromClass) : Change {
25
            return Change::removed(
26
                sprintf('Constant %s::%s was removed', $fromClass->getName(), $constant->getName()),
27
                true
28
            );
29
        }, $removedMethods)));
30
    }
31
32
    /** @return ReflectionClassConstant[] */
33
    private function accessibleConstants(ReflectionClass $class) : array
34
    {
35
        return array_filter($class->getReflectionConstants(), function (ReflectionClassConstant $constant) : bool {
36
            return $constant->isPublic() || $constant->isProtected();
37
        });
38
    }
39
}