Skip to content

Commit c998827

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 or a interface or enum method.
1 parent ff00744 commit c998827

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

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

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

63+
// Skip functions (including nested functions) and also interface and enum methods.
64+
if ($phpcsFile->hasCondition($stackPtr, [T_CLASS, T_ANON_CLASS, T_TRAIT]) === false
65+
|| $phpcsFile->hasCondition($stackPtr, [T_FUNCTION]) === true
66+
) {
67+
return;
68+
}
69+
6370
// Get function name.
6471
$methodName = $phpcsFile->getDeclarationName($stackPtr);
6572

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

+44
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,42 @@ 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+
}
8399
}
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+
}
115+
116+
new class extends ParentClass {
117+
public function __construct() {
118+
parent::__construct();
119+
}
120+
};
121+
122+
new class extends ParentClass {
123+
public function __construct() {
124+
$a = 10;
125+
parent::__construct($a);
126+
}
127+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional fatal error (calling parent inside a function).
4+
// Testing that the sniff is *not* triggered in this case.
5+
6+
function __construct() {
7+
parent::__construct();
8+
}

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ 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+
117 => 1,
5861
];
5962
default:
6063
return [];

0 commit comments

Comments
 (0)