Skip to content

Commit d2418c3

Browse files
committed
Make renaming method case a PATCH change
1 parent 63f11e9 commit d2418c3

File tree

5 files changed

+54
-72
lines changed

5 files changed

+54
-72
lines changed

docs/Ruleset.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ V096 | PATCH | Class private method parameter default removed
7070
V097 | MAJOR | Class public method parameter default value changed
7171
V098 | MAJOR | Class protected method parameter default value changed
7272
V099 | PATCH | Class private method parameter default value changed
73+
V150 | PATCH | Class public/protected/private method renamed (case only)
7374
VXXX | MAJOR | *Final class public method parameter added*
7475
VXXX | MAJOR | *Final class protected method parameter added*
7576
VXXX | PATCH | *Final class private method parameter added*
@@ -109,6 +110,7 @@ V076 | MAJOR | Interface method parameter typing removed
109110
V077 | MINOR | Interface method parameter default added
110111
V078 | MAJOR | Interface method parameter default removed
111112
V079 | MAJOR | Interface method parameter default value changed
113+
V151 | PATCH | Interface method renamed (case only)
112114

113115
# Trait
114116

@@ -158,6 +160,7 @@ V114 | MAJOR | Trait private method parameter default removed
158160
V115 | MAJOR | Trait public method parameter default value changed
159161
V116 | MAJOR | Trait protected method parameter default value changed
160162
V117 | MAJOR | Trait private method parameter default value changed
163+
V152 | PATCH | Trait method renamed (case only)
161164

162165
# To classify
163166

src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
use PHPSemVerChecker\Operation\ClassMethodParameterTypingAdded;
1818
use PHPSemVerChecker\Operation\ClassMethodParameterTypingRemoved;
1919
use PHPSemVerChecker\Operation\ClassMethodRemoved;
20+
use PHPSemVerChecker\Operation\ClassMethodRenamedCaseOnly;
2021
use 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
}

src/PHPSemVerChecker/Configuration/LevelMapping.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ class LevelMapping
121121
'V115' => Level::MAJOR,
122122
'V116' => Level::MAJOR,
123123
'V117' => Level::MAJOR,
124+
'V150' => Level::PATCH,
125+
'V151' => Level::PATCH,
126+
'V152' => Level::PATCH,
124127
];
125128

126129
public static function getLevelForCode($code)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Operation;
4+
5+
class ClassMethodRenamedCaseOnly extends ClassMethodOperationUnary
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $code = [
11+
'class' => ['V150'],
12+
'interface' => ['V151'],
13+
'trait' => ['V152'],
14+
];
15+
/**
16+
* @var string
17+
*/
18+
protected $reason = 'Method has been renamed (case only).';
19+
}

tests/PHPSemVerChecker/Analyzer/ClassMethodAnalyzerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ public function testClassMethodCaseChangeIsIgnored()
737737
$analyzer = new ClassMethodAnalyzer('class');
738738
$report = $analyzer->analyze($classBefore, $classAfter);
739739

740-
Assert::assertNoDifference($report);
740+
// var_dump( $report ); die;
741+
742+
Assert::assertDifference($report, 'class', Level::PATCH);
741743
}
742744
}

0 commit comments

Comments
 (0)