Skip to content

Commit 13ae2a0

Browse files
committed
Fixed notice
1 parent 73d2394 commit 13ae2a0

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $classPointer)
157157
$newTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokenPointer - 1);
158158
if ($tokens[$newTokenPointer]['code'] === T_NEW) {
159159
$variableTokenPointer = TokenHelper::findPreviousLocal($phpcsFile, T_VARIABLE, $newTokenPointer - 1);
160-
$scopeMethodPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $variableTokenPointer - 1);
161-
for ($i = $tokens[$scopeMethodPointer]['scope_opener']; $i < $tokens[$scopeMethodPointer]['scope_closer']; $i++) {
162-
if ($tokens[$i]['content'] === $tokens[$variableTokenPointer]['content']) {
163-
$findUsagesStartTokenPointer = $checkVariable($i);
160+
if ($variableTokenPointer !== null) {
161+
$scopeMethodPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $variableTokenPointer - 1);
162+
for ($i = $tokens[$scopeMethodPointer]['scope_opener']; $i < $tokens[$scopeMethodPointer]['scope_closer']; $i++) {
163+
if ($tokens[$i]['content'] === $tokens[$variableTokenPointer]['content']) {
164+
$findUsagesStartTokenPointer = $checkVariable($i);
165+
}
164166
}
167+
} else {
168+
$findUsagesStartTokenPointer = $tokenPointer + 1;
165169
}
166170
} else {
167171
$doubleColonTokenPointer = TokenHelper::findNextEffective($phpcsFile, $tokenPointer + 1);

tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnSelfInstance.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ private function setUp()
1515
{
1616
}
1717

18+
public static function foo()
19+
{
20+
return new self();
21+
}
22+
1823
}

tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnStaticInstance.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ClassWithPrivateMethodCalledOnSelfInstance
55

66
public static function create()
77
{
8-
$self = new self();
8+
$self = new static();
99
$self->setUp();
1010

1111
return $self;
@@ -15,4 +15,9 @@ private function setUp()
1515
{
1616
}
1717

18+
public static function foo()
19+
{
20+
return new static();
21+
}
22+
1823
}

0 commit comments

Comments
 (0)