Skip to content

Commit

Permalink
ReferenceUsedNamesOnlySniff: fixed "allowFullyQualifiedNameForCollidi…
Browse files Browse the repository at this point in the history
…ngFunctions" and "allowFullyQualifiedNameForCollidingConstants"
  • Loading branch information
kukulich committed Jan 8, 2018
1 parent 5309879 commit 52f3c79
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,21 @@ function (ReferencedName $referencedName): string {
$isGlobalFunctionFallback = $referencedName->isFunction() && $isGlobalFallback;
$isGlobalConstantFallback = $referencedName->isConstant() && $isGlobalFallback;

if ($referencedName->isClass() && $this->allowFullyQualifiedNameForCollidingClasses) {
$lowerCasedUnqualifiedClassName = strtolower($unqualifiedName);
if (isset($classReferencesIndex[$lowerCasedUnqualifiedClassName]) || array_key_exists($lowerCasedUnqualifiedClassName, $definedClassesIndex)) {
continue;
}
} elseif ($referencedName->isFunction() && $this->allowFullyQualifiedNameForCollidingFunctions) {
$lowerCasedUnqualifiedFunctionName = strtolower($unqualifiedName);
if (isset($functionReferencesIndex[$lowerCasedUnqualifiedFunctionName]) || array_key_exists($lowerCasedUnqualifiedFunctionName, $definedFunctionsIndex)) {
continue;
}
} elseif ($referencedName->isConstant() && $this->allowFullyQualifiedNameForCollidingConstants) {
if (isset($constantReferencesIndex[$unqualifiedName]) || array_key_exists($unqualifiedName, $definedConstantsIndex)) {
continue;
if ($isFullyQualified) {
if ($referencedName->isClass() && $this->allowFullyQualifiedNameForCollidingClasses) {
$lowerCasedUnqualifiedClassName = strtolower($unqualifiedName);
if (isset($classReferencesIndex[$lowerCasedUnqualifiedClassName]) || array_key_exists($lowerCasedUnqualifiedClassName, $definedClassesIndex)) {
continue;
}
} elseif ($referencedName->isFunction() && $this->allowFullyQualifiedNameForCollidingFunctions) {
$lowerCasedUnqualifiedFunctionName = strtolower($unqualifiedName);
if (isset($functionReferencesIndex[$lowerCasedUnqualifiedFunctionName]) || array_key_exists($lowerCasedUnqualifiedFunctionName, $definedFunctionsIndex)) {
continue;
}
} elseif ($referencedName->isConstant() && $this->allowFullyQualifiedNameForCollidingConstants) {
if (isset($constantReferencesIndex[$unqualifiedName]) || array_key_exists($unqualifiedName, $definedConstantsIndex)) {
continue;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,19 @@ public function testCollidingFullyQualifiedConstantNameDisallowed(): void
$this->assertSniffError($report, 12, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
}

public function testReferencingGlobalFunctionViaFallbackErrorsWithMoreComplexSettings(): void
{
$report = $this->checkFile(
__DIR__ . '/data/referencingGlobalFunctionViaFallbackErrorsWithMoreComplexSettings.php',
[
'allowFallbackGlobalFunctions' => false,
'allowFullyQualifiedNameForCollidingFunctions' => true,
'allowFullyQualifiedGlobalFunctions' => true,
]
);

$this->assertSame(1, $report->getErrorCount());
$this->assertSniffError($report, 17, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FALLBACK_GLOBAL_NAME);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace FooNamespace;

function php_version()
{
return '6.0.0';
}

class Foo
{

public function boo()
{
\php_version();
\max(1, 3);
min(1, 3);
}

}

0 comments on commit 52f3c79

Please sign in to comment.