Skip to content

Commit 19b397d

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 19b397d

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-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

+68
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class Bar {
6868
public function differentCase() {
6969
return parent::DIFFERENTcase();
7070
}
71+
72+
public function nestedFunctionShouldBailEarly() {
73+
function nestedFunctionShouldBailEarly() {
74+
return 1;
75+
}
76+
}
7177
}
7278

7379
abstract class AbstractFoo {
@@ -80,4 +86,66 @@ interface InterfaceFoo {
8086

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

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+
96 => 1,
59+
102 => 1,
60+
124 => 1,
61+
138 => 1,
5862
];
5963
default:
6064
return [];

0 commit comments

Comments
 (0)