1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Roave\ApiCompare; |
6
|
|
|
|
7
|
|
|
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\ClassBased; |
8
|
|
|
use Roave\ApiCompare\Comparator\BackwardsCompatibility\InterfaceBased\InterfaceBased; |
9
|
|
|
use Roave\ApiCompare\Comparator\BackwardsCompatibility\TraitBased\TraitBased; |
10
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
11
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
12
|
|
|
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; |
13
|
|
|
use function sprintf; |
14
|
|
|
|
15
|
|
|
class Comparator |
16
|
|
|
{ |
17
|
|
|
/** @var ClassBased */ |
18
|
|
|
private $classBasedComparisons; |
19
|
|
|
|
20
|
|
|
/** @var InterfaceBased */ |
21
|
|
|
private $interfaceBasedComparisons; |
22
|
|
|
|
23
|
|
|
/** @var TraitBased */ |
24
|
|
|
private $traitBasedComparisons; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
ClassBased $classBasedComparisons, |
28
|
|
|
InterfaceBased $interfaceBasedComparisons, |
29
|
|
|
TraitBased $traitBasedComparisons |
30
|
|
|
) { |
31
|
|
|
$this->classBasedComparisons = $classBasedComparisons; |
32
|
|
|
$this->interfaceBasedComparisons = $interfaceBasedComparisons; |
33
|
|
|
$this->traitBasedComparisons = $traitBasedComparisons; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function compare(ClassReflector $oldApi, ClassReflector $newApi) : Changes |
37
|
|
|
{ |
38
|
|
|
$changelog = Changes::new(); |
39
|
|
|
|
40
|
|
|
foreach ($oldApi->getAllClasses() as $oldClass) { |
41
|
|
|
$changelog = $this->examineClass($changelog, $oldClass, $newApi); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $changelog; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function examineClass(Changes $changelog, ReflectionClass $oldClass, ClassReflector $newApi) : Changes |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
/** @var ReflectionClass $newClass */ |
51
|
|
|
$newClass = $newApi->reflect($oldClass->getName()); |
52
|
|
|
} catch (IdentifierNotFound $exception) { |
53
|
|
|
return $changelog->withAddedChange( |
54
|
|
|
Change::removed(sprintf('Class %s has been deleted', $oldClass->getName()), true) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($oldClass->isInterface()) { |
59
|
|
|
return $changelog->mergeWith($this->interfaceBasedComparisons->compare($oldClass, $newClass)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($oldClass->isTrait()) { |
63
|
|
|
return $changelog->mergeWith($this->traitBasedComparisons->compare($oldClass, $newClass)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $changelog->mergeWith($this->classBasedComparisons->compare($oldClass, $newClass)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|