-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathClassMethodAnalyzer.php
199 lines (179 loc) · 6.54 KB
/
ClassMethodAnalyzer.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace PHPSemVerChecker\Analyzer;
use PhpParser\Node\Stmt;
use PHPSemVerChecker\Comparator\Implementation;
use PHPSemVerChecker\Comparator\Signature;
use PHPSemVerChecker\Operation\ClassMethodAdded;
use PHPSemVerChecker\Operation\ClassMethodCaseChanged;
use PHPSemVerChecker\Operation\ClassMethodImplementationChanged;
use PHPSemVerChecker\Operation\ClassMethodOperationUnary;
use PHPSemVerChecker\Operation\ClassMethodParameterAdded;
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultAdded;
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultRemoved;
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultValueChanged;
use PHPSemVerChecker\Operation\ClassMethodParameterNameChanged;
use PHPSemVerChecker\Operation\ClassMethodParameterRemoved;
use PHPSemVerChecker\Operation\ClassMethodParameterTypingAdded;
use PHPSemVerChecker\Operation\ClassMethodParameterTypingRemoved;
use PHPSemVerChecker\Operation\ClassMethodRemoved;
use PHPSemVerChecker\Operation\ClassMethodReturnTypeAdded;
use PHPSemVerChecker\Operation\ClassMethodReturnTypeChanged;
use PHPSemVerChecker\Operation\ClassMethodReturnTypeRemoved;
use PHPSemVerChecker\Report\Report;
class ClassMethodAnalyzer
{
/**
* @var string
*/
protected $context;
/**
* @var null|string
*/
protected $fileBefore;
/**
* @var null|string
*/
protected $fileAfter;
/**
* @param string $context
* @param string $fileBefore
* @param string $fileAfter
*/
public function __construct($context, $fileBefore = null, $fileAfter = null)
{
$this->context = $context;
$this->fileBefore = $fileBefore;
$this->fileAfter = $fileAfter;
}
/**
* @param \PhpParser\Node\Stmt $contextBefore
* @param \PhpParser\Node\Stmt $contextAfter
* @return \PHPSemVerChecker\Report\Report
*/
public function analyze(Stmt $contextBefore, Stmt $contextAfter)
{
$report = new Report();
$methodsBefore = $contextBefore->getMethods();
$methodsAfter = $contextAfter->getMethods();
$methodsBeforeKeyed = [];
foreach ($methodsBefore as $method) {
$methodsBeforeKeyed[strtolower($method->name)] = $method;
}
$methodsAfterKeyed = [];
foreach ($methodsAfter as $method) {
$methodsAfterKeyed[strtolower($method->name)] = $method;
}
$methodNamesBefore = array_keys($methodsBeforeKeyed);
$methodNamesAfter = array_keys($methodsAfterKeyed);
$methodsAdded = array_diff($methodNamesAfter, $methodNamesBefore);
$methodsRemoved = array_diff($methodNamesBefore, $methodNamesAfter);
$methodsToVerify = array_intersect($methodNamesBefore, $methodNamesAfter);
// Here we only care about public methods as they are the only part of the API we care about
// Removed methods can either be implemented in parent classes or not exist anymore
foreach ($methodsRemoved as $method) {
$methodBefore = $methodsBeforeKeyed[$method];
$data = new ClassMethodRemoved($this->context, $this->fileBefore, $contextBefore, $methodBefore);
$report->add($this->context, $data);
}
foreach ($methodsToVerify as $method) {
/** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */
$methodBefore = $methodsBeforeKeyed[strtolower($method)];
/** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */
$methodAfter = $methodsAfterKeyed[strtolower($method)];
// Leave non-strict comparison here
if ($methodBefore != $methodAfter) {
// Detect method case changed.
// If we entered this section then the normalized names (lowercase) were equal.
if ($methodBefore->name->toString() !== $methodAfter->name->toString()) {
$report->add(
$this->context,
new ClassMethodCaseChanged(
$this->context,
$this->fileBefore,
$contextAfter,
$methodBefore,
$this->fileAfter,
$contextAfter,
$methodAfter
)
);
}
if ($methodBefore->returnType !== $methodAfter->returnType) {
$class = null;
if ($methodBefore->returnType !== null && $methodAfter->returnType === null) {
$class = ClassMethodReturnTypeAdded::class;
} elseif ($methodAfter->returnType !== null && $methodBefore->returnType === null) {
$class = ClassMethodReturnTypeRemoved::class;
} elseif (strcasecmp($methodAfter->returnType->name, $methodBefore->returnType->name) !== 0) {
$class = ClassMethodReturnTypeChanged::class;
}
if ($class) {
$report->add(
$this->context,
new $class(
$this->context,
$this->fileBefore,
$contextAfter,
$methodBefore,
$this->fileAfter,
$contextAfter,
$methodAfter
)
);
}
}
$signatureResult = Signature::analyze($methodBefore->getParams(), $methodAfter->getParams());
$changes = [
'parameter_added' => ClassMethodParameterAdded::class,
'parameter_removed' => ClassMethodParameterRemoved::class,
'parameter_renamed' => ClassMethodParameterNameChanged::class,
'parameter_typing_added' => ClassMethodParameterTypingAdded::class,
'parameter_typing_removed' => ClassMethodParameterTypingRemoved::class,
'parameter_default_added' => ClassMethodParameterDefaultAdded::class,
'parameter_default_removed' => ClassMethodParameterDefaultRemoved::class,
'parameter_default_value_changed' => ClassMethodParameterDefaultValueChanged::class,
];
foreach ($changes as $changeType => $class) {
if ( ! $signatureResult[$changeType]) {
continue;
}
if (is_a($class, ClassMethodOperationUnary::class, true)) {
$data = new $class($this->context, $this->fileAfter, $contextAfter, $methodAfter);
} else {
$data = new $class(
$this->context,
$this->fileBefore,
$contextBefore,
$methodBefore,
$this->fileAfter,
$contextAfter,
$methodAfter
);
}
$report->add($this->context, $data);
}
// Difference in source code
// Cast to array because interfaces do not have stmts (= null)
if ( ! Implementation::isSame((array)$methodBefore->stmts, (array)$methodAfter->stmts)) {
$data = new ClassMethodImplementationChanged(
$this->context,
$this->fileBefore,
$contextBefore,
$methodBefore,
$this->fileAfter,
$contextAfter,
$methodAfter
);
$report->add($this->context, $data);
}
}
}
// Added methods implies MINOR BUMP
foreach ($methodsAdded as $method) {
$methodAfter = $methodsAfterKeyed[strtolower($method)];
$data = new ClassMethodAdded($this->context, $this->fileAfter, $contextAfter, $methodAfter);
$report->add($this->context, $data);
}
return $report;
}
}