|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Analyzer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node\Stmt; |
|
6
|
|
|
use PHPSemVerChecker\Comparator\Implementation; |
|
7
|
|
|
use PHPSemVerChecker\Comparator\Signature; |
|
8
|
|
|
use PHPSemVerChecker\Operation\ClassMethodAdded; |
|
9
|
|
|
use PHPSemVerChecker\Operation\ClassMethodCaseChanged; |
|
10
|
|
|
use PHPSemVerChecker\Operation\ClassMethodImplementationChanged; |
|
11
|
|
|
use PHPSemVerChecker\Operation\ClassMethodOperationUnary; |
|
12
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterAdded; |
|
13
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultAdded; |
|
14
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultRemoved; |
|
15
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterDefaultValueChanged; |
|
16
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterNameChanged; |
|
17
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterRemoved; |
|
18
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterTypingAdded; |
|
19
|
|
|
use PHPSemVerChecker\Operation\ClassMethodParameterTypingRemoved; |
|
20
|
|
|
use PHPSemVerChecker\Operation\ClassMethodRemoved; |
|
21
|
|
|
use PHPSemVerChecker\Report\Report; |
|
22
|
|
|
|
|
23
|
|
|
class ClassMethodAnalyzer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $context; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var null|string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $fileBefore; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var null|string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $fileAfter; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $context |
|
40
|
|
|
* @param string $fileBefore |
|
41
|
|
|
* @param string $fileAfter |
|
42
|
|
|
*/ |
|
43
|
106 |
|
public function __construct($context, $fileBefore = null, $fileAfter = null) |
|
44
|
|
|
{ |
|
45
|
106 |
|
$this->context = $context; |
|
46
|
106 |
|
$this->fileBefore = $fileBefore; |
|
47
|
106 |
|
$this->fileAfter = $fileAfter; |
|
48
|
106 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \PhpParser\Node\Stmt $contextBefore |
|
52
|
|
|
* @param \PhpParser\Node\Stmt $contextAfter |
|
53
|
|
|
* @return \PHPSemVerChecker\Report\Report |
|
54
|
|
|
*/ |
|
55
|
106 |
|
public function analyze(Stmt $contextBefore, Stmt $contextAfter) |
|
56
|
|
|
{ |
|
57
|
106 |
|
$report = new Report(); |
|
58
|
|
|
|
|
59
|
106 |
|
$methodsBefore = $contextBefore->getMethods(); |
|
60
|
106 |
|
$methodsAfter = $contextAfter->getMethods(); |
|
61
|
|
|
|
|
62
|
106 |
|
$methodsBeforeKeyed = []; |
|
63
|
106 |
|
foreach ($methodsBefore as $method) { |
|
64
|
96 |
|
$methodsBeforeKeyed[strtolower($method->name)] = $method; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
106 |
|
$methodsAfterKeyed = []; |
|
68
|
106 |
|
foreach ($methodsAfter as $method) { |
|
69
|
96 |
|
$methodsAfterKeyed[strtolower($method->name)] = $method; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
106 |
|
$methodNamesBefore = array_keys($methodsBeforeKeyed); |
|
73
|
106 |
|
$methodNamesAfter = array_keys($methodsAfterKeyed); |
|
74
|
106 |
|
$methodsAdded = array_diff($methodNamesAfter, $methodNamesBefore); |
|
75
|
106 |
|
$methodsRemoved = array_diff($methodNamesBefore, $methodNamesAfter); |
|
76
|
106 |
|
$methodsToVerify = array_intersect($methodNamesBefore, $methodNamesAfter); |
|
77
|
|
|
|
|
78
|
|
|
// Here we only care about public methods as they are the only part of the API we care about |
|
79
|
|
|
|
|
80
|
|
|
// Removed methods can either be implemented in parent classes or not exist anymore |
|
81
|
106 |
|
foreach ($methodsRemoved as $method) { |
|
82
|
7 |
|
$methodBefore = $methodsBeforeKeyed[$method]; |
|
83
|
7 |
|
$data = new ClassMethodRemoved($this->context, $this->fileBefore, $contextBefore, $methodBefore); |
|
84
|
7 |
|
$report->add($this->context, $data); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
106 |
|
foreach ($methodsToVerify as $method) { |
|
88
|
|
|
/** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */ |
|
89
|
89 |
|
$methodBefore = $methodsBeforeKeyed[strtolower($method)]; |
|
90
|
|
|
/** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */ |
|
91
|
89 |
|
$methodAfter = $methodsAfterKeyed[strtolower($method)]; |
|
92
|
|
|
|
|
93
|
|
|
// Leave non-strict comparison here |
|
94
|
89 |
|
if ($methodBefore != $methodAfter) { |
|
95
|
|
|
// Detect method case changed. |
|
96
|
|
|
// If we entered this section then the normalized names (lowercase) were equal. |
|
97
|
68 |
|
if ($methodBefore->name->toString() !== $methodAfter->name->toString()) { |
|
98
|
6 |
|
$report->add( |
|
99
|
6 |
|
$this->context, |
|
100
|
6 |
|
new ClassMethodCaseChanged( |
|
101
|
6 |
|
$this->context, |
|
102
|
6 |
|
$this->fileBefore, |
|
103
|
|
|
$contextAfter, |
|
104
|
|
|
$methodBefore, |
|
105
|
6 |
|
$this->fileAfter, |
|
106
|
|
|
$contextAfter, |
|
107
|
|
|
$methodAfter |
|
108
|
|
|
) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
68 |
|
$signatureResult = Signature::analyze($methodBefore->getParams(), $methodAfter->getParams()); |
|
113
|
|
|
|
|
114
|
|
|
$changes = [ |
|
115
|
68 |
|
'parameter_added' => ClassMethodParameterAdded::class, |
|
116
|
|
|
'parameter_removed' => ClassMethodParameterRemoved::class, |
|
117
|
|
|
'parameter_renamed' => ClassMethodParameterNameChanged::class, |
|
118
|
|
|
'parameter_typing_added' => ClassMethodParameterTypingAdded::class, |
|
119
|
|
|
'parameter_typing_removed' => ClassMethodParameterTypingRemoved::class, |
|
120
|
|
|
'parameter_default_added' => ClassMethodParameterDefaultAdded::class, |
|
121
|
|
|
'parameter_default_removed' => ClassMethodParameterDefaultRemoved::class, |
|
122
|
|
|
'parameter_default_value_changed' => ClassMethodParameterDefaultValueChanged::class, |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
68 |
|
foreach ($changes as $changeType => $class) { |
|
126
|
68 |
|
if ( ! $signatureResult[$changeType]) { |
|
127
|
68 |
|
continue; |
|
128
|
|
|
} |
|
129
|
56 |
|
if (is_a($class, ClassMethodOperationUnary::class, true)) { |
|
130
|
42 |
|
$data = new $class($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
|
131
|
|
|
} else { |
|
132
|
14 |
|
$data = new $class( |
|
133
|
14 |
|
$this->context, |
|
134
|
14 |
|
$this->fileBefore, |
|
135
|
|
|
$contextBefore, |
|
136
|
|
|
$methodBefore, |
|
137
|
14 |
|
$this->fileAfter, |
|
138
|
|
|
$contextAfter, |
|
139
|
|
|
$methodAfter |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
56 |
|
$report->add($this->context, $data); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Difference in source code |
|
146
|
|
|
// Cast to array because interfaces do not have stmts (= null) |
|
147
|
68 |
|
if ( ! Implementation::isSame((array)$methodBefore->stmts, (array)$methodAfter->stmts)) { |
|
148
|
6 |
|
$data = new ClassMethodImplementationChanged( |
|
149
|
6 |
|
$this->context, |
|
150
|
6 |
|
$this->fileBefore, |
|
151
|
|
|
$contextBefore, |
|
152
|
|
|
$methodBefore, |
|
153
|
6 |
|
$this->fileAfter, |
|
154
|
|
|
$contextAfter, |
|
155
|
|
|
$methodAfter |
|
156
|
|
|
); |
|
157
|
6 |
|
$report->add($this->context, $data); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Added methods implies MINOR BUMP |
|
163
|
106 |
|
foreach ($methodsAdded as $method) { |
|
164
|
7 |
|
$methodAfter = $methodsAfterKeyed[strtolower($method)]; |
|
165
|
7 |
|
$data = new ClassMethodAdded($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
|
166
|
7 |
|
$report->add($this->context, $data); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
106 |
|
return $report; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|