Skip to content

Commit 30084f8

Browse files
committed
Chained private methods should not be reported as unused
1 parent 0506d3a commit 30084f8

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $classPointer)
177177
if ($tokens[$variableTokenPointer]['code'] === T_VARIABLE && $tokens[$variableTokenPointer]['content'] === '$this') {
178178
$findUsagesStartTokenPointer = $checkObjectOperatorUsage($tokenPointer);
179179
} else {
180+
$possibleThisTokenPointer = $tokenPointer - 1;
181+
do {
182+
$possibleThisTokenPointer = TokenHelper::findPreviousLocal($phpcsFile, T_VARIABLE, $possibleThisTokenPointer - 1);
183+
} while ($possibleThisTokenPointer !== null && $tokens[$possibleThisTokenPointer]['content'] !== '$this');
184+
185+
if ($possibleThisTokenPointer !== null) {
186+
$possibleMethodNamePointer = TokenHelper::findNextEffective($phpcsFile, $tokenPointer + 1);
187+
if ($tokens[$possibleMethodNamePointer]['code'] === T_STRING) {
188+
$possibleMethodCallPointer = TokenHelper::findNextEffective($phpcsFile, $possibleMethodNamePointer + 1);
189+
if ($tokens[$possibleMethodCallPointer]['code'] === T_OPEN_PARENTHESIS) {
190+
$methodName = $tokens[$possibleMethodNamePointer]['content'];
191+
if (isset($reportedMethods[$methodName])) {
192+
unset($reportedMethods[$methodName]);
193+
$findUsagesStartTokenPointer = $possibleMethodCallPointer + 1;
194+
continue;
195+
}
196+
}
197+
}
198+
}
199+
180200
$findUsagesStartTokenPointer = $tokenPointer + 1;
181201
}
182202
} elseif ($token['code'] === T_DOUBLE_COLON) {

tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public function testClassWithPrivateElementsUsedOnStaticInstance()
113113
$this->assertNoSniffErrorInFile($report);
114114
}
115115

116+
public function testClassWithChainedPrivateMethods()
117+
{
118+
$report = $this->checkFile(__DIR__ . '/data/classWithChainedPrivateMethods.php');
119+
$this->assertNoSniffErrorInFile($report);
120+
}
121+
116122
public function testClassWithConstants()
117123
{
118124
$resultFile = $this->checkFile(__DIR__ . '/data/classWithConstants.php');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class Test
4+
{
5+
6+
public function __construct()
7+
{
8+
$this
9+
->testMethod1()
10+
->testMethod2()
11+
->testMethod3()
12+
;
13+
}
14+
15+
private function testMethod1()
16+
{
17+
return $this;
18+
}
19+
20+
private function testMethod2()
21+
{
22+
return $this;
23+
}
24+
25+
private function testMethod3()
26+
{
27+
return $this;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)