@@ -15,38 +15,38 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Returns the lines where errors should occur. |
|
20 | - * |
|
21 | - * The key of the array should represent the line number and the value |
|
22 | - * should represent the number of errors that should occur on that line. |
|
23 | - * |
|
24 | - * @return array<int, int> |
|
25 | - */ |
|
26 | - public function getErrorList() |
|
27 | - { |
|
28 | - return [ |
|
29 | - 5 => 1, |
|
30 | - 11 => 1, |
|
31 | - 17 => 2, |
|
32 | - ]; |
|
33 | - |
|
34 | - }//end getErrorList() |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns the lines where warnings should occur. |
|
39 | - * |
|
40 | - * The key of the array should represent the line number and the value |
|
41 | - * should represent the number of warnings that should occur on that line. |
|
42 | - * |
|
43 | - * @return array<int, int> |
|
44 | - */ |
|
45 | - public function getWarningList() |
|
46 | - { |
|
47 | - return []; |
|
48 | - |
|
49 | - }//end getWarningList() |
|
18 | + /** |
|
19 | + * Returns the lines where errors should occur. |
|
20 | + * |
|
21 | + * The key of the array should represent the line number and the value |
|
22 | + * should represent the number of errors that should occur on that line. |
|
23 | + * |
|
24 | + * @return array<int, int> |
|
25 | + */ |
|
26 | + public function getErrorList() |
|
27 | + { |
|
28 | + return [ |
|
29 | + 5 => 1, |
|
30 | + 11 => 1, |
|
31 | + 17 => 2, |
|
32 | + ]; |
|
33 | + |
|
34 | + }//end getErrorList() |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns the lines where warnings should occur. |
|
39 | + * |
|
40 | + * The key of the array should represent the line number and the value |
|
41 | + * should represent the number of warnings that should occur on that line. |
|
42 | + * |
|
43 | + * @return array<int, int> |
|
44 | + */ |
|
45 | + public function getWarningList() |
|
46 | + { |
|
47 | + return []; |
|
48 | + |
|
49 | + }//end getWarningList() |
|
50 | 50 | |
51 | 51 | |
52 | 52 | }//end class |
@@ -52,8 +52,8 @@ discard block |
||
52 | 52 | |
53 | 53 | // Without brackets around inline IF condition. |
54 | 54 | $var1 === TRUE |
55 | - ? $var2 = 0 |
|
56 | - : $var2 = 1; |
|
55 | + ? $var2 = 0 |
|
56 | + : $var2 = 1; |
|
57 | 57 | |
58 | 58 | $var1 === TRUE ? $var2 = 0 : $var2 = 1; |
59 | 59 | ?> |
@@ -61,13 +61,13 @@ discard block |
||
61 | 61 | $var1 === TRUE ? $var2 = 0 : $var2 = 1; |
62 | 62 | |
63 | 63 | if ($var2 === TRUE) { |
64 | - $var1 === TRUE ? $var2 = 0 : $var2 = 1; |
|
64 | + $var1 === TRUE ? $var2 = 0 : $var2 = 1; |
|
65 | 65 | } |
66 | 66 | $var1 === TRUE ? $var2 = 0 : $var2 = 1; |
67 | 67 | |
68 | 68 | $var1 |
69 | - ? $var2 = 0 |
|
70 | - : $var2 = 1; |
|
69 | + ? $var2 = 0 |
|
70 | + : $var2 = 1; |
|
71 | 71 | |
72 | 72 | $var1 ? $var2 = 0 : $var2 = 1; |
73 | 73 | |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $var1 ? $var2 = 0 : $var2 = 1; |
76 | 76 | |
77 | 77 | if ($var2 === TRUE) { |
78 | - $var1 ? $var2 = 0 : $var2 = 1; |
|
78 | + $var1 ? $var2 = 0 : $var2 = 1; |
|
79 | 79 | } |
80 | 80 | $var1 ? $var2 = 0 : $var2 = 1; |
81 | 81 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | } |
9 | 9 | |
10 | 10 | if ($value) { |
11 | -} else if (!$value) { |
|
11 | +} else if ( ! $value) { |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | if (is_array($array) === TRUE) { |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | } |
21 | 21 | |
22 | 22 | if (is_array($array)) { |
23 | -} else if (!myFunction($value)) { |
|
23 | +} else if ( ! myFunction($value)) { |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | if ($value === TRUE || $other === FALSE) { |
@@ -29,13 +29,13 @@ discard block |
||
29 | 29 | if ($value == TRUE || $other == FALSE) { |
30 | 30 | } |
31 | 31 | |
32 | -if ($value || !$other) { |
|
32 | +if ($value || ! $other) { |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | if ($one === TRUE || $two === TRUE || $three === FALSE || $four === TRUE) { |
36 | 36 | } |
37 | 37 | |
38 | -if ($one || $two || !$three || $four) { |
|
38 | +if ($one || $two || ! $three || $four) { |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | if ($var instanceof PHP_CodeSniffer) { |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $var1 ? $var2 = 0 : $var2 = 1; |
81 | 81 | |
82 | 82 | if ($value) { |
83 | -} elseif (!$value) { |
|
83 | +} elseif ( ! $value) { |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | if (false === ($parent instanceof Foo) && ($parent instanceof Bar) === false) { |
@@ -97,11 +97,11 @@ discard block |
||
97 | 97 | |
98 | 98 | do { |
99 | 99 | |
100 | -} while ($var1); |
|
100 | +}while ($var1); |
|
101 | 101 | |
102 | 102 | do { |
103 | 103 | |
104 | -} while ($var1 === TRUE); |
|
104 | +}while ($var1 === TRUE); |
|
105 | 105 | |
106 | 106 | for ($var1 = 10; $var1; $var1--) { |
107 | 107 | } |
@@ -17,38 +17,38 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * Processes the tokens outside the scope. |
|
22 | - * |
|
23 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. |
|
24 | - * @param int $stackPtr The position where this token was |
|
25 | - * found. |
|
26 | - * |
|
27 | - * @return void |
|
28 | - */ |
|
29 | - protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
|
30 | - { |
|
31 | - $functionName = $phpcsFile->getDeclarationName($stackPtr); |
|
32 | - if ($functionName === null) { |
|
33 | - return; |
|
34 | - } |
|
35 | - |
|
36 | - $errorData = [$functionName]; |
|
37 | - |
|
38 | - // Does this function claim to be magical? |
|
39 | - if (preg_match('|^__[^_]|', $functionName) !== 0) { |
|
40 | - $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; |
|
41 | - $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData); |
|
42 | - |
|
43 | - $functionName = ltrim($functionName, '_'); |
|
44 | - } |
|
45 | - |
|
46 | - if (Common::isCamelCaps($functionName, false, true, false) === false) { |
|
47 | - $error = 'Function name "%s" is not in camel caps format'; |
|
48 | - $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); |
|
49 | - } |
|
50 | - |
|
51 | - }//end processTokenOutsideScope() |
|
20 | + /** |
|
21 | + * Processes the tokens outside the scope. |
|
22 | + * |
|
23 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. |
|
24 | + * @param int $stackPtr The position where this token was |
|
25 | + * found. |
|
26 | + * |
|
27 | + * @return void |
|
28 | + */ |
|
29 | + protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
|
30 | + { |
|
31 | + $functionName = $phpcsFile->getDeclarationName($stackPtr); |
|
32 | + if ($functionName === null) { |
|
33 | + return; |
|
34 | + } |
|
35 | + |
|
36 | + $errorData = [$functionName]; |
|
37 | + |
|
38 | + // Does this function claim to be magical? |
|
39 | + if (preg_match('|^__[^_]|', $functionName) !== 0) { |
|
40 | + $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; |
|
41 | + $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData); |
|
42 | + |
|
43 | + $functionName = ltrim($functionName, '_'); |
|
44 | + } |
|
45 | + |
|
46 | + if (Common::isCamelCaps($functionName, false, true, false) === false) { |
|
47 | + $error = 'Function name "%s" is not in camel caps format'; |
|
48 | + $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); |
|
49 | + } |
|
50 | + |
|
51 | + }//end processTokenOutsideScope() |
|
52 | 52 | |
53 | 53 | |
54 | 54 | }//end class |
@@ -17,43 +17,43 @@ |
||
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 Tokens::$booleanOperators; |
|
28 | - |
|
29 | - }//end register() |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Processes this test, when one of its tokens is encountered. |
|
34 | - * |
|
35 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
36 | - * @param int $stackPtr The position of the current token |
|
37 | - * in the stack passed in $tokens. |
|
38 | - * |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function process(File $phpcsFile, $stackPtr) |
|
42 | - { |
|
43 | - $tokens = $phpcsFile->getTokens(); |
|
44 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
45 | - foreach ($tokens[$stackPtr]['nested_parenthesis'] as $open => $close) { |
|
46 | - if (isset($tokens[$open]['parenthesis_owner']) === true) { |
|
47 | - // Any owner means we are not just a simple statement. |
|
48 | - return; |
|
49 | - } |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - $error = 'Boolean operators are not allowed outside of control structure conditions'; |
|
54 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
55 | - |
|
56 | - }//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 | + return Tokens::$booleanOperators; |
|
28 | + |
|
29 | + }//end register() |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Processes this test, when one of its tokens is encountered. |
|
34 | + * |
|
35 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
36 | + * @param int $stackPtr The position of the current token |
|
37 | + * in the stack passed in $tokens. |
|
38 | + * |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function process(File $phpcsFile, $stackPtr) |
|
42 | + { |
|
43 | + $tokens = $phpcsFile->getTokens(); |
|
44 | + if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
45 | + foreach ($tokens[$stackPtr]['nested_parenthesis'] as $open => $close) { |
|
46 | + if (isset($tokens[$open]['parenthesis_owner']) === true) { |
|
47 | + // Any owner means we are not just a simple statement. |
|
48 | + return; |
|
49 | + } |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + $error = 'Boolean operators are not allowed outside of control structure conditions'; |
|
54 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
55 | + |
|
56 | + }//end process() |
|
57 | 57 | |
58 | 58 | |
59 | 59 | }//end class |
@@ -16,38 +16,38 @@ |
||
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_GLOBAL]; |
|
27 | - |
|
28 | - }//end register() |
|
29 | - |
|
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 the |
|
36 | - * stack passed in $tokens. |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function process(File $phpcsFile, $stackPtr) |
|
41 | - { |
|
42 | - $tokens = $phpcsFile->getTokens(); |
|
43 | - |
|
44 | - $nextVar = $tokens[$phpcsFile->findNext([T_VARIABLE], $stackPtr)]; |
|
45 | - $varName = str_replace('$', '', $nextVar['content']); |
|
46 | - $error = 'Use of the "global" keyword is forbidden; use "$GLOBALS[\'%s\']" instead'; |
|
47 | - $data = [$varName]; |
|
48 | - $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data); |
|
49 | - |
|
50 | - }//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_GLOBAL]; |
|
27 | + |
|
28 | + }//end register() |
|
29 | + |
|
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 the |
|
36 | + * stack passed in $tokens. |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function process(File $phpcsFile, $stackPtr) |
|
41 | + { |
|
42 | + $tokens = $phpcsFile->getTokens(); |
|
43 | + |
|
44 | + $nextVar = $tokens[$phpcsFile->findNext([T_VARIABLE], $stackPtr)]; |
|
45 | + $varName = str_replace('$', '', $nextVar['content']); |
|
46 | + $error = 'Use of the "global" keyword is forbidden; use "$GLOBALS[\'%s\']" instead'; |
|
47 | + $data = [$varName]; |
|
48 | + $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data); |
|
49 | + |
|
50 | + }//end process() |
|
51 | 51 | |
52 | 52 | |
53 | 53 | }//end class |
@@ -15,43 +15,43 @@ |
||
15 | 15 | class DisallowInlineIfSniff implements Sniff |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * A list of tokenizers this sniff supports. |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - public $supportedTokenizers = [ |
|
24 | - 'PHP', |
|
25 | - 'JS', |
|
26 | - ]; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * Returns an array of tokens this test wants to listen for. |
|
31 | - * |
|
32 | - * @return array |
|
33 | - */ |
|
34 | - public function register() |
|
35 | - { |
|
36 | - return [T_INLINE_THEN]; |
|
37 | - |
|
38 | - }//end register() |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * Processes this test, when one of its tokens is encountered. |
|
43 | - * |
|
44 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
45 | - * @param int $stackPtr The position of the current token |
|
46 | - * in the stack passed in $tokens. |
|
47 | - * |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function process(File $phpcsFile, $stackPtr) |
|
51 | - { |
|
52 | - $phpcsFile->addError('Inline IF statements are not allowed', $stackPtr, 'Found'); |
|
53 | - |
|
54 | - }//end process() |
|
18 | + /** |
|
19 | + * A list of tokenizers this sniff supports. |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + public $supportedTokenizers = [ |
|
24 | + 'PHP', |
|
25 | + 'JS', |
|
26 | + ]; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * Returns an array of tokens this test wants to listen for. |
|
31 | + * |
|
32 | + * @return array |
|
33 | + */ |
|
34 | + public function register() |
|
35 | + { |
|
36 | + return [T_INLINE_THEN]; |
|
37 | + |
|
38 | + }//end register() |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * Processes this test, when one of its tokens is encountered. |
|
43 | + * |
|
44 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
45 | + * @param int $stackPtr The position of the current token |
|
46 | + * in the stack passed in $tokens. |
|
47 | + * |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function process(File $phpcsFile, $stackPtr) |
|
51 | + { |
|
52 | + $phpcsFile->addError('Inline IF statements are not allowed', $stackPtr, 'Found'); |
|
53 | + |
|
54 | + }//end process() |
|
55 | 55 | |
56 | 56 | |
57 | 57 | }//end class |
@@ -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 [ |
|
27 | - T_START_HEREDOC, |
|
28 | - T_START_NOWDOC, |
|
29 | - ]; |
|
30 | - |
|
31 | - }//end register() |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * Processes this test, when one of its tokens is encountered. |
|
36 | - * |
|
37 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
38 | - * @param int $stackPtr The position of the current token in the |
|
39 | - * stack passed in $tokens. |
|
40 | - * |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function process(File $phpcsFile, $stackPtr) |
|
44 | - { |
|
45 | - $error = 'Use of heredoc and nowdoc syntax ("<<<") is not allowed; use standard strings or inline HTML instead'; |
|
46 | - $phpcsFile->addError($error, $stackPtr, 'NotAllowed'); |
|
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 [ |
|
27 | + T_START_HEREDOC, |
|
28 | + T_START_NOWDOC, |
|
29 | + ]; |
|
30 | + |
|
31 | + }//end register() |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * Processes this test, when one of its tokens is encountered. |
|
36 | + * |
|
37 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
38 | + * @param int $stackPtr The position of the current token in the |
|
39 | + * stack passed in $tokens. |
|
40 | + * |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function process(File $phpcsFile, $stackPtr) |
|
44 | + { |
|
45 | + $error = 'Use of heredoc and nowdoc syntax ("<<<") is not allowed; use standard strings or inline HTML instead'; |
|
46 | + $phpcsFile->addError($error, $stackPtr, 'NotAllowed'); |
|
47 | + |
|
48 | + }//end process() |
|
49 | 49 | |
50 | 50 | |
51 | 51 | }//end class |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | $i = $stackPtr; |
122 | 122 | do { |
123 | 123 | $i++; |
124 | - } while ($tokens[$i]['line'] !== ($tokens[$stackPtr]['line'] + 1)); |
|
124 | + }while ($tokens[$i]['line'] !== ($tokens[$stackPtr]['line'] + 1)); |
|
125 | 125 | |
126 | 126 | $error = 'Blank line found at start of embedded PHP content'; |
127 | 127 | $fix = $phpcsFile->addFixableError($error, $i, 'SpacingBefore'); |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | $indent, |
157 | 157 | $contentColumn, |
158 | 158 | ]; |
159 | - $fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data); |
|
159 | + $fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data); |
|
160 | 160 | if ($fix === true) { |
161 | 161 | $padding = str_repeat(' ', $indent); |
162 | 162 | if ($contentColumn === 0) { |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | $expected, |
215 | 215 | $found, |
216 | 216 | ]; |
217 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenTagIndent', $data); |
|
217 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenTagIndent', $data); |
|
218 | 218 | if ($fix === true) { |
219 | 219 | $phpcsFile->fixer->replaceToken(($stackPtr - 1), str_repeat(' ', $expected)); |
220 | 220 | } |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | $i = $closingTag; |
265 | 265 | do { |
266 | 266 | $i--; |
267 | - } while ($tokens[$i]['line'] !== ($tokens[$closingTag]['line'] - 1)); |
|
267 | + }while ($tokens[$i]['line'] !== ($tokens[$closingTag]['line'] - 1)); |
|
268 | 268 | |
269 | 269 | $error = 'Blank line found at end of embedded PHP content'; |
270 | 270 | $fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter'); |
@@ -17,386 +17,386 @@ |
||
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 [T_OPEN_TAG]; |
|
28 | - |
|
29 | - }//end register() |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Processes this test, when one of its tokens is encountered. |
|
34 | - * |
|
35 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
36 | - * @param int $stackPtr The position of the current token in the |
|
37 | - * stack passed in $tokens. |
|
38 | - * |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function process(File $phpcsFile, $stackPtr) |
|
42 | - { |
|
43 | - $tokens = $phpcsFile->getTokens(); |
|
44 | - |
|
45 | - // If the close php tag is on the same line as the opening |
|
46 | - // then we have an inline embedded PHP block. |
|
47 | - $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
48 | - if ($closeTag === false || $tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { |
|
49 | - $this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr); |
|
50 | - } else { |
|
51 | - $this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr); |
|
52 | - } |
|
53 | - |
|
54 | - }//end process() |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Validates embedded PHP that exists on multiple lines. |
|
59 | - * |
|
60 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
61 | - * @param int $stackPtr The position of the current token in the |
|
62 | - * stack passed in $tokens. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) |
|
67 | - { |
|
68 | - $tokens = $phpcsFile->getTokens(); |
|
69 | - |
|
70 | - $prevTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
|
71 | - if ($prevTag === false) { |
|
72 | - // This is the first open tag. |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
77 | - $closingTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
78 | - if ($closingTag !== false) { |
|
79 | - $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), $phpcsFile->numTokens, true); |
|
80 | - if ($nextContent === false) { |
|
81 | - // Final closing tag. It will be handled elsewhere. |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - // We have an opening and a closing tag, that lie within other content. |
|
86 | - if ($firstContent === $closingTag) { |
|
87 | - $error = 'Empty embedded PHP tag found'; |
|
88 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
|
89 | - if ($fix === true) { |
|
90 | - $phpcsFile->fixer->beginChangeset(); |
|
91 | - for ($i = $stackPtr; $i <= $closingTag; $i++) { |
|
92 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
93 | - } |
|
94 | - |
|
95 | - $phpcsFile->fixer->endChangeset(); |
|
96 | - } |
|
97 | - |
|
98 | - return; |
|
99 | - } |
|
100 | - }//end if |
|
101 | - |
|
102 | - if ($tokens[$firstContent]['line'] === $tokens[$stackPtr]['line']) { |
|
103 | - $error = 'Opening PHP tag must be on a line by itself'; |
|
104 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
|
105 | - if ($fix === true) { |
|
106 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true); |
|
107 | - $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
108 | - $phpcsFile->fixer->beginChangeset(); |
|
109 | - $phpcsFile->fixer->addNewline($stackPtr); |
|
110 | - $phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $padding)); |
|
111 | - $phpcsFile->fixer->endChangeset(); |
|
112 | - } |
|
113 | - } else { |
|
114 | - // Check the indent of the first line, except if it is a scope closer. |
|
115 | - if (isset($tokens[$firstContent]['scope_closer']) === false |
|
116 | - || $tokens[$firstContent]['scope_closer'] !== $firstContent |
|
117 | - ) { |
|
118 | - // Check for a blank line at the top. |
|
119 | - if ($tokens[$firstContent]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
120 | - // Find a token on the blank line to throw the error on. |
|
121 | - $i = $stackPtr; |
|
122 | - do { |
|
123 | - $i++; |
|
124 | - } while ($tokens[$i]['line'] !== ($tokens[$stackPtr]['line'] + 1)); |
|
125 | - |
|
126 | - $error = 'Blank line found at start of embedded PHP content'; |
|
127 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpacingBefore'); |
|
128 | - if ($fix === true) { |
|
129 | - $phpcsFile->fixer->beginChangeset(); |
|
130 | - for ($i = ($stackPtr + 1); $i < $firstContent; $i++) { |
|
131 | - if ($tokens[$i]['line'] === $tokens[$firstContent]['line'] |
|
132 | - || $tokens[$i]['line'] === $tokens[$stackPtr]['line'] |
|
133 | - ) { |
|
134 | - continue; |
|
135 | - } |
|
136 | - |
|
137 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
138 | - } |
|
139 | - |
|
140 | - $phpcsFile->fixer->endChangeset(); |
|
141 | - } |
|
142 | - }//end if |
|
143 | - |
|
144 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); |
|
145 | - if ($first === false) { |
|
146 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
147 | - $indent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
148 | - } else { |
|
149 | - $indent = ($tokens[($first + 1)]['column'] - 1); |
|
150 | - } |
|
151 | - |
|
152 | - $contentColumn = ($tokens[$firstContent]['column'] - 1); |
|
153 | - if ($contentColumn !== $indent) { |
|
154 | - $error = 'First line of embedded PHP code must be indented %s spaces; %s found'; |
|
155 | - $data = [ |
|
156 | - $indent, |
|
157 | - $contentColumn, |
|
158 | - ]; |
|
159 | - $fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data); |
|
160 | - if ($fix === true) { |
|
161 | - $padding = str_repeat(' ', $indent); |
|
162 | - if ($contentColumn === 0) { |
|
163 | - $phpcsFile->fixer->addContentBefore($firstContent, $padding); |
|
164 | - } else { |
|
165 | - $phpcsFile->fixer->replaceToken(($firstContent - 1), $padding); |
|
166 | - } |
|
167 | - } |
|
168 | - } |
|
169 | - }//end if |
|
170 | - }//end if |
|
171 | - |
|
172 | - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
173 | - if ($tokens[$lastContent]['line'] === $tokens[$stackPtr]['line'] |
|
174 | - && trim($tokens[$lastContent]['content']) !== '' |
|
175 | - ) { |
|
176 | - $error = 'Opening PHP tag must be on a line by itself'; |
|
177 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeOpen'); |
|
178 | - if ($fix === true) { |
|
179 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); |
|
180 | - if ($first === false) { |
|
181 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
182 | - $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
183 | - } else { |
|
184 | - $padding = ($tokens[($first + 1)]['column'] - 1); |
|
185 | - } |
|
186 | - |
|
187 | - $phpcsFile->fixer->addContentBefore($stackPtr, $phpcsFile->eolChar.str_repeat(' ', $padding)); |
|
188 | - } |
|
189 | - } else { |
|
190 | - // Find the first token on the first non-empty line we find. |
|
191 | - for ($first = ($stackPtr - 1); $first > 0; $first--) { |
|
192 | - if ($tokens[$first]['line'] === $tokens[$stackPtr]['line']) { |
|
193 | - continue; |
|
194 | - } else if (trim($tokens[$first]['content']) !== '') { |
|
195 | - $first = $phpcsFile->findFirstOnLine([], $first, true); |
|
196 | - break; |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - $expected = 0; |
|
201 | - if ($tokens[$first]['code'] === T_INLINE_HTML |
|
202 | - && trim($tokens[$first]['content']) !== '' |
|
203 | - ) { |
|
204 | - $expected = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
205 | - } else if ($tokens[$first]['code'] === T_WHITESPACE) { |
|
206 | - $expected = ($tokens[($first + 1)]['column'] - 1); |
|
207 | - } |
|
208 | - |
|
209 | - $expected += 4; |
|
210 | - $found = ($tokens[$stackPtr]['column'] - 1); |
|
211 | - if ($found > $expected) { |
|
212 | - $error = 'Opening PHP tag indent incorrect; expected no more than %s spaces but found %s'; |
|
213 | - $data = [ |
|
214 | - $expected, |
|
215 | - $found, |
|
216 | - ]; |
|
217 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenTagIndent', $data); |
|
218 | - if ($fix === true) { |
|
219 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), str_repeat(' ', $expected)); |
|
220 | - } |
|
221 | - } |
|
222 | - }//end if |
|
223 | - |
|
224 | - if ($closingTag === false) { |
|
225 | - return; |
|
226 | - } |
|
227 | - |
|
228 | - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closingTag - 1), ($stackPtr + 1), true); |
|
229 | - $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), null, true); |
|
230 | - |
|
231 | - if ($tokens[$lastContent]['line'] === $tokens[$closingTag]['line']) { |
|
232 | - $error = 'Closing PHP tag must be on a line by itself'; |
|
233 | - $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentBeforeEnd'); |
|
234 | - if ($fix === true) { |
|
235 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); |
|
236 | - $phpcsFile->fixer->beginChangeset(); |
|
237 | - $phpcsFile->fixer->addContentBefore($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); |
|
238 | - $phpcsFile->fixer->addNewlineBefore($closingTag); |
|
239 | - $phpcsFile->fixer->endChangeset(); |
|
240 | - } |
|
241 | - } else if ($tokens[$nextContent]['line'] === $tokens[$closingTag]['line']) { |
|
242 | - $error = 'Closing PHP tag must be on a line by itself'; |
|
243 | - $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentAfterEnd'); |
|
244 | - if ($fix === true) { |
|
245 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); |
|
246 | - $phpcsFile->fixer->beginChangeset(); |
|
247 | - $phpcsFile->fixer->addNewline($closingTag); |
|
248 | - $phpcsFile->fixer->addContent($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); |
|
249 | - $phpcsFile->fixer->endChangeset(); |
|
250 | - } |
|
251 | - }//end if |
|
252 | - |
|
253 | - $next = $phpcsFile->findNext(T_OPEN_TAG, ($closingTag + 1)); |
|
254 | - if ($next === false) { |
|
255 | - return; |
|
256 | - } |
|
257 | - |
|
258 | - // Check for a blank line at the bottom. |
|
259 | - if ((isset($tokens[$lastContent]['scope_closer']) === false |
|
260 | - || $tokens[$lastContent]['scope_closer'] !== $lastContent) |
|
261 | - && $tokens[$lastContent]['line'] < ($tokens[$closingTag]['line'] - 1) |
|
262 | - ) { |
|
263 | - // Find a token on the blank line to throw the error on. |
|
264 | - $i = $closingTag; |
|
265 | - do { |
|
266 | - $i--; |
|
267 | - } while ($tokens[$i]['line'] !== ($tokens[$closingTag]['line'] - 1)); |
|
268 | - |
|
269 | - $error = 'Blank line found at end of embedded PHP content'; |
|
270 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter'); |
|
271 | - if ($fix === true) { |
|
272 | - $phpcsFile->fixer->beginChangeset(); |
|
273 | - for ($i = ($lastContent + 1); $i < $closingTag; $i++) { |
|
274 | - if ($tokens[$i]['line'] === $tokens[$lastContent]['line'] |
|
275 | - || $tokens[$i]['line'] === $tokens[$closingTag]['line'] |
|
276 | - ) { |
|
277 | - continue; |
|
278 | - } |
|
279 | - |
|
280 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
281 | - } |
|
282 | - |
|
283 | - $phpcsFile->fixer->endChangeset(); |
|
284 | - } |
|
285 | - }//end if |
|
286 | - |
|
287 | - }//end validateMultilineEmbeddedPhp() |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Validates embedded PHP that exists on one line. |
|
292 | - * |
|
293 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
294 | - * @param int $stackPtr The position of the current token in the |
|
295 | - * stack passed in $tokens. |
|
296 | - * |
|
297 | - * @return void |
|
298 | - */ |
|
299 | - private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) |
|
300 | - { |
|
301 | - $tokens = $phpcsFile->getTokens(); |
|
302 | - |
|
303 | - // We only want one line PHP sections, so return if the closing tag is |
|
304 | - // on the next line. |
|
305 | - $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr, null, false); |
|
306 | - if ($tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { |
|
307 | - return; |
|
308 | - } |
|
309 | - |
|
310 | - // Check that there is one, and only one space at the start of the statement. |
|
311 | - $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $closeTag, true); |
|
312 | - |
|
313 | - if ($firstContent === false) { |
|
314 | - $error = 'Empty embedded PHP tag found'; |
|
315 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
|
316 | - if ($fix === true) { |
|
317 | - $phpcsFile->fixer->beginChangeset(); |
|
318 | - for ($i = $stackPtr; $i <= $closeTag; $i++) { |
|
319 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
320 | - } |
|
321 | - |
|
322 | - $phpcsFile->fixer->endChangeset(); |
|
323 | - } |
|
324 | - |
|
325 | - return; |
|
326 | - } |
|
327 | - |
|
328 | - // The open tag token always contains a single space after it. |
|
329 | - $leadingSpace = 1; |
|
330 | - if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
331 | - $leadingSpace = ($tokens[($stackPtr + 1)]['length'] + 1); |
|
332 | - } |
|
333 | - |
|
334 | - if ($leadingSpace !== 1) { |
|
335 | - $error = 'Expected 1 space after opening PHP tag; %s found'; |
|
336 | - $data = [$leadingSpace]; |
|
337 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
338 | - if ($fix === true) { |
|
339 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($closeTag - 1), $stackPtr, true); |
|
344 | - if ($prev !== $stackPtr) { |
|
345 | - if ((isset($tokens[$prev]['scope_opener']) === false |
|
346 | - || $tokens[$prev]['scope_opener'] !== $prev) |
|
347 | - && (isset($tokens[$prev]['scope_closer']) === false |
|
348 | - || $tokens[$prev]['scope_closer'] !== $prev) |
|
349 | - && $tokens[$prev]['code'] !== T_SEMICOLON |
|
350 | - ) { |
|
351 | - $error = 'Inline PHP statement must end with a semicolon'; |
|
352 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSemicolon'); |
|
353 | - if ($fix === true) { |
|
354 | - $phpcsFile->fixer->addContent($prev, ';'); |
|
355 | - } |
|
356 | - } else if ($tokens[$prev]['code'] === T_SEMICOLON) { |
|
357 | - $statementCount = 1; |
|
358 | - for ($i = ($stackPtr + 1); $i < $prev; $i++) { |
|
359 | - if ($tokens[$i]['code'] === T_SEMICOLON) { |
|
360 | - $statementCount++; |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - if ($statementCount > 1) { |
|
365 | - $error = 'Inline PHP statement must contain a single statement; %s found'; |
|
366 | - $data = [$statementCount]; |
|
367 | - $phpcsFile->addError($error, $stackPtr, 'MultipleStatements', $data); |
|
368 | - } |
|
369 | - } |
|
370 | - }//end if |
|
371 | - |
|
372 | - $trailingSpace = 0; |
|
373 | - if ($tokens[($closeTag - 1)]['code'] === T_WHITESPACE) { |
|
374 | - $trailingSpace = $tokens[($closeTag - 1)]['length']; |
|
375 | - } else if (($tokens[($closeTag - 1)]['code'] === T_COMMENT |
|
376 | - || isset(Tokens::$phpcsCommentTokens[$tokens[($closeTag - 1)]['code']]) === true) |
|
377 | - && substr($tokens[($closeTag - 1)]['content'], -1) === ' ' |
|
378 | - ) { |
|
379 | - $trailingSpace = (strlen($tokens[($closeTag - 1)]['content']) - strlen(rtrim($tokens[($closeTag - 1)]['content']))); |
|
380 | - } |
|
381 | - |
|
382 | - if ($trailingSpace !== 1) { |
|
383 | - $error = 'Expected 1 space before closing PHP tag; %s found'; |
|
384 | - $data = [$trailingSpace]; |
|
385 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeClose', $data); |
|
386 | - if ($fix === true) { |
|
387 | - if ($trailingSpace === 0) { |
|
388 | - $phpcsFile->fixer->addContentBefore($closeTag, ' '); |
|
389 | - } else if ($tokens[($closeTag - 1)]['code'] === T_COMMENT |
|
390 | - || isset(Tokens::$phpcsCommentTokens[$tokens[($closeTag - 1)]['code']]) === true |
|
391 | - ) { |
|
392 | - $phpcsFile->fixer->replaceToken(($closeTag - 1), rtrim($tokens[($closeTag - 1)]['content']).' '); |
|
393 | - } else { |
|
394 | - $phpcsFile->fixer->replaceToken(($closeTag - 1), ' '); |
|
395 | - } |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - }//end validateInlineEmbeddedPhp() |
|
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 [T_OPEN_TAG]; |
|
28 | + |
|
29 | + }//end register() |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Processes this test, when one of its tokens is encountered. |
|
34 | + * |
|
35 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
36 | + * @param int $stackPtr The position of the current token in the |
|
37 | + * stack passed in $tokens. |
|
38 | + * |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function process(File $phpcsFile, $stackPtr) |
|
42 | + { |
|
43 | + $tokens = $phpcsFile->getTokens(); |
|
44 | + |
|
45 | + // If the close php tag is on the same line as the opening |
|
46 | + // then we have an inline embedded PHP block. |
|
47 | + $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
48 | + if ($closeTag === false || $tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { |
|
49 | + $this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr); |
|
50 | + } else { |
|
51 | + $this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr); |
|
52 | + } |
|
53 | + |
|
54 | + }//end process() |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Validates embedded PHP that exists on multiple lines. |
|
59 | + * |
|
60 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
61 | + * @param int $stackPtr The position of the current token in the |
|
62 | + * stack passed in $tokens. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) |
|
67 | + { |
|
68 | + $tokens = $phpcsFile->getTokens(); |
|
69 | + |
|
70 | + $prevTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
|
71 | + if ($prevTag === false) { |
|
72 | + // This is the first open tag. |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
77 | + $closingTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); |
|
78 | + if ($closingTag !== false) { |
|
79 | + $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), $phpcsFile->numTokens, true); |
|
80 | + if ($nextContent === false) { |
|
81 | + // Final closing tag. It will be handled elsewhere. |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + // We have an opening and a closing tag, that lie within other content. |
|
86 | + if ($firstContent === $closingTag) { |
|
87 | + $error = 'Empty embedded PHP tag found'; |
|
88 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
|
89 | + if ($fix === true) { |
|
90 | + $phpcsFile->fixer->beginChangeset(); |
|
91 | + for ($i = $stackPtr; $i <= $closingTag; $i++) { |
|
92 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
93 | + } |
|
94 | + |
|
95 | + $phpcsFile->fixer->endChangeset(); |
|
96 | + } |
|
97 | + |
|
98 | + return; |
|
99 | + } |
|
100 | + }//end if |
|
101 | + |
|
102 | + if ($tokens[$firstContent]['line'] === $tokens[$stackPtr]['line']) { |
|
103 | + $error = 'Opening PHP tag must be on a line by itself'; |
|
104 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
|
105 | + if ($fix === true) { |
|
106 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true); |
|
107 | + $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
108 | + $phpcsFile->fixer->beginChangeset(); |
|
109 | + $phpcsFile->fixer->addNewline($stackPtr); |
|
110 | + $phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $padding)); |
|
111 | + $phpcsFile->fixer->endChangeset(); |
|
112 | + } |
|
113 | + } else { |
|
114 | + // Check the indent of the first line, except if it is a scope closer. |
|
115 | + if (isset($tokens[$firstContent]['scope_closer']) === false |
|
116 | + || $tokens[$firstContent]['scope_closer'] !== $firstContent |
|
117 | + ) { |
|
118 | + // Check for a blank line at the top. |
|
119 | + if ($tokens[$firstContent]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
120 | + // Find a token on the blank line to throw the error on. |
|
121 | + $i = $stackPtr; |
|
122 | + do { |
|
123 | + $i++; |
|
124 | + } while ($tokens[$i]['line'] !== ($tokens[$stackPtr]['line'] + 1)); |
|
125 | + |
|
126 | + $error = 'Blank line found at start of embedded PHP content'; |
|
127 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpacingBefore'); |
|
128 | + if ($fix === true) { |
|
129 | + $phpcsFile->fixer->beginChangeset(); |
|
130 | + for ($i = ($stackPtr + 1); $i < $firstContent; $i++) { |
|
131 | + if ($tokens[$i]['line'] === $tokens[$firstContent]['line'] |
|
132 | + || $tokens[$i]['line'] === $tokens[$stackPtr]['line'] |
|
133 | + ) { |
|
134 | + continue; |
|
135 | + } |
|
136 | + |
|
137 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
138 | + } |
|
139 | + |
|
140 | + $phpcsFile->fixer->endChangeset(); |
|
141 | + } |
|
142 | + }//end if |
|
143 | + |
|
144 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); |
|
145 | + if ($first === false) { |
|
146 | + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
147 | + $indent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
148 | + } else { |
|
149 | + $indent = ($tokens[($first + 1)]['column'] - 1); |
|
150 | + } |
|
151 | + |
|
152 | + $contentColumn = ($tokens[$firstContent]['column'] - 1); |
|
153 | + if ($contentColumn !== $indent) { |
|
154 | + $error = 'First line of embedded PHP code must be indented %s spaces; %s found'; |
|
155 | + $data = [ |
|
156 | + $indent, |
|
157 | + $contentColumn, |
|
158 | + ]; |
|
159 | + $fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data); |
|
160 | + if ($fix === true) { |
|
161 | + $padding = str_repeat(' ', $indent); |
|
162 | + if ($contentColumn === 0) { |
|
163 | + $phpcsFile->fixer->addContentBefore($firstContent, $padding); |
|
164 | + } else { |
|
165 | + $phpcsFile->fixer->replaceToken(($firstContent - 1), $padding); |
|
166 | + } |
|
167 | + } |
|
168 | + } |
|
169 | + }//end if |
|
170 | + }//end if |
|
171 | + |
|
172 | + $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
173 | + if ($tokens[$lastContent]['line'] === $tokens[$stackPtr]['line'] |
|
174 | + && trim($tokens[$lastContent]['content']) !== '' |
|
175 | + ) { |
|
176 | + $error = 'Opening PHP tag must be on a line by itself'; |
|
177 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeOpen'); |
|
178 | + if ($fix === true) { |
|
179 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); |
|
180 | + if ($first === false) { |
|
181 | + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
182 | + $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
183 | + } else { |
|
184 | + $padding = ($tokens[($first + 1)]['column'] - 1); |
|
185 | + } |
|
186 | + |
|
187 | + $phpcsFile->fixer->addContentBefore($stackPtr, $phpcsFile->eolChar.str_repeat(' ', $padding)); |
|
188 | + } |
|
189 | + } else { |
|
190 | + // Find the first token on the first non-empty line we find. |
|
191 | + for ($first = ($stackPtr - 1); $first > 0; $first--) { |
|
192 | + if ($tokens[$first]['line'] === $tokens[$stackPtr]['line']) { |
|
193 | + continue; |
|
194 | + } else if (trim($tokens[$first]['content']) !== '') { |
|
195 | + $first = $phpcsFile->findFirstOnLine([], $first, true); |
|
196 | + break; |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + $expected = 0; |
|
201 | + if ($tokens[$first]['code'] === T_INLINE_HTML |
|
202 | + && trim($tokens[$first]['content']) !== '' |
|
203 | + ) { |
|
204 | + $expected = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
205 | + } else if ($tokens[$first]['code'] === T_WHITESPACE) { |
|
206 | + $expected = ($tokens[($first + 1)]['column'] - 1); |
|
207 | + } |
|
208 | + |
|
209 | + $expected += 4; |
|
210 | + $found = ($tokens[$stackPtr]['column'] - 1); |
|
211 | + if ($found > $expected) { |
|
212 | + $error = 'Opening PHP tag indent incorrect; expected no more than %s spaces but found %s'; |
|
213 | + $data = [ |
|
214 | + $expected, |
|
215 | + $found, |
|
216 | + ]; |
|
217 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenTagIndent', $data); |
|
218 | + if ($fix === true) { |
|
219 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), str_repeat(' ', $expected)); |
|
220 | + } |
|
221 | + } |
|
222 | + }//end if |
|
223 | + |
|
224 | + if ($closingTag === false) { |
|
225 | + return; |
|
226 | + } |
|
227 | + |
|
228 | + $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closingTag - 1), ($stackPtr + 1), true); |
|
229 | + $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), null, true); |
|
230 | + |
|
231 | + if ($tokens[$lastContent]['line'] === $tokens[$closingTag]['line']) { |
|
232 | + $error = 'Closing PHP tag must be on a line by itself'; |
|
233 | + $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentBeforeEnd'); |
|
234 | + if ($fix === true) { |
|
235 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); |
|
236 | + $phpcsFile->fixer->beginChangeset(); |
|
237 | + $phpcsFile->fixer->addContentBefore($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); |
|
238 | + $phpcsFile->fixer->addNewlineBefore($closingTag); |
|
239 | + $phpcsFile->fixer->endChangeset(); |
|
240 | + } |
|
241 | + } else if ($tokens[$nextContent]['line'] === $tokens[$closingTag]['line']) { |
|
242 | + $error = 'Closing PHP tag must be on a line by itself'; |
|
243 | + $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentAfterEnd'); |
|
244 | + if ($fix === true) { |
|
245 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); |
|
246 | + $phpcsFile->fixer->beginChangeset(); |
|
247 | + $phpcsFile->fixer->addNewline($closingTag); |
|
248 | + $phpcsFile->fixer->addContent($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); |
|
249 | + $phpcsFile->fixer->endChangeset(); |
|
250 | + } |
|
251 | + }//end if |
|
252 | + |
|
253 | + $next = $phpcsFile->findNext(T_OPEN_TAG, ($closingTag + 1)); |
|
254 | + if ($next === false) { |
|
255 | + return; |
|
256 | + } |
|
257 | + |
|
258 | + // Check for a blank line at the bottom. |
|
259 | + if ((isset($tokens[$lastContent]['scope_closer']) === false |
|
260 | + || $tokens[$lastContent]['scope_closer'] !== $lastContent) |
|
261 | + && $tokens[$lastContent]['line'] < ($tokens[$closingTag]['line'] - 1) |
|
262 | + ) { |
|
263 | + // Find a token on the blank line to throw the error on. |
|
264 | + $i = $closingTag; |
|
265 | + do { |
|
266 | + $i--; |
|
267 | + } while ($tokens[$i]['line'] !== ($tokens[$closingTag]['line'] - 1)); |
|
268 | + |
|
269 | + $error = 'Blank line found at end of embedded PHP content'; |
|
270 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter'); |
|
271 | + if ($fix === true) { |
|
272 | + $phpcsFile->fixer->beginChangeset(); |
|
273 | + for ($i = ($lastContent + 1); $i < $closingTag; $i++) { |
|
274 | + if ($tokens[$i]['line'] === $tokens[$lastContent]['line'] |
|
275 | + || $tokens[$i]['line'] === $tokens[$closingTag]['line'] |
|
276 | + ) { |
|
277 | + continue; |
|
278 | + } |
|
279 | + |
|
280 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
281 | + } |
|
282 | + |
|
283 | + $phpcsFile->fixer->endChangeset(); |
|
284 | + } |
|
285 | + }//end if |
|
286 | + |
|
287 | + }//end validateMultilineEmbeddedPhp() |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Validates embedded PHP that exists on one line. |
|
292 | + * |
|
293 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
294 | + * @param int $stackPtr The position of the current token in the |
|
295 | + * stack passed in $tokens. |
|
296 | + * |
|
297 | + * @return void |
|
298 | + */ |
|
299 | + private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) |
|
300 | + { |
|
301 | + $tokens = $phpcsFile->getTokens(); |
|
302 | + |
|
303 | + // We only want one line PHP sections, so return if the closing tag is |
|
304 | + // on the next line. |
|
305 | + $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr, null, false); |
|
306 | + if ($tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { |
|
307 | + return; |
|
308 | + } |
|
309 | + |
|
310 | + // Check that there is one, and only one space at the start of the statement. |
|
311 | + $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $closeTag, true); |
|
312 | + |
|
313 | + if ($firstContent === false) { |
|
314 | + $error = 'Empty embedded PHP tag found'; |
|
315 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
|
316 | + if ($fix === true) { |
|
317 | + $phpcsFile->fixer->beginChangeset(); |
|
318 | + for ($i = $stackPtr; $i <= $closeTag; $i++) { |
|
319 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
320 | + } |
|
321 | + |
|
322 | + $phpcsFile->fixer->endChangeset(); |
|
323 | + } |
|
324 | + |
|
325 | + return; |
|
326 | + } |
|
327 | + |
|
328 | + // The open tag token always contains a single space after it. |
|
329 | + $leadingSpace = 1; |
|
330 | + if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
331 | + $leadingSpace = ($tokens[($stackPtr + 1)]['length'] + 1); |
|
332 | + } |
|
333 | + |
|
334 | + if ($leadingSpace !== 1) { |
|
335 | + $error = 'Expected 1 space after opening PHP tag; %s found'; |
|
336 | + $data = [$leadingSpace]; |
|
337 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
338 | + if ($fix === true) { |
|
339 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($closeTag - 1), $stackPtr, true); |
|
344 | + if ($prev !== $stackPtr) { |
|
345 | + if ((isset($tokens[$prev]['scope_opener']) === false |
|
346 | + || $tokens[$prev]['scope_opener'] !== $prev) |
|
347 | + && (isset($tokens[$prev]['scope_closer']) === false |
|
348 | + || $tokens[$prev]['scope_closer'] !== $prev) |
|
349 | + && $tokens[$prev]['code'] !== T_SEMICOLON |
|
350 | + ) { |
|
351 | + $error = 'Inline PHP statement must end with a semicolon'; |
|
352 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSemicolon'); |
|
353 | + if ($fix === true) { |
|
354 | + $phpcsFile->fixer->addContent($prev, ';'); |
|
355 | + } |
|
356 | + } else if ($tokens[$prev]['code'] === T_SEMICOLON) { |
|
357 | + $statementCount = 1; |
|
358 | + for ($i = ($stackPtr + 1); $i < $prev; $i++) { |
|
359 | + if ($tokens[$i]['code'] === T_SEMICOLON) { |
|
360 | + $statementCount++; |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + if ($statementCount > 1) { |
|
365 | + $error = 'Inline PHP statement must contain a single statement; %s found'; |
|
366 | + $data = [$statementCount]; |
|
367 | + $phpcsFile->addError($error, $stackPtr, 'MultipleStatements', $data); |
|
368 | + } |
|
369 | + } |
|
370 | + }//end if |
|
371 | + |
|
372 | + $trailingSpace = 0; |
|
373 | + if ($tokens[($closeTag - 1)]['code'] === T_WHITESPACE) { |
|
374 | + $trailingSpace = $tokens[($closeTag - 1)]['length']; |
|
375 | + } else if (($tokens[($closeTag - 1)]['code'] === T_COMMENT |
|
376 | + || isset(Tokens::$phpcsCommentTokens[$tokens[($closeTag - 1)]['code']]) === true) |
|
377 | + && substr($tokens[($closeTag - 1)]['content'], -1) === ' ' |
|
378 | + ) { |
|
379 | + $trailingSpace = (strlen($tokens[($closeTag - 1)]['content']) - strlen(rtrim($tokens[($closeTag - 1)]['content']))); |
|
380 | + } |
|
381 | + |
|
382 | + if ($trailingSpace !== 1) { |
|
383 | + $error = 'Expected 1 space before closing PHP tag; %s found'; |
|
384 | + $data = [$trailingSpace]; |
|
385 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeClose', $data); |
|
386 | + if ($fix === true) { |
|
387 | + if ($trailingSpace === 0) { |
|
388 | + $phpcsFile->fixer->addContentBefore($closeTag, ' '); |
|
389 | + } else if ($tokens[($closeTag - 1)]['code'] === T_COMMENT |
|
390 | + || isset(Tokens::$phpcsCommentTokens[$tokens[($closeTag - 1)]['code']]) === true |
|
391 | + ) { |
|
392 | + $phpcsFile->fixer->replaceToken(($closeTag - 1), rtrim($tokens[($closeTag - 1)]['content']).' '); |
|
393 | + } else { |
|
394 | + $phpcsFile->fixer->replaceToken(($closeTag - 1), ' '); |
|
395 | + } |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + }//end validateInlineEmbeddedPhp() |
|
400 | 400 | |
401 | 401 | |
402 | 402 | }//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_EVAL]; |
|
27 | - |
|
28 | - }//end register() |
|
29 | - |
|
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 | - $error = 'Use of eval() is discouraged'; |
|
43 | - $phpcsFile->addWarning($error, $stackPtr, 'Discouraged'); |
|
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_EVAL]; |
|
27 | + |
|
28 | + }//end register() |
|
29 | + |
|
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 | + $error = 'Use of eval() is discouraged'; |
|
43 | + $phpcsFile->addWarning($error, $stackPtr, 'Discouraged'); |
|
44 | + |
|
45 | + }//end process() |
|
46 | 46 | |
47 | 47 | |
48 | 48 | }//end class |