@@ -16,36 +16,36 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Returns an array of tokens this test wants to listen for. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function register() |
|
25 | - { |
|
26 | - return [T_OPEN_TAG]; |
|
27 | - |
|
28 | - }//end register() |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * Processes this sniff, when one of its tokens is encountered. |
|
33 | - * |
|
34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | - * @param int $stackPtr The position of the current token in |
|
36 | - * the stack passed in $tokens. |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function process(File $phpcsFile, $stackPtr) |
|
41 | - { |
|
42 | - $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
43 | - if ($closeTag === false) { |
|
44 | - $error = 'The PHP open tag does not have a corresponding PHP close tag'; |
|
45 | - $phpcsFile->addError($error, $stackPtr, 'NotFound'); |
|
46 | - } |
|
47 | - |
|
48 | - }//end process() |
|
19 | + /** |
|
20 | + * Returns an array of tokens this test wants to listen for. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function register() |
|
25 | + { |
|
26 | + return [T_OPEN_TAG]; |
|
27 | + |
|
28 | + }//end register() |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * Processes this sniff, when one of its tokens is encountered. |
|
33 | + * |
|
34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | + * @param int $stackPtr The position of the current token in |
|
36 | + * the stack passed in $tokens. |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function process(File $phpcsFile, $stackPtr) |
|
41 | + { |
|
42 | + $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
43 | + if ($closeTag === false) { |
|
44 | + $error = 'The PHP open tag does not have a corresponding PHP close tag'; |
|
45 | + $phpcsFile->addError($error, $stackPtr, 'NotFound'); |
|
46 | + } |
|
47 | + |
|
48 | + }//end process() |
|
49 | 49 | |
50 | 50 | |
51 | 51 | }//end class |
@@ -15,73 +15,73 @@ |
||
15 | 15 | class CharacterBeforePHPOpeningTagSniff implements Sniff |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * List of supported BOM definitions. |
|
20 | - * |
|
21 | - * Use encoding names as keys and hex BOM representations as values. |
|
22 | - * |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - protected $bomDefinitions = [ |
|
26 | - 'UTF-8' => 'efbbbf', |
|
27 | - 'UTF-16 (BE)' => 'feff', |
|
28 | - 'UTF-16 (LE)' => 'fffe', |
|
29 | - ]; |
|
18 | + /** |
|
19 | + * List of supported BOM definitions. |
|
20 | + * |
|
21 | + * Use encoding names as keys and hex BOM representations as values. |
|
22 | + * |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + protected $bomDefinitions = [ |
|
26 | + 'UTF-8' => 'efbbbf', |
|
27 | + 'UTF-16 (BE)' => 'feff', |
|
28 | + 'UTF-16 (LE)' => 'fffe', |
|
29 | + ]; |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * Returns an array of tokens this test wants to listen for. |
|
34 | - * |
|
35 | - * @return array |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - return [T_OPEN_TAG]; |
|
32 | + /** |
|
33 | + * Returns an array of tokens this test wants to listen for. |
|
34 | + * |
|
35 | + * @return array |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + return [T_OPEN_TAG]; |
|
40 | 40 | |
41 | - }//end register() |
|
41 | + }//end register() |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * Processes this sniff, when one of its tokens is encountered. |
|
46 | - * |
|
47 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
48 | - * @param int $stackPtr The position of the current token in |
|
49 | - * the stack passed in $tokens. |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function process(File $phpcsFile, $stackPtr) |
|
54 | - { |
|
55 | - $expected = 0; |
|
56 | - if ($stackPtr > 0) { |
|
57 | - // Allow a byte-order mark. |
|
58 | - $tokens = $phpcsFile->getTokens(); |
|
59 | - foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { |
|
60 | - $bomByteLength = (strlen($expectedBomHex) / 2); |
|
61 | - $htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength)); |
|
62 | - if ($htmlBomHex === $expectedBomHex) { |
|
63 | - $expected++; |
|
64 | - break; |
|
65 | - } |
|
66 | - } |
|
44 | + /** |
|
45 | + * Processes this sniff, when one of its tokens is encountered. |
|
46 | + * |
|
47 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
48 | + * @param int $stackPtr The position of the current token in |
|
49 | + * the stack passed in $tokens. |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function process(File $phpcsFile, $stackPtr) |
|
54 | + { |
|
55 | + $expected = 0; |
|
56 | + if ($stackPtr > 0) { |
|
57 | + // Allow a byte-order mark. |
|
58 | + $tokens = $phpcsFile->getTokens(); |
|
59 | + foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { |
|
60 | + $bomByteLength = (strlen($expectedBomHex) / 2); |
|
61 | + $htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength)); |
|
62 | + if ($htmlBomHex === $expectedBomHex) { |
|
63 | + $expected++; |
|
64 | + break; |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - // Allow a shebang line. |
|
69 | - $tokens = $phpcsFile->getTokens(); |
|
70 | - if (substr($tokens[0]['content'], 0, 2) === '#!') { |
|
71 | - $expected++; |
|
72 | - } |
|
73 | - } |
|
68 | + // Allow a shebang line. |
|
69 | + $tokens = $phpcsFile->getTokens(); |
|
70 | + if (substr($tokens[0]['content'], 0, 2) === '#!') { |
|
71 | + $expected++; |
|
72 | + } |
|
73 | + } |
|
74 | 74 | |
75 | - if ($stackPtr !== $expected) { |
|
76 | - $error = 'The opening PHP tag must be the first content in the file'; |
|
77 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
78 | - } |
|
75 | + if ($stackPtr !== $expected) { |
|
76 | + $error = 'The opening PHP tag must be the first content in the file'; |
|
77 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
78 | + } |
|
79 | 79 | |
80 | - // Skip the rest of the file so we don't pick up additional |
|
81 | - // open tags, typically embedded in HTML. |
|
82 | - return $phpcsFile->numTokens; |
|
80 | + // Skip the rest of the file so we don't pick up additional |
|
81 | + // open tags, typically embedded in HTML. |
|
82 | + return $phpcsFile->numTokens; |
|
83 | 83 | |
84 | - }//end process() |
|
84 | + }//end process() |
|
85 | 85 | |
86 | 86 | |
87 | 87 | }//end class |
@@ -17,126 +17,126 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * Returns an array of tokens this test wants to listen for. |
|
22 | - * |
|
23 | - * @return array |
|
24 | - */ |
|
25 | - public function register() |
|
26 | - { |
|
27 | - return [ |
|
28 | - T_HALT_COMPILER, |
|
29 | - T_ABSTRACT, |
|
30 | - T_ARRAY, |
|
31 | - T_AS, |
|
32 | - T_BREAK, |
|
33 | - T_CALLABLE, |
|
34 | - T_CASE, |
|
35 | - T_CATCH, |
|
36 | - T_CLASS, |
|
37 | - T_CLONE, |
|
38 | - T_CLOSURE, |
|
39 | - T_CONST, |
|
40 | - T_CONTINUE, |
|
41 | - T_DECLARE, |
|
42 | - T_DEFAULT, |
|
43 | - T_DO, |
|
44 | - T_ECHO, |
|
45 | - T_ELSE, |
|
46 | - T_ELSEIF, |
|
47 | - T_EMPTY, |
|
48 | - T_ENDDECLARE, |
|
49 | - T_ENDFOR, |
|
50 | - T_ENDFOREACH, |
|
51 | - T_ENDIF, |
|
52 | - T_ENDSWITCH, |
|
53 | - T_ENDWHILE, |
|
54 | - T_EVAL, |
|
55 | - T_EXIT, |
|
56 | - T_EXTENDS, |
|
57 | - T_FINAL, |
|
58 | - T_FINALLY, |
|
59 | - T_FOR, |
|
60 | - T_FOREACH, |
|
61 | - T_FUNCTION, |
|
62 | - T_GLOBAL, |
|
63 | - T_GOTO, |
|
64 | - T_IF, |
|
65 | - T_IMPLEMENTS, |
|
66 | - T_INCLUDE, |
|
67 | - T_INCLUDE_ONCE, |
|
68 | - T_INSTANCEOF, |
|
69 | - T_INSTEADOF, |
|
70 | - T_INTERFACE, |
|
71 | - T_ISSET, |
|
72 | - T_LIST, |
|
73 | - T_LOGICAL_AND, |
|
74 | - T_LOGICAL_OR, |
|
75 | - T_LOGICAL_XOR, |
|
76 | - T_NAMESPACE, |
|
77 | - T_NEW, |
|
78 | - T_PARENT, |
|
79 | - T_PRINT, |
|
80 | - T_PRIVATE, |
|
81 | - T_PROTECTED, |
|
82 | - T_PUBLIC, |
|
83 | - T_REQUIRE, |
|
84 | - T_REQUIRE_ONCE, |
|
85 | - T_RETURN, |
|
86 | - T_SELF, |
|
87 | - T_STATIC, |
|
88 | - T_SWITCH, |
|
89 | - T_THROW, |
|
90 | - T_TRAIT, |
|
91 | - T_TRY, |
|
92 | - T_UNSET, |
|
93 | - T_USE, |
|
94 | - T_VAR, |
|
95 | - T_WHILE, |
|
96 | - T_YIELD, |
|
97 | - T_YIELD_FROM, |
|
98 | - ]; |
|
20 | + /** |
|
21 | + * Returns an array of tokens this test wants to listen for. |
|
22 | + * |
|
23 | + * @return array |
|
24 | + */ |
|
25 | + public function register() |
|
26 | + { |
|
27 | + return [ |
|
28 | + T_HALT_COMPILER, |
|
29 | + T_ABSTRACT, |
|
30 | + T_ARRAY, |
|
31 | + T_AS, |
|
32 | + T_BREAK, |
|
33 | + T_CALLABLE, |
|
34 | + T_CASE, |
|
35 | + T_CATCH, |
|
36 | + T_CLASS, |
|
37 | + T_CLONE, |
|
38 | + T_CLOSURE, |
|
39 | + T_CONST, |
|
40 | + T_CONTINUE, |
|
41 | + T_DECLARE, |
|
42 | + T_DEFAULT, |
|
43 | + T_DO, |
|
44 | + T_ECHO, |
|
45 | + T_ELSE, |
|
46 | + T_ELSEIF, |
|
47 | + T_EMPTY, |
|
48 | + T_ENDDECLARE, |
|
49 | + T_ENDFOR, |
|
50 | + T_ENDFOREACH, |
|
51 | + T_ENDIF, |
|
52 | + T_ENDSWITCH, |
|
53 | + T_ENDWHILE, |
|
54 | + T_EVAL, |
|
55 | + T_EXIT, |
|
56 | + T_EXTENDS, |
|
57 | + T_FINAL, |
|
58 | + T_FINALLY, |
|
59 | + T_FOR, |
|
60 | + T_FOREACH, |
|
61 | + T_FUNCTION, |
|
62 | + T_GLOBAL, |
|
63 | + T_GOTO, |
|
64 | + T_IF, |
|
65 | + T_IMPLEMENTS, |
|
66 | + T_INCLUDE, |
|
67 | + T_INCLUDE_ONCE, |
|
68 | + T_INSTANCEOF, |
|
69 | + T_INSTEADOF, |
|
70 | + T_INTERFACE, |
|
71 | + T_ISSET, |
|
72 | + T_LIST, |
|
73 | + T_LOGICAL_AND, |
|
74 | + T_LOGICAL_OR, |
|
75 | + T_LOGICAL_XOR, |
|
76 | + T_NAMESPACE, |
|
77 | + T_NEW, |
|
78 | + T_PARENT, |
|
79 | + T_PRINT, |
|
80 | + T_PRIVATE, |
|
81 | + T_PROTECTED, |
|
82 | + T_PUBLIC, |
|
83 | + T_REQUIRE, |
|
84 | + T_REQUIRE_ONCE, |
|
85 | + T_RETURN, |
|
86 | + T_SELF, |
|
87 | + T_STATIC, |
|
88 | + T_SWITCH, |
|
89 | + T_THROW, |
|
90 | + T_TRAIT, |
|
91 | + T_TRY, |
|
92 | + T_UNSET, |
|
93 | + T_USE, |
|
94 | + T_VAR, |
|
95 | + T_WHILE, |
|
96 | + T_YIELD, |
|
97 | + T_YIELD_FROM, |
|
98 | + ]; |
|
99 | 99 | |
100 | - }//end register() |
|
100 | + }//end register() |
|
101 | 101 | |
102 | 102 | |
103 | - /** |
|
104 | - * Processes this sniff, when one of its tokens is encountered. |
|
105 | - * |
|
106 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
107 | - * @param int $stackPtr The position of the current token in the |
|
108 | - * stack passed in $tokens. |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function process(File $phpcsFile, $stackPtr) |
|
113 | - { |
|
114 | - $tokens = $phpcsFile->getTokens(); |
|
115 | - $keyword = $tokens[$stackPtr]['content']; |
|
116 | - if (strtolower($keyword) !== $keyword) { |
|
117 | - if ($keyword === strtoupper($keyword)) { |
|
118 | - $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'upper'); |
|
119 | - } else { |
|
120 | - $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'mixed'); |
|
121 | - } |
|
103 | + /** |
|
104 | + * Processes this sniff, when one of its tokens is encountered. |
|
105 | + * |
|
106 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
107 | + * @param int $stackPtr The position of the current token in the |
|
108 | + * stack passed in $tokens. |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function process(File $phpcsFile, $stackPtr) |
|
113 | + { |
|
114 | + $tokens = $phpcsFile->getTokens(); |
|
115 | + $keyword = $tokens[$stackPtr]['content']; |
|
116 | + if (strtolower($keyword) !== $keyword) { |
|
117 | + if ($keyword === strtoupper($keyword)) { |
|
118 | + $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'upper'); |
|
119 | + } else { |
|
120 | + $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'mixed'); |
|
121 | + } |
|
122 | 122 | |
123 | - $messageKeyword = Util\Common::prepareForOutput($keyword); |
|
123 | + $messageKeyword = Util\Common::prepareForOutput($keyword); |
|
124 | 124 | |
125 | - $error = 'PHP keywords must be lowercase; expected "%s" but found "%s"'; |
|
126 | - $data = [ |
|
127 | - strtolower($messageKeyword), |
|
128 | - $messageKeyword, |
|
129 | - ]; |
|
125 | + $error = 'PHP keywords must be lowercase; expected "%s" but found "%s"'; |
|
126 | + $data = [ |
|
127 | + strtolower($messageKeyword), |
|
128 | + $messageKeyword, |
|
129 | + ]; |
|
130 | 130 | |
131 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
132 | - if ($fix === true) { |
|
133 | - $phpcsFile->fixer->replaceToken($stackPtr, strtolower($keyword)); |
|
134 | - } |
|
135 | - } else { |
|
136 | - $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'lower'); |
|
137 | - }//end if |
|
131 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
132 | + if ($fix === true) { |
|
133 | + $phpcsFile->fixer->replaceToken($stackPtr, strtolower($keyword)); |
|
134 | + } |
|
135 | + } else { |
|
136 | + $phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'lower'); |
|
137 | + }//end if |
|
138 | 138 | |
139 | - }//end process() |
|
139 | + }//end process() |
|
140 | 140 | |
141 | 141 | |
142 | 142 | }//end class |
@@ -16,58 +16,58 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Returns an array of tokens this test wants to listen for. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function register() |
|
25 | - { |
|
26 | - return [ |
|
27 | - T_TRUE, |
|
28 | - T_FALSE, |
|
29 | - T_NULL, |
|
30 | - ]; |
|
19 | + /** |
|
20 | + * Returns an array of tokens this test wants to listen for. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function register() |
|
25 | + { |
|
26 | + return [ |
|
27 | + T_TRUE, |
|
28 | + T_FALSE, |
|
29 | + T_NULL, |
|
30 | + ]; |
|
31 | 31 | |
32 | - }//end register() |
|
32 | + }//end register() |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * Processes this sniff, when one of its tokens is encountered. |
|
37 | - * |
|
38 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
39 | - * @param int $stackPtr The position of the current token in the |
|
40 | - * stack passed in $tokens. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function process(File $phpcsFile, $stackPtr) |
|
45 | - { |
|
46 | - $tokens = $phpcsFile->getTokens(); |
|
47 | - $keyword = $tokens[$stackPtr]['content']; |
|
48 | - $expected = strtoupper($keyword); |
|
49 | - if ($keyword !== $expected) { |
|
50 | - if ($keyword === strtolower($keyword)) { |
|
51 | - $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'lower'); |
|
52 | - } else { |
|
53 | - $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'mixed'); |
|
54 | - } |
|
35 | + /** |
|
36 | + * Processes this sniff, when one of its tokens is encountered. |
|
37 | + * |
|
38 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
39 | + * @param int $stackPtr The position of the current token in the |
|
40 | + * stack passed in $tokens. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function process(File $phpcsFile, $stackPtr) |
|
45 | + { |
|
46 | + $tokens = $phpcsFile->getTokens(); |
|
47 | + $keyword = $tokens[$stackPtr]['content']; |
|
48 | + $expected = strtoupper($keyword); |
|
49 | + if ($keyword !== $expected) { |
|
50 | + if ($keyword === strtolower($keyword)) { |
|
51 | + $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'lower'); |
|
52 | + } else { |
|
53 | + $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'mixed'); |
|
54 | + } |
|
55 | 55 | |
56 | - $error = 'TRUE, FALSE and NULL must be uppercase; expected "%s" but found "%s"'; |
|
57 | - $data = [ |
|
58 | - $expected, |
|
59 | - $keyword, |
|
60 | - ]; |
|
56 | + $error = 'TRUE, FALSE and NULL must be uppercase; expected "%s" but found "%s"'; |
|
57 | + $data = [ |
|
58 | + $expected, |
|
59 | + $keyword, |
|
60 | + ]; |
|
61 | 61 | |
62 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
63 | - if ($fix === true) { |
|
64 | - $phpcsFile->fixer->replaceToken($stackPtr, $expected); |
|
65 | - } |
|
66 | - } else { |
|
67 | - $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'upper'); |
|
68 | - } |
|
62 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
63 | + if ($fix === true) { |
|
64 | + $phpcsFile->fixer->replaceToken($stackPtr, $expected); |
|
65 | + } |
|
66 | + } else { |
|
67 | + $phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'upper'); |
|
68 | + } |
|
69 | 69 | |
70 | - }//end process() |
|
70 | + }//end process() |
|
71 | 71 | |
72 | 72 | |
73 | 73 | }//end class |
@@ -17,55 +17,55 @@ |
||
17 | 17 | class SyntaxSniff implements Sniff |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * The path to the PHP version we are checking with. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $phpPath = null; |
|
20 | + /** |
|
21 | + * The path to the PHP version we are checking with. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $phpPath = null; |
|
26 | 26 | |
27 | 27 | |
28 | - /** |
|
29 | - * Returns an array of tokens this test wants to listen for. |
|
30 | - * |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - public function register() |
|
34 | - { |
|
35 | - return [T_OPEN_TAG]; |
|
28 | + /** |
|
29 | + * Returns an array of tokens this test wants to listen for. |
|
30 | + * |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + public function register() |
|
34 | + { |
|
35 | + return [T_OPEN_TAG]; |
|
36 | 36 | |
37 | - }//end register() |
|
37 | + }//end register() |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * Processes this test, when one of its tokens is encountered. |
|
42 | - * |
|
43 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
44 | - * @param int $stackPtr The position of the current token in |
|
45 | - * the stack passed in $tokens. |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function process(File $phpcsFile, $stackPtr) |
|
50 | - { |
|
51 | - if ($this->phpPath === null) { |
|
52 | - $this->phpPath = Config::getExecutablePath('php'); |
|
53 | - } |
|
40 | + /** |
|
41 | + * Processes this test, when one of its tokens is encountered. |
|
42 | + * |
|
43 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
44 | + * @param int $stackPtr The position of the current token in |
|
45 | + * the stack passed in $tokens. |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function process(File $phpcsFile, $stackPtr) |
|
50 | + { |
|
51 | + if ($this->phpPath === null) { |
|
52 | + $this->phpPath = Config::getExecutablePath('php'); |
|
53 | + } |
|
54 | 54 | |
55 | - $fileName = escapeshellarg($phpcsFile->getFilename()); |
|
56 | - $cmd = escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1"; |
|
57 | - $output = shell_exec($cmd); |
|
58 | - $matches = []; |
|
59 | - if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) { |
|
60 | - $error = trim($matches[1]); |
|
61 | - $line = (int) $matches[2]; |
|
62 | - $phpcsFile->addErrorOnLine("PHP syntax error: $error", $line, 'PHPSyntax'); |
|
63 | - } |
|
55 | + $fileName = escapeshellarg($phpcsFile->getFilename()); |
|
56 | + $cmd = escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1"; |
|
57 | + $output = shell_exec($cmd); |
|
58 | + $matches = []; |
|
59 | + if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) { |
|
60 | + $error = trim($matches[1]); |
|
61 | + $line = (int) $matches[2]; |
|
62 | + $phpcsFile->addErrorOnLine("PHP syntax error: $error", $line, 'PHPSyntax'); |
|
63 | + } |
|
64 | 64 | |
65 | - // Ignore the rest of the file. |
|
66 | - return ($phpcsFile->numTokens + 1); |
|
65 | + // Ignore the rest of the file. |
|
66 | + return ($phpcsFile->numTokens + 1); |
|
67 | 67 | |
68 | - }//end process() |
|
68 | + }//end process() |
|
69 | 69 | |
70 | 70 | |
71 | 71 | }//end class |
@@ -17,152 +17,152 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * Returns an array of tokens this test wants to listen for. |
|
22 | - * |
|
23 | - * @return array |
|
24 | - */ |
|
25 | - public function register() |
|
26 | - { |
|
27 | - $targets = [ |
|
28 | - T_OPEN_TAG, |
|
29 | - T_OPEN_TAG_WITH_ECHO, |
|
30 | - ]; |
|
31 | - |
|
32 | - $shortOpenTags = (bool) ini_get('short_open_tag'); |
|
33 | - if ($shortOpenTags === false) { |
|
34 | - $targets[] = T_INLINE_HTML; |
|
35 | - } |
|
36 | - |
|
37 | - return $targets; |
|
38 | - |
|
39 | - }//end register() |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * Processes this test, when one of its tokens is encountered. |
|
44 | - * |
|
45 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
46 | - * @param int $stackPtr The position of the current token |
|
47 | - * in the stack passed in $tokens. |
|
48 | - * |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function process(File $phpcsFile, $stackPtr) |
|
52 | - { |
|
53 | - $tokens = $phpcsFile->getTokens(); |
|
54 | - $token = $tokens[$stackPtr]; |
|
55 | - |
|
56 | - if ($token['code'] === T_OPEN_TAG && $token['content'] === '<?') { |
|
57 | - $error = 'Short PHP opening tag used; expected "<?php" but found "%s"'; |
|
58 | - $data = [$token['content']]; |
|
59 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
60 | - if ($fix === true) { |
|
61 | - $correctOpening = '<?php'; |
|
62 | - if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
63 | - // Avoid creation of invalid open tags like <?phpecho if the original was <?echo . |
|
64 | - $correctOpening .= ' '; |
|
65 | - } |
|
66 | - |
|
67 | - $phpcsFile->fixer->replaceToken($stackPtr, $correctOpening); |
|
68 | - } |
|
69 | - |
|
70 | - $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes'); |
|
71 | - } else { |
|
72 | - $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'no'); |
|
73 | - } |
|
74 | - |
|
75 | - if ($token['code'] === T_OPEN_TAG_WITH_ECHO) { |
|
76 | - $nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)]; |
|
77 | - $error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."'; |
|
78 | - $data = [ |
|
79 | - $nextVar['content'], |
|
80 | - $token['content'], |
|
81 | - $nextVar['content'], |
|
82 | - ]; |
|
83 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data); |
|
84 | - if ($fix === true) { |
|
85 | - if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
86 | - $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo '); |
|
87 | - } else { |
|
88 | - $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo'); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - if ($token['code'] === T_INLINE_HTML) { |
|
94 | - $content = $token['content']; |
|
95 | - $openerFound = strpos($content, '<?'); |
|
96 | - |
|
97 | - if ($openerFound === false) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - $closerFound = false; |
|
102 | - |
|
103 | - // Inspect current token and subsequent inline HTML token to find a close tag. |
|
104 | - for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) { |
|
105 | - if ($tokens[$i]['code'] !== T_INLINE_HTML) { |
|
106 | - break; |
|
107 | - } |
|
108 | - |
|
109 | - $closerFound = strrpos($tokens[$i]['content'], '?>'); |
|
110 | - if ($closerFound !== false) { |
|
111 | - if ($i !== $stackPtr) { |
|
112 | - break; |
|
113 | - } else if ($closerFound > $openerFound) { |
|
114 | - break; |
|
115 | - } else { |
|
116 | - $closerFound = false; |
|
117 | - } |
|
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - if ($closerFound !== false) { |
|
122 | - $error = 'Possible use of short open tags detected; found: %s'; |
|
123 | - $snippet = $this->getSnippet($content, '<?'); |
|
124 | - $data = ['<?'.$snippet]; |
|
125 | - |
|
126 | - $phpcsFile->addWarning($error, $stackPtr, 'PossibleFound', $data); |
|
127 | - |
|
128 | - // Skip forward to the token containing the closer. |
|
129 | - if (($i - 1) > $stackPtr) { |
|
130 | - return $i; |
|
131 | - } |
|
132 | - } |
|
133 | - }//end if |
|
134 | - |
|
135 | - }//end process() |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * Get a snippet from a HTML token. |
|
140 | - * |
|
141 | - * @param string $content The content of the HTML token. |
|
142 | - * @param string $start Partial string to use as a starting point for the snippet. |
|
143 | - * @param int $length The target length of the snippet to get. Defaults to 40. |
|
144 | - * |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - protected function getSnippet($content, $start='', $length=40) |
|
148 | - { |
|
149 | - $startPos = 0; |
|
150 | - |
|
151 | - if ($start !== '') { |
|
152 | - $startPos = strpos($content, $start); |
|
153 | - if ($startPos !== false) { |
|
154 | - $startPos += strlen($start); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - $snippet = substr($content, $startPos, $length); |
|
159 | - if ((strlen($content) - $startPos) > $length) { |
|
160 | - $snippet .= '...'; |
|
161 | - } |
|
162 | - |
|
163 | - return $snippet; |
|
164 | - |
|
165 | - }//end getSnippet() |
|
20 | + /** |
|
21 | + * Returns an array of tokens this test wants to listen for. |
|
22 | + * |
|
23 | + * @return array |
|
24 | + */ |
|
25 | + public function register() |
|
26 | + { |
|
27 | + $targets = [ |
|
28 | + T_OPEN_TAG, |
|
29 | + T_OPEN_TAG_WITH_ECHO, |
|
30 | + ]; |
|
31 | + |
|
32 | + $shortOpenTags = (bool) ini_get('short_open_tag'); |
|
33 | + if ($shortOpenTags === false) { |
|
34 | + $targets[] = T_INLINE_HTML; |
|
35 | + } |
|
36 | + |
|
37 | + return $targets; |
|
38 | + |
|
39 | + }//end register() |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * Processes this test, when one of its tokens is encountered. |
|
44 | + * |
|
45 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
46 | + * @param int $stackPtr The position of the current token |
|
47 | + * in the stack passed in $tokens. |
|
48 | + * |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function process(File $phpcsFile, $stackPtr) |
|
52 | + { |
|
53 | + $tokens = $phpcsFile->getTokens(); |
|
54 | + $token = $tokens[$stackPtr]; |
|
55 | + |
|
56 | + if ($token['code'] === T_OPEN_TAG && $token['content'] === '<?') { |
|
57 | + $error = 'Short PHP opening tag used; expected "<?php" but found "%s"'; |
|
58 | + $data = [$token['content']]; |
|
59 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); |
|
60 | + if ($fix === true) { |
|
61 | + $correctOpening = '<?php'; |
|
62 | + if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
63 | + // Avoid creation of invalid open tags like <?phpecho if the original was <?echo . |
|
64 | + $correctOpening .= ' '; |
|
65 | + } |
|
66 | + |
|
67 | + $phpcsFile->fixer->replaceToken($stackPtr, $correctOpening); |
|
68 | + } |
|
69 | + |
|
70 | + $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes'); |
|
71 | + } else { |
|
72 | + $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'no'); |
|
73 | + } |
|
74 | + |
|
75 | + if ($token['code'] === T_OPEN_TAG_WITH_ECHO) { |
|
76 | + $nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)]; |
|
77 | + $error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."'; |
|
78 | + $data = [ |
|
79 | + $nextVar['content'], |
|
80 | + $token['content'], |
|
81 | + $nextVar['content'], |
|
82 | + ]; |
|
83 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data); |
|
84 | + if ($fix === true) { |
|
85 | + if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
86 | + $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo '); |
|
87 | + } else { |
|
88 | + $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo'); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + if ($token['code'] === T_INLINE_HTML) { |
|
94 | + $content = $token['content']; |
|
95 | + $openerFound = strpos($content, '<?'); |
|
96 | + |
|
97 | + if ($openerFound === false) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + $closerFound = false; |
|
102 | + |
|
103 | + // Inspect current token and subsequent inline HTML token to find a close tag. |
|
104 | + for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) { |
|
105 | + if ($tokens[$i]['code'] !== T_INLINE_HTML) { |
|
106 | + break; |
|
107 | + } |
|
108 | + |
|
109 | + $closerFound = strrpos($tokens[$i]['content'], '?>'); |
|
110 | + if ($closerFound !== false) { |
|
111 | + if ($i !== $stackPtr) { |
|
112 | + break; |
|
113 | + } else if ($closerFound > $openerFound) { |
|
114 | + break; |
|
115 | + } else { |
|
116 | + $closerFound = false; |
|
117 | + } |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + if ($closerFound !== false) { |
|
122 | + $error = 'Possible use of short open tags detected; found: %s'; |
|
123 | + $snippet = $this->getSnippet($content, '<?'); |
|
124 | + $data = ['<?'.$snippet]; |
|
125 | + |
|
126 | + $phpcsFile->addWarning($error, $stackPtr, 'PossibleFound', $data); |
|
127 | + |
|
128 | + // Skip forward to the token containing the closer. |
|
129 | + if (($i - 1) > $stackPtr) { |
|
130 | + return $i; |
|
131 | + } |
|
132 | + } |
|
133 | + }//end if |
|
134 | + |
|
135 | + }//end process() |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * Get a snippet from a HTML token. |
|
140 | + * |
|
141 | + * @param string $content The content of the HTML token. |
|
142 | + * @param string $start Partial string to use as a starting point for the snippet. |
|
143 | + * @param int $length The target length of the snippet to get. Defaults to 40. |
|
144 | + * |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + protected function getSnippet($content, $start='', $length=40) |
|
148 | + { |
|
149 | + $startPos = 0; |
|
150 | + |
|
151 | + if ($start !== '') { |
|
152 | + $startPos = strpos($content, $start); |
|
153 | + if ($startPos !== false) { |
|
154 | + $startPos += strlen($start); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + $snippet = substr($content, $startPos, $length); |
|
159 | + if ((strlen($content) - $startPos) > $length) { |
|
160 | + $snippet .= '...'; |
|
161 | + } |
|
162 | + |
|
163 | + return $snippet; |
|
164 | + |
|
165 | + }//end getSnippet() |
|
166 | 166 | |
167 | 167 | |
168 | 168 | }//end class |
@@ -16,51 +16,51 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Returns an array of tokens this test wants to listen for. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function register() |
|
25 | - { |
|
26 | - return [T_STRING]; |
|
19 | + /** |
|
20 | + * Returns an array of tokens this test wants to listen for. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function register() |
|
25 | + { |
|
26 | + return [T_STRING]; |
|
27 | 27 | |
28 | - }//end register() |
|
28 | + }//end register() |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Processes this test, when one of its tokens is encountered. |
|
33 | - * |
|
34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | - * @param int $stackPtr The position of the current token in |
|
36 | - * the stack passed in $tokens. |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function process(File $phpcsFile, $stackPtr) |
|
41 | - { |
|
42 | - $tokens = $phpcsFile->getTokens(); |
|
31 | + /** |
|
32 | + * Processes this test, when one of its tokens is encountered. |
|
33 | + * |
|
34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | + * @param int $stackPtr The position of the current token in |
|
36 | + * the stack passed in $tokens. |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function process(File $phpcsFile, $stackPtr) |
|
41 | + { |
|
42 | + $tokens = $phpcsFile->getTokens(); |
|
43 | 43 | |
44 | - $ignore = [ |
|
45 | - T_DOUBLE_COLON => true, |
|
46 | - T_OBJECT_OPERATOR => true, |
|
47 | - T_FUNCTION => true, |
|
48 | - T_CONST => true, |
|
49 | - ]; |
|
44 | + $ignore = [ |
|
45 | + T_DOUBLE_COLON => true, |
|
46 | + T_OBJECT_OPERATOR => true, |
|
47 | + T_FUNCTION => true, |
|
48 | + T_CONST => true, |
|
49 | + ]; |
|
50 | 50 | |
51 | - $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
52 | - if (isset($ignore[$tokens[$prevToken]['code']]) === true) { |
|
53 | - // Not a call to a PHP function. |
|
54 | - return; |
|
55 | - } |
|
51 | + $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
52 | + if (isset($ignore[$tokens[$prevToken]['code']]) === true) { |
|
53 | + // Not a call to a PHP function. |
|
54 | + return; |
|
55 | + } |
|
56 | 56 | |
57 | - $function = strtolower($tokens[$stackPtr]['content']); |
|
58 | - if ($function === 'php_sapi_name') { |
|
59 | - $error = 'Use the PHP_SAPI constant instead of calling php_sapi_name()'; |
|
60 | - $phpcsFile->addError($error, $stackPtr, 'FunctionFound'); |
|
61 | - } |
|
57 | + $function = strtolower($tokens[$stackPtr]['content']); |
|
58 | + if ($function === 'php_sapi_name') { |
|
59 | + $error = 'Use the PHP_SAPI constant instead of calling php_sapi_name()'; |
|
60 | + $phpcsFile->addError($error, $stackPtr, 'FunctionFound'); |
|
61 | + } |
|
62 | 62 | |
63 | - }//end process() |
|
63 | + }//end process() |
|
64 | 64 | |
65 | 65 | |
66 | 66 | }//end class |
@@ -16,33 +16,33 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Returns an array of tokens this test wants to listen for. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function register() |
|
25 | - { |
|
26 | - return [T_BACKTICK]; |
|
27 | - |
|
28 | - }//end register() |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * Processes this sniff, when one of its tokens is encountered. |
|
33 | - * |
|
34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | - * @param int $stackPtr The position of the current token in |
|
36 | - * the stack passed in $tokens. |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function process(File $phpcsFile, $stackPtr) |
|
41 | - { |
|
42 | - $error = 'Use of the backtick operator is forbidden'; |
|
43 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
44 | - |
|
45 | - }//end process() |
|
19 | + /** |
|
20 | + * Returns an array of tokens this test wants to listen for. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function register() |
|
25 | + { |
|
26 | + return [T_BACKTICK]; |
|
27 | + |
|
28 | + }//end register() |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * Processes this sniff, when one of its tokens is encountered. |
|
33 | + * |
|
34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
35 | + * @param int $stackPtr The position of the current token in |
|
36 | + * the stack passed in $tokens. |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function process(File $phpcsFile, $stackPtr) |
|
41 | + { |
|
42 | + $error = 'Use of the backtick operator is forbidden'; |
|
43 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
44 | + |
|
45 | + }//end process() |
|
46 | 46 | |
47 | 47 | |
48 | 48 | }//end class |
@@ -17,146 +17,146 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * Returns an array of tokens this test wants to listen for. |
|
22 | - * |
|
23 | - * @return array |
|
24 | - */ |
|
25 | - public function register() |
|
26 | - { |
|
27 | - $tokens = Tokens::$castTokens; |
|
28 | - $tokens[] = T_FUNCTION; |
|
29 | - $tokens[] = T_CLOSURE; |
|
30 | - return $tokens; |
|
31 | - |
|
32 | - }//end register() |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * Processes this sniff, when one of its tokens is encountered. |
|
37 | - * |
|
38 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
39 | - * @param int $stackPtr The position of the current token in the |
|
40 | - * stack passed in $tokens. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function process(File $phpcsFile, $stackPtr) |
|
45 | - { |
|
46 | - $tokens = $phpcsFile->getTokens(); |
|
47 | - |
|
48 | - if (isset(Tokens::$castTokens[$tokens[$stackPtr]['code']]) === true) { |
|
49 | - // A cast token. |
|
50 | - if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) { |
|
51 | - if ($tokens[$stackPtr]['content'] === strtoupper($tokens[$stackPtr]['content'])) { |
|
52 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
53 | - } else { |
|
54 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
55 | - } |
|
56 | - |
|
57 | - $error = 'PHP type casts must be lowercase; expected "%s" but found "%s"'; |
|
58 | - $data = [ |
|
59 | - strtolower($tokens[$stackPtr]['content']), |
|
60 | - $tokens[$stackPtr]['content'], |
|
61 | - ]; |
|
62 | - |
|
63 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'TypeCastFound', $data); |
|
64 | - if ($fix === true) { |
|
65 | - $phpcsFile->fixer->replaceToken($stackPtr, strtolower($tokens[$stackPtr]['content'])); |
|
66 | - } |
|
67 | - } else { |
|
68 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
69 | - }//end if |
|
70 | - |
|
71 | - return; |
|
72 | - }//end if |
|
73 | - |
|
74 | - $phpTypes = [ |
|
75 | - 'self' => true, |
|
76 | - 'parent' => true, |
|
77 | - 'array' => true, |
|
78 | - 'callable' => true, |
|
79 | - 'bool' => true, |
|
80 | - 'float' => true, |
|
81 | - 'int' => true, |
|
82 | - 'string' => true, |
|
83 | - 'iterable' => true, |
|
84 | - 'void' => true, |
|
85 | - 'object' => true, |
|
86 | - ]; |
|
87 | - |
|
88 | - $props = $phpcsFile->getMethodProperties($stackPtr); |
|
89 | - |
|
90 | - // Strip off potential nullable indication. |
|
91 | - $returnType = ltrim($props['return_type'], '?'); |
|
92 | - $returnTypeLower = strtolower($returnType); |
|
93 | - |
|
94 | - if ($returnType !== '' |
|
95 | - && isset($phpTypes[$returnTypeLower]) === true |
|
96 | - ) { |
|
97 | - // A function return type. |
|
98 | - if ($returnTypeLower !== $returnType) { |
|
99 | - if ($returnType === strtoupper($returnType)) { |
|
100 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
101 | - } else { |
|
102 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
103 | - } |
|
104 | - |
|
105 | - $error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s"'; |
|
106 | - $token = $props['return_type_token']; |
|
107 | - $data = [ |
|
108 | - $returnTypeLower, |
|
109 | - $returnType, |
|
110 | - ]; |
|
111 | - |
|
112 | - $fix = $phpcsFile->addFixableError($error, $token, 'ReturnTypeFound', $data); |
|
113 | - if ($fix === true) { |
|
114 | - $phpcsFile->fixer->replaceToken($token, $returnTypeLower); |
|
115 | - } |
|
116 | - } else { |
|
117 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
118 | - }//end if |
|
119 | - }//end if |
|
120 | - |
|
121 | - $params = $phpcsFile->getMethodParameters($stackPtr); |
|
122 | - if (empty($params) === true) { |
|
123 | - return; |
|
124 | - } |
|
125 | - |
|
126 | - foreach ($params as $param) { |
|
127 | - // Strip off potential nullable indication. |
|
128 | - $typeHint = ltrim($param['type_hint'], '?'); |
|
129 | - $typeHintLower = strtolower($typeHint); |
|
130 | - |
|
131 | - if ($typeHint !== '' |
|
132 | - && isset($phpTypes[$typeHintLower]) === true |
|
133 | - ) { |
|
134 | - // A function return type. |
|
135 | - if ($typeHintLower !== $typeHint) { |
|
136 | - if ($typeHint === strtoupper($typeHint)) { |
|
137 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
138 | - } else { |
|
139 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
140 | - } |
|
141 | - |
|
142 | - $error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s"'; |
|
143 | - $token = $param['type_hint_token']; |
|
144 | - $data = [ |
|
145 | - $typeHintLower, |
|
146 | - $typeHint, |
|
147 | - ]; |
|
148 | - |
|
149 | - $fix = $phpcsFile->addFixableError($error, $token, 'ParamTypeFound', $data); |
|
150 | - if ($fix === true) { |
|
151 | - $phpcsFile->fixer->replaceToken($token, $typeHintLower); |
|
152 | - } |
|
153 | - } else { |
|
154 | - $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
155 | - }//end if |
|
156 | - }//end if |
|
157 | - }//end foreach |
|
158 | - |
|
159 | - }//end process() |
|
20 | + /** |
|
21 | + * Returns an array of tokens this test wants to listen for. |
|
22 | + * |
|
23 | + * @return array |
|
24 | + */ |
|
25 | + public function register() |
|
26 | + { |
|
27 | + $tokens = Tokens::$castTokens; |
|
28 | + $tokens[] = T_FUNCTION; |
|
29 | + $tokens[] = T_CLOSURE; |
|
30 | + return $tokens; |
|
31 | + |
|
32 | + }//end register() |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * Processes this sniff, when one of its tokens is encountered. |
|
37 | + * |
|
38 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
39 | + * @param int $stackPtr The position of the current token in the |
|
40 | + * stack passed in $tokens. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function process(File $phpcsFile, $stackPtr) |
|
45 | + { |
|
46 | + $tokens = $phpcsFile->getTokens(); |
|
47 | + |
|
48 | + if (isset(Tokens::$castTokens[$tokens[$stackPtr]['code']]) === true) { |
|
49 | + // A cast token. |
|
50 | + if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) { |
|
51 | + if ($tokens[$stackPtr]['content'] === strtoupper($tokens[$stackPtr]['content'])) { |
|
52 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
53 | + } else { |
|
54 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
55 | + } |
|
56 | + |
|
57 | + $error = 'PHP type casts must be lowercase; expected "%s" but found "%s"'; |
|
58 | + $data = [ |
|
59 | + strtolower($tokens[$stackPtr]['content']), |
|
60 | + $tokens[$stackPtr]['content'], |
|
61 | + ]; |
|
62 | + |
|
63 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'TypeCastFound', $data); |
|
64 | + if ($fix === true) { |
|
65 | + $phpcsFile->fixer->replaceToken($stackPtr, strtolower($tokens[$stackPtr]['content'])); |
|
66 | + } |
|
67 | + } else { |
|
68 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
69 | + }//end if |
|
70 | + |
|
71 | + return; |
|
72 | + }//end if |
|
73 | + |
|
74 | + $phpTypes = [ |
|
75 | + 'self' => true, |
|
76 | + 'parent' => true, |
|
77 | + 'array' => true, |
|
78 | + 'callable' => true, |
|
79 | + 'bool' => true, |
|
80 | + 'float' => true, |
|
81 | + 'int' => true, |
|
82 | + 'string' => true, |
|
83 | + 'iterable' => true, |
|
84 | + 'void' => true, |
|
85 | + 'object' => true, |
|
86 | + ]; |
|
87 | + |
|
88 | + $props = $phpcsFile->getMethodProperties($stackPtr); |
|
89 | + |
|
90 | + // Strip off potential nullable indication. |
|
91 | + $returnType = ltrim($props['return_type'], '?'); |
|
92 | + $returnTypeLower = strtolower($returnType); |
|
93 | + |
|
94 | + if ($returnType !== '' |
|
95 | + && isset($phpTypes[$returnTypeLower]) === true |
|
96 | + ) { |
|
97 | + // A function return type. |
|
98 | + if ($returnTypeLower !== $returnType) { |
|
99 | + if ($returnType === strtoupper($returnType)) { |
|
100 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
101 | + } else { |
|
102 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
103 | + } |
|
104 | + |
|
105 | + $error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s"'; |
|
106 | + $token = $props['return_type_token']; |
|
107 | + $data = [ |
|
108 | + $returnTypeLower, |
|
109 | + $returnType, |
|
110 | + ]; |
|
111 | + |
|
112 | + $fix = $phpcsFile->addFixableError($error, $token, 'ReturnTypeFound', $data); |
|
113 | + if ($fix === true) { |
|
114 | + $phpcsFile->fixer->replaceToken($token, $returnTypeLower); |
|
115 | + } |
|
116 | + } else { |
|
117 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
118 | + }//end if |
|
119 | + }//end if |
|
120 | + |
|
121 | + $params = $phpcsFile->getMethodParameters($stackPtr); |
|
122 | + if (empty($params) === true) { |
|
123 | + return; |
|
124 | + } |
|
125 | + |
|
126 | + foreach ($params as $param) { |
|
127 | + // Strip off potential nullable indication. |
|
128 | + $typeHint = ltrim($param['type_hint'], '?'); |
|
129 | + $typeHintLower = strtolower($typeHint); |
|
130 | + |
|
131 | + if ($typeHint !== '' |
|
132 | + && isset($phpTypes[$typeHintLower]) === true |
|
133 | + ) { |
|
134 | + // A function return type. |
|
135 | + if ($typeHintLower !== $typeHint) { |
|
136 | + if ($typeHint === strtoupper($typeHint)) { |
|
137 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); |
|
138 | + } else { |
|
139 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); |
|
140 | + } |
|
141 | + |
|
142 | + $error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s"'; |
|
143 | + $token = $param['type_hint_token']; |
|
144 | + $data = [ |
|
145 | + $typeHintLower, |
|
146 | + $typeHint, |
|
147 | + ]; |
|
148 | + |
|
149 | + $fix = $phpcsFile->addFixableError($error, $token, 'ParamTypeFound', $data); |
|
150 | + if ($fix === true) { |
|
151 | + $phpcsFile->fixer->replaceToken($token, $typeHintLower); |
|
152 | + } |
|
153 | + } else { |
|
154 | + $phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); |
|
155 | + }//end if |
|
156 | + }//end if |
|
157 | + }//end foreach |
|
158 | + |
|
159 | + }//end process() |
|
160 | 160 | |
161 | 161 | |
162 | 162 | }//end class |