Skip to content

Commit

Permalink
Fixed cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 27, 2018
1 parent ef6876d commit 43a8563
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 55 deletions.
30 changes: 4 additions & 26 deletions SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php
Expand Up @@ -6,7 +6,6 @@
use SlevomatCodingStandard\Helpers\NamespaceHelper;
use SlevomatCodingStandard\Helpers\ReferencedName;
use SlevomatCodingStandard\Helpers\ReferencedNameHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use SlevomatCodingStandard\Helpers\UseStatement;
use SlevomatCodingStandard\Helpers\UseStatementHelper;
Expand All @@ -20,12 +19,6 @@ class UnusedUsesSniff implements \PHP_CodeSniffer\Sniffs\Sniff
/** @var bool */
public $searchAnnotations = false;

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

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

/**
* @return mixed[]
*/
Expand All @@ -36,18 +29,6 @@ public function register(): array
];
}

/**
* @return string[]
*/
private function getIgnoredAnnotationNames(): array
{
if ($this->normalizedIgnoredAnnotationNames === null) {
$this->normalizedIgnoredAnnotationNames = SniffSettingsHelper::normalizeArray($this->ignoredAnnotationNames);
}

return $this->normalizedIgnoredAnnotationNames;
}

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @param \PHP_CodeSniffer\Files\File $phpcsFile
Expand Down Expand Up @@ -107,10 +88,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
$uniqueId = UseStatement::getUniqueId($useStatement->getType(), $nameAsReferencedInFile);

foreach ($annotations as $annotationName => $annotationsByName) {
if (
!in_array($annotationName, $this->getIgnoredAnnotationNames(), true)
&& preg_match('~^@(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=[^a-z\\d]|$)~i', $annotationName, $matches)
) {
if (preg_match('~^@(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=[^a-z\\d]|$)~i', $annotationName, $matches)) {
$usedNames[$uniqueId] = true;

if ($matches[1] !== $nameAsReferencedInFile) {
Expand All @@ -120,7 +98,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
'Case of reference name "%s" and use statement "%s" do not match.',
$matches[1],
$unusedNames[$uniqueId]->getNameAsReferencedInFile()
), $annotation->getStartPointer(), self::CODE_MISMATCHING_CASE);
), $annotation->getPointer(), self::CODE_MISMATCHING_CASE);
}
}
}
Expand Down Expand Up @@ -148,7 +126,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
'Case of reference name "%s" and use statement "%s" do not match.',
$matches[1],
$unusedNames[$uniqueId]->getNameAsReferencedInFile()
), $annotation->getStartPointer(), self::CODE_MISMATCHING_CASE);
), $annotation->getPointer(), self::CODE_MISMATCHING_CASE);
}

/** @var \SlevomatCodingStandard\Helpers\Annotation $annotation */
Expand Down Expand Up @@ -190,7 +168,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
'Case of reference name "%s" and use statement "%s" do not match.',
$matches[1],
$unusedNames[$uniqueId]->getNameAsReferencedInFile()
), $annotation->getStartPointer(), self::CODE_MISMATCHING_CASE);
), $annotation->getPointer(), self::CODE_MISMATCHING_CASE);
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions tests/Sniffs/Namespaces/UnusedUsesSniffTest.php
Expand Up @@ -177,35 +177,6 @@ public function testReportCaseInsensitiveUse(): void
self::assertSniffError($report, 75, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "uuid" and use statement "Uuid" do not match');
}

public function testIgnoredAnnotationNamesAreNotUsed(): void
{
$report = self::checkFile(__DIR__ . '/data/caseInsensitiveUse.php', [
'searchAnnotations' => true,
'ignoredAnnotationNames' => ['@ignore'],
], [UnusedUsesSniff::CODE_UNUSED_USE]);

self::assertSniffError($report, 8, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Ignore is not used in this file.');
}

public function testIgnoredAnnotationNames(): void
{
$report = self::checkFile(__DIR__ . '/data/caseInsensitiveUse.php', [
'searchAnnotations' => true,
'ignoredAnnotationNames' => ['@ignore'],
], [UnusedUsesSniff::CODE_MISMATCHING_CASE]);

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

self::assertSniffError($report, 26, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "bar" and use statement "Bar" do not match');
self::assertSniffError($report, 28, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "BAR" and use statement "Bar" do not match');

self::assertSniffError($report, 30, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "boo" and use statement "Boo" do not match');
self::assertSniffError($report, 31, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "BOO" and use statement "Boo" do not match');
self::assertSniffError($report, 33, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "boo" and use statement "Boo" do not match');

self::assertSniffError($report, 75, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "uuid" and use statement "Uuid" do not match');
}

public function testUsedTrait(): void
{
$report = self::checkFile(__DIR__ . '/data/usedTrait.php');
Expand Down

0 comments on commit 43a8563

Please sign in to comment.