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\BetterReflection\Reflection\ReflectionClass; |
10
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
11
|
|
|
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; |
12
|
|
|
use function sprintf; |
13
|
|
|
|
14
|
|
|
class Comparator |
15
|
|
|
{ |
16
|
|
|
/** @var ClassBased */ |
17
|
|
|
private $classBasedComparisons; |
18
|
|
|
|
19
|
|
|
/** @var InterfaceBased */ |
20
|
|
|
private $interfaceBasedComparisons; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
ClassBased $classBasedComparisons, |
24
|
|
|
InterfaceBased $interfaceBasedComparisons |
25
|
|
|
) { |
26
|
|
|
$this->classBasedComparisons = $classBasedComparisons; |
27
|
|
|
$this->interfaceBasedComparisons = $interfaceBasedComparisons; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function compare(ClassReflector $oldApi, ClassReflector $newApi) : Changes |
31
|
|
|
{ |
32
|
|
|
$changelog = Changes::new(); |
33
|
|
|
|
34
|
|
|
foreach ($oldApi->getAllClasses() as $oldClass) { |
35
|
|
|
$changelog = $this->examineClass($changelog, $oldClass, $newApi); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $changelog; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function examineClass(Changes $changelog, ReflectionClass $oldClass, ClassReflector $newApi) : Changes |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
/** @var ReflectionClass $newClass */ |
45
|
|
|
$newClass = $newApi->reflect($oldClass->getName()); |
46
|
|
|
} catch (IdentifierNotFound $exception) { |
47
|
|
|
return $changelog->withAddedChange( |
48
|
|
|
Change::removed(sprintf('Class %s has been deleted', $oldClass->getName()), true) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($oldClass->isInterface()) { |
53
|
|
|
$changelog = $changelog->mergeWith($this->interfaceBasedComparisons->compare($oldClass, $newClass)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $changelog->mergeWith($this->classBasedComparisons->compare($oldClass, $newClass)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|