Skip to content

Commit 667e0be

Browse files
committed
Detect function renaming too
1 parent ce3e042 commit 667e0be

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

docs/Ruleset.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ V070 | MINOR | Function parameter typing removed
1515
V071 | MINOR | Function parameter default added
1616
V072 | MAJOR | Function parameter default removed
1717
V073 | MINOR | Function parameter default value changed
18+
V160 | PATCH | Function renamed (case only)
1819

1920
# Classes
2021

src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPSemVerChecker\Operation\FunctionParameterTypingAdded;
1818
use PHPSemVerChecker\Operation\FunctionParameterTypingRemoved;
1919
use PHPSemVerChecker\Operation\FunctionRemoved;
20+
use PHPSemVerChecker\Operation\FunctionRenamedCaseOnly;
2021
use PHPSemVerChecker\Registry\Registry;
2122
use PHPSemVerChecker\Report\Report;
2223

@@ -35,28 +36,62 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
3536
{
3637
$report = new Report();
3738

38-
$keysBefore = array_keys($registryBefore->data['function']);
39-
$keysAfter = array_keys($registryAfter->data['function']);
40-
$added = array_diff($keysAfter, $keysBefore);
41-
$removed = array_diff($keysBefore, $keysAfter);
42-
$toVerify = array_intersect($keysBefore, $keysAfter);
39+
40+
$functionsBefore = $registryBefore->data['function'];
41+
$functionsAfter = $registryAfter->data['function'];
42+
43+
$functionsBeforeKeyed = [];
44+
$filesBeforeKeyed = [];
45+
foreach($functionsBefore as $key => $functionBefore)
46+
{
47+
$functionsBeforeKeyed[strtolower($functionBefore->name)] = $functionBefore;
48+
$filesBeforeKeyed[strtolower($functionBefore->name)] = $registryBefore->mapping['function'][$key];
49+
}
50+
51+
$functionsAfterKeyed = [];
52+
$filesAfterKeyed = [];
53+
foreach($functionsAfter as $key => $functionAfter)
54+
{
55+
$functionsAfterKeyed[strtolower($functionAfter->name)] = $functionAfter;
56+
$filesAfterKeyed[strtolower($functionAfter->name)] = $registryAfter->mapping['function'][$key];
57+
}
58+
59+
$functionNamesBefore = array_keys($functionsBeforeKeyed);
60+
$functionNamesAfter = array_keys($functionsAfterKeyed);
61+
$added = array_diff($functionNamesAfter, $functionNamesBefore);
62+
$removed = array_diff($functionNamesBefore, $functionNamesAfter);
63+
$toVerify = array_intersect($functionNamesBefore, $functionNamesAfter);
4364

4465
foreach ($removed as $key) {
45-
$fileBefore = $registryBefore->mapping['function'][$key];
46-
$functionBefore = $registryBefore->data['function'][$key];
66+
$fileBefore = $filesBeforeKeyed[$key];
67+
$functionBefore = $functionsBeforeKeyed[$key];
4768

4869
$data = new FunctionRemoved($fileBefore, $functionBefore);
4970
$report->addFunction($data);
5071
}
5172

5273
foreach ($toVerify as $key) {
53-
$fileBefore = $registryBefore->mapping['function'][$key];
54-
$functionBefore = $registryBefore->data['function'][$key];
55-
$fileAfter = $registryAfter->mapping['function'][$key];
56-
$functionAfter = $registryAfter->data['function'][$key];
74+
$fileBefore = $filesBeforeKeyed[$key];
75+
$functionBefore = $functionsBeforeKeyed[$key];
76+
$fileAfter = $filesAfterKeyed[$key];
77+
$functionAfter = $functionsAfterKeyed[$key];
5778

5879
// Leave non-strict comparison here
5980
if ($functionBefore != $functionAfter) {
81+
82+
// Check if the name of the interface has changed case.
83+
// If we entered this section then the normalized names (lowercase) were equal.
84+
if ($functionBefore->name !== $functionAfter->name) {
85+
$report->addFunction(
86+
new FunctionRenamedCaseOnly(
87+
$fileBefore,
88+
$functionBefore,
89+
$fileAfter,
90+
$functionAfter
91+
)
92+
);
93+
}
94+
6095
$signatureResult = Signature::analyze($functionBefore->getParams(), $functionAfter->getParams());
6196

6297
$changes = [

src/PHPSemVerChecker/Configuration/LevelMapping.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class LevelMapping
134134
'V157' => Level::PATCH,
135135
'V158' => Level::PATCH,
136136
'V159' => Level::PATCH,
137+
'V160' => Level::PATCH,
137138
];
138139

139140
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Operation;
4+
5+
class FunctionRenamedCaseOnly extends FunctionOperationDelta
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $code = 'V160';
11+
/**
12+
* @var string
13+
*/
14+
protected $reason = 'Function renamed (case only).';
15+
}

tests/PHPSemVerChecker/Analyzer/FunctionAnalyzerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,31 @@ public function testFunctionImplementationChanged()
390390
$this->assertSame('Function implementation changed.', $report['function'][$expectedLevel][0]->getReason());
391391
$this->assertSame('tmp', $report['function'][$expectedLevel][0]->getTarget());
392392
}
393+
394+
public function testFunctionCaseChanged()
395+
{
396+
$before = new Registry();
397+
$after = new Registry();
398+
399+
$before->addFunction(new Function_('somefunctionname', [
400+
'stmts' => [
401+
new FuncCall('someFunctionCall'),
402+
],
403+
]));
404+
405+
$after->addFunction(new Function_('someFunctionName', [
406+
'stmts' => [
407+
new FuncCall('someFunctionCall'),
408+
],
409+
]));
410+
411+
$analyzer = new FunctionAnalyzer();
412+
$report = $analyzer->analyze($before, $after);
413+
414+
$expectedLevel = Level::PATCH;
415+
Assert::assertDifference($report, 'function', $expectedLevel);
416+
$this->assertSame('V160', $report['function'][$expectedLevel][0]->getCode());
417+
$this->assertSame('Function renamed (case only).', $report['function'][$expectedLevel][0]->getReason());
418+
$this->assertSame('someFunctionName', $report['function'][$expectedLevel][0]->getTarget());
419+
}
393420
}

0 commit comments

Comments
 (0)