Skip to content

Commit 998b5e9

Browse files
committed
ReferenceUsedNamesOnlySniff: fixed support for functions and constants
1 parent df7bed1 commit 998b5e9

File tree

7 files changed

+49
-8
lines changed

7 files changed

+49
-8
lines changed

SlevomatCodingStandard/Helpers/UseStatement.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function getPointer(): int
8585
return $this->usePointer;
8686
}
8787

88+
public function getType(): string
89+
{
90+
return $this->type;
91+
}
92+
8893
public function isConstant(): bool
8994
{
9095
return $this->type === self::TYPE_CONSTANT;
@@ -100,13 +105,13 @@ public function hasSameType(self $that): bool
100105
return $this->type === $that->type;
101106
}
102107

103-
public function getTypeName(): ?string
108+
public static function getTypeName(string $type): ?string
104109
{
105-
if ($this->isConstant()) {
110+
if ($type === self::TYPE_CONSTANT) {
106111
return 'const';
107112
}
108113

109-
if ($this->isFunction()) {
114+
if ($type === self::TYPE_FUNCTION) {
110115
return 'function';
111116
}
112117

SlevomatCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function fixAlphabeticalOrder(
8787
$phpcsFile->fixer->addContent($firstUseStatement->getPointer(), implode($phpcsFile->eolChar, array_map(function (UseStatement $useStatement): string {
8888
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName());
8989

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

9393
if ($unqualifiedName === $useStatement->getNameAsReferencedInFile()) {

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
1010
use SlevomatCodingStandard\Helpers\StringHelper;
1111
use SlevomatCodingStandard\Helpers\TokenHelper;
12+
use SlevomatCodingStandard\Helpers\UseStatement;
1213
use SlevomatCodingStandard\Helpers\UseStatementHelper;
1314

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

219220
$canBeFixed = true;
220221
foreach ($useStatements as $useStatement) {
221-
if ($useStatement->getFullyQualifiedTypeName() !== $canonicalName
222+
if (
223+
$useStatement->getType() === $referencedName->getType()
224+
&& $useStatement->getFullyQualifiedTypeName() !== $canonicalName
222225
&& ($useStatement->getCanonicalNameAsReferencedInFile() === $canonicalNameToReference || array_key_exists($canonicalNameToReference, $definedClassesIndex))
223226
) {
224227
$canBeFixed = false;
@@ -251,7 +254,7 @@ function (ReferencedName $referencedName): string {
251254

252255
$alreadyUsed = false;
253256
foreach ($useStatements as $useStatement) {
254-
if ($useStatement->getFullyQualifiedTypeName() === $canonicalName) {
257+
if ($useStatement->getType() === $referencedName->getType() && $useStatement->getFullyQualifiedTypeName() === $canonicalName) {
255258
$nameToReference = $useStatement->getNameAsReferencedInFile();
256259
$alreadyUsed = true;
257260
break;
@@ -261,8 +264,11 @@ function (ReferencedName $referencedName): string {
261264
$phpcsFile->fixer->addContent($referencedName->getStartPointer(), $nameToReference);
262265

263266
if (!$alreadyUsed) {
267+
$useTypeName = UseStatement::getTypeName($referencedName->getType());
268+
$useTypeFormatted = $useTypeName !== null ? sprintf('%s ', $useTypeName) : '';
269+
264270
$phpcsFile->fixer->addNewline($useStatementPlacePointer);
265-
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s;', $canonicalName));
271+
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s%s;', $useTypeFormatted, $canonicalName));
266272
}
267273

268274
$phpcsFile->fixer->endChangeset();

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testCreatingNewObjectViaFullyQualifiedName(array $ignoredNames):
6565
* @dataProvider dataIgnoredNamesForIrrelevantTests
6666
* @param string[] $ignoredNames
6767
*/
68-
public function testReferencingConstantViaFullyQualifiedName(array $ignoredNames): void
68+
public function testReferencingClassConstantViaFullyQualifiedName(array $ignoredNames): void
6969
{
7070
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php', [
7171
'ignoredNames' => $ignoredNames,
@@ -78,6 +78,28 @@ public function testReferencingConstantViaFullyQualifiedName(array $ignoredNames
7878
);
7979
}
8080

81+
public function testReferencingConstantViaFullyQualifiedName(): void
82+
{
83+
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php');
84+
$this->assertSniffError(
85+
$report,
86+
16,
87+
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
88+
'\Boo\FOO'
89+
);
90+
}
91+
92+
public function testReferencingFunctionViaFullyQualifiedName(): void
93+
{
94+
$report = $this->checkFile(__DIR__ . '/data/shouldBeInUseStatement.php');
95+
$this->assertSniffError(
96+
$report,
97+
17,
98+
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
99+
'\Boo\foo'
100+
);
101+
}
102+
81103
/**
82104
* @dataProvider dataIgnoredNamesForIrrelevantTests
83105
* @param string[] $ignoredNames

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Some\ConstantClass;
88
use Foo\SomeError;
99
use Nette\ObjectPrototype;
10+
use function Boo\foo;
11+
use const Boo\FOO;
1012

1113
class Bar extends \ObjectPrototype implements Iterator
1214
{
@@ -19,6 +21,8 @@ public function bar()
1921
new \Some\CommonException();
2022
new \Exception();
2123
new ObjectPrototype();
24+
foo();
25+
FOO;
2226
}
2327

2428
public function foo(DoctrineColumn $doctrineColumn): ObjectPrototype

tests/Sniffs/Namespaces/data/fixableReferenceViaFullyQualifiedName.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function bar()
1515
new \Some\CommonException();
1616
new \Exception();
1717
new \Nette\ObjectPrototype();
18+
\Boo\foo();
19+
\Boo\FOO;
1820
}
1921

2022
public function foo(\Doctrine\ORM\Mapping\Column $doctrineColumn): \Nette\ObjectPrototype

tests/Sniffs/Namespaces/data/shouldBeInUseStatement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function bar()
1313
new \Some\CommonException();
1414
new \Exception();
1515
new \Nette\ObjectPrototype();
16+
\Boo\FOO;
17+
\Boo\foo();
1618
}
1719

1820
}

0 commit comments

Comments
 (0)