Skip to content

Commit 0eade67

Browse files
author
Michael Nozdrevatykh
committed
Add fully qualified class sniff
1 parent a663524 commit 0eade67

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\Classes;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File;
7+
use RuntimeException;
8+
9+
class FullyQualifiedSniff implements Sniff
10+
{
11+
/**
12+
* @return array
13+
*/
14+
public function register()
15+
{
16+
return array(T_DOUBLE_COLON, T_NEW);
17+
}
18+
19+
/**
20+
* @param File $phpcsFile
21+
* @param int $stackPtr
22+
* @return void
23+
*/
24+
public function process(File $phpcsFile, $stackPtr)
25+
{
26+
$classCall = $this->getClassCall($phpcsFile, $stackPtr);
27+
28+
if (strpos($classCall, '\\') === false) {
29+
return;
30+
}
31+
32+
$phpcsFile->addError(
33+
'Qualifier should be replaced with an import: "%s"',
34+
$stackPtr,
35+
'Found',
36+
[$classCall]
37+
);
38+
}
39+
40+
private function getClassCall(File $phpcsFile, $stackPtr): string
41+
{
42+
$tokens = $phpcsFile->getTokens();
43+
44+
switch ($tokens[$stackPtr]['code']) {
45+
case T_NEW:
46+
return $phpcsFile->getTokensAsString(
47+
$stackPtr,
48+
$phpcsFile->findEndOfStatement($stackPtr) - $stackPtr
49+
);
50+
51+
case T_DOUBLE_COLON:
52+
$classCallStart = $phpcsFile->findStartOfStatement($stackPtr);
53+
54+
return $phpcsFile->getTokensAsString(
55+
$classCallStart,
56+
$stackPtr - $classCallStart
57+
);
58+
}
59+
60+
throw new RuntimeException(sprintf(
61+
'Unknown token type: "%s"',
62+
$tokens[$stackPtr]['type']
63+
));
64+
}
65+
}

ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php"/>
1515
<rule ref="./custom-standards/Flyeralarm/Sniffs/Variable/LowerCamelCaseSniff.php"/>
1616
<rule ref="./custom-standards/Flyeralarm/Sniffs/ControlStructures/YodaSniff.php"/>
17+
<rule ref="./custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php"/>
1718
</ruleset>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// @expectedPass
4+
5+
namespace flyeralarm\Test;
6+
7+
use RuntimeException;
8+
9+
class FullyQualifiedSniff
10+
{
11+
public function a()
12+
{
13+
$className = RuntimeException::class;
14+
$a = new RuntimeException();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// @expectedError Qualifier should be replaced with an import: "new \RuntimeException()"
4+
5+
namespace flyeralarm\Test;
6+
7+
class FullyQualifiedSniff
8+
{
9+
public function a()
10+
{
11+
$a = new \RuntimeException();
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// @expectedError Qualifier should be replaced with an import: "$a = \RuntimeException"
4+
5+
namespace flyeralarm\Test;
6+
7+
class FullyQualifiedSniffStatic
8+
{
9+
public function a()
10+
{
11+
$a = \RuntimeException::class;
12+
}
13+
}

0 commit comments

Comments
 (0)