|
21 | 21 |
|
22 | 22 | class ClassMethodAnalyzer |
23 | 23 | { |
24 | | - protected $context; |
25 | | - protected $fileBefore; |
26 | | - protected $fileAfter; |
27 | | - |
28 | | - /** |
29 | | - * @param string $context |
30 | | - * @param string $fileBefore |
31 | | - * @param string $fileAfter |
32 | | - */ |
33 | | - public function __construct($context, $fileBefore = null, $fileAfter = null) |
34 | | - { |
35 | | - $this->context = $context; |
36 | | - $this->fileBefore = $fileBefore; |
37 | | - $this->fileAfter = $fileAfter; |
38 | | - } |
39 | | - |
40 | | - public function analyze(Stmt $contextBefore, Stmt $contextAfter) |
41 | | - { |
42 | | - $report = new Report(); |
43 | | - |
44 | | - $methodsBefore = $contextBefore->getMethods(); |
45 | | - $methodsAfter = $contextAfter->getMethods(); |
46 | | - |
47 | | - $methodsBeforeKeyed = []; |
48 | | - foreach ($methodsBefore as $method) { |
49 | | - $methodsBeforeKeyed[strtolower($method->name)] = $method; |
50 | | - } |
51 | | - |
52 | | - $methodsAfterKeyed = []; |
53 | | - foreach ($methodsAfter as $method) { |
54 | | - $methodsAfterKeyed[strtolower($method->name)] = $method; |
55 | | - } |
56 | | - |
57 | | - // Here we only care about public methods as they are the only part of the API we care about |
58 | | - |
59 | | - $methodNamesNotAddedOrRemoved = []; |
60 | | - |
61 | | - foreach($methodsBefore as $methodBefore) |
62 | | - { |
63 | | - // Removed methods can either be implemented in parent classes or not exist anymore |
64 | | - if($this->wasMethodRemoved($methodBefore, $methodsAfter)) |
65 | | - { |
66 | | - $data = new ClassMethodRemoved($this->context, $this->fileBefore, $contextBefore, $methodBefore); |
67 | | - $report->add($this->context, $data); |
68 | | - } else { |
69 | | - $methodNamesNotAddedOrRemoved[strtolower($methodBefore->name)] = true; |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - foreach($methodsAfter as $methodAfter) |
74 | | - { |
75 | | - // Added methods implies MINOR BUMP |
76 | | - if($this->wasMethodAdded($methodAfter, $methodsBefore)) |
77 | | - { |
78 | | - $data = new ClassMethodAdded($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
79 | | - $report->add($this->context, $data); |
80 | | - } else { |
81 | | - $methodNamesNotAddedOrRemoved[strtolower($methodAfter->name)] = true; |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - foreach (array_keys($methodNamesNotAddedOrRemoved) as $methodName) { |
86 | | - |
87 | | - /** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */ |
88 | | - $methodBefore = $methodsBeforeKeyed[$methodName]; |
89 | | - /** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */ |
90 | | - $methodAfter = $methodsAfterKeyed[$methodName]; |
91 | | - |
92 | | - if (!$this->areMethodsEqual($methodBefore, $methodAfter)) { |
93 | | - |
94 | | - $paramsBefore = $methodBefore->params; |
95 | | - $paramsAfter = $methodAfter->params; |
96 | | - |
97 | | - $signatureResult = Signature::analyze($paramsBefore, $paramsAfter); |
98 | | - |
99 | | - $changes = [ |
100 | | - 'parameter_added' => ClassMethodParameterAdded::class, |
101 | | - 'parameter_removed' => ClassMethodParameterRemoved::class, |
102 | | - 'parameter_renamed' => ClassMethodParameterNameChanged::class, |
103 | | - 'parameter_typing_added' => ClassMethodParameterTypingAdded::class, |
104 | | - 'parameter_typing_removed' => ClassMethodParameterTypingRemoved::class, |
105 | | - 'parameter_default_added' => ClassMethodParameterDefaultAdded::class, |
106 | | - 'parameter_default_removed' => ClassMethodParameterDefaultRemoved::class, |
107 | | - 'parameter_default_value_changed' => ClassMethodParameterDefaultValueChanged::class, |
108 | | - ]; |
109 | | - |
110 | | - foreach ($changes as $changeType => $class) { |
111 | | - if (!$signatureResult[$changeType]) { |
112 | | - continue; |
113 | | - } |
114 | | - if (is_a($class, ClassMethodOperationUnary::class, true)) { |
115 | | - $data = new $class($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
116 | | - } else { |
117 | | - $data = new $class( |
118 | | - $this->context, |
119 | | - $this->fileBefore, |
120 | | - $contextBefore, |
121 | | - $methodBefore, |
122 | | - $this->fileAfter, |
123 | | - $contextAfter, |
124 | | - $methodAfter |
125 | | - ); |
126 | | - } |
127 | | - $report->add($this->context, $data); |
128 | | - } |
129 | | - |
130 | | - // Difference in source code |
131 | | - // Cast to array because interfaces do not have stmts (= null) |
132 | | - if (!Implementation::isSame((array)$methodBefore->stmts, (array)$methodAfter->stmts)) { |
133 | | - $data = new ClassMethodImplementationChanged( |
134 | | - $this->context, |
135 | | - $this->fileBefore, |
136 | | - $contextBefore, |
137 | | - $methodBefore, |
138 | | - $this->fileAfter, |
139 | | - $contextAfter, |
140 | | - $methodAfter |
141 | | - ); |
142 | | - $report->add($this->context, $data); |
143 | | - } |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - return $report; |
148 | | - } |
149 | | - |
150 | | - private function areMethodsEqual(Stmt\ClassMethod $methodBefore, Stmt\ClassMethod $methodAfter) |
151 | | - { |
152 | | - if ($methodBefore == $methodAfter) { |
153 | | - return true; |
154 | | - }; |
155 | | - |
156 | | - return strtolower($methodBefore->name) === strtolower($methodAfter->name) |
157 | | - && $methodBefore->isPrivate() === $methodAfter->isPrivate() |
158 | | - && $methodBefore->isAbstract() === $methodAfter->isAbstract() |
159 | | - && $methodBefore->isFinal() === $methodAfter->isFinal() |
160 | | - && $methodBefore->isProtected() === $methodAfter->isProtected() |
161 | | - && $methodBefore->isPublic() === $methodAfter->isPublic() |
162 | | - && $methodBefore->isStatic() === $methodAfter->isStatic() |
163 | | - && $methodBefore->getReturnType() === $methodAfter->getReturnType() |
164 | | - // statements are objects, cannot be compared with === |
165 | | - && $methodBefore->getStmts() == $methodAfter->getStmts() |
166 | | - && $methodBefore->getParams() === $methodAfter->getParams() |
167 | | - && $methodBefore->returnsByRef() === $methodAfter->returnsByRef() |
168 | | - && $methodBefore->getType() === $methodAfter->getType() |
169 | | - && $methodBefore->getAttributes() === $methodAfter->getAttributes(); |
170 | | - } |
171 | | - |
172 | | - private function wasMethodAdded(Stmt\ClassMethod $method, $methodsAfter) |
173 | | - { |
174 | | - foreach($methodsAfter as $methodAfter) |
175 | | - { |
176 | | - if(strtolower($method->name) == strtolower($methodAfter->name)) |
177 | | - { |
178 | | - return false; |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - return true; |
183 | | - } |
184 | | - |
185 | | - private function wasMethodRemoved(Stmt\ClassMethod $method, $methodsBefore) |
186 | | - { |
187 | | - foreach($methodsBefore as $methodBefore) |
188 | | - { |
189 | | - if(strtolower($method->name) == strtolower($methodBefore->name)) |
190 | | - { |
191 | | - return false; |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - return true; |
196 | | - } |
| 24 | + protected $context; |
| 25 | + protected $fileBefore; |
| 26 | + protected $fileAfter; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param string $context |
| 30 | + * @param string $fileBefore |
| 31 | + * @param string $fileAfter |
| 32 | + */ |
| 33 | + public function __construct($context, $fileBefore = null, $fileAfter = null) |
| 34 | + { |
| 35 | + $this->context = $context; |
| 36 | + $this->fileBefore = $fileBefore; |
| 37 | + $this->fileAfter = $fileAfter; |
| 38 | + } |
| 39 | + |
| 40 | + public function analyze(Stmt $contextBefore, Stmt $contextAfter) |
| 41 | + { |
| 42 | + $report = new Report(); |
| 43 | + |
| 44 | + $methodsBefore = $contextBefore->getMethods(); |
| 45 | + $methodsAfter = $contextAfter->getMethods(); |
| 46 | + |
| 47 | + $methodsBeforeKeyed = []; |
| 48 | + foreach ($methodsBefore as $method) { |
| 49 | + $methodsBeforeKeyed[strtolower($method->name)] = $method; |
| 50 | + } |
| 51 | + |
| 52 | + $methodsAfterKeyed = []; |
| 53 | + foreach ($methodsAfter as $method) { |
| 54 | + $methodsAfterKeyed[strtolower($method->name)] = $method; |
| 55 | + } |
| 56 | + |
| 57 | + // Here we only care about public methods as they are the only part of the API we care about |
| 58 | + |
| 59 | + $methodNamesNotAddedOrRemoved = []; |
| 60 | + |
| 61 | + foreach ($methodsBefore as $methodBefore) { |
| 62 | + // Removed methods can either be implemented in parent classes or not exist anymore |
| 63 | + if ($this->wasMethodRemoved($methodBefore, $methodsAfter)) { |
| 64 | + $data = new ClassMethodRemoved($this->context, $this->fileBefore, $contextBefore, $methodBefore); |
| 65 | + $report->add($this->context, $data); |
| 66 | + } else { |
| 67 | + $methodNamesNotAddedOrRemoved[strtolower($methodBefore->name)] = true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + foreach ($methodsAfter as $methodAfter) { |
| 72 | + // Added methods implies MINOR BUMP |
| 73 | + if ($this->wasMethodAdded($methodAfter, $methodsBefore)) { |
| 74 | + $data = new ClassMethodAdded($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
| 75 | + $report->add($this->context, $data); |
| 76 | + } else { |
| 77 | + $methodNamesNotAddedOrRemoved[strtolower($methodAfter->name)] = true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + foreach (array_keys($methodNamesNotAddedOrRemoved) as $methodName) { |
| 82 | + |
| 83 | + /** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */ |
| 84 | + $methodBefore = $methodsBeforeKeyed[$methodName]; |
| 85 | + /** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */ |
| 86 | + $methodAfter = $methodsAfterKeyed[$methodName]; |
| 87 | + |
| 88 | + if (!$this->areMethodsEqual($methodBefore, $methodAfter)) { |
| 89 | + |
| 90 | + $paramsBefore = $methodBefore->params; |
| 91 | + $paramsAfter = $methodAfter->params; |
| 92 | + |
| 93 | + $signatureResult = Signature::analyze($paramsBefore, $paramsAfter); |
| 94 | + |
| 95 | + $changes = [ |
| 96 | + 'parameter_added' => ClassMethodParameterAdded::class, |
| 97 | + 'parameter_removed' => ClassMethodParameterRemoved::class, |
| 98 | + 'parameter_renamed' => ClassMethodParameterNameChanged::class, |
| 99 | + 'parameter_typing_added' => ClassMethodParameterTypingAdded::class, |
| 100 | + 'parameter_typing_removed' => ClassMethodParameterTypingRemoved::class, |
| 101 | + 'parameter_default_added' => ClassMethodParameterDefaultAdded::class, |
| 102 | + 'parameter_default_removed' => ClassMethodParameterDefaultRemoved::class, |
| 103 | + 'parameter_default_value_changed' => ClassMethodParameterDefaultValueChanged::class, |
| 104 | + ]; |
| 105 | + |
| 106 | + foreach ($changes as $changeType => $class) { |
| 107 | + if (!$signatureResult[$changeType]) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + if (is_a($class, ClassMethodOperationUnary::class, true)) { |
| 111 | + $data = new $class($this->context, $this->fileAfter, $contextAfter, $methodAfter); |
| 112 | + } else { |
| 113 | + $data = new $class( |
| 114 | + $this->context, |
| 115 | + $this->fileBefore, |
| 116 | + $contextBefore, |
| 117 | + $methodBefore, |
| 118 | + $this->fileAfter, |
| 119 | + $contextAfter, |
| 120 | + $methodAfter |
| 121 | + ); |
| 122 | + } |
| 123 | + $report->add($this->context, $data); |
| 124 | + } |
| 125 | + |
| 126 | + // Difference in source code |
| 127 | + // Cast to array because interfaces do not have stmts (= null) |
| 128 | + if (!Implementation::isSame((array)$methodBefore->stmts, (array)$methodAfter->stmts)) { |
| 129 | + $data = new ClassMethodImplementationChanged( |
| 130 | + $this->context, |
| 131 | + $this->fileBefore, |
| 132 | + $contextBefore, |
| 133 | + $methodBefore, |
| 134 | + $this->fileAfter, |
| 135 | + $contextAfter, |
| 136 | + $methodAfter |
| 137 | + ); |
| 138 | + $report->add($this->context, $data); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return $report; |
| 144 | + } |
| 145 | + |
| 146 | + private function areMethodsEqual(Stmt\ClassMethod $methodBefore, Stmt\ClassMethod $methodAfter) |
| 147 | + { |
| 148 | + if ($methodBefore == $methodAfter) { |
| 149 | + return true; |
| 150 | + }; |
| 151 | + |
| 152 | + return strtolower($methodBefore->name) === strtolower($methodAfter->name) |
| 153 | + && $methodBefore->isPrivate() === $methodAfter->isPrivate() |
| 154 | + && $methodBefore->isAbstract() === $methodAfter->isAbstract() |
| 155 | + && $methodBefore->isFinal() === $methodAfter->isFinal() |
| 156 | + && $methodBefore->isProtected() === $methodAfter->isProtected() |
| 157 | + && $methodBefore->isPublic() === $methodAfter->isPublic() |
| 158 | + && $methodBefore->isStatic() === $methodAfter->isStatic() |
| 159 | + && $methodBefore->getReturnType() === $methodAfter->getReturnType() |
| 160 | + // statements are objects, cannot be compared with === |
| 161 | + && $methodBefore->getStmts() == $methodAfter->getStmts() |
| 162 | + && $methodBefore->getParams() === $methodAfter->getParams() |
| 163 | + && $methodBefore->returnsByRef() === $methodAfter->returnsByRef() |
| 164 | + && $methodBefore->getType() === $methodAfter->getType() |
| 165 | + && $methodBefore->getAttributes() === $methodAfter->getAttributes(); |
| 166 | + } |
| 167 | + |
| 168 | + private function wasMethodAdded(Stmt\ClassMethod $method, $methodsAfter) |
| 169 | + { |
| 170 | + foreach ($methodsAfter as $methodAfter) { |
| 171 | + if (strtolower($method->name) == strtolower($methodAfter->name)) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + private function wasMethodRemoved(Stmt\ClassMethod $method, $methodsBefore) |
| 180 | + { |
| 181 | + foreach ($methodsBefore as $methodBefore) { |
| 182 | + if (strtolower($method->name) == strtolower($methodBefore->name)) { |
| 183 | + return false; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return true; |
| 188 | + } |
197 | 189 | } |
0 commit comments