Skip to content

Commit 4f955a3

Browse files
committed
ModernClassNameReferenceSniff: Fixed false positives with get_class()
1 parent b3132a4 commit 4f955a3

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

SlevomatCodingStandard/Sniffs/Classes/ModernClassNameReferenceSniff.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ private function checkFunctionCall(File $phpcsFile, int $pointer): void
8989
}
9090

9191
if ($functionName === 'get_class') {
92-
$parameterPointer = TokenHelper::findNext($phpcsFile, T_VARIABLE, $openParenthesisPointer + 1, $tokens[$openParenthesisPointer]['parenthesis_closer']);
93-
if ($parameterPointer !== null) {
92+
$parameterPointer = TokenHelper::findNextEffective($phpcsFile, $openParenthesisPointer + 1, $tokens[$openParenthesisPointer]['parenthesis_closer']);
93+
94+
if ($parameterPointer === null) {
95+
$fixedContent = 'self::class';
96+
} elseif ($tokens[$parameterPointer]['code'] !== T_VARIABLE) {
97+
return;
98+
} else {
9499
$parameterName = strtolower($tokens[$parameterPointer]['content']);
95100
if ($parameterName !== '$this') {
96101
return;
@@ -102,8 +107,6 @@ private function checkFunctionCall(File $phpcsFile, int $pointer): void
102107
}
103108

104109
$fixedContent = 'static::class';
105-
} else {
106-
$fixedContent = 'self::class';
107110
}
108111

109112
} elseif ($functionName === 'get_parent_class') {

tests/Sniffs/Classes/data/modernClassNameReferenceNoErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function getClassWithMoreThanThisParameter()
2727
{
2828
return get_class($this->anything);
2929
}
30+
31+
public function getClassWithSomethingElse()
32+
{
33+
return get_class(new stdClass());
34+
}
35+
3036
}

0 commit comments

Comments
 (0)