@@ -34,68 +34,68 @@ |
||
34 | 34 | { |
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * Registers the tokens that this sniff wants to listen for. |
|
39 | - * |
|
40 | - * @return int[] |
|
41 | - */ |
|
42 | - public function register() |
|
43 | - { |
|
44 | - return [T_FOR]; |
|
45 | - |
|
46 | - }//end register() |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Processes this test, when one of its tokens is encountered. |
|
51 | - * |
|
52 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
53 | - * @param int $stackPtr The position of the current token |
|
54 | - * in the stack passed in $tokens. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function process(File $phpcsFile, $stackPtr) |
|
59 | - { |
|
60 | - $tokens = $phpcsFile->getTokens(); |
|
61 | - $token = $tokens[$stackPtr]; |
|
62 | - |
|
63 | - // Skip invalid statement. |
|
64 | - if (isset($token['parenthesis_opener']) === false) { |
|
65 | - return; |
|
66 | - } |
|
67 | - |
|
68 | - $next = ++$token['parenthesis_opener']; |
|
69 | - $end = --$token['parenthesis_closer']; |
|
70 | - |
|
71 | - $position = 0; |
|
72 | - |
|
73 | - for (; $next <= $end; ++$next) { |
|
74 | - $code = $tokens[$next]['code']; |
|
75 | - if ($code === T_SEMICOLON) { |
|
76 | - ++$position; |
|
77 | - } |
|
78 | - |
|
79 | - if ($position < 1) { |
|
80 | - continue; |
|
81 | - } else if ($position > 1) { |
|
82 | - break; |
|
83 | - } else if ($code !== T_VARIABLE && $code !== T_STRING) { |
|
84 | - continue; |
|
85 | - } |
|
86 | - |
|
87 | - // Find next non empty token, if it is a open curly brace we have a |
|
88 | - // function call. |
|
89 | - $index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
90 | - |
|
91 | - if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) { |
|
92 | - $error = 'Avoid function calls in a FOR loop test part'; |
|
93 | - $phpcsFile->addWarning($error, $stackPtr, 'NotAllowed'); |
|
94 | - break; |
|
95 | - } |
|
96 | - }//end for |
|
97 | - |
|
98 | - }//end process() |
|
37 | + /** |
|
38 | + * Registers the tokens that this sniff wants to listen for. |
|
39 | + * |
|
40 | + * @return int[] |
|
41 | + */ |
|
42 | + public function register() |
|
43 | + { |
|
44 | + return [T_FOR]; |
|
45 | + |
|
46 | + }//end register() |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Processes this test, when one of its tokens is encountered. |
|
51 | + * |
|
52 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
53 | + * @param int $stackPtr The position of the current token |
|
54 | + * in the stack passed in $tokens. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function process(File $phpcsFile, $stackPtr) |
|
59 | + { |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | + $token = $tokens[$stackPtr]; |
|
62 | + |
|
63 | + // Skip invalid statement. |
|
64 | + if (isset($token['parenthesis_opener']) === false) { |
|
65 | + return; |
|
66 | + } |
|
67 | + |
|
68 | + $next = ++$token['parenthesis_opener']; |
|
69 | + $end = --$token['parenthesis_closer']; |
|
70 | + |
|
71 | + $position = 0; |
|
72 | + |
|
73 | + for (; $next <= $end; ++$next) { |
|
74 | + $code = $tokens[$next]['code']; |
|
75 | + if ($code === T_SEMICOLON) { |
|
76 | + ++$position; |
|
77 | + } |
|
78 | + |
|
79 | + if ($position < 1) { |
|
80 | + continue; |
|
81 | + } else if ($position > 1) { |
|
82 | + break; |
|
83 | + } else if ($code !== T_VARIABLE && $code !== T_STRING) { |
|
84 | + continue; |
|
85 | + } |
|
86 | + |
|
87 | + // Find next non empty token, if it is a open curly brace we have a |
|
88 | + // function call. |
|
89 | + $index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
90 | + |
|
91 | + if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) { |
|
92 | + $error = 'Avoid function calls in a FOR loop test part'; |
|
93 | + $phpcsFile->addWarning($error, $stackPtr, 'NotAllowed'); |
|
94 | + break; |
|
95 | + } |
|
96 | + }//end for |
|
97 | + |
|
98 | + }//end process() |
|
99 | 99 | |
100 | 100 | |
101 | 101 | }//end class |
@@ -30,56 +30,56 @@ |
||
30 | 30 | { |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Registers the tokens that this sniff wants to listen for. |
|
35 | - * |
|
36 | - * @return int[] |
|
37 | - */ |
|
38 | - public function register() |
|
39 | - { |
|
40 | - return [T_CLASS]; |
|
41 | - |
|
42 | - }//end register() |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token |
|
50 | - * in the stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - $tokens = $phpcsFile->getTokens(); |
|
57 | - $token = $tokens[$stackPtr]; |
|
58 | - |
|
59 | - // Skip for-statements without body. |
|
60 | - if (isset($token['scope_opener']) === false) { |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - // Fetch previous token. |
|
65 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
66 | - |
|
67 | - // Skip for non final class. |
|
68 | - if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) { |
|
69 | - return; |
|
70 | - } |
|
71 | - |
|
72 | - $next = ++$token['scope_opener']; |
|
73 | - $end = --$token['scope_closer']; |
|
74 | - |
|
75 | - for (; $next <= $end; ++$next) { |
|
76 | - if ($tokens[$next]['code'] === T_FINAL) { |
|
77 | - $error = 'Unnecessary FINAL modifier in FINAL class'; |
|
78 | - $phpcsFile->addWarning($error, $next, 'Found'); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - }//end process() |
|
33 | + /** |
|
34 | + * Registers the tokens that this sniff wants to listen for. |
|
35 | + * |
|
36 | + * @return int[] |
|
37 | + */ |
|
38 | + public function register() |
|
39 | + { |
|
40 | + return [T_CLASS]; |
|
41 | + |
|
42 | + }//end register() |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token |
|
50 | + * in the stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + $tokens = $phpcsFile->getTokens(); |
|
57 | + $token = $tokens[$stackPtr]; |
|
58 | + |
|
59 | + // Skip for-statements without body. |
|
60 | + if (isset($token['scope_opener']) === false) { |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + // Fetch previous token. |
|
65 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
66 | + |
|
67 | + // Skip for non final class. |
|
68 | + if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) { |
|
69 | + return; |
|
70 | + } |
|
71 | + |
|
72 | + $next = ++$token['scope_opener']; |
|
73 | + $end = --$token['scope_closer']; |
|
74 | + |
|
75 | + for (; $next <= $end; ++$next) { |
|
76 | + if ($tokens[$next]['code'] === T_FINAL) { |
|
77 | + $error = 'Unnecessary FINAL modifier in FINAL class'; |
|
78 | + $phpcsFile->addWarning($error, $next, 'Found'); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + }//end process() |
|
83 | 83 | |
84 | 84 | |
85 | 85 | }//end class |
@@ -36,99 +36,99 @@ |
||
36 | 36 | { |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * Registers the tokens that this sniff wants to listen for. |
|
41 | - * |
|
42 | - * @return int[] |
|
43 | - */ |
|
44 | - public function register() |
|
45 | - { |
|
46 | - return [T_FOR]; |
|
47 | - |
|
48 | - }//end register() |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Processes this test, when one of its tokens is encountered. |
|
53 | - * |
|
54 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
55 | - * @param int $stackPtr The position of the current token |
|
56 | - * in the stack passed in $tokens. |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function process(File $phpcsFile, $stackPtr) |
|
61 | - { |
|
62 | - $tokens = $phpcsFile->getTokens(); |
|
63 | - $token = $tokens[$stackPtr]; |
|
64 | - |
|
65 | - // Skip for-loop without body. |
|
66 | - if (isset($token['scope_opener']) === false) { |
|
67 | - return; |
|
68 | - } |
|
69 | - |
|
70 | - // Find incrementors for outer loop. |
|
71 | - $outer = $this->findIncrementers($tokens, $token); |
|
72 | - |
|
73 | - // Skip if empty. |
|
74 | - if (count($outer) === 0) { |
|
75 | - return; |
|
76 | - } |
|
77 | - |
|
78 | - // Find nested for loops. |
|
79 | - $start = ++$token['scope_opener']; |
|
80 | - $end = --$token['scope_closer']; |
|
81 | - |
|
82 | - for (; $start <= $end; ++$start) { |
|
83 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
84 | - continue; |
|
85 | - } |
|
86 | - |
|
87 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
88 | - $diff = array_intersect($outer, $inner); |
|
89 | - |
|
90 | - if (count($diff) !== 0) { |
|
91 | - $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
92 | - $data = [join(', ', $diff)]; |
|
93 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - }//end process() |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Get all used variables in the incrementer part of a for statement. |
|
102 | - * |
|
103 | - * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
104 | - * @param array(string=>mixed) $token Current for loop token |
|
105 | - * |
|
106 | - * @return string[] List of all found incrementer variables. |
|
107 | - */ |
|
108 | - protected function findIncrementers(array $tokens, array $token) |
|
109 | - { |
|
110 | - // Skip invalid statement. |
|
111 | - if (isset($token['parenthesis_opener']) === false) { |
|
112 | - return []; |
|
113 | - } |
|
114 | - |
|
115 | - $start = ++$token['parenthesis_opener']; |
|
116 | - $end = --$token['parenthesis_closer']; |
|
117 | - |
|
118 | - $incrementers = []; |
|
119 | - $semicolons = 0; |
|
120 | - for ($next = $start; $next <= $end; ++$next) { |
|
121 | - $code = $tokens[$next]['code']; |
|
122 | - if ($code === T_SEMICOLON) { |
|
123 | - ++$semicolons; |
|
124 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
125 | - $incrementers[] = $tokens[$next]['content']; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return $incrementers; |
|
130 | - |
|
131 | - }//end findIncrementers() |
|
39 | + /** |
|
40 | + * Registers the tokens that this sniff wants to listen for. |
|
41 | + * |
|
42 | + * @return int[] |
|
43 | + */ |
|
44 | + public function register() |
|
45 | + { |
|
46 | + return [T_FOR]; |
|
47 | + |
|
48 | + }//end register() |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Processes this test, when one of its tokens is encountered. |
|
53 | + * |
|
54 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
55 | + * @param int $stackPtr The position of the current token |
|
56 | + * in the stack passed in $tokens. |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function process(File $phpcsFile, $stackPtr) |
|
61 | + { |
|
62 | + $tokens = $phpcsFile->getTokens(); |
|
63 | + $token = $tokens[$stackPtr]; |
|
64 | + |
|
65 | + // Skip for-loop without body. |
|
66 | + if (isset($token['scope_opener']) === false) { |
|
67 | + return; |
|
68 | + } |
|
69 | + |
|
70 | + // Find incrementors for outer loop. |
|
71 | + $outer = $this->findIncrementers($tokens, $token); |
|
72 | + |
|
73 | + // Skip if empty. |
|
74 | + if (count($outer) === 0) { |
|
75 | + return; |
|
76 | + } |
|
77 | + |
|
78 | + // Find nested for loops. |
|
79 | + $start = ++$token['scope_opener']; |
|
80 | + $end = --$token['scope_closer']; |
|
81 | + |
|
82 | + for (; $start <= $end; ++$start) { |
|
83 | + if ($tokens[$start]['code'] !== T_FOR) { |
|
84 | + continue; |
|
85 | + } |
|
86 | + |
|
87 | + $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
88 | + $diff = array_intersect($outer, $inner); |
|
89 | + |
|
90 | + if (count($diff) !== 0) { |
|
91 | + $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
92 | + $data = [join(', ', $diff)]; |
|
93 | + $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + }//end process() |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Get all used variables in the incrementer part of a for statement. |
|
102 | + * |
|
103 | + * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
104 | + * @param array(string=>mixed) $token Current for loop token |
|
105 | + * |
|
106 | + * @return string[] List of all found incrementer variables. |
|
107 | + */ |
|
108 | + protected function findIncrementers(array $tokens, array $token) |
|
109 | + { |
|
110 | + // Skip invalid statement. |
|
111 | + if (isset($token['parenthesis_opener']) === false) { |
|
112 | + return []; |
|
113 | + } |
|
114 | + |
|
115 | + $start = ++$token['parenthesis_opener']; |
|
116 | + $end = --$token['parenthesis_closer']; |
|
117 | + |
|
118 | + $incrementers = []; |
|
119 | + $semicolons = 0; |
|
120 | + for ($next = $start; $next <= $end; ++$next) { |
|
121 | + $code = $tokens[$next]['code']; |
|
122 | + if ($code === T_SEMICOLON) { |
|
123 | + ++$semicolons; |
|
124 | + } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
125 | + $incrementers[] = $tokens[$next]['content']; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return $incrementers; |
|
130 | + |
|
131 | + }//end findIncrementers() |
|
132 | 132 | |
133 | 133 | |
134 | 134 | }//end class |
@@ -29,133 +29,133 @@ |
||
29 | 29 | { |
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * Registers the tokens that this sniff wants to listen for. |
|
34 | - * |
|
35 | - * @return int[] |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - return [T_FUNCTION]; |
|
40 | - |
|
41 | - }//end register() |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Processes this test, 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 |
|
49 | - * in the stack passed in $tokens. |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function process(File $phpcsFile, $stackPtr) |
|
54 | - { |
|
55 | - $tokens = $phpcsFile->getTokens(); |
|
56 | - $token = $tokens[$stackPtr]; |
|
57 | - |
|
58 | - // Skip function without body. |
|
59 | - if (isset($token['scope_opener']) === false) { |
|
60 | - return; |
|
61 | - } |
|
62 | - |
|
63 | - // Get function name. |
|
64 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
65 | - |
|
66 | - // Get all parameters from method signature. |
|
67 | - $signature = []; |
|
68 | - foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) { |
|
69 | - $signature[] = $param['name']; |
|
70 | - } |
|
71 | - |
|
72 | - $next = ++$token['scope_opener']; |
|
73 | - $end = --$token['scope_closer']; |
|
74 | - |
|
75 | - for (; $next <= $end; ++$next) { |
|
76 | - $code = $tokens[$next]['code']; |
|
77 | - |
|
78 | - if (isset(Tokens::$emptyTokens[$code]) === true) { |
|
79 | - continue; |
|
80 | - } else if ($code === T_RETURN) { |
|
81 | - continue; |
|
82 | - } |
|
83 | - |
|
84 | - break; |
|
85 | - } |
|
86 | - |
|
87 | - // Any token except 'parent' indicates correct code. |
|
88 | - if ($tokens[$next]['code'] !== T_PARENT) { |
|
89 | - return; |
|
90 | - } |
|
91 | - |
|
92 | - // Find next non empty token index, should be double colon. |
|
93 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
94 | - |
|
95 | - // Skip for invalid code. |
|
96 | - if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) { |
|
97 | - return; |
|
98 | - } |
|
99 | - |
|
100 | - // Find next non empty token index, should be the function name. |
|
101 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
102 | - |
|
103 | - // Skip for invalid code or other method. |
|
104 | - if ($next === false || $tokens[$next]['content'] !== $methodName) { |
|
105 | - return; |
|
106 | - } |
|
107 | - |
|
108 | - // Find next non empty token index, should be the open parenthesis. |
|
109 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
110 | - |
|
111 | - // Skip for invalid code. |
|
112 | - if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
|
113 | - return; |
|
114 | - } |
|
115 | - |
|
116 | - $parameters = ['']; |
|
117 | - $parenthesisCount = 1; |
|
118 | - $count = count($tokens); |
|
119 | - for (++$next; $next < $count; ++$next) { |
|
120 | - $code = $tokens[$next]['code']; |
|
121 | - |
|
122 | - if ($code === T_OPEN_PARENTHESIS) { |
|
123 | - ++$parenthesisCount; |
|
124 | - } else if ($code === T_CLOSE_PARENTHESIS) { |
|
125 | - --$parenthesisCount; |
|
126 | - } else if ($parenthesisCount === 1 && $code === T_COMMA) { |
|
127 | - $parameters[] = ''; |
|
128 | - } else if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
129 | - $parameters[(count($parameters) - 1)] .= $tokens[$next]['content']; |
|
130 | - } |
|
131 | - |
|
132 | - if ($parenthesisCount === 0) { |
|
133 | - break; |
|
134 | - } |
|
135 | - }//end for |
|
136 | - |
|
137 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
138 | - if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) { |
|
139 | - return; |
|
140 | - } |
|
141 | - |
|
142 | - // Check rest of the scope. |
|
143 | - for (++$next; $next <= $end; ++$next) { |
|
144 | - $code = $tokens[$next]['code']; |
|
145 | - // Skip for any other content. |
|
146 | - if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
147 | - return; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - $parameters = array_map('trim', $parameters); |
|
152 | - $parameters = array_filter($parameters); |
|
153 | - |
|
154 | - if (count($parameters) === count($signature) && $parameters === $signature) { |
|
155 | - $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found'); |
|
156 | - } |
|
157 | - |
|
158 | - }//end process() |
|
32 | + /** |
|
33 | + * Registers the tokens that this sniff wants to listen for. |
|
34 | + * |
|
35 | + * @return int[] |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + return [T_FUNCTION]; |
|
40 | + |
|
41 | + }//end register() |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Processes this test, 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 |
|
49 | + * in the stack passed in $tokens. |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function process(File $phpcsFile, $stackPtr) |
|
54 | + { |
|
55 | + $tokens = $phpcsFile->getTokens(); |
|
56 | + $token = $tokens[$stackPtr]; |
|
57 | + |
|
58 | + // Skip function without body. |
|
59 | + if (isset($token['scope_opener']) === false) { |
|
60 | + return; |
|
61 | + } |
|
62 | + |
|
63 | + // Get function name. |
|
64 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
65 | + |
|
66 | + // Get all parameters from method signature. |
|
67 | + $signature = []; |
|
68 | + foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) { |
|
69 | + $signature[] = $param['name']; |
|
70 | + } |
|
71 | + |
|
72 | + $next = ++$token['scope_opener']; |
|
73 | + $end = --$token['scope_closer']; |
|
74 | + |
|
75 | + for (; $next <= $end; ++$next) { |
|
76 | + $code = $tokens[$next]['code']; |
|
77 | + |
|
78 | + if (isset(Tokens::$emptyTokens[$code]) === true) { |
|
79 | + continue; |
|
80 | + } else if ($code === T_RETURN) { |
|
81 | + continue; |
|
82 | + } |
|
83 | + |
|
84 | + break; |
|
85 | + } |
|
86 | + |
|
87 | + // Any token except 'parent' indicates correct code. |
|
88 | + if ($tokens[$next]['code'] !== T_PARENT) { |
|
89 | + return; |
|
90 | + } |
|
91 | + |
|
92 | + // Find next non empty token index, should be double colon. |
|
93 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
94 | + |
|
95 | + // Skip for invalid code. |
|
96 | + if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) { |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + // Find next non empty token index, should be the function name. |
|
101 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
102 | + |
|
103 | + // Skip for invalid code or other method. |
|
104 | + if ($next === false || $tokens[$next]['content'] !== $methodName) { |
|
105 | + return; |
|
106 | + } |
|
107 | + |
|
108 | + // Find next non empty token index, should be the open parenthesis. |
|
109 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
110 | + |
|
111 | + // Skip for invalid code. |
|
112 | + if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
|
113 | + return; |
|
114 | + } |
|
115 | + |
|
116 | + $parameters = ['']; |
|
117 | + $parenthesisCount = 1; |
|
118 | + $count = count($tokens); |
|
119 | + for (++$next; $next < $count; ++$next) { |
|
120 | + $code = $tokens[$next]['code']; |
|
121 | + |
|
122 | + if ($code === T_OPEN_PARENTHESIS) { |
|
123 | + ++$parenthesisCount; |
|
124 | + } else if ($code === T_CLOSE_PARENTHESIS) { |
|
125 | + --$parenthesisCount; |
|
126 | + } else if ($parenthesisCount === 1 && $code === T_COMMA) { |
|
127 | + $parameters[] = ''; |
|
128 | + } else if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
129 | + $parameters[(count($parameters) - 1)] .= $tokens[$next]['content']; |
|
130 | + } |
|
131 | + |
|
132 | + if ($parenthesisCount === 0) { |
|
133 | + break; |
|
134 | + } |
|
135 | + }//end for |
|
136 | + |
|
137 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
138 | + if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) { |
|
139 | + return; |
|
140 | + } |
|
141 | + |
|
142 | + // Check rest of the scope. |
|
143 | + for (++$next; $next <= $end; ++$next) { |
|
144 | + $code = $tokens[$next]['code']; |
|
145 | + // Skip for any other content. |
|
146 | + if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
147 | + return; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + $parameters = array_map('trim', $parameters); |
|
152 | + $parameters = array_filter($parameters); |
|
153 | + |
|
154 | + if (count($parameters) === count($signature) && $parameters === $signature) { |
|
155 | + $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found'); |
|
156 | + } |
|
157 | + |
|
158 | + }//end process() |
|
159 | 159 | |
160 | 160 | |
161 | 161 | }//end class |
@@ -158,7 +158,7 @@ |
||
158 | 158 | } |
159 | 159 | |
160 | 160 | $startPos = $hasAssignment; |
161 | - } while ($startPos < $closer); |
|
161 | + }while ($startPos < $closer); |
|
162 | 162 | |
163 | 163 | }//end process() |
164 | 164 |
@@ -20,152 +20,152 @@ |
||
20 | 20 | class AssignmentInConditionSniff implements Sniff |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * Assignment tokens to trigger on. |
|
25 | - * |
|
26 | - * Set in the register() method. |
|
27 | - * |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $assignmentTokens = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * The tokens that indicate the start of a condition. |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $conditionStartTokens = []; |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Registers the tokens that this sniff wants to listen for. |
|
42 | - * |
|
43 | - * @return int[] |
|
44 | - */ |
|
45 | - public function register() |
|
46 | - { |
|
47 | - $this->assignmentTokens = Tokens::$assignmentTokens; |
|
48 | - unset($this->assignmentTokens[T_DOUBLE_ARROW]); |
|
49 | - |
|
50 | - $starters = Tokens::$booleanOperators; |
|
51 | - $starters[T_SEMICOLON] = T_SEMICOLON; |
|
52 | - $starters[T_OPEN_PARENTHESIS] = T_OPEN_PARENTHESIS; |
|
53 | - |
|
54 | - $this->conditionStartTokens = $starters; |
|
55 | - |
|
56 | - return [ |
|
57 | - T_IF, |
|
58 | - T_ELSEIF, |
|
59 | - T_FOR, |
|
60 | - T_SWITCH, |
|
61 | - T_CASE, |
|
62 | - T_WHILE, |
|
63 | - T_MATCH, |
|
64 | - ]; |
|
65 | - |
|
66 | - }//end register() |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Processes this test, when one of its tokens is encountered. |
|
71 | - * |
|
72 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
73 | - * @param int $stackPtr The position of the current token |
|
74 | - * in the stack passed in $tokens. |
|
75 | - * |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function process(File $phpcsFile, $stackPtr) |
|
79 | - { |
|
80 | - $tokens = $phpcsFile->getTokens(); |
|
81 | - $token = $tokens[$stackPtr]; |
|
82 | - |
|
83 | - // Find the condition opener/closer. |
|
84 | - if ($token['code'] === T_FOR) { |
|
85 | - if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { |
|
86 | - return; |
|
87 | - } |
|
88 | - |
|
89 | - $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($token['parenthesis_opener'] + 1), ($token['parenthesis_closer'])); |
|
90 | - if ($semicolon === false) { |
|
91 | - return; |
|
92 | - } |
|
93 | - |
|
94 | - $opener = $semicolon; |
|
95 | - |
|
96 | - $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($opener + 1), ($token['parenthesis_closer'])); |
|
97 | - if ($semicolon === false) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - $closer = $semicolon; |
|
102 | - unset($semicolon); |
|
103 | - } else if ($token['code'] === T_CASE) { |
|
104 | - if (isset($token['scope_opener']) === false) { |
|
105 | - return; |
|
106 | - } |
|
107 | - |
|
108 | - $opener = $stackPtr; |
|
109 | - $closer = $token['scope_opener']; |
|
110 | - } else { |
|
111 | - if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { |
|
112 | - return; |
|
113 | - } |
|
114 | - |
|
115 | - $opener = $token['parenthesis_opener']; |
|
116 | - $closer = $token['parenthesis_closer']; |
|
117 | - }//end if |
|
118 | - |
|
119 | - $startPos = $opener; |
|
120 | - |
|
121 | - do { |
|
122 | - $hasAssignment = $phpcsFile->findNext($this->assignmentTokens, ($startPos + 1), $closer); |
|
123 | - if ($hasAssignment === false) { |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - // Examine whether the left side is a variable. |
|
128 | - $hasVariable = false; |
|
129 | - $conditionStart = $startPos; |
|
130 | - $altConditionStart = $phpcsFile->findPrevious($this->conditionStartTokens, ($hasAssignment - 1), $startPos); |
|
131 | - if ($altConditionStart !== false) { |
|
132 | - $conditionStart = $altConditionStart; |
|
133 | - } |
|
134 | - |
|
135 | - for ($i = $hasAssignment; $i > $conditionStart; $i--) { |
|
136 | - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { |
|
137 | - continue; |
|
138 | - } |
|
139 | - |
|
140 | - // If this is a variable or array, we've seen all we need to see. |
|
141 | - if ($tokens[$i]['code'] === T_VARIABLE || $tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET) { |
|
142 | - $hasVariable = true; |
|
143 | - break; |
|
144 | - } |
|
145 | - |
|
146 | - // If this is a function call or something, we are OK. |
|
147 | - if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) { |
|
148 | - break; |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - if ($hasVariable === true) { |
|
153 | - $errorCode = 'Found'; |
|
154 | - if ($token['code'] === T_WHILE) { |
|
155 | - $errorCode = 'FoundInWhileCondition'; |
|
156 | - } |
|
157 | - |
|
158 | - $phpcsFile->addWarning( |
|
159 | - 'Variable assignment found within a condition. Did you mean to do a comparison ?', |
|
160 | - $hasAssignment, |
|
161 | - $errorCode |
|
162 | - ); |
|
163 | - } |
|
164 | - |
|
165 | - $startPos = $hasAssignment; |
|
166 | - } while ($startPos < $closer); |
|
167 | - |
|
168 | - }//end process() |
|
23 | + /** |
|
24 | + * Assignment tokens to trigger on. |
|
25 | + * |
|
26 | + * Set in the register() method. |
|
27 | + * |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $assignmentTokens = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * The tokens that indicate the start of a condition. |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $conditionStartTokens = []; |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Registers the tokens that this sniff wants to listen for. |
|
42 | + * |
|
43 | + * @return int[] |
|
44 | + */ |
|
45 | + public function register() |
|
46 | + { |
|
47 | + $this->assignmentTokens = Tokens::$assignmentTokens; |
|
48 | + unset($this->assignmentTokens[T_DOUBLE_ARROW]); |
|
49 | + |
|
50 | + $starters = Tokens::$booleanOperators; |
|
51 | + $starters[T_SEMICOLON] = T_SEMICOLON; |
|
52 | + $starters[T_OPEN_PARENTHESIS] = T_OPEN_PARENTHESIS; |
|
53 | + |
|
54 | + $this->conditionStartTokens = $starters; |
|
55 | + |
|
56 | + return [ |
|
57 | + T_IF, |
|
58 | + T_ELSEIF, |
|
59 | + T_FOR, |
|
60 | + T_SWITCH, |
|
61 | + T_CASE, |
|
62 | + T_WHILE, |
|
63 | + T_MATCH, |
|
64 | + ]; |
|
65 | + |
|
66 | + }//end register() |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Processes this test, when one of its tokens is encountered. |
|
71 | + * |
|
72 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
73 | + * @param int $stackPtr The position of the current token |
|
74 | + * in the stack passed in $tokens. |
|
75 | + * |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function process(File $phpcsFile, $stackPtr) |
|
79 | + { |
|
80 | + $tokens = $phpcsFile->getTokens(); |
|
81 | + $token = $tokens[$stackPtr]; |
|
82 | + |
|
83 | + // Find the condition opener/closer. |
|
84 | + if ($token['code'] === T_FOR) { |
|
85 | + if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { |
|
86 | + return; |
|
87 | + } |
|
88 | + |
|
89 | + $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($token['parenthesis_opener'] + 1), ($token['parenthesis_closer'])); |
|
90 | + if ($semicolon === false) { |
|
91 | + return; |
|
92 | + } |
|
93 | + |
|
94 | + $opener = $semicolon; |
|
95 | + |
|
96 | + $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($opener + 1), ($token['parenthesis_closer'])); |
|
97 | + if ($semicolon === false) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + $closer = $semicolon; |
|
102 | + unset($semicolon); |
|
103 | + } else if ($token['code'] === T_CASE) { |
|
104 | + if (isset($token['scope_opener']) === false) { |
|
105 | + return; |
|
106 | + } |
|
107 | + |
|
108 | + $opener = $stackPtr; |
|
109 | + $closer = $token['scope_opener']; |
|
110 | + } else { |
|
111 | + if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { |
|
112 | + return; |
|
113 | + } |
|
114 | + |
|
115 | + $opener = $token['parenthesis_opener']; |
|
116 | + $closer = $token['parenthesis_closer']; |
|
117 | + }//end if |
|
118 | + |
|
119 | + $startPos = $opener; |
|
120 | + |
|
121 | + do { |
|
122 | + $hasAssignment = $phpcsFile->findNext($this->assignmentTokens, ($startPos + 1), $closer); |
|
123 | + if ($hasAssignment === false) { |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + // Examine whether the left side is a variable. |
|
128 | + $hasVariable = false; |
|
129 | + $conditionStart = $startPos; |
|
130 | + $altConditionStart = $phpcsFile->findPrevious($this->conditionStartTokens, ($hasAssignment - 1), $startPos); |
|
131 | + if ($altConditionStart !== false) { |
|
132 | + $conditionStart = $altConditionStart; |
|
133 | + } |
|
134 | + |
|
135 | + for ($i = $hasAssignment; $i > $conditionStart; $i--) { |
|
136 | + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { |
|
137 | + continue; |
|
138 | + } |
|
139 | + |
|
140 | + // If this is a variable or array, we've seen all we need to see. |
|
141 | + if ($tokens[$i]['code'] === T_VARIABLE || $tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET) { |
|
142 | + $hasVariable = true; |
|
143 | + break; |
|
144 | + } |
|
145 | + |
|
146 | + // If this is a function call or something, we are OK. |
|
147 | + if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) { |
|
148 | + break; |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + if ($hasVariable === true) { |
|
153 | + $errorCode = 'Found'; |
|
154 | + if ($token['code'] === T_WHILE) { |
|
155 | + $errorCode = 'FoundInWhileCondition'; |
|
156 | + } |
|
157 | + |
|
158 | + $phpcsFile->addWarning( |
|
159 | + 'Variable assignment found within a condition. Did you mean to do a comparison ?', |
|
160 | + $hasAssignment, |
|
161 | + $errorCode |
|
162 | + ); |
|
163 | + } |
|
164 | + |
|
165 | + $startPos = $hasAssignment; |
|
166 | + } while ($startPos < $closer); |
|
167 | + |
|
168 | + }//end process() |
|
169 | 169 | |
170 | 170 | |
171 | 171 | }//end class |
@@ -30,62 +30,62 @@ |
||
30 | 30 | { |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Registers the tokens that this sniff wants to listen for. |
|
35 | - * |
|
36 | - * @return int[] |
|
37 | - */ |
|
38 | - public function register() |
|
39 | - { |
|
40 | - return [T_FOR]; |
|
41 | - |
|
42 | - }//end register() |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token |
|
50 | - * in the stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - $tokens = $phpcsFile->getTokens(); |
|
57 | - $token = $tokens[$stackPtr]; |
|
58 | - |
|
59 | - // Skip invalid statement. |
|
60 | - if (isset($token['parenthesis_opener']) === false) { |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - $next = ++$token['parenthesis_opener']; |
|
65 | - $end = --$token['parenthesis_closer']; |
|
66 | - |
|
67 | - $parts = [ |
|
68 | - 0, |
|
69 | - 0, |
|
70 | - 0, |
|
71 | - ]; |
|
72 | - $index = 0; |
|
73 | - |
|
74 | - for (; $next <= $end; ++$next) { |
|
75 | - $code = $tokens[$next]['code']; |
|
76 | - if ($code === T_SEMICOLON) { |
|
77 | - ++$index; |
|
78 | - } else if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
79 | - ++$parts[$index]; |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) { |
|
84 | - $error = 'This FOR loop can be simplified to a WHILE loop'; |
|
85 | - $phpcsFile->addWarning($error, $stackPtr, 'CanSimplify'); |
|
86 | - } |
|
87 | - |
|
88 | - }//end process() |
|
33 | + /** |
|
34 | + * Registers the tokens that this sniff wants to listen for. |
|
35 | + * |
|
36 | + * @return int[] |
|
37 | + */ |
|
38 | + public function register() |
|
39 | + { |
|
40 | + return [T_FOR]; |
|
41 | + |
|
42 | + }//end register() |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token |
|
50 | + * in the stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + $tokens = $phpcsFile->getTokens(); |
|
57 | + $token = $tokens[$stackPtr]; |
|
58 | + |
|
59 | + // Skip invalid statement. |
|
60 | + if (isset($token['parenthesis_opener']) === false) { |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + $next = ++$token['parenthesis_opener']; |
|
65 | + $end = --$token['parenthesis_closer']; |
|
66 | + |
|
67 | + $parts = [ |
|
68 | + 0, |
|
69 | + 0, |
|
70 | + 0, |
|
71 | + ]; |
|
72 | + $index = 0; |
|
73 | + |
|
74 | + for (; $next <= $end; ++$next) { |
|
75 | + $code = $tokens[$next]['code']; |
|
76 | + if ($code === T_SEMICOLON) { |
|
77 | + ++$index; |
|
78 | + } else if (isset(Tokens::$emptyTokens[$code]) === false) { |
|
79 | + ++$parts[$index]; |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) { |
|
84 | + $error = 'This FOR loop can be simplified to a WHILE loop'; |
|
85 | + $phpcsFile->addWarning($error, $stackPtr, 'CanSimplify'); |
|
86 | + } |
|
87 | + |
|
88 | + }//end process() |
|
89 | 89 | |
90 | 90 | |
91 | 91 | }//end class |
@@ -58,7 +58,7 @@ |
||
58 | 58 | * |
59 | 59 | * @return void |
60 | 60 | */ |
61 | - protected function addError($phpcsFile, $stackPtr, $function, $pattern=null) |
|
61 | + protected function addError($phpcsFile, $stackPtr, $function, $pattern = null) |
|
62 | 62 | { |
63 | 63 | $data = [$function]; |
64 | 64 | $error = 'Function %s() has been deprecated'; |
@@ -13,61 +13,61 @@ |
||
13 | 13 | class DeprecatedFunctionsSniff extends ForbiddenFunctionsSniff |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * A list of forbidden functions with their alternatives. |
|
18 | - * |
|
19 | - * The value is NULL if no alternative exists. IE, the |
|
20 | - * function should just not be used. |
|
21 | - * |
|
22 | - * @var array<string, string|null> |
|
23 | - */ |
|
24 | - public $forbiddenFunctions = []; |
|
16 | + /** |
|
17 | + * A list of forbidden functions with their alternatives. |
|
18 | + * |
|
19 | + * The value is NULL if no alternative exists. IE, the |
|
20 | + * function should just not be used. |
|
21 | + * |
|
22 | + * @var array<string, string|null> |
|
23 | + */ |
|
24 | + public $forbiddenFunctions = []; |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * Uses the Reflection API to get a list of deprecated functions. |
|
31 | - */ |
|
32 | - public function __construct() |
|
33 | - { |
|
34 | - $functions = get_defined_functions(); |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * Uses the Reflection API to get a list of deprecated functions. |
|
31 | + */ |
|
32 | + public function __construct() |
|
33 | + { |
|
34 | + $functions = get_defined_functions(); |
|
35 | 35 | |
36 | - foreach ($functions['internal'] as $functionName) { |
|
37 | - $function = new \ReflectionFunction($functionName); |
|
36 | + foreach ($functions['internal'] as $functionName) { |
|
37 | + $function = new \ReflectionFunction($functionName); |
|
38 | 38 | |
39 | - if ($function->isDeprecated() === true) { |
|
40 | - $this->forbiddenFunctions[$functionName] = null; |
|
41 | - } |
|
42 | - } |
|
39 | + if ($function->isDeprecated() === true) { |
|
40 | + $this->forbiddenFunctions[$functionName] = null; |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - }//end __construct() |
|
44 | + }//end __construct() |
|
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Generates the error or warning for this sniff. |
|
49 | - * |
|
50 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
51 | - * @param int $stackPtr The position of the forbidden function |
|
52 | - * in the token array. |
|
53 | - * @param string $function The name of the forbidden function. |
|
54 | - * @param string $pattern The pattern used for the match. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - protected function addError($phpcsFile, $stackPtr, $function, $pattern=null) |
|
59 | - { |
|
60 | - $data = [$function]; |
|
61 | - $error = 'Function %s() has been deprecated'; |
|
62 | - $type = 'Deprecated'; |
|
47 | + /** |
|
48 | + * Generates the error or warning for this sniff. |
|
49 | + * |
|
50 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
51 | + * @param int $stackPtr The position of the forbidden function |
|
52 | + * in the token array. |
|
53 | + * @param string $function The name of the forbidden function. |
|
54 | + * @param string $pattern The pattern used for the match. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + protected function addError($phpcsFile, $stackPtr, $function, $pattern=null) |
|
59 | + { |
|
60 | + $data = [$function]; |
|
61 | + $error = 'Function %s() has been deprecated'; |
|
62 | + $type = 'Deprecated'; |
|
63 | 63 | |
64 | - if ($this->error === true) { |
|
65 | - $phpcsFile->addError($error, $stackPtr, $type, $data); |
|
66 | - } else { |
|
67 | - $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
|
68 | - } |
|
64 | + if ($this->error === true) { |
|
65 | + $phpcsFile->addError($error, $stackPtr, $type, $data); |
|
66 | + } else { |
|
67 | + $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
|
68 | + } |
|
69 | 69 | |
70 | - }//end addError() |
|
70 | + }//end addError() |
|
71 | 71 | |
72 | 72 | |
73 | 73 | }//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 |
@@ -16,35 +16,35 @@ |
||
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_GOTO, |
|
28 | - T_GOTO_LABEL, |
|
29 | - ]; |
|
30 | - |
|
31 | - }//end register() |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * Processes this sniff, 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 |
|
39 | - * the stack passed in $tokens. |
|
40 | - * |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function process(File $phpcsFile, $stackPtr) |
|
44 | - { |
|
45 | - $phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found'); |
|
46 | - |
|
47 | - }//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_GOTO, |
|
28 | + T_GOTO_LABEL, |
|
29 | + ]; |
|
30 | + |
|
31 | + }//end register() |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * Processes this sniff, 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 |
|
39 | + * the stack passed in $tokens. |
|
40 | + * |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function process(File $phpcsFile, $stackPtr) |
|
44 | + { |
|
45 | + $phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found'); |
|
46 | + |
|
47 | + }//end process() |
|
48 | 48 | |
49 | 49 | |
50 | 50 | }//end class |