-
Notifications
You must be signed in to change notification settings - Fork 59
/
CompareClasses.php
108 lines (90 loc) · 3.36 KB
/
CompareClasses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Roave\BackwardCompatibility;
use Psl\Dict;
use Psl\Regex;
use Psl\Str;
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\ClassBased;
use Roave\BackwardCompatibility\DetectChanges\BCBreak\EnumBased\EnumBased;
use Roave\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased\InterfaceBased;
use Roave\BackwardCompatibility\DetectChanges\BCBreak\TraitBased\TraitBased;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\Reflector;
final class CompareClasses implements CompareApi
{
public function __construct(
private ClassBased $classBasedComparisons,
private InterfaceBased $interfaceBasedComparisons,
private TraitBased $traitBasedComparisons,
private EnumBased $enumBasedComparisons,
) {
}
public function __invoke(
Reflector $definedSymbols,
Reflector $pastSourcesWithDependencies,
Reflector $newSourcesWithDependencies,
): Changes {
$definedApiClassNames = Dict\map(
Dict\filter(
$definedSymbols->reflectAllClasses(),
function (ReflectionClass $class): bool {
return ! ($class->isAnonymous() || $this->isInternalDocComment($class->getDocComment()));
},
),
static function (ReflectionClass $class): string {
return $class->getName();
},
);
return Changes::fromIterator($this->makeSymbolsIterator(
$definedApiClassNames,
$pastSourcesWithDependencies,
$newSourcesWithDependencies,
));
}
/**
* @param string[] $definedApiClassNames
*
* @return iterable<int, Change>
*/
private function makeSymbolsIterator(
array $definedApiClassNames,
Reflector $pastSourcesWithDependencies,
Reflector $newSourcesWithDependencies,
): iterable {
foreach ($definedApiClassNames as $apiClassName) {
$oldSymbol = $pastSourcesWithDependencies->reflectClass($apiClassName);
yield from $this->examineSymbol($oldSymbol, $newSourcesWithDependencies);
}
}
/** @return iterable<int, Change> */
private function examineSymbol(
ReflectionClass $oldSymbol,
Reflector $newSourcesWithDependencies,
): iterable {
try {
$newClass = $newSourcesWithDependencies->reflectClass($oldSymbol->getName());
} catch (IdentifierNotFound) {
yield Change::removed(Str\format('Class %s has been deleted', $oldSymbol->getName()));
return;
}
if ($oldSymbol->isInterface()) {
yield from ($this->interfaceBasedComparisons)($oldSymbol, $newClass);
return;
}
if ($oldSymbol->isTrait()) {
yield from ($this->traitBasedComparisons)($oldSymbol, $newClass);
return;
}
if ($oldSymbol->isEnum()) {
yield from ($this->enumBasedComparisons)($oldSymbol, $newClass);
return;
}
yield from ($this->classBasedComparisons)($oldSymbol, $newClass);
}
private function isInternalDocComment(string|null $comment): bool
{
return $comment !== null
&& Regex\matches($comment, '/\s+@internal\s+/');
}
}