Skip to content

Commit 5767c24

Browse files
committed
Fix up NoInlineFullyQualifiedClassNameSniff
1 parent f50d70b commit 5767c24

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

PSR2R/Sniffs/Namespaces/NoInlineFullyQualifiedClassNameSniff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,18 @@ protected function checkUseForExtends(File $phpcsFile, int $nextIndex): void {
236236
$startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $nextIndex + 1, null, true);
237237
$endIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $endIndex - 1, null, true);
238238

239-
if (!$this->contains($phpcsFile, T_NS_SEPARATOR, $startIndex, $endIndex)) {
239+
// Check for namespace separator (PHP < 8) or fully qualified name token (PHP 8+)
240+
$hasNamespace = $this->contains($phpcsFile, T_NS_SEPARATOR, $startIndex, $endIndex);
241+
if (!$hasNamespace && defined('T_NAME_FULLY_QUALIFIED')) {
242+
$hasNamespace = $this->contains($phpcsFile, T_NAME_FULLY_QUALIFIED, $startIndex, $endIndex);
243+
}
244+
if (!$hasNamespace) {
240245
return;
241246
}
242247

243248
$extractedUseStatements = $this->extractUseStatements($phpcsFile, $startIndex, $endIndex);
244249
foreach ($extractedUseStatements as $extractedUseStatement) {
245-
if (strpos($extractedUseStatement['statement'], '\\') === false) {
250+
if (!str_contains($extractedUseStatement['statement'], '\\')) {
246251
continue;
247252
}
248253

@@ -264,6 +269,8 @@ protected function checkUseForExtends(File $phpcsFile, int $nextIndex): void {
264269

265270
if ($addedUseStatement['alias'] !== null) {
266271
$phpcsFile->fixer->replaceToken($extractedUseStatement['end'], $addedUseStatement['alias']);
272+
} else {
273+
$phpcsFile->fixer->replaceToken($extractedUseStatement['end'], $addedUseStatement['shortName']);
267274
}
268275

269276
/** @noinspection DisconnectedForeachInstructionInspection */
@@ -466,6 +473,8 @@ protected function checkUseForImplements(File $phpcsFile, int $nextIndex): void
466473

467474
if ($addedUseStatement['alias'] !== null) {
468475
$phpcsFile->fixer->replaceToken($extractedUseStatement['end'], $addedUseStatement['alias']);
476+
} else {
477+
$phpcsFile->fixer->replaceToken($extractedUseStatement['end'], $addedUseStatement['shortName']);
469478
}
470479

471480
/** @noinspection DisconnectedForeachInstructionInspection */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
* For full license information, please view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
namespace PSR2R\Test\PSR2R\Sniffs\Namespaces;
9+
10+
use PSR2R\Sniffs\Namespaces\NoInlineFullyQualifiedClassNameSniff;
11+
use PSR2R\Test\TestCase;
12+
13+
class NoInlineFullyQualifiedClassNameSniffTest extends TestCase
14+
{
15+
/**
16+
* @return void
17+
*/
18+
public function testNoInlineFullyQualifiedClassNameSniffer(): void
19+
{
20+
$this->assertSnifferFindsErrors(new NoInlineFullyQualifiedClassNameSniff(), 11);
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function testNoInlineFullyQualifiedClassNameFixer(): void
27+
{
28+
$this->assertSnifferCanFixErrors(new NoInlineFullyQualifiedClassNameSniff());
29+
}
30+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Test;
4+
5+
use Some\Base\Exception;
6+
use Psr\Log\LoggerInterface;
7+
use Another\SerializableInterface;
8+
use Some\CountableInterface;
9+
10+
// Anonymous class implementing interface with FQCN
11+
class AnonymousClassImplements {
12+
protected function test(): void {
13+
$logger = new class implements LoggerInterface {
14+
public function emergency($message, array $context = []): void {}
15+
public function alert($message, array $context = []): void {}
16+
public function critical($message, array $context = []): void {}
17+
public function error($message, array $context = []): void {}
18+
public function warning($message, array $context = []): void {}
19+
public function notice($message, array $context = []): void {}
20+
public function info($message, array $context = []): void {}
21+
public function debug($message, array $context = []): void {}
22+
public function log($level, $message, array $context = []): void {}
23+
};
24+
}
25+
}
26+
27+
// Anonymous class extending class with FQCN
28+
class AnonymousClassExtends {
29+
protected function test(): void {
30+
$obj = new class extends Exception {
31+
};
32+
}
33+
}
34+
35+
// Anonymous class implementing multiple interfaces with FQCN
36+
class AnonymousClassMultipleInterfaces {
37+
protected function test(): void {
38+
$obj = new class implements LoggerInterface, SerializableInterface {
39+
public function emergency($message, array $context = []): void {}
40+
public function alert($message, array $context = []): void {}
41+
public function critical($message, array $context = []): void {}
42+
public function error($message, array $context = []): void {}
43+
public function warning($message, array $context = []): void {}
44+
public function notice($message, array $context = []): void {}
45+
public function info($message, array $context = []): void {}
46+
public function debug($message, array $context = []): void {}
47+
public function log($level, $message, array $context = []): void {}
48+
public function serialize(): string { return ''; }
49+
public function unserialize($data): void {}
50+
};
51+
}
52+
}
53+
54+
// Regular class implementing interface with FQCN
55+
class RegularClassImplements implements CountableInterface {
56+
public function count(): int {
57+
return 0;
58+
}
59+
}
60+
61+
// Regular class extending class with FQCN
62+
class RegularClassExtends extends Exception {
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Test;
4+
5+
// Anonymous class implementing interface with FQCN
6+
class AnonymousClassImplements {
7+
protected function test(): void {
8+
$logger = new class implements \Psr\Log\LoggerInterface {
9+
public function emergency($message, array $context = []): void {}
10+
public function alert($message, array $context = []): void {}
11+
public function critical($message, array $context = []): void {}
12+
public function error($message, array $context = []): void {}
13+
public function warning($message, array $context = []): void {}
14+
public function notice($message, array $context = []): void {}
15+
public function info($message, array $context = []): void {}
16+
public function debug($message, array $context = []): void {}
17+
public function log($level, $message, array $context = []): void {}
18+
};
19+
}
20+
}
21+
22+
// Anonymous class extending class with FQCN
23+
class AnonymousClassExtends {
24+
protected function test(): void {
25+
$obj = new class extends \Some\Base\Exception {
26+
};
27+
}
28+
}
29+
30+
// Anonymous class implementing multiple interfaces with FQCN
31+
class AnonymousClassMultipleInterfaces {
32+
protected function test(): void {
33+
$obj = new class implements \Psr\Log\LoggerInterface, \Another\SerializableInterface {
34+
public function emergency($message, array $context = []): void {}
35+
public function alert($message, array $context = []): void {}
36+
public function critical($message, array $context = []): void {}
37+
public function error($message, array $context = []): void {}
38+
public function warning($message, array $context = []): void {}
39+
public function notice($message, array $context = []): void {}
40+
public function info($message, array $context = []): void {}
41+
public function debug($message, array $context = []): void {}
42+
public function log($level, $message, array $context = []): void {}
43+
public function serialize(): string { return ''; }
44+
public function unserialize($data): void {}
45+
};
46+
}
47+
}
48+
49+
// Regular class implementing interface with FQCN
50+
class RegularClassImplements implements \Some\CountableInterface {
51+
public function count(): int {
52+
return 0;
53+
}
54+
}
55+
56+
// Regular class extending class with FQCN
57+
class RegularClassExtends extends \Some\Base\Exception {
58+
}

0 commit comments

Comments
 (0)