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 array_map; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
class Comparator |
17
|
|
|
{ |
18
|
|
|
/** @var ClassBased */ |
19
|
|
|
private $classBasedComparisons; |
20
|
|
|
|
21
|
|
|
/** @var InterfaceBased */ |
22
|
|
|
private $interfaceBasedComparisons; |
23
|
|
|
|
24
|
|
|
/** @var TraitBased */ |
25
|
|
|
private $traitBasedComparisons; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
ClassBased $classBasedComparisons, |
29
|
|
|
InterfaceBased $interfaceBasedComparisons, |
30
|
|
|
TraitBased $traitBasedComparisons |
31
|
|
|
) { |
32
|
|
|
$this->classBasedComparisons = $classBasedComparisons; |
33
|
|
|
$this->interfaceBasedComparisons = $interfaceBasedComparisons; |
34
|
|
|
$this->traitBasedComparisons = $traitBasedComparisons; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ClassReflector $definedSymbols containing only defined symbols in the compared API |
39
|
|
|
* @param ClassReflector $pastSourcesWithDependencies capable of giving us symbols with their dependencies from the |
40
|
|
|
* old version of the sources |
41
|
|
|
* @param ClassReflector $newSourcesWithDependencies capable of giving us symbols with their dependencies from the |
42
|
|
|
* old version of the sources |
43
|
|
|
*/ |
44
|
|
|
public function compare( |
45
|
|
|
ClassReflector $definedSymbols, |
46
|
|
|
ClassReflector $pastSourcesWithDependencies, |
47
|
|
|
ClassReflector $newSourcesWithDependencies |
48
|
|
|
) : Changes { |
49
|
|
|
$changelog = Changes::new(); |
50
|
|
|
|
51
|
|
|
$definedApiClassNames = array_map(function (ReflectionClass $class) : string { |
52
|
|
|
return $class->getName(); |
53
|
|
|
}, $definedSymbols->getAllClasses()); |
54
|
|
|
|
55
|
|
|
foreach ($definedApiClassNames as $apiClassName) { |
56
|
|
|
/** @var ReflectionClass $oldSymbol */ |
57
|
|
|
$oldSymbol = $pastSourcesWithDependencies->reflect($apiClassName); |
58
|
|
|
$changelog = $this->examineSymbol($changelog, $oldSymbol, $newSourcesWithDependencies); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $changelog; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function examineSymbol( |
65
|
|
|
Changes $changelog, |
66
|
|
|
ReflectionClass $oldSymbol, |
67
|
|
|
ClassReflector $newSourcesWithDependencies |
68
|
|
|
) : Changes { |
69
|
|
|
try { |
70
|
|
|
/** @var ReflectionClass $newClass */ |
71
|
|
|
$newClass = $newSourcesWithDependencies->reflect($oldSymbol->getName()); |
72
|
|
|
} catch (IdentifierNotFound $exception) { |
73
|
|
|
return $changelog->withAddedChange( |
74
|
|
|
Change::removed(sprintf('Class %s has been deleted', $oldSymbol->getName()), true) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($oldSymbol->isInterface()) { |
79
|
|
|
return $changelog->mergeWith($this->interfaceBasedComparisons->compare($oldSymbol, $newClass)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($oldSymbol->isTrait()) { |
83
|
|
|
return $changelog->mergeWith($this->traitBasedComparisons->compare($oldSymbol, $newClass)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $changelog->mergeWith($this->classBasedComparisons->compare($oldSymbol, $newClass)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|