Skip to content

Commit d45ad1e

Browse files
committed
Improved FullyQualifiedClassNameAfterKeywordSniff for T_USE
1 parent 3b7bbc2 commit d45ad1e

File tree

4 files changed

+152
-4
lines changed

4 files changed

+152
-4
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedClassNameAfterKeywordSniff.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ public function register(): array
5656
*/
5757
public function process(\PHP_CodeSniffer_File $phpcsFile, $keywordPointer)
5858
{
59+
$tokens = $phpcsFile->getTokens();
60+
5961
$nameStartPointer = TokenHelper::findNextEffective($phpcsFile, $keywordPointer + 1);
60-
$this->checkReferencedName($phpcsFile, $keywordPointer, $nameStartPointer);
62+
if (!in_array($tokens[$nameStartPointer]['code'], TokenHelper::$nameTokenCodes, true)) {
63+
return;
64+
}
6165

62-
$tokens = $phpcsFile->getTokens();
63-
if ($tokens[$keywordPointer]['code'] === T_IMPLEMENTS) {
64-
$possibleCommaPointer = $keywordPointer + 1;
66+
$possibleCommaPointer = $this->checkReferencedName($phpcsFile, $keywordPointer, $nameStartPointer);
67+
68+
if (in_array($tokens[$keywordPointer]['code'], [T_IMPLEMENTS, T_USE], true)) {
6569
while (true) {
6670
$possibleCommaPointer = TokenHelper::findNextExcluding($phpcsFile, array_merge(TokenHelper::$nameTokenCodes, [T_WHITESPACE]), $possibleCommaPointer);
6771
if ($possibleCommaPointer !== null) {
@@ -81,6 +85,20 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $keywordPointer)
8185
private function checkReferencedName(\PHP_CodeSniffer_File $phpcsFile, int $keywordPointer, int $nameStartPointer): int
8286
{
8387
$tokens = $phpcsFile->getTokens();
88+
89+
if ($tokens[$keywordPointer]['code'] === T_USE) {
90+
$conditions = $tokens[$keywordPointer]['conditions'];
91+
92+
if (count($conditions) === 0) {
93+
return $nameStartPointer + 1;
94+
}
95+
96+
$lastCondition = array_values($conditions)[count($conditions) - 1];
97+
if ($lastCondition === T_NAMESPACE) {
98+
return $nameStartPointer + 1;
99+
}
100+
}
101+
84102
$nameStartToken = $tokens[$nameStartPointer];
85103
$endPointer = ReferencedNameHelper::getReferencedNameEndPointer($phpcsFile, $nameStartPointer);
86104
if ($nameStartToken['code'] !== T_NS_SEPARATOR) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Namespaces;
4+
5+
class FullyQualifiedClassNameAfterKeywordSniffUseTest extends \SlevomatCodingStandard\Sniffs\TestCase
6+
{
7+
8+
protected function getSniffClassName(): string
9+
{
10+
return FullyQualifiedClassNameAfterKeywordSniff::class;
11+
}
12+
13+
private function getFileReport(): \PHP_CodeSniffer_File
14+
{
15+
return $this->checkFile(
16+
__DIR__ . '/data/fullyQualifiedUse.php',
17+
['keywordsToCheck' => ['T_USE']]
18+
);
19+
}
20+
21+
public function testIgnoreUseInClosure()
22+
{
23+
$this->assertNoSniffError($this->getFileReport(), 34);
24+
}
25+
26+
public function testIgnoreUseInNamespace()
27+
{
28+
$this->assertNoSniffError($this->getFileReport(), 5);
29+
}
30+
31+
public function testIgnoreUseInNamespaceWithParenthesis()
32+
{
33+
$this->assertNoSniffErrorInFile($this->checkFile(
34+
__DIR__ . '/data/fullyQualifiedUseNamespaceWithParenthesis.php',
35+
['keywordsToCheck' => ['T_USE']]
36+
));
37+
}
38+
39+
public function testNonFullyQualifiedUse()
40+
{
41+
$this->assertSniffError(
42+
$this->getFileReport(),
43+
14,
44+
FullyQualifiedClassNameAfterKeywordSniff::getErrorCode('use'),
45+
'Dolor in use'
46+
);
47+
}
48+
49+
public function testFullyQualifiedUse()
50+
{
51+
$this->assertNoSniffError($this->getFileReport(), 9);
52+
}
53+
54+
public function testMultipleFullyQualifiedUse()
55+
{
56+
$this->assertNoSniffError($this->getFileReport(), 19);
57+
}
58+
59+
public function testMultipleUseWithFirstWrong()
60+
{
61+
$this->assertSniffError(
62+
$this->getFileReport(),
63+
24,
64+
FullyQualifiedClassNameAfterKeywordSniff::getErrorCode('use'),
65+
'Dolor in use'
66+
);
67+
}
68+
69+
public function testMultipleUseWithSecondAndThirdWrong()
70+
{
71+
$report = $this->getFileReport();
72+
$this->assertSniffError(
73+
$report,
74+
29,
75+
FullyQualifiedClassNameAfterKeywordSniff::getErrorCode('use'),
76+
'Amet in use'
77+
);
78+
$this->assertSniffError(
79+
$report,
80+
29,
81+
FullyQualifiedClassNameAfterKeywordSniff::getErrorCode('use'),
82+
'Omega in use'
83+
);
84+
}
85+
86+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Foo;
4+
5+
use Dolor;
6+
7+
class Foo
8+
{
9+
use \SomeClass, \Some\OtherClass;
10+
}
11+
12+
class Ipsum
13+
{
14+
use /*\Omega, */ Dolor;
15+
}
16+
17+
class Lorem
18+
{
19+
use \Dolor, \Amet;
20+
}
21+
22+
class LoremConsecteur
23+
{
24+
use Dolor, \Amet;
25+
}
26+
27+
class Bar
28+
{
29+
use \Dolor, Amet, Omega;
30+
}
31+
32+
$foo = null;
33+
34+
$boo = function () use ($foo) {
35+
36+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Foo
4+
{
5+
6+
use Dolor;
7+
8+
}

0 commit comments

Comments
 (0)