1717use PHPSemVerChecker \Operation \ClassMethodParameterTypingAdded ;
1818use PHPSemVerChecker \Operation \ClassMethodParameterTypingRemoved ;
1919use PHPSemVerChecker \Operation \ClassMethodRemoved ;
20+ use PHPSemVerChecker \Operation \ClassMethodRenamedCaseOnly ;
2021use PHPSemVerChecker \Report \Report ;
2122
22- class ClassMethodAnalyzer
23- {
23+ class ClassMethodAnalyzer {
2424 protected $ context ;
2525 protected $ fileBefore ;
2626 protected $ fileAfter ;
@@ -54,42 +54,34 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter)
5454 $ methodsAfterKeyed [strtolower ($ method ->name )] = $ method ;
5555 }
5656
57- // Here we only care about public methods as they are the only part of the API we care about
58-
59- $ methodNamesNotAddedOrRemoved = [];
57+ $ methodNamesBefore = array_keys ($ methodsBeforeKeyed );
58+ $ methodNamesAfter = array_keys ($ methodsAfterKeyed );
59+ $ methodsAdded = array_diff ($ methodNamesAfter , $ methodNamesBefore );
60+ $ methodsRemoved = array_diff ($ methodNamesBefore , $ methodNamesAfter );
61+ $ methodsToVerify = array_intersect ($ methodNamesBefore , $ methodNamesAfter );
6062
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- }
63+ // Here we only care about public methods as they are the only part of the API we care about
7064
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- }
65+ // Removed methods can either be implemented in parent classes or not exist anymore
66+ foreach ($ methodsRemoved as $ method ) {
67+ $ methodBefore = $ methodsBeforeKeyed [$ method ];
68+ $ data = new ClassMethodRemoved ($ this ->context , $ this ->fileBefore , $ contextBefore , $ methodBefore );
69+ $ report ->add ($ this ->context , $ data );
7970 }
8071
81- foreach (array_keys ($ methodNamesNotAddedOrRemoved ) as $ methodName ) {
82-
72+ foreach ($ methodsToVerify as $ method ) {
8373 /** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */
84- $ methodBefore = $ methodsBeforeKeyed [$ methodName ];
74+ $ methodBefore = $ methodsBeforeKeyed [strtolower ( $ method ) ];
8575 /** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */
86- $ methodAfter = $ methodsAfterKeyed [$ methodName ];
76+ $ methodAfter = $ methodsAfterKeyed [strtolower ( $ method ) ];
8777
88- if (!$ this ->areMethodsEqual ($ methodBefore , $ methodAfter )) {
78+ // Leave non-strict comparison here
79+ if ($ methodBefore != $ methodAfter ) {
8980
9081 $ signatureResult = Signature::analyze ($ methodBefore , $ methodAfter );
9182
9283 $ changes = [
84+ 'function_renamed_case_only ' => ClassMethodRenamedCaseOnly::class,
9385 'parameter_added ' => ClassMethodParameterAdded::class,
9486 'parameter_removed ' => ClassMethodParameterRemoved::class,
9587 'parameter_renamed ' => ClassMethodParameterNameChanged::class,
@@ -101,7 +93,7 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter)
10193 ];
10294
10395 foreach ($ changes as $ changeType => $ class ) {
104- if (! $ signatureResult [$ changeType ]) {
96+ if ( ! $ signatureResult [$ changeType ]) {
10597 continue ;
10698 }
10799 if (is_a ($ class , ClassMethodOperationUnary::class, true )) {
@@ -137,50 +129,13 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter)
137129 }
138130 }
139131
140- return $ report ;
141- }
142-
143- private function areMethodsEqual (Stmt \ClassMethod $ methodBefore , Stmt \ClassMethod $ methodAfter )
144- {
145- if ($ methodBefore == $ methodAfter ) {
146- return true ;
147- };
148-
149- return strtolower ($ methodBefore ->name ) === strtolower ($ methodAfter ->name )
150- && $ methodBefore ->isPrivate () === $ methodAfter ->isPrivate ()
151- && $ methodBefore ->isAbstract () === $ methodAfter ->isAbstract ()
152- && $ methodBefore ->isFinal () === $ methodAfter ->isFinal ()
153- && $ methodBefore ->isProtected () === $ methodAfter ->isProtected ()
154- && $ methodBefore ->isPublic () === $ methodAfter ->isPublic ()
155- && $ methodBefore ->isStatic () === $ methodAfter ->isStatic ()
156- && $ methodBefore ->getReturnType () === $ methodAfter ->getReturnType ()
157- // statements are objects, cannot be compared with ===
158- && $ methodBefore ->getStmts () == $ methodAfter ->getStmts ()
159- && $ methodBefore ->getParams () === $ methodAfter ->getParams ()
160- && $ methodBefore ->returnsByRef () === $ methodAfter ->returnsByRef ()
161- && $ methodBefore ->getType () === $ methodAfter ->getType ()
162- && $ methodBefore ->getAttributes () === $ methodAfter ->getAttributes ();
163- }
164-
165- private function wasMethodAdded (Stmt \ClassMethod $ method , $ methodsAfter )
166- {
167- foreach ($ methodsAfter as $ methodAfter ) {
168- if (strtolower ($ method ->name ) == strtolower ($ methodAfter ->name )) {
169- return false ;
170- }
171- }
172-
173- return true ;
174- }
175-
176- private function wasMethodRemoved (Stmt \ClassMethod $ method , $ methodsBefore )
177- {
178- foreach ($ methodsBefore as $ methodBefore ) {
179- if (strtolower ($ method ->name ) == strtolower ($ methodBefore ->name )) {
180- return false ;
181- }
132+ // Added methods implies MINOR BUMP
133+ foreach ($ methodsAdded as $ method ) {
134+ $ methodAfter = $ methodsAfterKeyed [strtolower ($ method )];
135+ $ data = new ClassMethodAdded ($ this ->context , $ this ->fileAfter , $ contextAfter , $ methodAfter );
136+ $ report ->add ($ this ->context , $ data );
182137 }
183138
184- return true ;
139+ return $ report ;
185140 }
186141}
0 commit comments