1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Analyzer; |
4
|
|
|
|
5
|
|
|
use PHPSemVerChecker\Comparator\Implementation; |
6
|
|
|
use PHPSemVerChecker\Comparator\Signature; |
7
|
|
|
use PHPSemVerChecker\Operation\FunctionAdded; |
8
|
|
|
use PHPSemVerChecker\Operation\FunctionImplementationChanged; |
9
|
|
|
use PHPSemVerChecker\Operation\FunctionOperationUnary; |
10
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterAdded; |
11
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterChanged; |
12
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterDefaultAdded; |
13
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterDefaultRemoved; |
14
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterDefaultValueChanged; |
15
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterNameChanged; |
16
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterRemoved; |
17
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterTypingAdded; |
18
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterTypingRemoved; |
19
|
|
|
use PHPSemVerChecker\Operation\FunctionRemoved; |
20
|
|
|
use PHPSemVerChecker\Operation\FunctionCaseChanged; |
21
|
|
|
use PHPSemVerChecker\Registry\Registry; |
22
|
|
|
use PHPSemVerChecker\Report\Report; |
23
|
|
|
|
24
|
|
|
class FunctionAnalyzer { |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $context = 'function'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryBefore |
32
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryAfter |
33
|
|
|
* @return \PHPSemVerChecker\Report\Report |
34
|
|
|
*/ |
35
|
17 |
|
public function analyze(Registry $registryBefore, Registry $registryAfter) |
36
|
|
|
{ |
37
|
17 |
|
$report = new Report(); |
38
|
|
|
|
39
|
|
|
|
40
|
17 |
|
$functionsBefore = $registryBefore->data['function']; |
41
|
17 |
|
$functionsAfter = $registryAfter->data['function']; |
42
|
|
|
|
43
|
17 |
|
$functionsBeforeKeyed = []; |
44
|
17 |
|
$filesBeforeKeyed = []; |
45
|
17 |
|
foreach($functionsBefore as $key => $functionBefore) |
46
|
|
|
{ |
47
|
15 |
|
$functionsBeforeKeyed[strtolower($functionBefore->name)] = $functionBefore; |
48
|
15 |
|
$filesBeforeKeyed[strtolower($functionBefore->name)] = $registryBefore->mapping['function'][$key]; |
49
|
17 |
|
} |
50
|
|
|
|
51
|
17 |
|
$functionsAfterKeyed = []; |
52
|
17 |
|
$filesAfterKeyed = []; |
53
|
17 |
|
foreach($functionsAfter as $key => $functionAfter) |
54
|
|
|
{ |
55
|
15 |
|
$functionsAfterKeyed[strtolower($functionAfter->name)] = $functionAfter; |
56
|
15 |
|
$filesAfterKeyed[strtolower($functionAfter->name)] = $registryAfter->mapping['function'][$key]; |
57
|
17 |
|
} |
58
|
|
|
|
59
|
17 |
|
$functionNamesBefore = array_keys($functionsBeforeKeyed); |
60
|
17 |
|
$functionNamesAfter = array_keys($functionsAfterKeyed); |
61
|
17 |
|
$added = array_diff($functionNamesAfter, $functionNamesBefore); |
62
|
17 |
|
$removed = array_diff($functionNamesBefore, $functionNamesAfter); |
63
|
17 |
|
$toVerify = array_intersect($functionNamesBefore, $functionNamesAfter); |
64
|
|
|
|
65
|
17 |
|
foreach ($removed as $key) { |
66
|
1 |
|
$fileBefore = $filesBeforeKeyed[$key]; |
67
|
1 |
|
$functionBefore = $functionsBeforeKeyed[$key]; |
68
|
|
|
|
69
|
1 |
|
$data = new FunctionRemoved($fileBefore, $functionBefore); |
70
|
1 |
|
$report->addFunction($data); |
71
|
17 |
|
} |
72
|
|
|
|
73
|
17 |
|
foreach ($toVerify as $key) { |
74
|
14 |
|
$fileBefore = $filesBeforeKeyed[$key]; |
75
|
14 |
|
$functionBefore = $functionsBeforeKeyed[$key]; |
76
|
14 |
|
$fileAfter = $filesAfterKeyed[$key]; |
77
|
14 |
|
$functionAfter = $functionsAfterKeyed[$key]; |
78
|
|
|
|
79
|
|
|
// Leave non-strict comparison here |
80
|
14 |
|
if ($functionBefore != $functionAfter) { |
81
|
|
|
|
82
|
|
|
// Check if the name of the interface has changed case. |
83
|
|
|
// If we entered this section then the normalized names (lowercase) were equal. |
84
|
11 |
|
if ($functionBefore->name !== $functionAfter->name) { |
85
|
1 |
|
$report->addFunction( |
86
|
1 |
|
new FunctionCaseChanged( |
87
|
1 |
|
$fileBefore, |
88
|
1 |
|
$functionBefore, |
89
|
1 |
|
$fileAfter, |
90
|
|
|
$functionAfter |
91
|
1 |
|
) |
92
|
1 |
|
); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
11 |
|
$signatureResult = Signature::analyze($functionBefore->getParams(), $functionAfter->getParams()); |
96
|
|
|
|
97
|
|
|
$changes = [ |
98
|
11 |
|
'parameter_added' => FunctionParameterAdded::class, |
99
|
11 |
|
'parameter_removed' => FunctionParameterRemoved::class, |
100
|
11 |
|
'parameter_renamed' => FunctionParameterNameChanged::class, |
101
|
11 |
|
'parameter_typing_added' => FunctionParameterTypingAdded::class, |
102
|
11 |
|
'parameter_typing_removed' => FunctionParameterTypingRemoved::class, |
103
|
11 |
|
'parameter_default_added' => FunctionParameterDefaultAdded::class, |
104
|
11 |
|
'parameter_default_removed' => FunctionParameterDefaultRemoved::class, |
105
|
11 |
|
'parameter_default_value_changed' => FunctionParameterDefaultValueChanged::class, |
106
|
11 |
|
]; |
107
|
|
|
|
108
|
11 |
|
foreach ($changes as $changeType => $class) { |
109
|
11 |
|
if ( ! $signatureResult[$changeType]) { |
110
|
11 |
|
continue; |
111
|
|
|
} |
112
|
9 |
|
if (is_a($class, FunctionOperationUnary::class, true)) { |
113
|
7 |
|
$data = new $class($fileAfter, $functionAfter); |
114
|
7 |
|
} else { |
115
|
3 |
|
$data = new $class($fileBefore, $functionBefore, $fileAfter, $functionAfter); |
116
|
|
|
} |
117
|
9 |
|
$report->addFunction($data); |
118
|
11 |
|
} |
119
|
|
|
|
120
|
|
|
// Difference in source code |
121
|
11 |
|
if ( ! Implementation::isSame($functionBefore->stmts, $functionAfter->stmts)) { |
122
|
1 |
|
$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter); |
123
|
1 |
|
$report->addFunction($data); |
124
|
1 |
|
} |
125
|
11 |
|
} |
126
|
17 |
|
} |
127
|
|
|
|
128
|
17 |
|
foreach ($added as $key) { |
129
|
1 |
|
$fileAfter = $registryAfter->mapping['function'][$key]; |
130
|
1 |
|
$functionAfter = $registryAfter->data['function'][$key]; |
131
|
|
|
|
132
|
1 |
|
$data = new FunctionAdded($fileAfter, $functionAfter); |
133
|
1 |
|
$report->addFunction($data); |
134
|
17 |
|
} |
135
|
|
|
|
136
|
17 |
|
return $report; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|