Skip to content

Commit

Permalink
Add include property to AbstractFullyQualifiedGlobalReference sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatsche authored and kukulich committed Mar 26, 2020
1 parent 5dfe52b commit 7376b83
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Checks that class/trait/interface members are in the correct order.
Sniff provides the following settings:

* `groups`: order of groups. Use multiple groups in one `<element value="">` to not differentiate among them. You can use specific groups or shortcuts.
* `enableFinalMethods`: enables groups for `final` methods
* `enableFinalMethods`: enables groups for `final` methods

**List of supported groups**:
uses,
Expand Down Expand Up @@ -901,6 +901,7 @@ All references to global constants must be referenced via a fully qualified name

Sniff provides the following settings:

* `include`: list of global constants that must be referenced via FQN. If not set all constants are considered.
* `exclude`: list of global constants that are allowed not to be referenced via FQN.

#### SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions 🔧
Expand All @@ -909,6 +910,7 @@ All references to global functions must be referenced via a fully qualified name

Sniff provides the following settings:

* `include`: list of global functions that must be referenced via FQN. If not set all functions are considered.
* `exclude`: list of global functions that are allowed not to be referenced via FQN.

#### SlevomatCodingStandard.Namespaces.MultipleUsesPerLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ abstract class AbstractFullyQualifiedGlobalReference implements Sniff
/** @var string[] */
public $exclude = [];

/** @var string[] */
public $include = [];

/** @var string[]|null */
private $normalizedExclude;

/** @var string[]|null */
private $normalizedInclude;

abstract protected function getNotFullyQualifiedMessage(): string;

abstract protected function isCaseSensitive(): bool;
Expand Down Expand Up @@ -63,6 +69,7 @@ public function process(File $phpcsFile, $openTagPointer): void

$namespacePointers = NamespaceHelper::getAllNamespacesPointers($phpcsFile);
$referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $openTagPointer);
$include = array_flip($this->getNormalizedInclude());
$exclude = array_flip($this->getNormalizedExclude());

foreach ($referencedNames as $referencedName) {
Expand Down Expand Up @@ -96,6 +103,10 @@ public function process(File $phpcsFile, $openTagPointer): void
}
}

if ($include !== [] && !array_key_exists($canonicalName, $include)) {
continue;
}

if (array_key_exists($canonicalName, $exclude)) {
continue;
}
Expand All @@ -117,17 +128,37 @@ public function process(File $phpcsFile, $openTagPointer): void
private function getNormalizedExclude(): array
{
if ($this->normalizedExclude === null) {
$exclude = SniffSettingsHelper::normalizeArray($this->exclude);
$this->normalizedExclude = $this->normalizeNames($this->exclude);
}
return $this->normalizedExclude;
}

if (!$this->isCaseSensitive()) {
$exclude = array_map(static function (string $name): string {
return strtolower($name);
}, $exclude);
}
/**
* @return string[]
*/
private function getNormalizedInclude(): array
{
if ($this->normalizedInclude === null) {
$this->normalizedInclude = $this->normalizeNames($this->include);
}
return $this->normalizedInclude;
}

/**
* @param string[] $names
* @return string[]
*/
private function normalizeNames(array $names): array
{
$names = SniffSettingsHelper::normalizeArray($names);

$this->normalizedExclude = $exclude;
if (!$this->isCaseSensitive()) {
$names = array_map(static function (string $name): string {
return strtolower($name);
}, $names);
}
return $this->normalizedExclude;

return $names;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testErrors(): void

public function testExcludeErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalConstantsExcludeErrors.php', [
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalConstantsIncludeExcludeErrors.php', [
'exclude' => ['PHP_VERSION'],
]);

Expand All @@ -44,4 +44,17 @@ public function testExcludeErrors(): void
self::assertAllFixedInFile($report);
}

public function testIncludeErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalConstantsIncludeExcludeErrors.php', [
'include' => ['PHP_OS'],
]);

self::assertSame(1, $report->getErrorCount());

self::assertSniffError($report, 28, FullyQualifiedGlobalConstantsSniff::CODE_NON_FULLY_QUALIFIED, 'Constant PHP_OS should be referenced via a fully qualified name.');

self::assertAllFixedInFile($report);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testErrors(): void

public function testExcludeErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalFunctionsExcludeErrors.php', [
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalFunctionsIncludeExcludeErrors.php', [
'exclude' => ['min'],
]);

Expand All @@ -44,4 +44,17 @@ public function testExcludeErrors(): void
self::assertAllFixedInFile($report);
}

public function testIncludeErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedGlobalFunctionsIncludeExcludeErrors.php', [
'include' => ['max'],
]);

self::assertSame(1, $report->getErrorCount());

self::assertSniffError($report, 28, FullyQualifiedGlobalFunctionsSniff::CODE_NON_FULLY_QUALIFIED, 'Function max() should be referenced via a fully qualified name.');

self::assertAllFixedInFile($report);
}

}

0 comments on commit 7376b83

Please sign in to comment.