-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathFunctionAnalyzer.php
More file actions
162 lines (142 loc) · 5.77 KB
/
FunctionAnalyzer.php
File metadata and controls
162 lines (142 loc) · 5.77 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace PHPSemVerChecker\Analyzer;
use PHPSemVerChecker\Comparator\Implementation;
use PHPSemVerChecker\Comparator\Signature;
use PHPSemVerChecker\Operation\FunctionAdded;
use PHPSemVerChecker\Operation\FunctionCaseChanged;
use PHPSemVerChecker\Operation\FunctionImplementationChanged;
use PHPSemVerChecker\Operation\FunctionOperationUnary;
use PHPSemVerChecker\Operation\FunctionParameterAdded;
use PHPSemVerChecker\Operation\FunctionParameterChanged;
use PHPSemVerChecker\Operation\FunctionParameterDefaultAdded;
use PHPSemVerChecker\Operation\FunctionParameterDefaultRemoved;
use PHPSemVerChecker\Operation\FunctionParameterDefaultValueChanged;
use PHPSemVerChecker\Operation\FunctionParameterNameChanged;
use PHPSemVerChecker\Operation\FunctionParameterRemoved;
use PHPSemVerChecker\Operation\FunctionParameterTypingAdded;
use PHPSemVerChecker\Operation\FunctionParameterTypingRemoved;
use PHPSemVerChecker\Operation\FunctionRemoved;
use PHPSemVerChecker\Operation\FunctionReturnTypeAdded;
use PHPSemVerChecker\Operation\FunctionReturnTypeChanged;
use PHPSemVerChecker\Operation\FunctionReturnTypeRemoved;
use PHPSemVerChecker\Registry\Registry;
use PHPSemVerChecker\Report\Report;
class FunctionAnalyzer
{
/**
* @var string
*/
protected $context = 'function';
/**
* @param \PHPSemVerChecker\Registry\Registry $registryBefore
* @param \PHPSemVerChecker\Registry\Registry $registryAfter
* @return \PHPSemVerChecker\Report\Report
*/
public function analyze(Registry $registryBefore, Registry $registryAfter)
{
$report = new Report();
$functionsBefore = $registryBefore->data['function'];
$functionsAfter = $registryAfter->data['function'];
$filesBeforeKeyed = [];
$functionsBeforeKeyed = [];
foreach ($functionsBefore as $key => $functionBefore) {
$filesBeforeKeyed[strtolower($key)] = $registryBefore->mapping['function'][$key];
$functionsBeforeKeyed[strtolower($key)] = $functionBefore;
}
$filesAfterKeyed = [];
$functionsAfterKeyed = [];
foreach ($functionsAfter as $key => $functionAfter) {
$filesAfterKeyed[strtolower($key)] = $registryAfter->mapping['function'][$key];
$functionsAfterKeyed[strtolower($key)] = $functionAfter;
}
$functionNamesBefore = array_keys($functionsBeforeKeyed);
$functionNamesAfter = array_keys($functionsAfterKeyed);
$added = array_diff($functionNamesAfter, $functionNamesBefore);
$removed = array_diff($functionNamesBefore, $functionNamesAfter);
$toVerify = array_intersect($functionNamesBefore, $functionNamesAfter);
foreach ($removed as $key) {
$fileBefore = $filesBeforeKeyed[$key];
$functionBefore = $functionsBeforeKeyed[$key];
$data = new FunctionRemoved($fileBefore, $functionBefore);
$report->addFunction($data);
}
foreach ($toVerify as $key) {
$fileBefore = $filesBeforeKeyed[$key];
$functionBefore = $functionsBeforeKeyed[$key];
$fileAfter = $filesAfterKeyed[$key];
$functionAfter = $functionsAfterKeyed[$key];
// Leave non-strict comparison here
if ($functionBefore != $functionAfter) {
// Check if the name of the function has changed case.
// If we entered this section then the normalized names (lowercase) were equal.
if ($functionBefore->name->toString() !== $functionAfter->name->toString()) {
$report->addFunction(
new FunctionCaseChanged(
$fileBefore,
$functionBefore,
$fileAfter,
$functionAfter
)
);
}
if ($functionBefore->returnType !== $functionAfter->returnType) {
print_r($functionBefore->returnType);
print_r($functionAfter->returnType);
$class = null;
if ($functionBefore->returnType !== null && $functionAfter->returnType === null) {
$class = FunctionReturnTypeAdded::class;
} elseif ($functionAfter->returnType !== null && $functionBefore->returnType === null) {
$class = FunctionReturnTypeRemoved::class;
} elseif (strcasecmp($functionAfter->returnType->name, $functionBefore->returnType->name) !== 0) {
$class = FunctionReturnTypeChanged::class;
}
if ($class) {
$report->add(
$this->context,
new $class(
$fileBefore,
$functionBefore,
$fileAfter,
$functionAfter
)
);
}
}
$signatureResult = Signature::analyze($functionBefore->getParams(), $functionAfter->getParams());
$changes = [
'parameter_added' => FunctionParameterAdded::class,
'parameter_removed' => FunctionParameterRemoved::class,
'parameter_renamed' => FunctionParameterNameChanged::class,
'parameter_typing_added' => FunctionParameterTypingAdded::class,
'parameter_typing_removed' => FunctionParameterTypingRemoved::class,
'parameter_default_added' => FunctionParameterDefaultAdded::class,
'parameter_default_removed' => FunctionParameterDefaultRemoved::class,
'parameter_default_value_changed' => FunctionParameterDefaultValueChanged::class,
];
foreach ($changes as $changeType => $class) {
if ( ! $signatureResult[$changeType]) {
continue;
}
if (is_a($class, FunctionOperationUnary::class, true)) {
$data = new $class($fileAfter, $functionAfter);
} else {
$data = new $class($fileBefore, $functionBefore, $fileAfter, $functionAfter);
}
$report->addFunction($data);
}
// Difference in source code
if ( ! Implementation::isSame($functionBefore->stmts, $functionAfter->stmts)) {
$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter);
$report->addFunction($data);
}
}
}
foreach ($added as $key) {
$fileAfter = $filesAfterKeyed[$key];
$functionAfter = $functionsAfterKeyed[$key];
$data = new FunctionAdded($fileAfter, $functionAfter);
$report->addFunction($data);
}
return $report;
}
}