Skip to content

Commit 5b806df

Browse files
committed
FullyQualifiedGlobalConstantsSniff and FullyQualifiedGlobalFunctionsSniff fixes
1 parent ce4cf54 commit 5b806df

11 files changed

+33
-17
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/AbstractFullyQualifiedGlobalReference.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
4444

4545
foreach ($referencedNames as $referencedName) {
4646
$name = $referencedName->getNameAsReferencedInFile();
47-
$canonicalName = $this->isCaseSensitive() ? $name : strtolower($name);
48-
$referenceNamePointer = $referencedName->getStartPointer();
47+
$namePointer = $referencedName->getStartPointer();
4948

5049
if (!$this->isValidType($referencedName)) {
5150
continue;
@@ -59,18 +58,23 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
5958
continue;
6059
}
6160

61+
$canonicalName = $this->isCaseSensitive() ? $name : strtolower($name);
62+
6263
if (array_key_exists($canonicalName, $useStatements)) {
63-
continue;
64+
$fullyQualifiedName = NamespaceHelper::resolveName($phpcsFile, $name, $referencedName->getType(), $useStatements, $namePointer);
65+
if (NamespaceHelper::hasNamespace($fullyQualifiedName)) {
66+
continue;
67+
}
6468
}
6569

6670
if (array_key_exists($canonicalName, $exclude)) {
6771
continue;
6872
}
6973

70-
$fix = $phpcsFile->addFixableError(sprintf($this->getNotFullyQualifiedMessage(), $tokens[$referenceNamePointer]['content']), $referenceNamePointer, self::CODE_NON_FULLY_QUALIFIED);
74+
$fix = $phpcsFile->addFixableError(sprintf($this->getNotFullyQualifiedMessage(), $tokens[$namePointer]['content']), $namePointer, self::CODE_NON_FULLY_QUALIFIED);
7175
if ($fix) {
7276
$phpcsFile->fixer->beginChangeset();
73-
$phpcsFile->fixer->addContentBefore($referenceNamePointer, NamespaceHelper::NAMESPACE_SEPARATOR);
77+
$phpcsFile->fixer->addContentBefore($namePointer, NamespaceHelper::NAMESPACE_SEPARATOR);
7478
$phpcsFile->fixer->endChangeset();
7579
}
7680
}

SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedGlobalFunctionsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function getNotFullyQualifiedMessage(): string
1515

1616
protected function isCaseSensitive(): bool
1717
{
18-
return true;
18+
return false;
1919
}
2020

2121
protected function isValidType(ReferencedName $name): bool

tests/Sniffs/Namespaces/FullyQualifiedGlobalConstantsSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testErrors(): void
1818
$this->assertSame(2, $report->getErrorCount());
1919

2020
$this->assertSniffError($report, 17, FullyQualifiedGlobalConstantsSniff::CODE_NON_FULLY_QUALIFIED, 'Constant PHP_VERSION should be referenced via a fully qualified name.');
21-
$this->assertSniffError($report, 28, FullyQualifiedGlobalConstantsSniff::CODE_NON_FULLY_QUALIFIED, 'Constant PHP_OS should be referenced via a fully qualified name.');
21+
$this->assertSniffError($report, 31, FullyQualifiedGlobalConstantsSniff::CODE_NON_FULLY_QUALIFIED, 'Constant PHP_OS should be referenced via a fully qualified name.');
2222

2323
$this->assertAllFixedInFile($report);
2424
}

tests/Sniffs/Namespaces/FullyQualifiedGlobalFunctionsSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testErrors(): void
1818
$this->assertSame(2, $report->getErrorCount());
1919

2020
$this->assertSniffError($report, 17, FullyQualifiedGlobalFunctionsSniff::CODE_NON_FULLY_QUALIFIED, 'Function min() should be referenced via a fully qualified name.');
21-
$this->assertSniffError($report, 28, FullyQualifiedGlobalFunctionsSniff::CODE_NON_FULLY_QUALIFIED, 'Function max() should be referenced via a fully qualified name.');
21+
$this->assertSniffError($report, 31, FullyQualifiedGlobalFunctionsSniff::CODE_NON_FULLY_QUALIFIED, 'Function MaX() should be referenced via a fully qualified name.');
2222

2323
$this->assertAllFixedInFile($report);
2424
}

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalConstantsErrors.fixed.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public function __construct()
2323

2424
}
2525

26-
namespace {
26+
namespace
27+
{
28+
29+
use const PHP_OS;
2730

2831
\PHP_OS;
2932

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalConstantsErrors.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public function __construct()
2323

2424
}
2525

26-
namespace {
26+
namespace
27+
{
28+
29+
use const PHP_OS;
2730

2831
PHP_OS;
2932

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalFunctionsErrors.fixed.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ public function __construct()
2323

2424
}
2525

26-
namespace {
26+
namespace
27+
{
28+
29+
use function MAX;
2730

28-
\max(10, 100);
31+
\MaX(10, 100);
2932

3033
}

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalFunctionsErrors.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ public function __construct()
2323

2424
}
2525

26-
namespace {
26+
namespace
27+
{
28+
29+
use function MAX;
2730

28-
max(10, 100);
31+
MaX(10, 100);
2932

3033
}

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalFunctionsExcludeErrors.fixed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Boo implements Doo
1414
{
1515
public function __construct()
1616
{
17-
min(10, 100);
17+
mIn(10, 100);
1818
\FullyQualified\hehe();
1919
Qualified\haha();
2020
hihi();

tests/Sniffs/Namespaces/data/fullyQualifiedGlobalFunctionsExcludeErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Boo implements Doo
1414
{
1515
public function __construct()
1616
{
17-
min(10, 100);
17+
mIn(10, 100);
1818
\FullyQualified\hehe();
1919
Qualified\haha();
2020
hihi();

0 commit comments

Comments
 (0)