Skip to content

Commit 35c45ad

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/UselessOverridingMethod: consider case-insensitivity of method names
Method names in PHP are case insensitive for ASCII characters. This commit changes the sniff to compare the name of the declared method with the name of the called method in a case-insensitive manner for the ASCII characters. The sniff will now flag methods as useless when PHP would consider the called method the same as the declared method, even when the called method name is in a different case as the name used in the function declaration. Includes unit tests.
1 parent 776bc88 commit 35c45ad

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public function process(File $phpcsFile, $stackPtr)
9797
return;
9898
}
9999

100-
// Find next non empty token index, should be the function name.
100+
// Find next non empty token index, should be the name of the method being called.
101101
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
102102

103103
// Skip for invalid code or other method.
104-
if ($next === false || $tokens[$next]['content'] !== $methodName) {
104+
if ($next === false || strcasecmp($tokens[$next]['content'], $methodName) !== 0) {
105105
return;
106106
}
107107

src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ class Bar {
6464
public function sameNumberDifferentParameters($a, $b) {
6565
return parent::sameNumberDifferentParameters($this->prop[$a], $this->{$b});
6666
}
67+
68+
public function differentCase() {
69+
return parent::DIFFERENTcase();
70+
}
71+
72+
public function differentCaseSameNonAnsiiCharáctêrs() {
73+
// This should be flagged, only ASCII chars have changed case.
74+
return parent::DIFFERENTcaseSameNonAnsiiCharáctêrs();
75+
}
76+
77+
public function differentCaseDifferentNonAnsiiCharáctêrs() {
78+
// This should not be flagged as non-ASCII chars have changed case, making this a different method name.
79+
return parent::DIFFERENTcaseDifferentNonAnsiiCharÁctÊrs();
80+
}
6781
}
6882

6983
abstract class AbstractFoo {

src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public function getWarningList($testFile='')
5454
16 => 1,
5555
38 => 1,
5656
56 => 1,
57+
68 => 1,
58+
72 => 1,
5759
];
5860
default:
5961
return [];

0 commit comments

Comments
 (0)