1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Analyzer; |
4
|
|
|
|
5
|
|
|
use PHPSemVerChecker\Operation\InterfaceAdded; |
6
|
|
|
use PHPSemVerChecker\Operation\InterfaceCaseChanged; |
7
|
|
|
use PHPSemVerChecker\Operation\InterfaceRemoved; |
8
|
|
|
use PHPSemVerChecker\Registry\Registry; |
9
|
|
|
use PHPSemVerChecker\Report\Report; |
10
|
|
|
|
11
|
|
|
class InterfaceAnalyzer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $context = 'interface'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryBefore |
20
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryAfter |
21
|
|
|
* @return \PHPSemVerChecker\Report\Report |
22
|
|
|
*/ |
23
|
5 |
|
public function analyze(Registry $registryBefore, Registry $registryAfter) |
24
|
|
|
{ |
25
|
5 |
|
$report = new Report(); |
26
|
|
|
|
27
|
5 |
|
$interfacesBefore = $registryBefore->data['interface']; |
28
|
5 |
|
$interfacesAfter = $registryAfter->data['interface']; |
29
|
|
|
|
30
|
5 |
|
$interfacesBeforeKeyed = []; |
31
|
5 |
|
$filesBeforeKeyed = []; |
32
|
5 |
|
foreach ($interfacesBefore as $key => $interfaceBefore) { |
33
|
3 |
|
$interfacesBeforeKeyed[strtolower($interfaceBefore->name)] = $interfaceBefore; |
34
|
3 |
|
$filesBeforeKeyed[strtolower($interfaceBefore->name)] = $registryBefore->mapping['interface'][$key]; |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
$interfacesAfterKeyed = []; |
38
|
5 |
|
$filesAfterKeyed = []; |
39
|
5 |
|
foreach ($interfacesAfter as $key => $interfaceAfter) { |
40
|
3 |
|
$interfacesAfterKeyed[strtolower($interfaceAfter->name)] = $interfaceAfter; |
41
|
3 |
|
$filesAfterKeyed[strtolower($interfaceAfter->name)] = $registryAfter->mapping['interface'][$key]; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
$interfaceNamesBefore = array_keys($interfacesBeforeKeyed); |
45
|
5 |
|
$interfaceNamesAfter = array_keys($interfacesAfterKeyed); |
46
|
5 |
|
$added = array_diff($interfaceNamesAfter, $interfaceNamesBefore); |
47
|
5 |
|
$removed = array_diff($interfaceNamesBefore, $interfaceNamesAfter); |
48
|
5 |
|
$toVerify = array_intersect($interfaceNamesBefore, $interfaceNamesAfter); |
49
|
|
|
|
50
|
5 |
|
foreach ($removed as $key) { |
51
|
1 |
|
$fileBefore = $filesBeforeKeyed[$key]; |
52
|
1 |
|
$interfaceBefore = $interfacesBeforeKeyed[$key]; |
53
|
|
|
|
54
|
1 |
|
$data = new InterfaceRemoved($fileBefore, $interfaceBefore); |
55
|
1 |
|
$report->addInterface($data); |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
foreach ($toVerify as $key) { |
59
|
2 |
|
$fileBefore = $filesBeforeKeyed[$key]; |
60
|
|
|
/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */ |
61
|
2 |
|
$interfaceBefore = $interfacesBeforeKeyed[$key]; |
62
|
2 |
|
$fileAfter = $filesAfterKeyed[$key]; |
63
|
|
|
/** @var \PhpParser\Node\Stmt\Interface_ $interfaceBefore */ |
64
|
2 |
|
$interfaceAfter = $interfacesAfterKeyed[$key]; |
65
|
|
|
|
66
|
|
|
// Leave non-strict comparison here |
67
|
2 |
|
if ($interfaceBefore != $interfaceAfter) { |
68
|
|
|
// Check if the name of the interface has changed case. |
69
|
|
|
// If we entered this section then the normalized names (lowercase) were equal. |
70
|
1 |
|
if ($interfaceBefore->name !== $interfaceAfter->name) { |
71
|
1 |
|
$report->add( |
72
|
1 |
|
'interface', |
73
|
1 |
|
new InterfaceCaseChanged( |
74
|
1 |
|
$fileBefore, |
75
|
1 |
|
$interfaceBefore, |
76
|
1 |
|
$fileAfter, |
77
|
1 |
|
$interfaceAfter |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$analyzer = new ClassMethodAnalyzer('interface', $fileBefore, $fileAfter); |
83
|
1 |
|
$interfaceMethodReport = $analyzer->analyze($interfaceBefore, $interfaceAfter); |
84
|
2 |
|
$report->merge($interfaceMethodReport); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
foreach ($added as $key) { |
89
|
|
|
|
90
|
1 |
|
$fileAfter = $filesAfterKeyed[$key]; |
91
|
1 |
|
$interfaceAfter = $interfacesAfterKeyed[$key]; |
92
|
|
|
|
93
|
1 |
|
$data = new InterfaceAdded($fileAfter, $interfaceAfter); |
94
|
1 |
|
$report->addInterface($data); |
95
|
|
|
} |
96
|
|
|
|
97
|
5 |
|
return $report; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|