Skip to content

Commit

Permalink
ReferenceUsedNamesOnlySniff: fixed support for functions and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jan 4, 2018
1 parent df7bed1 commit 998b5e9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
11 changes: 8 additions & 3 deletions SlevomatCodingStandard/Helpers/UseStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function getPointer(): int
return $this->usePointer;
}

public function getType(): string
{
return $this->type;
}

public function isConstant(): bool
{
return $this->type === self::TYPE_CONSTANT;
Expand All @@ -100,13 +105,13 @@ public function hasSameType(self $that): bool
return $this->type === $that->type;
}

public function getTypeName(): ?string
public static function getTypeName(string $type): ?string
{
if ($this->isConstant()) {
if ($type === self::TYPE_CONSTANT) {
return 'const';
}

if ($this->isFunction()) {
if ($type === self::TYPE_FUNCTION) {
return 'function';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function fixAlphabeticalOrder(
$phpcsFile->fixer->addContent($firstUseStatement->getPointer(), implode($phpcsFile->eolChar, array_map(function (UseStatement $useStatement): string {
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName());

$useTypeName = $useStatement->getTypeName();
$useTypeName = UseStatement::getTypeName($useStatement->getType());
$useTypeFormatted = $useTypeName !== null ? sprintf('%s ', $useTypeName) : '';

if ($unqualifiedName === $useStatement->getNameAsReferencedInFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\StringHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use SlevomatCodingStandard\Helpers\UseStatement;
use SlevomatCodingStandard\Helpers\UseStatementHelper;

class ReferenceUsedNamesOnlySniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expand Down Expand Up @@ -218,7 +219,9 @@ function (ReferencedName $referencedName): string {

$canBeFixed = true;
foreach ($useStatements as $useStatement) {
if ($useStatement->getFullyQualifiedTypeName() !== $canonicalName
if (
$useStatement->getType() === $referencedName->getType()
&& $useStatement->getFullyQualifiedTypeName() !== $canonicalName
&& ($useStatement->getCanonicalNameAsReferencedInFile() === $canonicalNameToReference || array_key_exists($canonicalNameToReference, $definedClassesIndex))
) {
$canBeFixed = false;
Expand Down Expand Up @@ -251,7 +254,7 @@ function (ReferencedName $referencedName): string {

$alreadyUsed = false;
foreach ($useStatements as $useStatement) {
if ($useStatement->getFullyQualifiedTypeName() === $canonicalName) {
if ($useStatement->getType() === $referencedName->getType() && $useStatement->getFullyQualifiedTypeName() === $canonicalName) {
$nameToReference = $useStatement->getNameAsReferencedInFile();
$alreadyUsed = true;
break;
Expand All @@ -261,8 +264,11 @@ function (ReferencedName $referencedName): string {
$phpcsFile->fixer->addContent($referencedName->getStartPointer(), $nameToReference);

if (!$alreadyUsed) {
$useTypeName = UseStatement::getTypeName($referencedName->getType());
$useTypeFormatted = $useTypeName !== null ? sprintf('%s ', $useTypeName) : '';

$phpcsFile->fixer->addNewline($useStatementPlacePointer);
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s;', $canonicalName));
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s%s;', $useTypeFormatted, $canonicalName));
}

$phpcsFile->fixer->endChangeset();
Expand Down
24 changes: 23 additions & 1 deletion tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testCreatingNewObjectViaFullyQualifiedName(array $ignoredNames):
* @dataProvider dataIgnoredNamesForIrrelevantTests
* @param string[] $ignoredNames
*/
public function testReferencingConstantViaFullyQualifiedName(array $ignoredNames): void
public function testReferencingClassConstantViaFullyQualifiedName(array $ignoredNames): void
{
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php', [
'ignoredNames' => $ignoredNames,
Expand All @@ -78,6 +78,28 @@ public function testReferencingConstantViaFullyQualifiedName(array $ignoredNames
);
}

public function testReferencingConstantViaFullyQualifiedName(): void
{
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php');
$this->assertSniffError(
$report,
16,
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
'\Boo\FOO'
);
}

public function testReferencingFunctionViaFullyQualifiedName(): void
{
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php');
$this->assertSniffError(
$report,
17,
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
'\Boo\foo'
);
}

/**
* @dataProvider dataIgnoredNamesForIrrelevantTests
* @param string[] $ignoredNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Some\ConstantClass;
use Foo\SomeError;
use Nette\ObjectPrototype;
use function Boo\foo;
use const Boo\FOO;

class Bar extends \ObjectPrototype implements Iterator
{
Expand All @@ -19,6 +21,8 @@ public function bar()
new \Some\CommonException();
new \Exception();
new ObjectPrototype();
foo();
FOO;
}

public function foo(DoctrineColumn $doctrineColumn): ObjectPrototype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function bar()
new \Some\CommonException();
new \Exception();
new \Nette\ObjectPrototype();
\Boo\foo();
\Boo\FOO;
}

public function foo(\Doctrine\ORM\Mapping\Column $doctrineColumn): \Nette\ObjectPrototype
Expand Down
2 changes: 2 additions & 0 deletions tests/Sniffs/Namespaces/data/shouldBeInUseStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function bar()
new \Some\CommonException();
new \Exception();
new \Nette\ObjectPrototype();
\Boo\FOO;
\Boo\foo();
}

}

0 comments on commit 998b5e9

Please sign in to comment.