Completed
Pull Request — master (#38)
by Marco
02:14
created

ClassBecameAbstract   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A compare() 0 16 4
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 function sprintf;
12
13
/**
14
 * A class cannot become abstract without introducing an explicit BC break, since
15
 * all child classes or implementors need to be changed to implement its abstract API,
16
 * and all instantiations start to fail.
17
 */
18
final class ClassBecameAbstract implements ClassBased
19
{
20
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
21
    {
22
        Assert::that($fromClass->getName())->same($toClass->getName());
23
24
        if ($fromClass->isInterface() !== $toClass->isInterface()) {
25
            // checking whether a class became an interface is done in `ClassBecameInterface`
26
            return Changes::new();
27
        }
28
29
        if ($fromClass->isAbstract() || ! $toClass->isAbstract()) {
30
            return Changes::new();
31
        }
32
33
        return Changes::fromArray([Change::changed(
34
            sprintf('Class %s became abstract', $fromClass->getName()),
35
            true
36
        ),
37
        ]);
38
    }
39
}
40