Skip to content

Commit 7592b77

Browse files
committed
ReferenceUsedNamesOnlySniff: Fixed false positives
1 parent a6c2bac commit 7592b77

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,15 @@ public function process(File $phpcsFile, $openTagPointer): void
246246
&& !NamespaceHelper::hasNamespace($name)
247247
&& NamespaceHelper::findCurrentNamespaceName($phpcsFile, $startPointer) !== null
248248
&& !array_key_exists(UseStatement::getUniqueId($reference->type, $name), $useStatements);
249-
$isGlobalFunctionFallback = $reference->isFunction && $isGlobalFallback;
250-
$isGlobalConstantFallback = $reference->isConstant && $isGlobalFallback;
249+
250+
$isGlobalFunctionFallback = false;
251+
if ($reference->isFunction && $isGlobalFallback) {
252+
$isGlobalFunctionFallback = !array_key_exists(strtolower($reference->name), $definedFunctionsIndex);
253+
}
254+
$isGlobalConstantFallback = false;
255+
if ($reference->isConstant && $isGlobalFallback) {
256+
$isGlobalConstantFallback = !array_key_exists($reference->name, $definedConstantsIndex);
257+
}
251258

252259
if ($isFullyQualified) {
253260
if ($reference->isClass && $this->allowFullyQualifiedNameForCollidingClasses) {

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public function testReferencingGlobalFunctionViaFallback(): void
115115
);
116116
}
117117

118+
public function testReferencingDefinedFunction(): void
119+
{
120+
$report = self::checkFile(__DIR__ . '/data/referencingDefinedFunction.php', [
121+
'allowFallbackGlobalFunctions' => false,
122+
]);
123+
self::assertNoSniffErrorInFile($report);
124+
}
125+
118126
public function testReferencingGlobalConstantViaFallback(): void
119127
{
120128
$report = self::checkFile(__DIR__ . '/data/shouldBeInUseStatement.php', [
@@ -128,6 +136,14 @@ public function testReferencingGlobalConstantViaFallback(): void
128136
);
129137
}
130138

139+
public function testReferencingDefinedConstant(): void
140+
{
141+
$report = self::checkFile(__DIR__ . '/data/referencingDefinedConstant.php', [
142+
'allowFallbackGlobalConstants' => false,
143+
]);
144+
self::assertNoSniffErrorInFile($report);
145+
}
146+
131147
/**
132148
* @dataProvider dataIgnoredNamesForIrrelevantTests
133149
* @param string[] $ignoredNames
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Whatever;
4+
5+
const HELLO = 'world';
6+
7+
echo HELLO;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Whatever;
4+
5+
function hello()
6+
{
7+
return 'world';
8+
}
9+
10+
echo hello();

0 commit comments

Comments
 (0)