Skip to content

Commit d4ebdc7

Browse files
committed
Generic/UselessOverridingMethod: bail if not checking a class method
The UselessOverridingMethod is triggered when the T_FUNCTION token is found. This token is used for regular functions and also methods. The sniff should run only for class and trait methods (including abstract and anonymous classes). Those are the only methods that can have a parent. So this commit changes the sniff to bail early when dealing with a regular function, nested function or enum method.
1 parent ff00744 commit d4ebdc7

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public function process(File $phpcsFile, $stackPtr)
6060
return;
6161
}
6262

63+
$conditions = $token['conditions'];
64+
$lastCondition = end($conditions);
65+
66+
// Skip functions that are not a method part of a class, anon class or trait.
67+
if (in_array($lastCondition, [T_CLASS, T_ANON_CLASS, T_TRAIT]) === false) {
68+
return;
69+
}
70+
6371
// Get function name.
6472
$methodName = $phpcsFile->getDeclarationName($stackPtr);
6573

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

+59
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class Bar {
6868
public function differentCase() {
6969
return parent::DIFFERENTcase();
7070
}
71+
72+
public function nestedFunctionShouldBailEarly() {
73+
function nestedFunctionShouldBailEarly() {
74+
// Invalid code needed to ensure an error is NOT triggered and the sniff bails early when handling nested function.
75+
parent::nestedFunctionShouldBailEarly();
76+
}
77+
}
7178
}
7279

7380
abstract class AbstractFoo {
@@ -80,4 +87,56 @@ interface InterfaceFoo {
8087

8188
trait TraitFoo {
8289
abstract public function sniffShouldBailEarly();
90+
91+
public function usefulMethodInTrait() {
92+
parent::usefulMethodInTrait();
93+
94+
return 1;
95+
}
96+
97+
public function uselessMethodInTrait() {
98+
return parent::uselessMethodInTrait();
99+
}
100+
}
101+
102+
abstract class AbstractBar extends AbstractFoo {
103+
public function uselessMethodInAbstractClass() {
104+
parent::uselessMethodInAbstractClass();
105+
}
106+
107+
public function usefulMethodInAbstractClass() {
108+
$a = 1;
109+
parent::usefulMethodInAbstractClass($a);
110+
}
111+
}
112+
113+
enum EnumFoo {
114+
public function sniffShouldBailEarly() {
115+
// Invalid code needed to ensure an error is NOT triggered and the sniff bails early when handling an enum method.
116+
parent::sniffShouldBailEarly();
117+
}
118+
}
119+
120+
function shouldBailEarly() {
121+
// Invalid code needed to ensure an error is NOT triggered and the sniff bails early when handling a regular function.
122+
parent::shouldBailEarly();
123+
}
124+
125+
new class extends ParentClass {
126+
public function uselessOverridingMethod() {
127+
parent::uselessOverridingMethod();
128+
}
129+
130+
public function usefulOverridingMethod() {
131+
$a = 10;
132+
parent::usefulOverridingMethod($a);
133+
}
134+
};
135+
136+
function foo() {
137+
new class extends ParentClass {
138+
public function uselessOverridingMethod() {
139+
parent::uselessOverridingMethod();
140+
}
141+
};
83142
}

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ public function getWarningList($testFile='')
5050
switch ($testFile) {
5151
case 'UselessOverridingMethodUnitTest.1.inc':
5252
return [
53-
4 => 1,
54-
16 => 1,
55-
38 => 1,
56-
56 => 1,
57-
68 => 1,
53+
4 => 1,
54+
16 => 1,
55+
38 => 1,
56+
56 => 1,
57+
68 => 1,
58+
97 => 1,
59+
103 => 1,
60+
126 => 1,
61+
138 => 1,
5862
];
5963
default:
6064
return [];

0 commit comments

Comments
 (0)