File tree Expand file tree Collapse file tree 6 files changed +126
-1
lines changed
custom-standards/Flyeralarm/Sniffs/Classes Expand file tree Collapse file tree 6 files changed +126
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" ?>
2+ <ruleset name =" FLYERALARM Coding Guidelines" >
3+ <description >A custom coding standard for FLYERALARM</description >
4+ <rule ref =" PSR2" />
5+ <rule ref =" Generic.Formatting.SpaceAfterCast" />
6+ <rule ref =" Generic.Arrays.DisallowLongArraySyntax.Found" />
7+ <rule ref =" Generic.PHP.DisallowShortOpenTag" />
8+ <rule ref =" Generic.NamingConventions.UpperCaseConstantName" />
9+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php" />
10+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/ForbiddenKeywordsSniff.php" />
11+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/NamespacesSniff.php" />
12+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php" />
13+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php" />
14+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php" />
15+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/Variable/LowerCamelCaseSniff.php" />
16+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/ControlStructures/YodaSniff.php" />
17+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php" />
18+ </ruleset >
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ function processDir($dirPath)
2525 sprintf (
2626 "%s -w -p -s --standard=%s %s " ,
2727 escapeshellcmd (__DIR__ . '/../vendor/bin/phpcs ' ),
28- escapeshellarg (__DIR__ . '/../ ruleset.xml ' ),
28+ escapeshellarg (__DIR__ . '/ruleset.xml ' ),
2929 escapeshellarg ($ dirPath . $ file )
3030 )
3131 );
You can’t perform that action at this time.
0 commit comments