@@ -51,7 +51,7 @@ |
||
| 51 | 51 | /** |
| 52 | 52 | * Returns an array of tokens this test wants to listen for. |
| 53 | 53 | * |
| 54 | - * @return array |
|
| 54 | + * @return integer[] |
|
| 55 | 55 | */ |
| 56 | 56 | public function register() |
| 57 | 57 | { |
@@ -33,237 +33,237 @@ |
||
| 33 | 33 | { |
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Whether ASP tags are enabled or not. |
|
| 38 | - * |
|
| 39 | - * @var bool |
|
| 40 | - */ |
|
| 41 | - private $_aspTags = false; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The current PHP version. |
|
| 45 | - * |
|
| 46 | - * @var integer |
|
| 47 | - */ |
|
| 48 | - private $_phpVersion = null; |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Returns an array of tokens this test wants to listen for. |
|
| 53 | - * |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function register() |
|
| 57 | - { |
|
| 58 | - if ($this->_phpVersion === null) { |
|
| 59 | - $this->_phpVersion = PHP_CodeSniffer::getConfigData('php_version'); |
|
| 60 | - if ($this->_phpVersion === null) { |
|
| 61 | - $this->_phpVersion = PHP_VERSION_ID; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if ($this->_phpVersion < 70000) { |
|
| 66 | - $this->_aspTags = (boolean) ini_get('asp_tags'); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return array( |
|
| 70 | - T_OPEN_TAG, |
|
| 71 | - T_OPEN_TAG_WITH_ECHO, |
|
| 72 | - T_INLINE_HTML, |
|
| 73 | - ); |
|
| 74 | - |
|
| 75 | - }//end register() |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Processes this test, when one of its tokens is encountered. |
|
| 80 | - * |
|
| 81 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 82 | - * @param int $stackPtr The position of the current token |
|
| 83 | - * in the stack passed in $tokens. |
|
| 84 | - * |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 88 | - { |
|
| 89 | - $tokens = $phpcsFile->getTokens(); |
|
| 90 | - $openTag = $tokens[$stackPtr]; |
|
| 91 | - $content = $openTag['content']; |
|
| 92 | - |
|
| 93 | - if (trim($content) === '') { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if ($openTag['code'] === T_OPEN_TAG) { |
|
| 98 | - if ($content === '<%') { |
|
| 99 | - $error = 'ASP style opening tag used; expected "<?php" but found "%s"'; |
|
| 100 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 101 | - $errorCode = 'ASPOpenTagFound'; |
|
| 102 | - } else if (strpos($content, '<script ') !== false) { |
|
| 103 | - $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
|
| 104 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '</script>'); |
|
| 105 | - $errorCode = 'ScriptOpenTagFound'; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if (isset($error, $closer, $errorCode) === true) { |
|
| 109 | - $data = array($content); |
|
| 110 | - |
|
| 111 | - if ($closer === false) { |
|
| 112 | - $phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
|
| 113 | - } else { |
|
| 114 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data); |
|
| 115 | - if ($fix === true) { |
|
| 116 | - $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return; |
|
| 122 | - }//end if |
|
| 123 | - |
|
| 124 | - if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO && $content === '<%=') { |
|
| 125 | - $error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."'; |
|
| 126 | - $nextVar = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 127 | - $snippet = $this->getSnippet($tokens[$nextVar]['content']); |
|
| 128 | - $data = array( |
|
| 129 | - $snippet, |
|
| 130 | - $content, |
|
| 131 | - $snippet, |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 135 | - |
|
| 136 | - if ($closer === false) { |
|
| 137 | - $phpcsFile->addError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 138 | - } else { |
|
| 139 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 140 | - if ($fix === true) { |
|
| 141 | - $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer, true); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return; |
|
| 146 | - }//end if |
|
| 147 | - |
|
| 148 | - // Account for incorrect script open tags. |
|
| 149 | - // The "(?:<s)?" in the regex is to work-around a bug in PHP 5.2. |
|
| 150 | - if ($openTag['code'] === T_INLINE_HTML |
|
| 151 | - && preg_match('`((?:<s)?cript (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1 |
|
| 152 | - ) { |
|
| 153 | - $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
|
| 154 | - $snippet = $this->getSnippet($content, $match[1]); |
|
| 155 | - $data = array($match[1].$snippet); |
|
| 156 | - |
|
| 157 | - $phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data); |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ($openTag['code'] === T_INLINE_HTML && $this->_aspTags === false) { |
|
| 162 | - if (strpos($content, '<%=') !== false) { |
|
| 163 | - $error = 'Possible use of ASP style short opening tags detected. Needs manual inspection. Found: %s'; |
|
| 164 | - $snippet = $this->getSnippet($content, '<%='); |
|
| 165 | - $data = array('<%='.$snippet); |
|
| 166 | - |
|
| 167 | - $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data); |
|
| 168 | - } else if (strpos($content, '<%') !== false) { |
|
| 169 | - $error = 'Possible use of ASP style opening tags detected. Needs manual inspection. Found: %s'; |
|
| 170 | - $snippet = $this->getSnippet($content, '<%'); |
|
| 171 | - $data = array('<%'.$snippet); |
|
| 172 | - |
|
| 173 | - $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - }//end process() |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Get a snippet from a HTML token. |
|
| 182 | - * |
|
| 183 | - * @param string $content The content of the HTML token. |
|
| 184 | - * @param string $start_at Partial string to use as a starting point for the snippet. |
|
| 185 | - * @param int $length The target length of the snippet to get. Defaults to 40. |
|
| 186 | - * |
|
| 187 | - * @return string |
|
| 188 | - */ |
|
| 189 | - protected function getSnippet($content, $start_at = '', $length = 40) |
|
| 190 | - { |
|
| 191 | - $start_pos = 0; |
|
| 192 | - |
|
| 193 | - if ($start_at !== '') { |
|
| 194 | - $start_pos = strpos($content, $start_at); |
|
| 195 | - if ($start_pos !== false) { |
|
| 196 | - $start_pos += strlen($start_at); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $snippet = substr($content, $start_pos, $length); |
|
| 201 | - if ((strlen($content) - $start_pos) > $length) { |
|
| 202 | - $snippet .= '...'; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return $snippet; |
|
| 206 | - |
|
| 207 | - }//end getSnippet() |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Try and find a matching PHP closing tag. |
|
| 212 | - * |
|
| 213 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 214 | - * @param array $tokens The token stack. |
|
| 215 | - * @param int $stackPtr The position of the current token |
|
| 216 | - * in the stack passed in $tokens. |
|
| 217 | - * @param string $content The expected content of the closing tag to match the opener. |
|
| 218 | - * |
|
| 219 | - * @return int|false Pointer to the position in the stack for the closing tag or false if not found. |
|
| 220 | - */ |
|
| 221 | - protected function findClosingTag(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content) |
|
| 222 | - { |
|
| 223 | - $closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1)); |
|
| 224 | - |
|
| 225 | - if ($closer !== false && $content === trim($tokens[$closer]['content'])) { |
|
| 226 | - return $closer; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return false; |
|
| 230 | - |
|
| 231 | - }//end findClosingTag() |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Add a changeset to replace the alternative PHP tags. |
|
| 236 | - * |
|
| 237 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 238 | - * @param array $tokens The token stack. |
|
| 239 | - * @param int $open_tag_pointer Stack pointer to the PHP open tag. |
|
| 240 | - * @param int $close_tag_pointer Stack pointer to the PHP close tag. |
|
| 241 | - * @param bool $echo Whether to add 'echo' or not. |
|
| 242 | - * |
|
| 243 | - * @return void |
|
| 244 | - */ |
|
| 245 | - protected function addChangeset(PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false) |
|
| 246 | - { |
|
| 247 | - // Build up the open tag replacement and make sure there's always whitespace behind it. |
|
| 248 | - $open_replacement = '<?php'; |
|
| 249 | - if ($echo === true) { |
|
| 250 | - $open_replacement .= ' echo'; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - if ($tokens[($open_tag_pointer + 1)]['code'] !== T_WHITESPACE) { |
|
| 254 | - $open_replacement .= ' '; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - // Make sure we don't remove any line breaks after the closing tag. |
|
| 258 | - $regex = '`'.preg_quote(trim($tokens[$close_tag_pointer]['content'])).'`'; |
|
| 259 | - $close_replacement = preg_replace($regex, '?>', $tokens[$close_tag_pointer]['content']); |
|
| 260 | - |
|
| 261 | - $phpcsFile->fixer->beginChangeset(); |
|
| 262 | - $phpcsFile->fixer->replaceToken($open_tag_pointer, $open_replacement); |
|
| 263 | - $phpcsFile->fixer->replaceToken($close_tag_pointer, $close_replacement); |
|
| 264 | - $phpcsFile->fixer->endChangeset(); |
|
| 265 | - |
|
| 266 | - }//end addChangeset() |
|
| 36 | + /** |
|
| 37 | + * Whether ASP tags are enabled or not. |
|
| 38 | + * |
|
| 39 | + * @var bool |
|
| 40 | + */ |
|
| 41 | + private $_aspTags = false; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The current PHP version. |
|
| 45 | + * |
|
| 46 | + * @var integer |
|
| 47 | + */ |
|
| 48 | + private $_phpVersion = null; |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Returns an array of tokens this test wants to listen for. |
|
| 53 | + * |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function register() |
|
| 57 | + { |
|
| 58 | + if ($this->_phpVersion === null) { |
|
| 59 | + $this->_phpVersion = PHP_CodeSniffer::getConfigData('php_version'); |
|
| 60 | + if ($this->_phpVersion === null) { |
|
| 61 | + $this->_phpVersion = PHP_VERSION_ID; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if ($this->_phpVersion < 70000) { |
|
| 66 | + $this->_aspTags = (boolean) ini_get('asp_tags'); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return array( |
|
| 70 | + T_OPEN_TAG, |
|
| 71 | + T_OPEN_TAG_WITH_ECHO, |
|
| 72 | + T_INLINE_HTML, |
|
| 73 | + ); |
|
| 74 | + |
|
| 75 | + }//end register() |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Processes this test, when one of its tokens is encountered. |
|
| 80 | + * |
|
| 81 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 82 | + * @param int $stackPtr The position of the current token |
|
| 83 | + * in the stack passed in $tokens. |
|
| 84 | + * |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 88 | + { |
|
| 89 | + $tokens = $phpcsFile->getTokens(); |
|
| 90 | + $openTag = $tokens[$stackPtr]; |
|
| 91 | + $content = $openTag['content']; |
|
| 92 | + |
|
| 93 | + if (trim($content) === '') { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if ($openTag['code'] === T_OPEN_TAG) { |
|
| 98 | + if ($content === '<%') { |
|
| 99 | + $error = 'ASP style opening tag used; expected "<?php" but found "%s"'; |
|
| 100 | + $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 101 | + $errorCode = 'ASPOpenTagFound'; |
|
| 102 | + } else if (strpos($content, '<script ') !== false) { |
|
| 103 | + $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
|
| 104 | + $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '</script>'); |
|
| 105 | + $errorCode = 'ScriptOpenTagFound'; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if (isset($error, $closer, $errorCode) === true) { |
|
| 109 | + $data = array($content); |
|
| 110 | + |
|
| 111 | + if ($closer === false) { |
|
| 112 | + $phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
|
| 113 | + } else { |
|
| 114 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data); |
|
| 115 | + if ($fix === true) { |
|
| 116 | + $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return; |
|
| 122 | + }//end if |
|
| 123 | + |
|
| 124 | + if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO && $content === '<%=') { |
|
| 125 | + $error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."'; |
|
| 126 | + $nextVar = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 127 | + $snippet = $this->getSnippet($tokens[$nextVar]['content']); |
|
| 128 | + $data = array( |
|
| 129 | + $snippet, |
|
| 130 | + $content, |
|
| 131 | + $snippet, |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 135 | + |
|
| 136 | + if ($closer === false) { |
|
| 137 | + $phpcsFile->addError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 138 | + } else { |
|
| 139 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 140 | + if ($fix === true) { |
|
| 141 | + $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer, true); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return; |
|
| 146 | + }//end if |
|
| 147 | + |
|
| 148 | + // Account for incorrect script open tags. |
|
| 149 | + // The "(?:<s)?" in the regex is to work-around a bug in PHP 5.2. |
|
| 150 | + if ($openTag['code'] === T_INLINE_HTML |
|
| 151 | + && preg_match('`((?:<s)?cript (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1 |
|
| 152 | + ) { |
|
| 153 | + $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
|
| 154 | + $snippet = $this->getSnippet($content, $match[1]); |
|
| 155 | + $data = array($match[1].$snippet); |
|
| 156 | + |
|
| 157 | + $phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data); |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ($openTag['code'] === T_INLINE_HTML && $this->_aspTags === false) { |
|
| 162 | + if (strpos($content, '<%=') !== false) { |
|
| 163 | + $error = 'Possible use of ASP style short opening tags detected. Needs manual inspection. Found: %s'; |
|
| 164 | + $snippet = $this->getSnippet($content, '<%='); |
|
| 165 | + $data = array('<%='.$snippet); |
|
| 166 | + |
|
| 167 | + $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data); |
|
| 168 | + } else if (strpos($content, '<%') !== false) { |
|
| 169 | + $error = 'Possible use of ASP style opening tags detected. Needs manual inspection. Found: %s'; |
|
| 170 | + $snippet = $this->getSnippet($content, '<%'); |
|
| 171 | + $data = array('<%'.$snippet); |
|
| 172 | + |
|
| 173 | + $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + }//end process() |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Get a snippet from a HTML token. |
|
| 182 | + * |
|
| 183 | + * @param string $content The content of the HTML token. |
|
| 184 | + * @param string $start_at Partial string to use as a starting point for the snippet. |
|
| 185 | + * @param int $length The target length of the snippet to get. Defaults to 40. |
|
| 186 | + * |
|
| 187 | + * @return string |
|
| 188 | + */ |
|
| 189 | + protected function getSnippet($content, $start_at = '', $length = 40) |
|
| 190 | + { |
|
| 191 | + $start_pos = 0; |
|
| 192 | + |
|
| 193 | + if ($start_at !== '') { |
|
| 194 | + $start_pos = strpos($content, $start_at); |
|
| 195 | + if ($start_pos !== false) { |
|
| 196 | + $start_pos += strlen($start_at); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $snippet = substr($content, $start_pos, $length); |
|
| 201 | + if ((strlen($content) - $start_pos) > $length) { |
|
| 202 | + $snippet .= '...'; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return $snippet; |
|
| 206 | + |
|
| 207 | + }//end getSnippet() |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Try and find a matching PHP closing tag. |
|
| 212 | + * |
|
| 213 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 214 | + * @param array $tokens The token stack. |
|
| 215 | + * @param int $stackPtr The position of the current token |
|
| 216 | + * in the stack passed in $tokens. |
|
| 217 | + * @param string $content The expected content of the closing tag to match the opener. |
|
| 218 | + * |
|
| 219 | + * @return int|false Pointer to the position in the stack for the closing tag or false if not found. |
|
| 220 | + */ |
|
| 221 | + protected function findClosingTag(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content) |
|
| 222 | + { |
|
| 223 | + $closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1)); |
|
| 224 | + |
|
| 225 | + if ($closer !== false && $content === trim($tokens[$closer]['content'])) { |
|
| 226 | + return $closer; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return false; |
|
| 230 | + |
|
| 231 | + }//end findClosingTag() |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Add a changeset to replace the alternative PHP tags. |
|
| 236 | + * |
|
| 237 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 238 | + * @param array $tokens The token stack. |
|
| 239 | + * @param int $open_tag_pointer Stack pointer to the PHP open tag. |
|
| 240 | + * @param int $close_tag_pointer Stack pointer to the PHP close tag. |
|
| 241 | + * @param bool $echo Whether to add 'echo' or not. |
|
| 242 | + * |
|
| 243 | + * @return void |
|
| 244 | + */ |
|
| 245 | + protected function addChangeset(PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false) |
|
| 246 | + { |
|
| 247 | + // Build up the open tag replacement and make sure there's always whitespace behind it. |
|
| 248 | + $open_replacement = '<?php'; |
|
| 249 | + if ($echo === true) { |
|
| 250 | + $open_replacement .= ' echo'; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + if ($tokens[($open_tag_pointer + 1)]['code'] !== T_WHITESPACE) { |
|
| 254 | + $open_replacement .= ' '; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + // Make sure we don't remove any line breaks after the closing tag. |
|
| 258 | + $regex = '`'.preg_quote(trim($tokens[$close_tag_pointer]['content'])).'`'; |
|
| 259 | + $close_replacement = preg_replace($regex, '?>', $tokens[$close_tag_pointer]['content']); |
|
| 260 | + |
|
| 261 | + $phpcsFile->fixer->beginChangeset(); |
|
| 262 | + $phpcsFile->fixer->replaceToken($open_tag_pointer, $open_replacement); |
|
| 263 | + $phpcsFile->fixer->replaceToken($close_tag_pointer, $close_replacement); |
|
| 264 | + $phpcsFile->fixer->endChangeset(); |
|
| 265 | + |
|
| 266 | + }//end addChangeset() |
|
| 267 | 267 | |
| 268 | 268 | |
| 269 | 269 | }//end class |
@@ -55,15 +55,15 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function register() |
| 57 | 57 | { |
| 58 | - if ($this->_phpVersion === null) { |
|
| 59 | - $this->_phpVersion = PHP_CodeSniffer::getConfigData('php_version'); |
|
| 60 | - if ($this->_phpVersion === null) { |
|
| 58 | + if ( $this->_phpVersion === null ) { |
|
| 59 | + $this->_phpVersion = PHP_CodeSniffer::getConfigData( 'php_version' ); |
|
| 60 | + if ( $this->_phpVersion === null ) { |
|
| 61 | 61 | $this->_phpVersion = PHP_VERSION_ID; |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if ($this->_phpVersion < 70000) { |
|
| 66 | - $this->_aspTags = (boolean) ini_get('asp_tags'); |
|
| 65 | + if ( $this->_phpVersion < 70000 ) { |
|
| 66 | + $this->_aspTags = (boolean) ini_get( 'asp_tags' ); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | return array( |
@@ -84,36 +84,36 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @return void |
| 86 | 86 | */ |
| 87 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 87 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 88 | 88 | { |
| 89 | 89 | $tokens = $phpcsFile->getTokens(); |
| 90 | - $openTag = $tokens[$stackPtr]; |
|
| 91 | - $content = $openTag['content']; |
|
| 90 | + $openTag = $tokens[ $stackPtr ]; |
|
| 91 | + $content = $openTag[ 'content' ]; |
|
| 92 | 92 | |
| 93 | - if (trim($content) === '') { |
|
| 93 | + if ( trim( $content ) === '' ) { |
|
| 94 | 94 | return; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if ($openTag['code'] === T_OPEN_TAG) { |
|
| 98 | - if ($content === '<%') { |
|
| 97 | + if ( $openTag[ 'code' ] === T_OPEN_TAG ) { |
|
| 98 | + if ( $content === '<%' ) { |
|
| 99 | 99 | $error = 'ASP style opening tag used; expected "<?php" but found "%s"'; |
| 100 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 100 | + $closer = $this->findClosingTag( $phpcsFile, $tokens, $stackPtr, '%>' ); |
|
| 101 | 101 | $errorCode = 'ASPOpenTagFound'; |
| 102 | - } else if (strpos($content, '<script ') !== false) { |
|
| 102 | + } else if ( strpos( $content, '<script ' ) !== false ) { |
|
| 103 | 103 | $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
| 104 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '</script>'); |
|
| 104 | + $closer = $this->findClosingTag( $phpcsFile, $tokens, $stackPtr, '</script>' ); |
|
| 105 | 105 | $errorCode = 'ScriptOpenTagFound'; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - if (isset($error, $closer, $errorCode) === true) { |
|
| 109 | - $data = array($content); |
|
| 108 | + if ( isset( $error, $closer, $errorCode ) === true ) { |
|
| 109 | + $data = array( $content ); |
|
| 110 | 110 | |
| 111 | - if ($closer === false) { |
|
| 112 | - $phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
|
| 111 | + if ( $closer === false ) { |
|
| 112 | + $phpcsFile->addError( $error, $stackPtr, $errorCode, $data ); |
|
| 113 | 113 | } else { |
| 114 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data); |
|
| 115 | - if ($fix === true) { |
|
| 116 | - $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer); |
|
| 114 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, $errorCode, $data ); |
|
| 115 | + if ( $fix === true ) { |
|
| 116 | + $this->addChangeset( $phpcsFile, $tokens, $stackPtr, $closer ); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -121,24 +121,24 @@ discard block |
||
| 121 | 121 | return; |
| 122 | 122 | }//end if |
| 123 | 123 | |
| 124 | - if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO && $content === '<%=') { |
|
| 124 | + if ( $openTag[ 'code' ] === T_OPEN_TAG_WITH_ECHO && $content === '<%=' ) { |
|
| 125 | 125 | $error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."'; |
| 126 | - $nextVar = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 127 | - $snippet = $this->getSnippet($tokens[$nextVar]['content']); |
|
| 126 | + $nextVar = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
| 127 | + $snippet = $this->getSnippet( $tokens[ $nextVar ][ 'content' ] ); |
|
| 128 | 128 | $data = array( |
| 129 | 129 | $snippet, |
| 130 | 130 | $content, |
| 131 | 131 | $snippet, |
| 132 | 132 | ); |
| 133 | 133 | |
| 134 | - $closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>'); |
|
| 134 | + $closer = $this->findClosingTag( $phpcsFile, $tokens, $stackPtr, '%>' ); |
|
| 135 | 135 | |
| 136 | - if ($closer === false) { |
|
| 137 | - $phpcsFile->addError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 136 | + if ( $closer === false ) { |
|
| 137 | + $phpcsFile->addError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); |
|
| 138 | 138 | } else { |
| 139 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ASPShortOpenTagFound', $data); |
|
| 140 | - if ($fix === true) { |
|
| 141 | - $this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer, true); |
|
| 139 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); |
|
| 140 | + if ( $fix === true ) { |
|
| 141 | + $this->addChangeset( $phpcsFile, $tokens, $stackPtr, $closer, true ); |
|
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
@@ -147,30 +147,30 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | // Account for incorrect script open tags. |
| 149 | 149 | // The "(?:<s)?" in the regex is to work-around a bug in PHP 5.2. |
| 150 | - if ($openTag['code'] === T_INLINE_HTML |
|
| 151 | - && preg_match('`((?:<s)?cript (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1 |
|
| 150 | + if ( $openTag[ 'code' ] === T_INLINE_HTML |
|
| 151 | + && preg_match( '`((?:<s)?cript (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match ) === 1 |
|
| 152 | 152 | ) { |
| 153 | 153 | $error = 'Script style opening tag used; expected "<?php" but found "%s"'; |
| 154 | - $snippet = $this->getSnippet($content, $match[1]); |
|
| 155 | - $data = array($match[1].$snippet); |
|
| 154 | + $snippet = $this->getSnippet( $content, $match[ 1 ] ); |
|
| 155 | + $data = array( $match[ 1 ] . $snippet ); |
|
| 156 | 156 | |
| 157 | - $phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data); |
|
| 157 | + $phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); |
|
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ($openTag['code'] === T_INLINE_HTML && $this->_aspTags === false) { |
|
| 162 | - if (strpos($content, '<%=') !== false) { |
|
| 161 | + if ( $openTag[ 'code' ] === T_INLINE_HTML && $this->_aspTags === false ) { |
|
| 162 | + if ( strpos( $content, '<%=' ) !== false ) { |
|
| 163 | 163 | $error = 'Possible use of ASP style short opening tags detected. Needs manual inspection. Found: %s'; |
| 164 | - $snippet = $this->getSnippet($content, '<%='); |
|
| 165 | - $data = array('<%='.$snippet); |
|
| 164 | + $snippet = $this->getSnippet( $content, '<%=' ); |
|
| 165 | + $data = array( '<%=' . $snippet ); |
|
| 166 | 166 | |
| 167 | - $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data); |
|
| 168 | - } else if (strpos($content, '<%') !== false) { |
|
| 167 | + $phpcsFile->addWarning( $error, $stackPtr, 'MaybeASPShortOpenTagFound', $data ); |
|
| 168 | + } else if ( strpos( $content, '<%' ) !== false ) { |
|
| 169 | 169 | $error = 'Possible use of ASP style opening tags detected. Needs manual inspection. Found: %s'; |
| 170 | - $snippet = $this->getSnippet($content, '<%'); |
|
| 171 | - $data = array('<%'.$snippet); |
|
| 170 | + $snippet = $this->getSnippet( $content, '<%' ); |
|
| 171 | + $data = array( '<%' . $snippet ); |
|
| 172 | 172 | |
| 173 | - $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data); |
|
| 173 | + $phpcsFile->addWarning( $error, $stackPtr, 'MaybeASPOpenTagFound', $data ); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
@@ -186,19 +186,19 @@ discard block |
||
| 186 | 186 | * |
| 187 | 187 | * @return string |
| 188 | 188 | */ |
| 189 | - protected function getSnippet($content, $start_at = '', $length = 40) |
|
| 189 | + protected function getSnippet( $content, $start_at = '', $length = 40 ) |
|
| 190 | 190 | { |
| 191 | 191 | $start_pos = 0; |
| 192 | 192 | |
| 193 | - if ($start_at !== '') { |
|
| 194 | - $start_pos = strpos($content, $start_at); |
|
| 195 | - if ($start_pos !== false) { |
|
| 196 | - $start_pos += strlen($start_at); |
|
| 193 | + if ( $start_at !== '' ) { |
|
| 194 | + $start_pos = strpos( $content, $start_at ); |
|
| 195 | + if ( $start_pos !== false ) { |
|
| 196 | + $start_pos += strlen( $start_at ); |
|
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - $snippet = substr($content, $start_pos, $length); |
|
| 201 | - if ((strlen($content) - $start_pos) > $length) { |
|
| 200 | + $snippet = substr( $content, $start_pos, $length ); |
|
| 201 | + if ( ( strlen( $content ) - $start_pos ) > $length ) { |
|
| 202 | 202 | $snippet .= '...'; |
| 203 | 203 | } |
| 204 | 204 | |
@@ -218,11 +218,11 @@ discard block |
||
| 218 | 218 | * |
| 219 | 219 | * @return int|false Pointer to the position in the stack for the closing tag or false if not found. |
| 220 | 220 | */ |
| 221 | - protected function findClosingTag(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content) |
|
| 221 | + protected function findClosingTag( PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content ) |
|
| 222 | 222 | { |
| 223 | - $closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1)); |
|
| 223 | + $closer = $phpcsFile->findNext( T_CLOSE_TAG, ( $stackPtr + 1 ) ); |
|
| 224 | 224 | |
| 225 | - if ($closer !== false && $content === trim($tokens[$closer]['content'])) { |
|
| 225 | + if ( $closer !== false && $content === trim( $tokens[ $closer ][ 'content' ] ) ) { |
|
| 226 | 226 | return $closer; |
| 227 | 227 | } |
| 228 | 228 | |
@@ -242,25 +242,25 @@ discard block |
||
| 242 | 242 | * |
| 243 | 243 | * @return void |
| 244 | 244 | */ |
| 245 | - protected function addChangeset(PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false) |
|
| 245 | + protected function addChangeset( PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false ) |
|
| 246 | 246 | { |
| 247 | 247 | // Build up the open tag replacement and make sure there's always whitespace behind it. |
| 248 | 248 | $open_replacement = '<?php'; |
| 249 | - if ($echo === true) { |
|
| 249 | + if ( $echo === true ) { |
|
| 250 | 250 | $open_replacement .= ' echo'; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if ($tokens[($open_tag_pointer + 1)]['code'] !== T_WHITESPACE) { |
|
| 253 | + if ( $tokens[ ( $open_tag_pointer + 1 ) ][ 'code' ] !== T_WHITESPACE ) { |
|
| 254 | 254 | $open_replacement .= ' '; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | // Make sure we don't remove any line breaks after the closing tag. |
| 258 | - $regex = '`'.preg_quote(trim($tokens[$close_tag_pointer]['content'])).'`'; |
|
| 259 | - $close_replacement = preg_replace($regex, '?>', $tokens[$close_tag_pointer]['content']); |
|
| 258 | + $regex = '`' . preg_quote( trim( $tokens[ $close_tag_pointer ][ 'content' ] ) ) . '`'; |
|
| 259 | + $close_replacement = preg_replace( $regex, '?>', $tokens[ $close_tag_pointer ][ 'content' ] ); |
|
| 260 | 260 | |
| 261 | 261 | $phpcsFile->fixer->beginChangeset(); |
| 262 | - $phpcsFile->fixer->replaceToken($open_tag_pointer, $open_replacement); |
|
| 263 | - $phpcsFile->fixer->replaceToken($close_tag_pointer, $close_replacement); |
|
| 262 | + $phpcsFile->fixer->replaceToken( $open_tag_pointer, $open_replacement ); |
|
| 263 | + $phpcsFile->fixer->replaceToken( $close_tag_pointer, $close_replacement ); |
|
| 264 | 264 | $phpcsFile->fixer->endChangeset(); |
| 265 | 265 | |
| 266 | 266 | }//end addChangeset() |
@@ -29,8 +29,7 @@ discard block |
||
| 29 | 29 | * @version Release: @package_version@ |
| 30 | 30 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 31 | 31 | */ |
| 32 | -class Generic_Sniffs_PHP_DisallowAlternativePHPTagsSniff implements PHP_CodeSniffer_Sniff |
|
| 33 | -{ |
|
| 32 | +class Generic_Sniffs_PHP_DisallowAlternativePHPTagsSniff implements PHP_CodeSniffer_Sniff { |
|
| 34 | 33 | |
| 35 | 34 | |
| 36 | 35 | /** |
@@ -53,8 +52,7 @@ discard block |
||
| 53 | 52 | * |
| 54 | 53 | * @return array |
| 55 | 54 | */ |
| 56 | - public function register() |
|
| 57 | - { |
|
| 55 | + public function register() { |
|
| 58 | 56 | if ($this->_phpVersion === null) { |
| 59 | 57 | $this->_phpVersion = PHP_CodeSniffer::getConfigData('php_version'); |
| 60 | 58 | if ($this->_phpVersion === null) { |
@@ -84,8 +82,7 @@ discard block |
||
| 84 | 82 | * |
| 85 | 83 | * @return void |
| 86 | 84 | */ |
| 87 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 88 | - { |
|
| 85 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 89 | 86 | $tokens = $phpcsFile->getTokens(); |
| 90 | 87 | $openTag = $tokens[$stackPtr]; |
| 91 | 88 | $content = $openTag['content']; |
@@ -186,8 +183,7 @@ discard block |
||
| 186 | 183 | * |
| 187 | 184 | * @return string |
| 188 | 185 | */ |
| 189 | - protected function getSnippet($content, $start_at = '', $length = 40) |
|
| 190 | - { |
|
| 186 | + protected function getSnippet($content, $start_at = '', $length = 40) { |
|
| 191 | 187 | $start_pos = 0; |
| 192 | 188 | |
| 193 | 189 | if ($start_at !== '') { |
@@ -218,8 +214,7 @@ discard block |
||
| 218 | 214 | * |
| 219 | 215 | * @return int|false Pointer to the position in the stack for the closing tag or false if not found. |
| 220 | 216 | */ |
| 221 | - protected function findClosingTag(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content) |
|
| 222 | - { |
|
| 217 | + protected function findClosingTag(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content) { |
|
| 223 | 218 | $closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1)); |
| 224 | 219 | |
| 225 | 220 | if ($closer !== false && $content === trim($tokens[$closer]['content'])) { |
@@ -242,8 +237,7 @@ discard block |
||
| 242 | 237 | * |
| 243 | 238 | * @return void |
| 244 | 239 | */ |
| 245 | - protected function addChangeset(PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false) |
|
| 246 | - { |
|
| 240 | + protected function addChangeset(PHP_CodeSniffer_File $phpcsFile, $tokens, $open_tag_pointer, $close_tag_pointer, $echo = false) { |
|
| 247 | 241 | // Build up the open tag replacement and make sure there's always whitespace behind it. |
| 248 | 242 | $open_replacement = '<?php'; |
| 249 | 243 | if ($echo === true) { |
@@ -44,7 +44,7 @@ |
||
| 44 | 44 | /** |
| 45 | 45 | * Returns an array of tokens this test wants to listen for. |
| 46 | 46 | * |
| 47 | - * @return array |
|
| 47 | + * @return string[] |
|
| 48 | 48 | */ |
| 49 | 49 | public function register() |
| 50 | 50 | { |
@@ -33,47 +33,47 @@ |
||
| 33 | 33 | class Generic_Sniffs_PHP_NoSilencedErrorsSniff implements PHP_CodeSniffer_Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * If true, an error will be thrown; otherwise a warning. |
|
| 38 | - * |
|
| 39 | - * @var bool |
|
| 40 | - */ |
|
| 41 | - public $error = false; |
|
| 36 | + /** |
|
| 37 | + * If true, an error will be thrown; otherwise a warning. |
|
| 38 | + * |
|
| 39 | + * @var bool |
|
| 40 | + */ |
|
| 41 | + public $error = false; |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Returns an array of tokens this test wants to listen for. |
|
| 46 | - * |
|
| 47 | - * @return array |
|
| 48 | - */ |
|
| 49 | - public function register() |
|
| 50 | - { |
|
| 51 | - return array(T_ASPERAND); |
|
| 44 | + /** |
|
| 45 | + * Returns an array of tokens this test wants to listen for. |
|
| 46 | + * |
|
| 47 | + * @return array |
|
| 48 | + */ |
|
| 49 | + public function register() |
|
| 50 | + { |
|
| 51 | + return array(T_ASPERAND); |
|
| 52 | 52 | |
| 53 | - }//end register() |
|
| 53 | + }//end register() |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Processes this test, when one of its tokens is encountered. |
|
| 58 | - * |
|
| 59 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 60 | - * @param int $stackPtr The position of the current token |
|
| 61 | - * in the stack passed in $tokens. |
|
| 62 | - * |
|
| 63 | - * @return void |
|
| 64 | - */ |
|
| 65 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 66 | - { |
|
| 67 | - $error = 'Silencing errors is forbidden'; |
|
| 68 | - if ($this->error === true) { |
|
| 69 | - $error = 'Silencing errors is forbidden'; |
|
| 70 | - $phpcsFile->addError($error, $stackPtr, 'Forbidden'); |
|
| 71 | - } else { |
|
| 72 | - $error = 'Silencing errors is discouraged'; |
|
| 73 | - $phpcsFile->addWarning($error, $stackPtr, 'Discouraged'); |
|
| 74 | - } |
|
| 56 | + /** |
|
| 57 | + * Processes this test, when one of its tokens is encountered. |
|
| 58 | + * |
|
| 59 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 60 | + * @param int $stackPtr The position of the current token |
|
| 61 | + * in the stack passed in $tokens. |
|
| 62 | + * |
|
| 63 | + * @return void |
|
| 64 | + */ |
|
| 65 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 66 | + { |
|
| 67 | + $error = 'Silencing errors is forbidden'; |
|
| 68 | + if ($this->error === true) { |
|
| 69 | + $error = 'Silencing errors is forbidden'; |
|
| 70 | + $phpcsFile->addError($error, $stackPtr, 'Forbidden'); |
|
| 71 | + } else { |
|
| 72 | + $error = 'Silencing errors is discouraged'; |
|
| 73 | + $phpcsFile->addWarning($error, $stackPtr, 'Discouraged'); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - }//end process() |
|
| 76 | + }//end process() |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | 79 | }//end class |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function register() |
| 50 | 50 | { |
| 51 | - return array(T_ASPERAND); |
|
| 51 | + return array( T_ASPERAND ); |
|
| 52 | 52 | |
| 53 | 53 | }//end register() |
| 54 | 54 | |
@@ -62,15 +62,15 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @return void |
| 64 | 64 | */ |
| 65 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 65 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 66 | 66 | { |
| 67 | 67 | $error = 'Silencing errors is forbidden'; |
| 68 | - if ($this->error === true) { |
|
| 68 | + if ( $this->error === true ) { |
|
| 69 | 69 | $error = 'Silencing errors is forbidden'; |
| 70 | - $phpcsFile->addError($error, $stackPtr, 'Forbidden'); |
|
| 70 | + $phpcsFile->addError( $error, $stackPtr, 'Forbidden' ); |
|
| 71 | 71 | } else { |
| 72 | 72 | $error = 'Silencing errors is discouraged'; |
| 73 | - $phpcsFile->addWarning($error, $stackPtr, 'Discouraged'); |
|
| 73 | + $phpcsFile->addWarning( $error, $stackPtr, 'Discouraged' ); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | }//end process() |
@@ -30,8 +30,7 @@ discard block |
||
| 30 | 30 | * @version Release: @package_version@ |
| 31 | 31 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 32 | 32 | */ |
| 33 | -class Generic_Sniffs_PHP_NoSilencedErrorsSniff implements PHP_CodeSniffer_Sniff |
|
| 34 | -{ |
|
| 33 | +class Generic_Sniffs_PHP_NoSilencedErrorsSniff implements PHP_CodeSniffer_Sniff { |
|
| 35 | 34 | |
| 36 | 35 | /** |
| 37 | 36 | * If true, an error will be thrown; otherwise a warning. |
@@ -46,8 +45,7 @@ discard block |
||
| 46 | 45 | * |
| 47 | 46 | * @return array |
| 48 | 47 | */ |
| 49 | - public function register() |
|
| 50 | - { |
|
| 48 | + public function register() { |
|
| 51 | 49 | return array(T_ASPERAND); |
| 52 | 50 | |
| 53 | 51 | }//end register() |
@@ -62,8 +60,7 @@ discard block |
||
| 62 | 60 | * |
| 63 | 61 | * @return void |
| 64 | 62 | */ |
| 65 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 66 | - { |
|
| 63 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 67 | 64 | $error = 'Silencing errors is forbidden'; |
| 68 | 65 | if ($this->error === true) { |
| 69 | 66 | $error = 'Silencing errors is forbidden'; |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | /** |
| 47 | 47 | * Returns an array of tokens this test wants to listen for. |
| 48 | 48 | * |
| 49 | - * @return array |
|
| 49 | + * @return integer[] |
|
| 50 | 50 | */ |
| 51 | 51 | public function register() |
| 52 | 52 | { |
@@ -28,177 +28,177 @@ |
||
| 28 | 28 | class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The Subversion properties that should be set. |
|
| 33 | - * |
|
| 34 | - * Key of array is the SVN property and the value is the |
|
| 35 | - * exact value the property should have or NULL if the |
|
| 36 | - * property should just be set but the value is not fixed. |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $properties = array( |
|
| 41 | - 'svn:keywords' => 'Author Id Revision', |
|
| 42 | - 'svn:eol-style' => 'native', |
|
| 43 | - ); |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Returns an array of tokens this test wants to listen for. |
|
| 48 | - * |
|
| 49 | - * @return array |
|
| 50 | - */ |
|
| 51 | - public function register() |
|
| 52 | - { |
|
| 53 | - return array(T_OPEN_TAG); |
|
| 54 | - |
|
| 55 | - }//end register() |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Processes this test, when one of its tokens is encountered. |
|
| 60 | - * |
|
| 61 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 62 | - * @param int $stackPtr The position of the current token |
|
| 63 | - * in the stack passed in $tokens. |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 68 | - { |
|
| 69 | - $tokens = $phpcsFile->getTokens(); |
|
| 70 | - |
|
| 71 | - // Make sure this is the first PHP open tag so we don't process the |
|
| 72 | - // same file twice. |
|
| 73 | - $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
|
| 74 | - if ($prevOpenTag !== false) { |
|
| 75 | - return; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $path = $phpcsFile->getFileName(); |
|
| 79 | - $properties = $this->getProperties($path); |
|
| 80 | - if ($properties === null) { |
|
| 81 | - // Not under version control. |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $allProperties = ($properties + $this->properties); |
|
| 86 | - foreach ($allProperties as $key => $value) { |
|
| 87 | - if (isset($properties[$key]) === true |
|
| 88 | - && isset($this->properties[$key]) === false |
|
| 89 | - ) { |
|
| 90 | - $error = 'Unexpected Subversion property "%s" = "%s"'; |
|
| 91 | - $data = array( |
|
| 92 | - $key, |
|
| 93 | - $properties[$key], |
|
| 94 | - ); |
|
| 95 | - $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data); |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if (isset($properties[$key]) === false |
|
| 100 | - && isset($this->properties[$key]) === true |
|
| 101 | - ) { |
|
| 102 | - $error = 'Missing Subversion property "%s" = "%s"'; |
|
| 103 | - $data = array( |
|
| 104 | - $key, |
|
| 105 | - $this->properties[$key], |
|
| 106 | - ); |
|
| 107 | - $phpcsFile->addError($error, $stackPtr, 'Missing', $data); |
|
| 108 | - continue; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if ($properties[$key] !== null |
|
| 112 | - && $properties[$key] !== $this->properties[$key] |
|
| 113 | - ) { |
|
| 114 | - $error = 'Subversion property "%s" = "%s" does not match "%s"'; |
|
| 115 | - $data = array( |
|
| 116 | - $key, |
|
| 117 | - $properties[$key], |
|
| 118 | - $this->properties[$key], |
|
| 119 | - ); |
|
| 120 | - $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data); |
|
| 121 | - } |
|
| 122 | - }//end foreach |
|
| 123 | - |
|
| 124 | - }//end process() |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns the Subversion properties which are actually set on a path. |
|
| 129 | - * |
|
| 130 | - * Returns NULL if the file is not under version control. |
|
| 131 | - * |
|
| 132 | - * @param string $path The path to return Subversion properties on. |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - * @throws PHP_CodeSniffer_Exception If Subversion properties file could |
|
| 136 | - * not be opened. |
|
| 137 | - */ |
|
| 138 | - protected function getProperties($path) |
|
| 139 | - { |
|
| 140 | - $properties = array(); |
|
| 141 | - |
|
| 142 | - $paths = array(); |
|
| 143 | - $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work'; |
|
| 144 | - $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base'; |
|
| 145 | - |
|
| 146 | - $foundPath = false; |
|
| 147 | - foreach ($paths as $path) { |
|
| 148 | - if (file_exists($path) === true) { |
|
| 149 | - $foundPath = true; |
|
| 150 | - |
|
| 151 | - $handle = fopen($path, 'r'); |
|
| 152 | - if ($handle === false) { |
|
| 153 | - $error = 'Error opening file; could not get Subversion properties'; |
|
| 154 | - throw new PHP_CodeSniffer_Exception($error); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - while (feof($handle) === false) { |
|
| 158 | - // Read a key length line. Might be END, though. |
|
| 159 | - $buffer = trim(fgets($handle)); |
|
| 160 | - |
|
| 161 | - // Check for the end of the hash. |
|
| 162 | - if ($buffer === 'END') { |
|
| 163 | - break; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - // Now read that much into a buffer. |
|
| 167 | - $key = fread($handle, substr($buffer, 2)); |
|
| 168 | - |
|
| 169 | - // Suck up extra newline after key data. |
|
| 170 | - fgetc($handle); |
|
| 171 | - |
|
| 172 | - // Read a value length line. |
|
| 173 | - $buffer = trim(fgets($handle)); |
|
| 174 | - |
|
| 175 | - // Now read that much into a buffer. |
|
| 176 | - $length = substr($buffer, 2); |
|
| 177 | - if ($length === '0') { |
|
| 178 | - // Length of value is ZERO characters, so |
|
| 179 | - // value is actually empty. |
|
| 180 | - $value = ''; |
|
| 181 | - } else { |
|
| 182 | - $value = fread($handle, $length); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Suck up extra newline after value data. |
|
| 186 | - fgetc($handle); |
|
| 187 | - |
|
| 188 | - $properties[$key] = $value; |
|
| 189 | - }//end while |
|
| 190 | - |
|
| 191 | - fclose($handle); |
|
| 192 | - }//end if |
|
| 193 | - }//end foreach |
|
| 194 | - |
|
| 195 | - if ($foundPath === false) { |
|
| 196 | - return null; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $properties; |
|
| 200 | - |
|
| 201 | - }//end getProperties() |
|
| 31 | + /** |
|
| 32 | + * The Subversion properties that should be set. |
|
| 33 | + * |
|
| 34 | + * Key of array is the SVN property and the value is the |
|
| 35 | + * exact value the property should have or NULL if the |
|
| 36 | + * property should just be set but the value is not fixed. |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $properties = array( |
|
| 41 | + 'svn:keywords' => 'Author Id Revision', |
|
| 42 | + 'svn:eol-style' => 'native', |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Returns an array of tokens this test wants to listen for. |
|
| 48 | + * |
|
| 49 | + * @return array |
|
| 50 | + */ |
|
| 51 | + public function register() |
|
| 52 | + { |
|
| 53 | + return array(T_OPEN_TAG); |
|
| 54 | + |
|
| 55 | + }//end register() |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Processes this test, when one of its tokens is encountered. |
|
| 60 | + * |
|
| 61 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 62 | + * @param int $stackPtr The position of the current token |
|
| 63 | + * in the stack passed in $tokens. |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 68 | + { |
|
| 69 | + $tokens = $phpcsFile->getTokens(); |
|
| 70 | + |
|
| 71 | + // Make sure this is the first PHP open tag so we don't process the |
|
| 72 | + // same file twice. |
|
| 73 | + $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
|
| 74 | + if ($prevOpenTag !== false) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $path = $phpcsFile->getFileName(); |
|
| 79 | + $properties = $this->getProperties($path); |
|
| 80 | + if ($properties === null) { |
|
| 81 | + // Not under version control. |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $allProperties = ($properties + $this->properties); |
|
| 86 | + foreach ($allProperties as $key => $value) { |
|
| 87 | + if (isset($properties[$key]) === true |
|
| 88 | + && isset($this->properties[$key]) === false |
|
| 89 | + ) { |
|
| 90 | + $error = 'Unexpected Subversion property "%s" = "%s"'; |
|
| 91 | + $data = array( |
|
| 92 | + $key, |
|
| 93 | + $properties[$key], |
|
| 94 | + ); |
|
| 95 | + $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data); |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if (isset($properties[$key]) === false |
|
| 100 | + && isset($this->properties[$key]) === true |
|
| 101 | + ) { |
|
| 102 | + $error = 'Missing Subversion property "%s" = "%s"'; |
|
| 103 | + $data = array( |
|
| 104 | + $key, |
|
| 105 | + $this->properties[$key], |
|
| 106 | + ); |
|
| 107 | + $phpcsFile->addError($error, $stackPtr, 'Missing', $data); |
|
| 108 | + continue; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if ($properties[$key] !== null |
|
| 112 | + && $properties[$key] !== $this->properties[$key] |
|
| 113 | + ) { |
|
| 114 | + $error = 'Subversion property "%s" = "%s" does not match "%s"'; |
|
| 115 | + $data = array( |
|
| 116 | + $key, |
|
| 117 | + $properties[$key], |
|
| 118 | + $this->properties[$key], |
|
| 119 | + ); |
|
| 120 | + $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data); |
|
| 121 | + } |
|
| 122 | + }//end foreach |
|
| 123 | + |
|
| 124 | + }//end process() |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns the Subversion properties which are actually set on a path. |
|
| 129 | + * |
|
| 130 | + * Returns NULL if the file is not under version control. |
|
| 131 | + * |
|
| 132 | + * @param string $path The path to return Subversion properties on. |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + * @throws PHP_CodeSniffer_Exception If Subversion properties file could |
|
| 136 | + * not be opened. |
|
| 137 | + */ |
|
| 138 | + protected function getProperties($path) |
|
| 139 | + { |
|
| 140 | + $properties = array(); |
|
| 141 | + |
|
| 142 | + $paths = array(); |
|
| 143 | + $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work'; |
|
| 144 | + $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base'; |
|
| 145 | + |
|
| 146 | + $foundPath = false; |
|
| 147 | + foreach ($paths as $path) { |
|
| 148 | + if (file_exists($path) === true) { |
|
| 149 | + $foundPath = true; |
|
| 150 | + |
|
| 151 | + $handle = fopen($path, 'r'); |
|
| 152 | + if ($handle === false) { |
|
| 153 | + $error = 'Error opening file; could not get Subversion properties'; |
|
| 154 | + throw new PHP_CodeSniffer_Exception($error); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + while (feof($handle) === false) { |
|
| 158 | + // Read a key length line. Might be END, though. |
|
| 159 | + $buffer = trim(fgets($handle)); |
|
| 160 | + |
|
| 161 | + // Check for the end of the hash. |
|
| 162 | + if ($buffer === 'END') { |
|
| 163 | + break; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + // Now read that much into a buffer. |
|
| 167 | + $key = fread($handle, substr($buffer, 2)); |
|
| 168 | + |
|
| 169 | + // Suck up extra newline after key data. |
|
| 170 | + fgetc($handle); |
|
| 171 | + |
|
| 172 | + // Read a value length line. |
|
| 173 | + $buffer = trim(fgets($handle)); |
|
| 174 | + |
|
| 175 | + // Now read that much into a buffer. |
|
| 176 | + $length = substr($buffer, 2); |
|
| 177 | + if ($length === '0') { |
|
| 178 | + // Length of value is ZERO characters, so |
|
| 179 | + // value is actually empty. |
|
| 180 | + $value = ''; |
|
| 181 | + } else { |
|
| 182 | + $value = fread($handle, $length); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Suck up extra newline after value data. |
|
| 186 | + fgetc($handle); |
|
| 187 | + |
|
| 188 | + $properties[$key] = $value; |
|
| 189 | + }//end while |
|
| 190 | + |
|
| 191 | + fclose($handle); |
|
| 192 | + }//end if |
|
| 193 | + }//end foreach |
|
| 194 | + |
|
| 195 | + if ($foundPath === false) { |
|
| 196 | + return null; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $properties; |
|
| 200 | + |
|
| 201 | + }//end getProperties() |
|
| 202 | 202 | |
| 203 | 203 | |
| 204 | 204 | }//end class |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function register() |
| 52 | 52 | { |
| 53 | - return array(T_OPEN_TAG); |
|
| 53 | + return array( T_OPEN_TAG ); |
|
| 54 | 54 | |
| 55 | 55 | }//end register() |
| 56 | 56 | |
@@ -64,60 +64,60 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return void |
| 66 | 66 | */ |
| 67 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 67 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 68 | 68 | { |
| 69 | 69 | $tokens = $phpcsFile->getTokens(); |
| 70 | 70 | |
| 71 | 71 | // Make sure this is the first PHP open tag so we don't process the |
| 72 | 72 | // same file twice. |
| 73 | - $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
|
| 74 | - if ($prevOpenTag !== false) { |
|
| 73 | + $prevOpenTag = $phpcsFile->findPrevious( T_OPEN_TAG, ( $stackPtr - 1 ) ); |
|
| 74 | + if ( $prevOpenTag !== false ) { |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $path = $phpcsFile->getFileName(); |
| 79 | - $properties = $this->getProperties($path); |
|
| 80 | - if ($properties === null) { |
|
| 79 | + $properties = $this->getProperties( $path ); |
|
| 80 | + if ( $properties === null ) { |
|
| 81 | 81 | // Not under version control. |
| 82 | 82 | return; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $allProperties = ($properties + $this->properties); |
|
| 86 | - foreach ($allProperties as $key => $value) { |
|
| 87 | - if (isset($properties[$key]) === true |
|
| 88 | - && isset($this->properties[$key]) === false |
|
| 85 | + $allProperties = ( $properties + $this->properties ); |
|
| 86 | + foreach ( $allProperties as $key => $value ) { |
|
| 87 | + if ( isset( $properties[ $key ] ) === true |
|
| 88 | + && isset( $this->properties[ $key ] ) === false |
|
| 89 | 89 | ) { |
| 90 | 90 | $error = 'Unexpected Subversion property "%s" = "%s"'; |
| 91 | 91 | $data = array( |
| 92 | 92 | $key, |
| 93 | - $properties[$key], |
|
| 93 | + $properties[ $key ], |
|
| 94 | 94 | ); |
| 95 | - $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data); |
|
| 95 | + $phpcsFile->addError( $error, $stackPtr, 'Unexpected', $data ); |
|
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - if (isset($properties[$key]) === false |
|
| 100 | - && isset($this->properties[$key]) === true |
|
| 99 | + if ( isset( $properties[ $key ] ) === false |
|
| 100 | + && isset( $this->properties[ $key ] ) === true |
|
| 101 | 101 | ) { |
| 102 | 102 | $error = 'Missing Subversion property "%s" = "%s"'; |
| 103 | 103 | $data = array( |
| 104 | 104 | $key, |
| 105 | - $this->properties[$key], |
|
| 105 | + $this->properties[ $key ], |
|
| 106 | 106 | ); |
| 107 | - $phpcsFile->addError($error, $stackPtr, 'Missing', $data); |
|
| 107 | + $phpcsFile->addError( $error, $stackPtr, 'Missing', $data ); |
|
| 108 | 108 | continue; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - if ($properties[$key] !== null |
|
| 112 | - && $properties[$key] !== $this->properties[$key] |
|
| 111 | + if ( $properties[ $key ] !== null |
|
| 112 | + && $properties[ $key ] !== $this->properties[ $key ] |
|
| 113 | 113 | ) { |
| 114 | 114 | $error = 'Subversion property "%s" = "%s" does not match "%s"'; |
| 115 | 115 | $data = array( |
| 116 | 116 | $key, |
| 117 | - $properties[$key], |
|
| 118 | - $this->properties[$key], |
|
| 117 | + $properties[ $key ], |
|
| 118 | + $this->properties[ $key ], |
|
| 119 | 119 | ); |
| 120 | - $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data); |
|
| 120 | + $phpcsFile->addError( $error, $stackPtr, 'NoMatch', $data ); |
|
| 121 | 121 | } |
| 122 | 122 | }//end foreach |
| 123 | 123 | |
@@ -135,64 +135,64 @@ discard block |
||
| 135 | 135 | * @throws PHP_CodeSniffer_Exception If Subversion properties file could |
| 136 | 136 | * not be opened. |
| 137 | 137 | */ |
| 138 | - protected function getProperties($path) |
|
| 138 | + protected function getProperties( $path ) |
|
| 139 | 139 | { |
| 140 | 140 | $properties = array(); |
| 141 | 141 | |
| 142 | 142 | $paths = array(); |
| 143 | - $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work'; |
|
| 144 | - $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base'; |
|
| 143 | + $paths[ ] = dirname( $path ) . '/.svn/props/' . basename( $path ) . '.svn-work'; |
|
| 144 | + $paths[ ] = dirname( $path ) . '/.svn/prop-base/' . basename( $path ) . '.svn-base'; |
|
| 145 | 145 | |
| 146 | 146 | $foundPath = false; |
| 147 | - foreach ($paths as $path) { |
|
| 148 | - if (file_exists($path) === true) { |
|
| 147 | + foreach ( $paths as $path ) { |
|
| 148 | + if ( file_exists( $path ) === true ) { |
|
| 149 | 149 | $foundPath = true; |
| 150 | 150 | |
| 151 | - $handle = fopen($path, 'r'); |
|
| 152 | - if ($handle === false) { |
|
| 151 | + $handle = fopen( $path, 'r' ); |
|
| 152 | + if ( $handle === false ) { |
|
| 153 | 153 | $error = 'Error opening file; could not get Subversion properties'; |
| 154 | - throw new PHP_CodeSniffer_Exception($error); |
|
| 154 | + throw new PHP_CodeSniffer_Exception( $error ); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - while (feof($handle) === false) { |
|
| 157 | + while ( feof( $handle ) === false ) { |
|
| 158 | 158 | // Read a key length line. Might be END, though. |
| 159 | - $buffer = trim(fgets($handle)); |
|
| 159 | + $buffer = trim( fgets( $handle ) ); |
|
| 160 | 160 | |
| 161 | 161 | // Check for the end of the hash. |
| 162 | - if ($buffer === 'END') { |
|
| 162 | + if ( $buffer === 'END' ) { |
|
| 163 | 163 | break; |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | // Now read that much into a buffer. |
| 167 | - $key = fread($handle, substr($buffer, 2)); |
|
| 167 | + $key = fread( $handle, substr( $buffer, 2 ) ); |
|
| 168 | 168 | |
| 169 | 169 | // Suck up extra newline after key data. |
| 170 | - fgetc($handle); |
|
| 170 | + fgetc( $handle ); |
|
| 171 | 171 | |
| 172 | 172 | // Read a value length line. |
| 173 | - $buffer = trim(fgets($handle)); |
|
| 173 | + $buffer = trim( fgets( $handle ) ); |
|
| 174 | 174 | |
| 175 | 175 | // Now read that much into a buffer. |
| 176 | - $length = substr($buffer, 2); |
|
| 177 | - if ($length === '0') { |
|
| 176 | + $length = substr( $buffer, 2 ); |
|
| 177 | + if ( $length === '0' ) { |
|
| 178 | 178 | // Length of value is ZERO characters, so |
| 179 | 179 | // value is actually empty. |
| 180 | 180 | $value = ''; |
| 181 | 181 | } else { |
| 182 | - $value = fread($handle, $length); |
|
| 182 | + $value = fread( $handle, $length ); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Suck up extra newline after value data. |
| 186 | - fgetc($handle); |
|
| 186 | + fgetc( $handle ); |
|
| 187 | 187 | |
| 188 | - $properties[$key] = $value; |
|
| 188 | + $properties[ $key ] = $value; |
|
| 189 | 189 | }//end while |
| 190 | 190 | |
| 191 | - fclose($handle); |
|
| 191 | + fclose( $handle ); |
|
| 192 | 192 | }//end if |
| 193 | 193 | }//end foreach |
| 194 | 194 | |
| 195 | - if ($foundPath === false) { |
|
| 195 | + if ( $foundPath === false ) { |
|
| 196 | 196 | return null; |
| 197 | 197 | } |
| 198 | 198 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * The Subversion properties that should be set. |
@@ -48,8 +47,7 @@ discard block |
||
| 48 | 47 | * |
| 49 | 48 | * @return array |
| 50 | 49 | */ |
| 51 | - public function register() |
|
| 52 | - { |
|
| 50 | + public function register() { |
|
| 53 | 51 | return array(T_OPEN_TAG); |
| 54 | 52 | |
| 55 | 53 | }//end register() |
@@ -64,8 +62,7 @@ discard block |
||
| 64 | 62 | * |
| 65 | 63 | * @return void |
| 66 | 64 | */ |
| 67 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 68 | - { |
|
| 65 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 69 | 66 | $tokens = $phpcsFile->getTokens(); |
| 70 | 67 | |
| 71 | 68 | // Make sure this is the first PHP open tag so we don't process the |
@@ -135,8 +132,7 @@ discard block |
||
| 135 | 132 | * @throws PHP_CodeSniffer_Exception If Subversion properties file could |
| 136 | 133 | * not be opened. |
| 137 | 134 | */ |
| 138 | - protected function getProperties($path) |
|
| 139 | - { |
|
| 135 | + protected function getProperties($path) { |
|
| 140 | 136 | $properties = array(); |
| 141 | 137 | |
| 142 | 138 | $paths = array(); |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | /** |
| 51 | 51 | * Returns an array of tokens this test wants to listen for. |
| 52 | 52 | * |
| 53 | - * @return array |
|
| 53 | + * @return integer[] |
|
| 54 | 54 | */ |
| 55 | 55 | public function register() |
| 56 | 56 | { |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | * @param int $stackPtr The position of the current token in |
| 67 | 67 | * the stack passed in $tokens. |
| 68 | 68 | * |
| 69 | - * @return void |
|
| 69 | + * @return integer |
|
| 70 | 70 | */ |
| 71 | 71 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 72 | 72 | { |
@@ -28,114 +28,114 @@ |
||
| 28 | 28 | class Generic_Sniffs_WhiteSpace_DisallowSpaceIndentSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * A list of tokenizers this sniff supports. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public $supportedTokenizers = array( |
|
| 37 | - 'PHP', |
|
| 38 | - 'JS', |
|
| 39 | - 'CSS', |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The --tab-width CLI value that is being used. |
|
| 44 | - * |
|
| 45 | - * @var int |
|
| 46 | - */ |
|
| 47 | - private $_tabWidth = null; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Returns an array of tokens this test wants to listen for. |
|
| 52 | - * |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public function register() |
|
| 56 | - { |
|
| 57 | - return array(T_OPEN_TAG); |
|
| 58 | - |
|
| 59 | - }//end register() |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Processes this test, when one of its tokens is encountered. |
|
| 64 | - * |
|
| 65 | - * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
|
| 66 | - * @param int $stackPtr The position of the current token in |
|
| 67 | - * the stack passed in $tokens. |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 72 | - { |
|
| 73 | - if ($this->_tabWidth === null) { |
|
| 74 | - $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
| 75 | - if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 76 | - // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 77 | - // It shouldn't really matter because indent checks elsewhere in the |
|
| 78 | - // standard should fix things up. |
|
| 79 | - $this->_tabWidth = 4; |
|
| 80 | - } else { |
|
| 81 | - $this->_tabWidth = $cliValues['tabWidth']; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $checkTokens = array( |
|
| 86 | - T_WHITESPACE => true, |
|
| 87 | - T_INLINE_HTML => true, |
|
| 88 | - T_DOC_COMMENT_WHITESPACE => true, |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - $tokens = $phpcsFile->getTokens(); |
|
| 92 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 93 | - if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) { |
|
| 94 | - continue; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // If tabs are being converted to spaces, the original content |
|
| 98 | - // should be used instead of the converted content. |
|
| 99 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
| 100 | - $content = $tokens[$i]['orig_content']; |
|
| 101 | - } else { |
|
| 102 | - $content = $tokens[$i]['content']; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ($content[0] === ' ') { |
|
| 106 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE && $content === ' ') { |
|
| 107 | - // Ignore file/class-level DocBlock. |
|
| 108 | - continue; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Space are considered ok if they are proceeded by tabs and not followed |
|
| 112 | - // by tabs, as is the case with standard docblock comments. |
|
| 113 | - $phpcsFile->recordMetric($i, 'Line indent', 'spaces'); |
|
| 114 | - $error = 'Tabs must be used to indent lines; spaces are not allowed'; |
|
| 115 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed'); |
|
| 116 | - if ($fix === true) { |
|
| 117 | - $trimmed = ltrim($content, ' '); |
|
| 118 | - $numSpaces = (strlen($content) - strlen($trimmed)); |
|
| 119 | - if ($numSpaces < $this->_tabWidth) { |
|
| 120 | - $numTabs = 1; |
|
| 121 | - $padding = "\t"; |
|
| 122 | - } else { |
|
| 123 | - $numTabs = floor($numSpaces / $this->_tabWidth); |
|
| 124 | - $remaining = ($numSpaces - ($numTabs * $this->_tabWidth)); |
|
| 125 | - $padding = str_repeat("\t", $numTabs).$padding = str_repeat(' ', $remaining); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $phpcsFile->fixer->replaceToken($i, $padding.$trimmed); |
|
| 129 | - } |
|
| 130 | - } else if ($content[0] === "\t") { |
|
| 131 | - $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); |
|
| 132 | - }//end if |
|
| 133 | - }//end for |
|
| 134 | - |
|
| 135 | - // Ignore the rest of the file. |
|
| 136 | - return ($phpcsFile->numTokens + 1); |
|
| 137 | - |
|
| 138 | - }//end process() |
|
| 31 | + /** |
|
| 32 | + * A list of tokenizers this sniff supports. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public $supportedTokenizers = array( |
|
| 37 | + 'PHP', |
|
| 38 | + 'JS', |
|
| 39 | + 'CSS', |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The --tab-width CLI value that is being used. |
|
| 44 | + * |
|
| 45 | + * @var int |
|
| 46 | + */ |
|
| 47 | + private $_tabWidth = null; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Returns an array of tokens this test wants to listen for. |
|
| 52 | + * |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public function register() |
|
| 56 | + { |
|
| 57 | + return array(T_OPEN_TAG); |
|
| 58 | + |
|
| 59 | + }//end register() |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Processes this test, when one of its tokens is encountered. |
|
| 64 | + * |
|
| 65 | + * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
|
| 66 | + * @param int $stackPtr The position of the current token in |
|
| 67 | + * the stack passed in $tokens. |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 72 | + { |
|
| 73 | + if ($this->_tabWidth === null) { |
|
| 74 | + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
| 75 | + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 76 | + // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 77 | + // It shouldn't really matter because indent checks elsewhere in the |
|
| 78 | + // standard should fix things up. |
|
| 79 | + $this->_tabWidth = 4; |
|
| 80 | + } else { |
|
| 81 | + $this->_tabWidth = $cliValues['tabWidth']; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $checkTokens = array( |
|
| 86 | + T_WHITESPACE => true, |
|
| 87 | + T_INLINE_HTML => true, |
|
| 88 | + T_DOC_COMMENT_WHITESPACE => true, |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + $tokens = $phpcsFile->getTokens(); |
|
| 92 | + for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 93 | + if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) { |
|
| 94 | + continue; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // If tabs are being converted to spaces, the original content |
|
| 98 | + // should be used instead of the converted content. |
|
| 99 | + if (isset($tokens[$i]['orig_content']) === true) { |
|
| 100 | + $content = $tokens[$i]['orig_content']; |
|
| 101 | + } else { |
|
| 102 | + $content = $tokens[$i]['content']; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ($content[0] === ' ') { |
|
| 106 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE && $content === ' ') { |
|
| 107 | + // Ignore file/class-level DocBlock. |
|
| 108 | + continue; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Space are considered ok if they are proceeded by tabs and not followed |
|
| 112 | + // by tabs, as is the case with standard docblock comments. |
|
| 113 | + $phpcsFile->recordMetric($i, 'Line indent', 'spaces'); |
|
| 114 | + $error = 'Tabs must be used to indent lines; spaces are not allowed'; |
|
| 115 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed'); |
|
| 116 | + if ($fix === true) { |
|
| 117 | + $trimmed = ltrim($content, ' '); |
|
| 118 | + $numSpaces = (strlen($content) - strlen($trimmed)); |
|
| 119 | + if ($numSpaces < $this->_tabWidth) { |
|
| 120 | + $numTabs = 1; |
|
| 121 | + $padding = "\t"; |
|
| 122 | + } else { |
|
| 123 | + $numTabs = floor($numSpaces / $this->_tabWidth); |
|
| 124 | + $remaining = ($numSpaces - ($numTabs * $this->_tabWidth)); |
|
| 125 | + $padding = str_repeat("\t", $numTabs).$padding = str_repeat(' ', $remaining); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $phpcsFile->fixer->replaceToken($i, $padding.$trimmed); |
|
| 129 | + } |
|
| 130 | + } else if ($content[0] === "\t") { |
|
| 131 | + $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); |
|
| 132 | + }//end if |
|
| 133 | + }//end for |
|
| 134 | + |
|
| 135 | + // Ignore the rest of the file. |
|
| 136 | + return ($phpcsFile->numTokens + 1); |
|
| 137 | + |
|
| 138 | + }//end process() |
|
| 139 | 139 | |
| 140 | 140 | |
| 141 | 141 | }//end class |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function register() |
| 56 | 56 | { |
| 57 | - return array(T_OPEN_TAG); |
|
| 57 | + return array( T_OPEN_TAG ); |
|
| 58 | 58 | |
| 59 | 59 | }//end register() |
| 60 | 60 | |
@@ -68,17 +68,17 @@ discard block |
||
| 68 | 68 | * |
| 69 | 69 | * @return void |
| 70 | 70 | */ |
| 71 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 71 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 72 | 72 | { |
| 73 | - if ($this->_tabWidth === null) { |
|
| 73 | + if ( $this->_tabWidth === null ) { |
|
| 74 | 74 | $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
| 75 | - if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 75 | + if ( isset( $cliValues[ 'tabWidth' ] ) === false || $cliValues[ 'tabWidth' ] === 0 ) { |
|
| 76 | 76 | // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
| 77 | 77 | // It shouldn't really matter because indent checks elsewhere in the |
| 78 | 78 | // standard should fix things up. |
| 79 | 79 | $this->_tabWidth = 4; |
| 80 | 80 | } else { |
| 81 | - $this->_tabWidth = $cliValues['tabWidth']; |
|
| 81 | + $this->_tabWidth = $cliValues[ 'tabWidth' ]; |
|
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | |
@@ -89,51 +89,51 @@ discard block |
||
| 89 | 89 | ); |
| 90 | 90 | |
| 91 | 91 | $tokens = $phpcsFile->getTokens(); |
| 92 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 93 | - if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) { |
|
| 92 | + for ( $i = ( $stackPtr + 1 ); $i < $phpcsFile->numTokens; $i++ ) { |
|
| 93 | + if ( $tokens[ $i ][ 'column' ] !== 1 || isset( $checkTokens[ $tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 94 | 94 | continue; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // If tabs are being converted to spaces, the original content |
| 98 | 98 | // should be used instead of the converted content. |
| 99 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
| 100 | - $content = $tokens[$i]['orig_content']; |
|
| 99 | + if ( isset( $tokens[ $i ][ 'orig_content' ] ) === true ) { |
|
| 100 | + $content = $tokens[ $i ][ 'orig_content' ]; |
|
| 101 | 101 | } else { |
| 102 | - $content = $tokens[$i]['content']; |
|
| 102 | + $content = $tokens[ $i ][ 'content' ]; |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if ($content[0] === ' ') { |
|
| 106 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE && $content === ' ') { |
|
| 105 | + if ( $content[ 0 ] === ' ' ) { |
|
| 106 | + if ( $tokens[ $i ][ 'code' ] === T_DOC_COMMENT_WHITESPACE && $content === ' ' ) { |
|
| 107 | 107 | // Ignore file/class-level DocBlock. |
| 108 | 108 | continue; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Space are considered ok if they are proceeded by tabs and not followed |
| 112 | 112 | // by tabs, as is the case with standard docblock comments. |
| 113 | - $phpcsFile->recordMetric($i, 'Line indent', 'spaces'); |
|
| 113 | + $phpcsFile->recordMetric( $i, 'Line indent', 'spaces' ); |
|
| 114 | 114 | $error = 'Tabs must be used to indent lines; spaces are not allowed'; |
| 115 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed'); |
|
| 116 | - if ($fix === true) { |
|
| 117 | - $trimmed = ltrim($content, ' '); |
|
| 118 | - $numSpaces = (strlen($content) - strlen($trimmed)); |
|
| 119 | - if ($numSpaces < $this->_tabWidth) { |
|
| 115 | + $fix = $phpcsFile->addFixableError( $error, $i, 'SpacesUsed' ); |
|
| 116 | + if ( $fix === true ) { |
|
| 117 | + $trimmed = ltrim( $content, ' ' ); |
|
| 118 | + $numSpaces = ( strlen( $content ) - strlen( $trimmed ) ); |
|
| 119 | + if ( $numSpaces < $this->_tabWidth ) { |
|
| 120 | 120 | $numTabs = 1; |
| 121 | 121 | $padding = "\t"; |
| 122 | 122 | } else { |
| 123 | - $numTabs = floor($numSpaces / $this->_tabWidth); |
|
| 124 | - $remaining = ($numSpaces - ($numTabs * $this->_tabWidth)); |
|
| 125 | - $padding = str_repeat("\t", $numTabs).$padding = str_repeat(' ', $remaining); |
|
| 123 | + $numTabs = floor( $numSpaces / $this->_tabWidth ); |
|
| 124 | + $remaining = ( $numSpaces - ( $numTabs * $this->_tabWidth ) ); |
|
| 125 | + $padding = str_repeat( "\t", $numTabs ) . $padding = str_repeat( ' ', $remaining ); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - $phpcsFile->fixer->replaceToken($i, $padding.$trimmed); |
|
| 128 | + $phpcsFile->fixer->replaceToken( $i, $padding . $trimmed ); |
|
| 129 | 129 | } |
| 130 | - } else if ($content[0] === "\t") { |
|
| 131 | - $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); |
|
| 130 | + } else if ( $content[ 0 ] === "\t" ) { |
|
| 131 | + $phpcsFile->recordMetric( $i, 'Line indent', 'tabs' ); |
|
| 132 | 132 | }//end if |
| 133 | 133 | }//end for |
| 134 | 134 | |
| 135 | 135 | // Ignore the rest of the file. |
| 136 | - return ($phpcsFile->numTokens + 1); |
|
| 136 | + return ( $phpcsFile->numTokens + 1 ); |
|
| 137 | 137 | |
| 138 | 138 | }//end process() |
| 139 | 139 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class Generic_Sniffs_WhiteSpace_DisallowSpaceIndentSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class Generic_Sniffs_WhiteSpace_DisallowSpaceIndentSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * A list of tokenizers this sniff supports. |
@@ -52,8 +51,7 @@ discard block |
||
| 52 | 51 | * |
| 53 | 52 | * @return array |
| 54 | 53 | */ |
| 55 | - public function register() |
|
| 56 | - { |
|
| 54 | + public function register() { |
|
| 57 | 55 | return array(T_OPEN_TAG); |
| 58 | 56 | |
| 59 | 57 | }//end register() |
@@ -68,8 +66,7 @@ discard block |
||
| 68 | 66 | * |
| 69 | 67 | * @return void |
| 70 | 68 | */ |
| 71 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 72 | - { |
|
| 69 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 73 | 70 | if ($this->_tabWidth === null) { |
| 74 | 71 | $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
| 75 | 72 | if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | /** |
| 118 | 118 | * Returns an array of tokens this test wants to listen for. |
| 119 | 119 | * |
| 120 | - * @return array |
|
| 120 | + * @return integer[] |
|
| 121 | 121 | */ |
| 122 | 122 | public function register() |
| 123 | 123 | { |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * @param int $stackPtr The position of the current token |
| 138 | 138 | * in the stack passed in $tokens. |
| 139 | 139 | * |
| 140 | - * @return void |
|
| 140 | + * @return integer |
|
| 141 | 141 | */ |
| 142 | 142 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 143 | 143 | { |
@@ -32,1227 +32,1227 @@ |
||
| 32 | 32 | class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * A list of tokenizers this sniff supports. |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - public $supportedTokenizers = array( |
|
| 41 | - 'PHP', |
|
| 42 | - 'JS', |
|
| 43 | - ); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The number of spaces code should be indented. |
|
| 47 | - * |
|
| 48 | - * @var int |
|
| 49 | - */ |
|
| 50 | - public $indent = 4; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Does the indent need to be exactly right? |
|
| 54 | - * |
|
| 55 | - * If TRUE, indent needs to be exactly $indent spaces. If FALSE, |
|
| 56 | - * indent needs to be at least $indent spaces (but can be more). |
|
| 57 | - * |
|
| 58 | - * @var bool |
|
| 59 | - */ |
|
| 60 | - public $exact = false; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Should tabs be used for indenting? |
|
| 64 | - * |
|
| 65 | - * If TRUE, fixes will be made using tabs instead of spaces. |
|
| 66 | - * The size of each tab is important, so it should be specified |
|
| 67 | - * using the --tab-width CLI argument. |
|
| 68 | - * |
|
| 69 | - * @var bool |
|
| 70 | - */ |
|
| 71 | - public $tabIndent = false; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The --tab-width CLI value that is being used. |
|
| 75 | - * |
|
| 76 | - * @var int |
|
| 77 | - */ |
|
| 78 | - private $_tabWidth = null; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * List of tokens not needing to be checked for indentation. |
|
| 82 | - * |
|
| 83 | - * Useful to allow Sniffs based on this to easily ignore/skip some |
|
| 84 | - * tokens from verification. For example, inline HTML sections |
|
| 85 | - * or PHP open/close tags can escape from here and have their own |
|
| 86 | - * rules elsewhere. |
|
| 87 | - * |
|
| 88 | - * @var int[] |
|
| 89 | - */ |
|
| 90 | - public $ignoreIndentationTokens = array(); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * List of tokens not needing to be checked for indentation. |
|
| 94 | - * |
|
| 95 | - * This is a cached copy of the public version of this var, which |
|
| 96 | - * can be set in a ruleset file, and some core ignored tokens. |
|
| 97 | - * |
|
| 98 | - * @var int[] |
|
| 99 | - */ |
|
| 100 | - private $_ignoreIndentationTokens = array(); |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Any scope openers that should not cause an indent. |
|
| 104 | - * |
|
| 105 | - * @var int[] |
|
| 106 | - */ |
|
| 107 | - protected $nonIndentingScopes = array(); |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Show debug output for this sniff. |
|
| 111 | - * |
|
| 112 | - * @var bool |
|
| 113 | - */ |
|
| 114 | - private $_debug = false; |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Returns an array of tokens this test wants to listen for. |
|
| 119 | - * |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function register() |
|
| 123 | - { |
|
| 124 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 125 | - $this->_debug = false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return array(T_OPEN_TAG); |
|
| 129 | - |
|
| 130 | - }//end register() |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Processes this test, when one of its tokens is encountered. |
|
| 135 | - * |
|
| 136 | - * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
|
| 137 | - * @param int $stackPtr The position of the current token |
|
| 138 | - * in the stack passed in $tokens. |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 143 | - { |
|
| 144 | - $debug = PHP_CodeSniffer::getConfigData('scope_indent_debug'); |
|
| 145 | - if ($debug !== null) { |
|
| 146 | - $this->_debug = (bool) $debug; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if ($this->_tabWidth === null) { |
|
| 150 | - $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
| 151 | - if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 152 | - // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 153 | - // It shouldn't really matter because indent checks elsewhere in the |
|
| 154 | - // standard should fix things up. |
|
| 155 | - $this->_tabWidth = 4; |
|
| 156 | - } else { |
|
| 157 | - $this->_tabWidth = $cliValues['tabWidth']; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $currentIndent = 0; |
|
| 162 | - $lastOpenTag = $stackPtr; |
|
| 163 | - $lastCloseTag = null; |
|
| 164 | - $openScopes = array(); |
|
| 165 | - $adjustments = array(); |
|
| 166 | - $setIndents = array(); |
|
| 167 | - |
|
| 168 | - $tokens = $phpcsFile->getTokens(); |
|
| 169 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 170 | - $trimmed = ltrim($tokens[$first]['content']); |
|
| 171 | - if ($trimmed === '') { |
|
| 172 | - $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 173 | - } else { |
|
| 174 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if ($this->_debug === true) { |
|
| 178 | - $line = $tokens[$stackPtr]['line']; |
|
| 179 | - echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if (empty($this->_ignoreIndentationTokens) === true) { |
|
| 183 | - $this->_ignoreIndentationTokens = array(T_INLINE_HTML => true); |
|
| 184 | - foreach ($this->ignoreIndentationTokens as $token) { |
|
| 185 | - if (is_int($token) === false) { |
|
| 186 | - if (defined($token) === false) { |
|
| 187 | - continue; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $token = constant($token); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $this->_ignoreIndentationTokens[$token] = true; |
|
| 194 | - } |
|
| 195 | - }//end if |
|
| 196 | - |
|
| 197 | - $this->exact = (bool) $this->exact; |
|
| 198 | - $this->tabIndent = (bool) $this->tabIndent; |
|
| 199 | - |
|
| 200 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 201 | - if ($i === false) { |
|
| 202 | - // Something has gone very wrong; maybe a parse error. |
|
| 203 | - break; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - $checkToken = null; |
|
| 207 | - $checkIndent = null; |
|
| 208 | - |
|
| 209 | - $exact = (bool) $this->exact; |
|
| 210 | - if ($exact === true && isset($tokens[$i]['nested_parenthesis']) === true) { |
|
| 211 | - // Don't check indents exactly between parenthesis as they |
|
| 212 | - // tend to have custom rules, such as with multi-line function calls |
|
| 213 | - // and control structure conditions. |
|
| 214 | - $exact = false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Detect line changes and figure out where the indent is. |
|
| 218 | - if ($tokens[$i]['column'] === 1) { |
|
| 219 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 220 | - if ($trimmed === '') { |
|
| 221 | - if (isset($tokens[($i + 1)]) === true |
|
| 222 | - && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 223 | - ) { |
|
| 224 | - $checkToken = ($i + 1); |
|
| 225 | - $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 226 | - } |
|
| 227 | - } else { |
|
| 228 | - $checkToken = $i; |
|
| 229 | - $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - // Closing parenthesis should just be indented to at least |
|
| 234 | - // the same level as where they were opened (but can be more). |
|
| 235 | - if (($checkToken !== null |
|
| 236 | - && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 237 | - && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 238 | - || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 239 | - && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 240 | - ) { |
|
| 241 | - if ($checkToken !== null) { |
|
| 242 | - $parenCloser = $checkToken; |
|
| 243 | - } else { |
|
| 244 | - $parenCloser = $i; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if ($this->_debug === true) { |
|
| 248 | - $line = $tokens[$i]['line']; |
|
| 249 | - echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 253 | - if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 254 | - $parens = 0; |
|
| 255 | - if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 256 | - && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 257 | - ) { |
|
| 258 | - end($tokens[$parenCloser]['nested_parenthesis']); |
|
| 259 | - $parens = key($tokens[$parenCloser]['nested_parenthesis']); |
|
| 260 | - if ($this->_debug === true) { |
|
| 261 | - $line = $tokens[$parens]['line']; |
|
| 262 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $condition = 0; |
|
| 267 | - if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 268 | - && empty($tokens[$parenCloser]['conditions']) === false |
|
| 269 | - ) { |
|
| 270 | - end($tokens[$parenCloser]['conditions']); |
|
| 271 | - $condition = key($tokens[$parenCloser]['conditions']); |
|
| 272 | - if ($this->_debug === true) { |
|
| 273 | - $line = $tokens[$condition]['line']; |
|
| 274 | - $type = $tokens[$condition]['type']; |
|
| 275 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - if ($parens > $condition) { |
|
| 280 | - if ($this->_debug === true) { |
|
| 281 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $parenOpener = $parens; |
|
| 285 | - $condition = 0; |
|
| 286 | - } else if ($condition > 0) { |
|
| 287 | - if ($this->_debug === true) { |
|
| 288 | - echo "\t* using condition *".PHP_EOL; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $parenOpener = $condition; |
|
| 292 | - $parens = 0; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - $exact = false; |
|
| 296 | - |
|
| 297 | - $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 298 | - $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 299 | - |
|
| 300 | - if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 301 | - if ($this->_debug === true) { |
|
| 302 | - echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 306 | - if (isset($adjustments[$condition]) === true) { |
|
| 307 | - $checkIndent += $adjustments[$condition]; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $currentIndent = $checkIndent; |
|
| 311 | - |
|
| 312 | - if ($this->_debug === true) { |
|
| 313 | - $type = $tokens[$lastOpenTag]['type']; |
|
| 314 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 315 | - } |
|
| 316 | - } else if ($condition > 0 |
|
| 317 | - && isset($tokens[$condition]['scope_opener']) === true |
|
| 318 | - && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 319 | - ) { |
|
| 320 | - $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 321 | - if (isset($adjustments[$condition]) === true) { |
|
| 322 | - $checkIndent += $adjustments[$condition]; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - $currentIndent = $checkIndent; |
|
| 326 | - |
|
| 327 | - if ($this->_debug === true) { |
|
| 328 | - $type = $tokens[$condition]['type']; |
|
| 329 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 330 | - } |
|
| 331 | - } else { |
|
| 332 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 333 | - |
|
| 334 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 335 | - if (isset($adjustments[$first]) === true) { |
|
| 336 | - $checkIndent += $adjustments[$first]; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - if ($this->_debug === true) { |
|
| 340 | - $line = $tokens[$first]['line']; |
|
| 341 | - $type = $tokens[$first]['type']; |
|
| 342 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ($first === $tokens[$parenCloser]['parenthesis_opener']) { |
|
| 346 | - // This is unlikely to be the start of the statement, so look |
|
| 347 | - // back further to find it. |
|
| 348 | - $first--; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 352 | - if ($prev !== $first) { |
|
| 353 | - // This is not the start of the statement. |
|
| 354 | - if ($this->_debug === true) { |
|
| 355 | - $line = $tokens[$prev]['line']; |
|
| 356 | - $type = $tokens[$prev]['type']; |
|
| 357 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 361 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 362 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 363 | - if ($this->_debug === true) { |
|
| 364 | - $line = $tokens[$first]['line']; |
|
| 365 | - $type = $tokens[$first]['type']; |
|
| 366 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 371 | - && $tokens[$first]['scope_closer'] === $first |
|
| 372 | - ) { |
|
| 373 | - if ($this->_debug === true) { |
|
| 374 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 378 | - $scopeCloser = $first; |
|
| 379 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 380 | - |
|
| 381 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 382 | - if (isset($adjustments[$first]) === true) { |
|
| 383 | - $currentIndent += $adjustments[$first]; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // Make sure it is divisible by our expected indent. |
|
| 387 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 388 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - $setIndents[$first] = $currentIndent; |
|
| 392 | - |
|
| 393 | - if ($this->_debug === true) { |
|
| 394 | - $type = $tokens[$first]['type']; |
|
| 395 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 396 | - } |
|
| 397 | - }//end if |
|
| 398 | - } else { |
|
| 399 | - // Don't force current indent to divisible because there could be custom |
|
| 400 | - // rules in place between parenthesis, such as with arrays. |
|
| 401 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 402 | - if (isset($adjustments[$first]) === true) { |
|
| 403 | - $currentIndent += $adjustments[$first]; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - $setIndents[$first] = $currentIndent; |
|
| 407 | - |
|
| 408 | - if ($this->_debug === true) { |
|
| 409 | - $type = $tokens[$first]['type']; |
|
| 410 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 411 | - } |
|
| 412 | - }//end if |
|
| 413 | - }//end if |
|
| 414 | - } else if ($this->_debug === true) { |
|
| 415 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 416 | - }//end if |
|
| 417 | - }//end if |
|
| 418 | - |
|
| 419 | - // Closing short array bracket should just be indented to at least |
|
| 420 | - // the same level as where it was opened (but can be more). |
|
| 421 | - if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 422 | - || ($checkToken !== null |
|
| 423 | - && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 424 | - ) { |
|
| 425 | - if ($checkToken !== null) { |
|
| 426 | - $arrayCloser = $checkToken; |
|
| 427 | - } else { |
|
| 428 | - $arrayCloser = $i; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - if ($this->_debug === true) { |
|
| 432 | - $line = $tokens[$arrayCloser]['line']; |
|
| 433 | - echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 437 | - if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 438 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 439 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 440 | - if (isset($adjustments[$first]) === true) { |
|
| 441 | - $checkIndent += $adjustments[$first]; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $exact = false; |
|
| 445 | - |
|
| 446 | - if ($this->_debug === true) { |
|
| 447 | - $line = $tokens[$first]['line']; |
|
| 448 | - $type = $tokens[$first]['type']; |
|
| 449 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 453 | - // This is unlikely to be the start of the statement, so look |
|
| 454 | - // back further to find it. |
|
| 455 | - $first--; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 459 | - if ($prev !== $first) { |
|
| 460 | - // This is not the start of the statement. |
|
| 461 | - if ($this->_debug === true) { |
|
| 462 | - $line = $tokens[$prev]['line']; |
|
| 463 | - $type = $tokens[$prev]['type']; |
|
| 464 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 468 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 469 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 470 | - if ($this->_debug === true) { |
|
| 471 | - $line = $tokens[$first]['line']; |
|
| 472 | - $type = $tokens[$first]['type']; |
|
| 473 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 478 | - && $tokens[$first]['scope_closer'] === $first |
|
| 479 | - ) { |
|
| 480 | - // The first token is a scope closer and would have already |
|
| 481 | - // been processed and set the indent level correctly, so |
|
| 482 | - // don't adjust it again. |
|
| 483 | - if ($this->_debug === true) { |
|
| 484 | - echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - if (isset($setIndents[$first]) === true) { |
|
| 488 | - $currentIndent = $setIndents[$first]; |
|
| 489 | - if ($this->_debug === true) { |
|
| 490 | - echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - } else { |
|
| 494 | - // Don't force current indent to be divisible because there could be custom |
|
| 495 | - // rules in place for arrays. |
|
| 496 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 497 | - if (isset($adjustments[$first]) === true) { |
|
| 498 | - $currentIndent += $adjustments[$first]; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - $setIndents[$first] = $currentIndent; |
|
| 502 | - |
|
| 503 | - if ($this->_debug === true) { |
|
| 504 | - $type = $tokens[$first]['type']; |
|
| 505 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 506 | - } |
|
| 507 | - }//end if |
|
| 508 | - } else if ($this->_debug === true) { |
|
| 509 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 510 | - }//end if |
|
| 511 | - }//end if |
|
| 512 | - |
|
| 513 | - // Adjust lines within scopes while auto-fixing. |
|
| 514 | - if ($checkToken !== null |
|
| 515 | - && $exact === false |
|
| 516 | - && (empty($tokens[$checkToken]['conditions']) === false |
|
| 517 | - || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 518 | - && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 519 | - ) { |
|
| 520 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 521 | - end($tokens[$checkToken]['conditions']); |
|
| 522 | - $condition = key($tokens[$checkToken]['conditions']); |
|
| 523 | - } else { |
|
| 524 | - $condition = $tokens[$checkToken]['scope_condition']; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 528 | - |
|
| 529 | - if (isset($adjustments[$first]) === true |
|
| 530 | - && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 531 | - || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 532 | - ) { |
|
| 533 | - $padding = ($tokenIndent + $adjustments[$first]); |
|
| 534 | - if ($padding > 0) { |
|
| 535 | - if ($this->tabIndent === true) { |
|
| 536 | - $numTabs = floor($padding / $this->_tabWidth); |
|
| 537 | - $numSpaces = ($padding - ($numTabs * $this->_tabWidth)); |
|
| 538 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 539 | - } else { |
|
| 540 | - $padding = str_repeat(' ', $padding); |
|
| 541 | - } |
|
| 542 | - } else { |
|
| 543 | - $padding = ''; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - if ($checkToken === $i) { |
|
| 547 | - $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 548 | - } else { |
|
| 549 | - // Easier to just replace the entire indent. |
|
| 550 | - $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - if ($this->_debug === true) { |
|
| 554 | - $length = strlen($padding); |
|
| 555 | - $line = $tokens[$checkToken]['line']; |
|
| 556 | - $type = $tokens[$checkToken]['type']; |
|
| 557 | - echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - $adjustments[$checkToken] = $adjustments[$first]; |
|
| 561 | - |
|
| 562 | - if ($this->_debug === true) { |
|
| 563 | - $line = $tokens[$checkToken]['line']; |
|
| 564 | - $type = $tokens[$checkToken]['type']; |
|
| 565 | - echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 566 | - } |
|
| 567 | - }//end if |
|
| 568 | - }//end if |
|
| 569 | - |
|
| 570 | - // Scope closers reset the required indent to the same level as the opening condition. |
|
| 571 | - if (($checkToken !== null |
|
| 572 | - && isset($openScopes[$checkToken]) === true |
|
| 573 | - || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 574 | - && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 575 | - && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 576 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 577 | - || ($checkToken === null |
|
| 578 | - && isset($openScopes[$i]) === true |
|
| 579 | - || (isset($tokens[$i]['scope_condition']) === true |
|
| 580 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 581 | - && $tokens[$i]['scope_closer'] === $i |
|
| 582 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 583 | - ) { |
|
| 584 | - if ($this->_debug === true) { |
|
| 585 | - if ($checkToken === null) { |
|
| 586 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 587 | - $line = $tokens[$i]['line']; |
|
| 588 | - } else { |
|
| 589 | - $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 590 | - $line = $tokens[$checkToken]['line']; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - $scopeCloser = $checkToken; |
|
| 597 | - if ($scopeCloser === null) { |
|
| 598 | - $scopeCloser = $i; |
|
| 599 | - } else { |
|
| 600 | - array_pop($openScopes); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 604 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 605 | - |
|
| 606 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 607 | - if (isset($adjustments[$first]) === true) { |
|
| 608 | - $currentIndent += $adjustments[$first]; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - // Make sure it is divisible by our expected indent. |
|
| 612 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 613 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - $setIndents[$scopeCloser] = $currentIndent; |
|
| 617 | - |
|
| 618 | - if ($this->_debug === true) { |
|
| 619 | - $type = $tokens[$scopeCloser]['type']; |
|
| 620 | - echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // We only check the indent of scope closers if they are |
|
| 624 | - // curly braces because other constructs tend to have different rules. |
|
| 625 | - if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 626 | - $exact = true; |
|
| 627 | - } else { |
|
| 628 | - $checkToken = null; |
|
| 629 | - } |
|
| 630 | - }//end if |
|
| 631 | - }//end if |
|
| 632 | - |
|
| 633 | - // Handle scope for JS object notation. |
|
| 634 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 635 | - && (($checkToken !== null |
|
| 636 | - && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 637 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 638 | - || ($checkToken === null |
|
| 639 | - && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 640 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 641 | - ) { |
|
| 642 | - if ($this->_debug === true) { |
|
| 643 | - $line = $tokens[$i]['line']; |
|
| 644 | - echo "Close JS object on line $line".PHP_EOL; |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - $scopeCloser = $checkToken; |
|
| 648 | - if ($scopeCloser === null) { |
|
| 649 | - $scopeCloser = $i; |
|
| 650 | - } else { |
|
| 651 | - array_pop($openScopes); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $parens = 0; |
|
| 655 | - if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 656 | - && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 657 | - ) { |
|
| 658 | - end($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 659 | - $parens = key($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 660 | - if ($this->_debug === true) { |
|
| 661 | - $line = $tokens[$parens]['line']; |
|
| 662 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - $condition = 0; |
|
| 667 | - if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 668 | - && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 669 | - ) { |
|
| 670 | - end($tokens[$scopeCloser]['conditions']); |
|
| 671 | - $condition = key($tokens[$scopeCloser]['conditions']); |
|
| 672 | - if ($this->_debug === true) { |
|
| 673 | - $line = $tokens[$condition]['line']; |
|
| 674 | - $type = $tokens[$condition]['type']; |
|
| 675 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - if ($parens > $condition) { |
|
| 680 | - if ($this->_debug === true) { |
|
| 681 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 685 | - $condition = 0; |
|
| 686 | - } else if ($condition > 0) { |
|
| 687 | - if ($this->_debug === true) { |
|
| 688 | - echo "\t* using condition *".PHP_EOL; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 692 | - $parens = 0; |
|
| 693 | - } else { |
|
| 694 | - if ($this->_debug === true) { |
|
| 695 | - $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 696 | - echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 700 | - }//end if |
|
| 701 | - |
|
| 702 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 703 | - if (isset($adjustments[$first]) === true) { |
|
| 704 | - $currentIndent += $adjustments[$first]; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - if ($parens > 0 || $condition > 0) { |
|
| 708 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 709 | - if (isset($adjustments[$first]) === true) { |
|
| 710 | - $checkIndent += $adjustments[$first]; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - if ($condition > 0) { |
|
| 714 | - $checkIndent += $this->indent; |
|
| 715 | - $currentIndent += $this->indent; |
|
| 716 | - $exact = true; |
|
| 717 | - } |
|
| 718 | - } else { |
|
| 719 | - $checkIndent = $currentIndent; |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - // Make sure it is divisible by our expected indent. |
|
| 723 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 724 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 725 | - $setIndents[$first] = $currentIndent; |
|
| 726 | - |
|
| 727 | - if ($this->_debug === true) { |
|
| 728 | - $type = $tokens[$first]['type']; |
|
| 729 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 730 | - } |
|
| 731 | - }//end if |
|
| 732 | - |
|
| 733 | - if ($checkToken !== null |
|
| 734 | - && isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 735 | - && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false |
|
| 736 | - && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 737 | - ) { |
|
| 738 | - $exact = true; |
|
| 739 | - |
|
| 740 | - $lastOpener = null; |
|
| 741 | - if (empty($openScopes) === false) { |
|
| 742 | - end($openScopes); |
|
| 743 | - $lastOpener = current($openScopes); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - // A scope opener that shares a closer with another token (like multiple |
|
| 747 | - // CASEs using the same BREAK) needs to reduce the indent level so its |
|
| 748 | - // indent is checked correctly. It will then increase the indent again |
|
| 749 | - // (as all openers do) after being checked. |
|
| 750 | - if ($lastOpener !== null |
|
| 751 | - && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 752 | - && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 753 | - && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 754 | - ) { |
|
| 755 | - $currentIndent -= $this->indent; |
|
| 756 | - $setIndents[$lastOpener] = $currentIndent; |
|
| 757 | - if ($this->_debug === true) { |
|
| 758 | - $line = $tokens[$i]['line']; |
|
| 759 | - $type = $tokens[$lastOpener]['type']; |
|
| 760 | - echo "Shared closer found on line $line".PHP_EOL; |
|
| 761 | - echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 766 | - && $tokenIndent > $currentIndent |
|
| 767 | - ) { |
|
| 768 | - // The opener is indented more than needed, which is fine. |
|
| 769 | - // But just check that it is divisible by our expected indent. |
|
| 770 | - $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 771 | - $exact = false; |
|
| 772 | - |
|
| 773 | - if ($this->_debug === true) { |
|
| 774 | - $line = $tokens[$i]['line']; |
|
| 775 | - echo "Closure found on line $line".PHP_EOL; |
|
| 776 | - echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 777 | - } |
|
| 778 | - } |
|
| 779 | - }//end if |
|
| 780 | - |
|
| 781 | - // Method prefix indentation has to be exact or else if will break |
|
| 782 | - // the rest of the function declaration, and potentially future ones. |
|
| 783 | - if ($checkToken !== null |
|
| 784 | - && isset(PHP_CodeSniffer_Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 785 | - && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 786 | - ) { |
|
| 787 | - $exact = true; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - // JS property indentation has to be exact or else if will break |
|
| 791 | - // things like function and object indentation. |
|
| 792 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 793 | - $exact = true; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - // PHP tags needs to be indented to exact column positions |
|
| 797 | - // so they don't cause problems with indent checks for the code |
|
| 798 | - // within them, but they don't need to line up with the current indent. |
|
| 799 | - if ($checkToken !== null |
|
| 800 | - && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 801 | - || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 802 | - || $tokens[$checkToken]['code'] === T_CLOSE_TAG) |
|
| 803 | - ) { |
|
| 804 | - $exact = true; |
|
| 805 | - $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 806 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - // Check the line indent. |
|
| 810 | - if ($checkIndent === null) { |
|
| 811 | - $checkIndent = $currentIndent; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - $adjusted = false; |
|
| 815 | - if ($checkToken !== null |
|
| 816 | - && isset($this->_ignoreIndentationTokens[$tokens[$checkToken]['code']]) === false |
|
| 817 | - && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 818 | - || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 819 | - ) { |
|
| 820 | - $type = 'IncorrectExact'; |
|
| 821 | - $error = 'Line indented incorrectly; expected '; |
|
| 822 | - if ($exact === false) { |
|
| 823 | - $error .= 'at least '; |
|
| 824 | - $type = 'Incorrect'; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - if ($this->tabIndent === true) { |
|
| 828 | - $error .= '%s tabs, found %s'; |
|
| 829 | - $data = array( |
|
| 830 | - floor($checkIndent / $this->_tabWidth), |
|
| 831 | - floor($tokenIndent / $this->_tabWidth), |
|
| 832 | - ); |
|
| 833 | - } else { |
|
| 834 | - $error .= '%s spaces, found %s'; |
|
| 835 | - $data = array( |
|
| 836 | - $checkIndent, |
|
| 837 | - $tokenIndent, |
|
| 838 | - ); |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - if ($this->_debug === true) { |
|
| 842 | - $line = $tokens[$checkToken]['line']; |
|
| 843 | - $message = vsprintf($error, $data); |
|
| 844 | - echo "[Line $line] $message".PHP_EOL; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 848 | - if ($fix === true || $this->_debug === true) { |
|
| 849 | - $padding = ''; |
|
| 850 | - if ($this->tabIndent === true) { |
|
| 851 | - $numTabs = floor($checkIndent / $this->_tabWidth); |
|
| 852 | - if ($numTabs > 0) { |
|
| 853 | - $numSpaces = ($checkIndent - ($numTabs * $this->_tabWidth)); |
|
| 854 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 855 | - } |
|
| 856 | - } else if ($checkIndent > 0) { |
|
| 857 | - $padding = str_repeat(' ', $checkIndent); |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - if ($checkToken === $i) { |
|
| 861 | - $accepted = $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 862 | - } else { |
|
| 863 | - // Easier to just replace the entire indent. |
|
| 864 | - $accepted = $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - if ($accepted === true) { |
|
| 868 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 869 | - if ($this->_debug === true) { |
|
| 870 | - $line = $tokens[$checkToken]['line']; |
|
| 871 | - $type = $tokens[$checkToken]['type']; |
|
| 872 | - echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 873 | - } |
|
| 874 | - } |
|
| 875 | - } else { |
|
| 876 | - // Assume the change would be applied and continue |
|
| 877 | - // checking indents under this assumption. This gives more |
|
| 878 | - // technically accurate error messages. |
|
| 879 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 880 | - }//end if |
|
| 881 | - }//end if |
|
| 882 | - |
|
| 883 | - if ($checkToken !== null) { |
|
| 884 | - $i = $checkToken; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - // Completely skip here/now docs as the indent is a part of the |
|
| 888 | - // content itself. |
|
| 889 | - if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 890 | - || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 891 | - ) { |
|
| 892 | - $i = $phpcsFile->findNext(array(T_END_HEREDOC, T_END_NOWDOC), ($i + 1)); |
|
| 893 | - continue; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - // Completely skip multi-line strings as the indent is a part of the |
|
| 897 | - // content itself. |
|
| 898 | - if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 899 | - || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 900 | - ) { |
|
| 901 | - $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 902 | - $i--; |
|
| 903 | - continue; |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - // Completely skip doc comments as they tend to have complex |
|
| 907 | - // indentation rules. |
|
| 908 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 909 | - $i = $tokens[$i]['comment_closer']; |
|
| 910 | - continue; |
|
| 911 | - } |
|
| 912 | - |
|
| 913 | - // Open tags reset the indent level. |
|
| 914 | - if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 915 | - || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 916 | - ) { |
|
| 917 | - if ($this->_debug === true) { |
|
| 918 | - $line = $tokens[$i]['line']; |
|
| 919 | - echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - if ($checkToken === null) { |
|
| 923 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 924 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 925 | - } else { |
|
| 926 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - $lastOpenTag = $i; |
|
| 930 | - |
|
| 931 | - if (isset($adjustments[$i]) === true) { |
|
| 932 | - $currentIndent += $adjustments[$i]; |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - // Make sure it is divisible by our expected indent. |
|
| 936 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 937 | - $setIndents[$i] = $currentIndent; |
|
| 938 | - |
|
| 939 | - if ($this->_debug === true) { |
|
| 940 | - $type = $tokens[$i]['type']; |
|
| 941 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - continue; |
|
| 945 | - }//end if |
|
| 946 | - |
|
| 947 | - // Close tags reset the indent level, unless they are closing a tag |
|
| 948 | - // opened on the same line. |
|
| 949 | - if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 950 | - if ($this->_debug === true) { |
|
| 951 | - $line = $tokens[$i]['line']; |
|
| 952 | - echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 956 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 957 | - $lastCloseTag = $i; |
|
| 958 | - } else { |
|
| 959 | - if ($lastCloseTag === null) { |
|
| 960 | - $currentIndent = 0; |
|
| 961 | - } else { |
|
| 962 | - $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 963 | - } |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - if (isset($adjustments[$i]) === true) { |
|
| 967 | - $currentIndent += $adjustments[$i]; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - // Make sure it is divisible by our expected indent. |
|
| 971 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 972 | - $setIndents[$i] = $currentIndent; |
|
| 973 | - |
|
| 974 | - if ($this->_debug === true) { |
|
| 975 | - $type = $tokens[$i]['type']; |
|
| 976 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - continue; |
|
| 980 | - }//end if |
|
| 981 | - |
|
| 982 | - // Anon classes and functions set the indent based on their own indent level. |
|
| 983 | - if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 984 | - $closer = $tokens[$i]['scope_closer']; |
|
| 985 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 986 | - if ($this->_debug === true) { |
|
| 987 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 988 | - $line = $tokens[$i]['line']; |
|
| 989 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - $i = $closer; |
|
| 993 | - continue; |
|
| 994 | - } |
|
| 995 | - |
|
| 996 | - if ($this->_debug === true) { |
|
| 997 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 998 | - $line = $tokens[$i]['line']; |
|
| 999 | - echo "Open $type on line $line".PHP_EOL; |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1003 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1004 | - |
|
| 1005 | - if (isset($adjustments[$first]) === true) { |
|
| 1006 | - $currentIndent += $adjustments[$first]; |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - // Make sure it is divisible by our expected indent. |
|
| 1010 | - $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1011 | - $i = $tokens[$i]['scope_opener']; |
|
| 1012 | - $setIndents[$i] = $currentIndent; |
|
| 1013 | - |
|
| 1014 | - if ($this->_debug === true) { |
|
| 1015 | - $type = $tokens[$i]['type']; |
|
| 1016 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - continue; |
|
| 1020 | - }//end if |
|
| 1021 | - |
|
| 1022 | - // Scope openers increase the indent level. |
|
| 1023 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1024 | - && isset($tokens[$i]['scope_opener']) === true |
|
| 1025 | - && $tokens[$i]['scope_opener'] === $i |
|
| 1026 | - ) { |
|
| 1027 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1028 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1029 | - if ($this->_debug === true) { |
|
| 1030 | - $line = $tokens[$i]['line']; |
|
| 1031 | - $type = $tokens[$i]['type']; |
|
| 1032 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - $i = $closer; |
|
| 1036 | - continue; |
|
| 1037 | - } |
|
| 1038 | - |
|
| 1039 | - $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1040 | - if (isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$condition]) === true |
|
| 1041 | - && in_array($condition, $this->nonIndentingScopes) === false |
|
| 1042 | - ) { |
|
| 1043 | - if ($this->_debug === true) { |
|
| 1044 | - $line = $tokens[$i]['line']; |
|
| 1045 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1046 | - echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - $currentIndent += $this->indent; |
|
| 1050 | - $setIndents[$i] = $currentIndent; |
|
| 1051 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1052 | - |
|
| 1053 | - if ($this->_debug === true) { |
|
| 1054 | - $type = $tokens[$i]['type']; |
|
| 1055 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - continue; |
|
| 1059 | - } |
|
| 1060 | - }//end if |
|
| 1061 | - |
|
| 1062 | - // JS objects set the indent level. |
|
| 1063 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 1064 | - && $tokens[$i]['code'] === T_OBJECT |
|
| 1065 | - ) { |
|
| 1066 | - $closer = $tokens[$i]['bracket_closer']; |
|
| 1067 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1068 | - if ($this->_debug === true) { |
|
| 1069 | - $line = $tokens[$i]['line']; |
|
| 1070 | - echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - $i = $closer; |
|
| 1074 | - continue; |
|
| 1075 | - } |
|
| 1076 | - |
|
| 1077 | - if ($this->_debug === true) { |
|
| 1078 | - $line = $tokens[$i]['line']; |
|
| 1079 | - echo "Open JS object on line $line".PHP_EOL; |
|
| 1080 | - } |
|
| 1081 | - |
|
| 1082 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1083 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1084 | - if (isset($adjustments[$first]) === true) { |
|
| 1085 | - $currentIndent += $adjustments[$first]; |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - // Make sure it is divisible by our expected indent. |
|
| 1089 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1090 | - $setIndents[$first] = $currentIndent; |
|
| 1091 | - |
|
| 1092 | - if ($this->_debug === true) { |
|
| 1093 | - $type = $tokens[$first]['type']; |
|
| 1094 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - continue; |
|
| 1098 | - }//end if |
|
| 1099 | - |
|
| 1100 | - // Closing an anon class or function. |
|
| 1101 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1102 | - && $tokens[$i]['scope_closer'] === $i |
|
| 1103 | - && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1104 | - || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1105 | - ) { |
|
| 1106 | - if ($this->_debug === true) { |
|
| 1107 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1108 | - $line = $tokens[$i]['line']; |
|
| 1109 | - echo "Close $type on line $line".PHP_EOL; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - $prev = false; |
|
| 1113 | - |
|
| 1114 | - $object = 0; |
|
| 1115 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1116 | - $conditions = $tokens[$i]['conditions']; |
|
| 1117 | - krsort($conditions, SORT_NUMERIC); |
|
| 1118 | - foreach ($conditions as $token => $condition) { |
|
| 1119 | - if ($condition === T_OBJECT) { |
|
| 1120 | - $object = $token; |
|
| 1121 | - break; |
|
| 1122 | - } |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - if ($this->_debug === true && $object !== 0) { |
|
| 1126 | - $line = $tokens[$object]['line']; |
|
| 1127 | - echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1128 | - } |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - $parens = 0; |
|
| 1132 | - if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1133 | - && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1134 | - ) { |
|
| 1135 | - end($tokens[$i]['nested_parenthesis']); |
|
| 1136 | - $parens = key($tokens[$i]['nested_parenthesis']); |
|
| 1137 | - if ($this->_debug === true) { |
|
| 1138 | - $line = $tokens[$parens]['line']; |
|
| 1139 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1140 | - } |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - $condition = 0; |
|
| 1144 | - if (isset($tokens[$i]['conditions']) === true |
|
| 1145 | - && empty($tokens[$i]['conditions']) === false |
|
| 1146 | - ) { |
|
| 1147 | - end($tokens[$i]['conditions']); |
|
| 1148 | - $condition = key($tokens[$i]['conditions']); |
|
| 1149 | - if ($this->_debug === true) { |
|
| 1150 | - $line = $tokens[$condition]['line']; |
|
| 1151 | - $type = $tokens[$condition]['type']; |
|
| 1152 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1153 | - } |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - if ($parens > $object && $parens > $condition) { |
|
| 1157 | - if ($this->_debug === true) { |
|
| 1158 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1162 | - $object = 0; |
|
| 1163 | - $condition = 0; |
|
| 1164 | - } else if ($object > 0 && $object >= $condition) { |
|
| 1165 | - if ($this->_debug === true) { |
|
| 1166 | - echo "\t* using object *".PHP_EOL; |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - $prev = $object; |
|
| 1170 | - $parens = 0; |
|
| 1171 | - $condition = 0; |
|
| 1172 | - } else if ($condition > 0) { |
|
| 1173 | - if ($this->_debug === true) { |
|
| 1174 | - echo "\t* using condition *".PHP_EOL; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - $prev = $condition; |
|
| 1178 | - $object = 0; |
|
| 1179 | - $parens = 0; |
|
| 1180 | - }//end if |
|
| 1181 | - |
|
| 1182 | - if ($prev === false) { |
|
| 1183 | - $prev = $phpcsFile->findPrevious(array(T_EQUAL, T_RETURN), ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1184 | - if ($prev === false) { |
|
| 1185 | - $prev = $i; |
|
| 1186 | - if ($this->_debug === true) { |
|
| 1187 | - echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1188 | - } |
|
| 1189 | - } |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - if ($this->_debug === true) { |
|
| 1193 | - $line = $tokens[$prev]['line']; |
|
| 1194 | - $type = $tokens[$prev]['type']; |
|
| 1195 | - echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1199 | - if ($this->_debug === true) { |
|
| 1200 | - $line = $tokens[$first]['line']; |
|
| 1201 | - $type = $tokens[$first]['type']; |
|
| 1202 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1206 | - if ($prev !== $first) { |
|
| 1207 | - // This is not the start of the statement. |
|
| 1208 | - if ($this->_debug === true) { |
|
| 1209 | - $line = $tokens[$prev]['line']; |
|
| 1210 | - $type = $tokens[$prev]['type']; |
|
| 1211 | - echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1215 | - if ($this->_debug === true) { |
|
| 1216 | - $line = $tokens[$first]['line']; |
|
| 1217 | - $type = $tokens[$first]['type']; |
|
| 1218 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1219 | - } |
|
| 1220 | - } |
|
| 1221 | - |
|
| 1222 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1223 | - if ($object > 0 || $condition > 0) { |
|
| 1224 | - $currentIndent += $this->indent; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 1228 | - && $tokens[$first]['scope_closer'] === $first |
|
| 1229 | - ) { |
|
| 1230 | - if ($this->_debug === true) { |
|
| 1231 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1235 | - $currentIndent = $setIndents[$first]; |
|
| 1236 | - } else if ($this->_debug === true) { |
|
| 1237 | - echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1238 | - } |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - // Make sure it is divisible by our expected indent. |
|
| 1242 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1243 | - $setIndents[$first] = $currentIndent; |
|
| 1244 | - |
|
| 1245 | - if ($this->_debug === true) { |
|
| 1246 | - $type = $tokens[$first]['type']; |
|
| 1247 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1248 | - } |
|
| 1249 | - }//end if |
|
| 1250 | - }//end for |
|
| 1251 | - |
|
| 1252 | - // Don't process the rest of the file. |
|
| 1253 | - return $phpcsFile->numTokens; |
|
| 1254 | - |
|
| 1255 | - }//end process() |
|
| 35 | + /** |
|
| 36 | + * A list of tokenizers this sniff supports. |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + public $supportedTokenizers = array( |
|
| 41 | + 'PHP', |
|
| 42 | + 'JS', |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The number of spaces code should be indented. |
|
| 47 | + * |
|
| 48 | + * @var int |
|
| 49 | + */ |
|
| 50 | + public $indent = 4; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Does the indent need to be exactly right? |
|
| 54 | + * |
|
| 55 | + * If TRUE, indent needs to be exactly $indent spaces. If FALSE, |
|
| 56 | + * indent needs to be at least $indent spaces (but can be more). |
|
| 57 | + * |
|
| 58 | + * @var bool |
|
| 59 | + */ |
|
| 60 | + public $exact = false; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Should tabs be used for indenting? |
|
| 64 | + * |
|
| 65 | + * If TRUE, fixes will be made using tabs instead of spaces. |
|
| 66 | + * The size of each tab is important, so it should be specified |
|
| 67 | + * using the --tab-width CLI argument. |
|
| 68 | + * |
|
| 69 | + * @var bool |
|
| 70 | + */ |
|
| 71 | + public $tabIndent = false; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The --tab-width CLI value that is being used. |
|
| 75 | + * |
|
| 76 | + * @var int |
|
| 77 | + */ |
|
| 78 | + private $_tabWidth = null; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * List of tokens not needing to be checked for indentation. |
|
| 82 | + * |
|
| 83 | + * Useful to allow Sniffs based on this to easily ignore/skip some |
|
| 84 | + * tokens from verification. For example, inline HTML sections |
|
| 85 | + * or PHP open/close tags can escape from here and have their own |
|
| 86 | + * rules elsewhere. |
|
| 87 | + * |
|
| 88 | + * @var int[] |
|
| 89 | + */ |
|
| 90 | + public $ignoreIndentationTokens = array(); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * List of tokens not needing to be checked for indentation. |
|
| 94 | + * |
|
| 95 | + * This is a cached copy of the public version of this var, which |
|
| 96 | + * can be set in a ruleset file, and some core ignored tokens. |
|
| 97 | + * |
|
| 98 | + * @var int[] |
|
| 99 | + */ |
|
| 100 | + private $_ignoreIndentationTokens = array(); |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Any scope openers that should not cause an indent. |
|
| 104 | + * |
|
| 105 | + * @var int[] |
|
| 106 | + */ |
|
| 107 | + protected $nonIndentingScopes = array(); |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Show debug output for this sniff. |
|
| 111 | + * |
|
| 112 | + * @var bool |
|
| 113 | + */ |
|
| 114 | + private $_debug = false; |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Returns an array of tokens this test wants to listen for. |
|
| 119 | + * |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function register() |
|
| 123 | + { |
|
| 124 | + if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 125 | + $this->_debug = false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return array(T_OPEN_TAG); |
|
| 129 | + |
|
| 130 | + }//end register() |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Processes this test, when one of its tokens is encountered. |
|
| 135 | + * |
|
| 136 | + * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
|
| 137 | + * @param int $stackPtr The position of the current token |
|
| 138 | + * in the stack passed in $tokens. |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 143 | + { |
|
| 144 | + $debug = PHP_CodeSniffer::getConfigData('scope_indent_debug'); |
|
| 145 | + if ($debug !== null) { |
|
| 146 | + $this->_debug = (bool) $debug; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if ($this->_tabWidth === null) { |
|
| 150 | + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
| 151 | + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 152 | + // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 153 | + // It shouldn't really matter because indent checks elsewhere in the |
|
| 154 | + // standard should fix things up. |
|
| 155 | + $this->_tabWidth = 4; |
|
| 156 | + } else { |
|
| 157 | + $this->_tabWidth = $cliValues['tabWidth']; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $currentIndent = 0; |
|
| 162 | + $lastOpenTag = $stackPtr; |
|
| 163 | + $lastCloseTag = null; |
|
| 164 | + $openScopes = array(); |
|
| 165 | + $adjustments = array(); |
|
| 166 | + $setIndents = array(); |
|
| 167 | + |
|
| 168 | + $tokens = $phpcsFile->getTokens(); |
|
| 169 | + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 170 | + $trimmed = ltrim($tokens[$first]['content']); |
|
| 171 | + if ($trimmed === '') { |
|
| 172 | + $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 173 | + } else { |
|
| 174 | + $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if ($this->_debug === true) { |
|
| 178 | + $line = $tokens[$stackPtr]['line']; |
|
| 179 | + echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if (empty($this->_ignoreIndentationTokens) === true) { |
|
| 183 | + $this->_ignoreIndentationTokens = array(T_INLINE_HTML => true); |
|
| 184 | + foreach ($this->ignoreIndentationTokens as $token) { |
|
| 185 | + if (is_int($token) === false) { |
|
| 186 | + if (defined($token) === false) { |
|
| 187 | + continue; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $token = constant($token); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $this->_ignoreIndentationTokens[$token] = true; |
|
| 194 | + } |
|
| 195 | + }//end if |
|
| 196 | + |
|
| 197 | + $this->exact = (bool) $this->exact; |
|
| 198 | + $this->tabIndent = (bool) $this->tabIndent; |
|
| 199 | + |
|
| 200 | + for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 201 | + if ($i === false) { |
|
| 202 | + // Something has gone very wrong; maybe a parse error. |
|
| 203 | + break; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + $checkToken = null; |
|
| 207 | + $checkIndent = null; |
|
| 208 | + |
|
| 209 | + $exact = (bool) $this->exact; |
|
| 210 | + if ($exact === true && isset($tokens[$i]['nested_parenthesis']) === true) { |
|
| 211 | + // Don't check indents exactly between parenthesis as they |
|
| 212 | + // tend to have custom rules, such as with multi-line function calls |
|
| 213 | + // and control structure conditions. |
|
| 214 | + $exact = false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Detect line changes and figure out where the indent is. |
|
| 218 | + if ($tokens[$i]['column'] === 1) { |
|
| 219 | + $trimmed = ltrim($tokens[$i]['content']); |
|
| 220 | + if ($trimmed === '') { |
|
| 221 | + if (isset($tokens[($i + 1)]) === true |
|
| 222 | + && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 223 | + ) { |
|
| 224 | + $checkToken = ($i + 1); |
|
| 225 | + $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 226 | + } |
|
| 227 | + } else { |
|
| 228 | + $checkToken = $i; |
|
| 229 | + $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + // Closing parenthesis should just be indented to at least |
|
| 234 | + // the same level as where they were opened (but can be more). |
|
| 235 | + if (($checkToken !== null |
|
| 236 | + && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 237 | + && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 238 | + || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 239 | + && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 240 | + ) { |
|
| 241 | + if ($checkToken !== null) { |
|
| 242 | + $parenCloser = $checkToken; |
|
| 243 | + } else { |
|
| 244 | + $parenCloser = $i; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if ($this->_debug === true) { |
|
| 248 | + $line = $tokens[$i]['line']; |
|
| 249 | + echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 253 | + if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 254 | + $parens = 0; |
|
| 255 | + if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 256 | + && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 257 | + ) { |
|
| 258 | + end($tokens[$parenCloser]['nested_parenthesis']); |
|
| 259 | + $parens = key($tokens[$parenCloser]['nested_parenthesis']); |
|
| 260 | + if ($this->_debug === true) { |
|
| 261 | + $line = $tokens[$parens]['line']; |
|
| 262 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $condition = 0; |
|
| 267 | + if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 268 | + && empty($tokens[$parenCloser]['conditions']) === false |
|
| 269 | + ) { |
|
| 270 | + end($tokens[$parenCloser]['conditions']); |
|
| 271 | + $condition = key($tokens[$parenCloser]['conditions']); |
|
| 272 | + if ($this->_debug === true) { |
|
| 273 | + $line = $tokens[$condition]['line']; |
|
| 274 | + $type = $tokens[$condition]['type']; |
|
| 275 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + if ($parens > $condition) { |
|
| 280 | + if ($this->_debug === true) { |
|
| 281 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $parenOpener = $parens; |
|
| 285 | + $condition = 0; |
|
| 286 | + } else if ($condition > 0) { |
|
| 287 | + if ($this->_debug === true) { |
|
| 288 | + echo "\t* using condition *".PHP_EOL; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $parenOpener = $condition; |
|
| 292 | + $parens = 0; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + $exact = false; |
|
| 296 | + |
|
| 297 | + $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 298 | + $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 299 | + |
|
| 300 | + if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 301 | + if ($this->_debug === true) { |
|
| 302 | + echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 306 | + if (isset($adjustments[$condition]) === true) { |
|
| 307 | + $checkIndent += $adjustments[$condition]; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $currentIndent = $checkIndent; |
|
| 311 | + |
|
| 312 | + if ($this->_debug === true) { |
|
| 313 | + $type = $tokens[$lastOpenTag]['type']; |
|
| 314 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 315 | + } |
|
| 316 | + } else if ($condition > 0 |
|
| 317 | + && isset($tokens[$condition]['scope_opener']) === true |
|
| 318 | + && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 319 | + ) { |
|
| 320 | + $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 321 | + if (isset($adjustments[$condition]) === true) { |
|
| 322 | + $checkIndent += $adjustments[$condition]; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + $currentIndent = $checkIndent; |
|
| 326 | + |
|
| 327 | + if ($this->_debug === true) { |
|
| 328 | + $type = $tokens[$condition]['type']; |
|
| 329 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 330 | + } |
|
| 331 | + } else { |
|
| 332 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 333 | + |
|
| 334 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 335 | + if (isset($adjustments[$first]) === true) { |
|
| 336 | + $checkIndent += $adjustments[$first]; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + if ($this->_debug === true) { |
|
| 340 | + $line = $tokens[$first]['line']; |
|
| 341 | + $type = $tokens[$first]['type']; |
|
| 342 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ($first === $tokens[$parenCloser]['parenthesis_opener']) { |
|
| 346 | + // This is unlikely to be the start of the statement, so look |
|
| 347 | + // back further to find it. |
|
| 348 | + $first--; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 352 | + if ($prev !== $first) { |
|
| 353 | + // This is not the start of the statement. |
|
| 354 | + if ($this->_debug === true) { |
|
| 355 | + $line = $tokens[$prev]['line']; |
|
| 356 | + $type = $tokens[$prev]['type']; |
|
| 357 | + echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 361 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 362 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 363 | + if ($this->_debug === true) { |
|
| 364 | + $line = $tokens[$first]['line']; |
|
| 365 | + $type = $tokens[$first]['type']; |
|
| 366 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 371 | + && $tokens[$first]['scope_closer'] === $first |
|
| 372 | + ) { |
|
| 373 | + if ($this->_debug === true) { |
|
| 374 | + echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 378 | + $scopeCloser = $first; |
|
| 379 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 380 | + |
|
| 381 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 382 | + if (isset($adjustments[$first]) === true) { |
|
| 383 | + $currentIndent += $adjustments[$first]; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // Make sure it is divisible by our expected indent. |
|
| 387 | + if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 388 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + $setIndents[$first] = $currentIndent; |
|
| 392 | + |
|
| 393 | + if ($this->_debug === true) { |
|
| 394 | + $type = $tokens[$first]['type']; |
|
| 395 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 396 | + } |
|
| 397 | + }//end if |
|
| 398 | + } else { |
|
| 399 | + // Don't force current indent to divisible because there could be custom |
|
| 400 | + // rules in place between parenthesis, such as with arrays. |
|
| 401 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 402 | + if (isset($adjustments[$first]) === true) { |
|
| 403 | + $currentIndent += $adjustments[$first]; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + $setIndents[$first] = $currentIndent; |
|
| 407 | + |
|
| 408 | + if ($this->_debug === true) { |
|
| 409 | + $type = $tokens[$first]['type']; |
|
| 410 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 411 | + } |
|
| 412 | + }//end if |
|
| 413 | + }//end if |
|
| 414 | + } else if ($this->_debug === true) { |
|
| 415 | + echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 416 | + }//end if |
|
| 417 | + }//end if |
|
| 418 | + |
|
| 419 | + // Closing short array bracket should just be indented to at least |
|
| 420 | + // the same level as where it was opened (but can be more). |
|
| 421 | + if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 422 | + || ($checkToken !== null |
|
| 423 | + && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 424 | + ) { |
|
| 425 | + if ($checkToken !== null) { |
|
| 426 | + $arrayCloser = $checkToken; |
|
| 427 | + } else { |
|
| 428 | + $arrayCloser = $i; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + if ($this->_debug === true) { |
|
| 432 | + $line = $tokens[$arrayCloser]['line']; |
|
| 433 | + echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 437 | + if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 438 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 439 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 440 | + if (isset($adjustments[$first]) === true) { |
|
| 441 | + $checkIndent += $adjustments[$first]; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $exact = false; |
|
| 445 | + |
|
| 446 | + if ($this->_debug === true) { |
|
| 447 | + $line = $tokens[$first]['line']; |
|
| 448 | + $type = $tokens[$first]['type']; |
|
| 449 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 453 | + // This is unlikely to be the start of the statement, so look |
|
| 454 | + // back further to find it. |
|
| 455 | + $first--; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 459 | + if ($prev !== $first) { |
|
| 460 | + // This is not the start of the statement. |
|
| 461 | + if ($this->_debug === true) { |
|
| 462 | + $line = $tokens[$prev]['line']; |
|
| 463 | + $type = $tokens[$prev]['type']; |
|
| 464 | + echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 468 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 469 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 470 | + if ($this->_debug === true) { |
|
| 471 | + $line = $tokens[$first]['line']; |
|
| 472 | + $type = $tokens[$first]['type']; |
|
| 473 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 478 | + && $tokens[$first]['scope_closer'] === $first |
|
| 479 | + ) { |
|
| 480 | + // The first token is a scope closer and would have already |
|
| 481 | + // been processed and set the indent level correctly, so |
|
| 482 | + // don't adjust it again. |
|
| 483 | + if ($this->_debug === true) { |
|
| 484 | + echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + if (isset($setIndents[$first]) === true) { |
|
| 488 | + $currentIndent = $setIndents[$first]; |
|
| 489 | + if ($this->_debug === true) { |
|
| 490 | + echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + } else { |
|
| 494 | + // Don't force current indent to be divisible because there could be custom |
|
| 495 | + // rules in place for arrays. |
|
| 496 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 497 | + if (isset($adjustments[$first]) === true) { |
|
| 498 | + $currentIndent += $adjustments[$first]; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + $setIndents[$first] = $currentIndent; |
|
| 502 | + |
|
| 503 | + if ($this->_debug === true) { |
|
| 504 | + $type = $tokens[$first]['type']; |
|
| 505 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 506 | + } |
|
| 507 | + }//end if |
|
| 508 | + } else if ($this->_debug === true) { |
|
| 509 | + echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 510 | + }//end if |
|
| 511 | + }//end if |
|
| 512 | + |
|
| 513 | + // Adjust lines within scopes while auto-fixing. |
|
| 514 | + if ($checkToken !== null |
|
| 515 | + && $exact === false |
|
| 516 | + && (empty($tokens[$checkToken]['conditions']) === false |
|
| 517 | + || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 518 | + && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 519 | + ) { |
|
| 520 | + if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 521 | + end($tokens[$checkToken]['conditions']); |
|
| 522 | + $condition = key($tokens[$checkToken]['conditions']); |
|
| 523 | + } else { |
|
| 524 | + $condition = $tokens[$checkToken]['scope_condition']; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 528 | + |
|
| 529 | + if (isset($adjustments[$first]) === true |
|
| 530 | + && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 531 | + || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 532 | + ) { |
|
| 533 | + $padding = ($tokenIndent + $adjustments[$first]); |
|
| 534 | + if ($padding > 0) { |
|
| 535 | + if ($this->tabIndent === true) { |
|
| 536 | + $numTabs = floor($padding / $this->_tabWidth); |
|
| 537 | + $numSpaces = ($padding - ($numTabs * $this->_tabWidth)); |
|
| 538 | + $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 539 | + } else { |
|
| 540 | + $padding = str_repeat(' ', $padding); |
|
| 541 | + } |
|
| 542 | + } else { |
|
| 543 | + $padding = ''; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + if ($checkToken === $i) { |
|
| 547 | + $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 548 | + } else { |
|
| 549 | + // Easier to just replace the entire indent. |
|
| 550 | + $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + if ($this->_debug === true) { |
|
| 554 | + $length = strlen($padding); |
|
| 555 | + $line = $tokens[$checkToken]['line']; |
|
| 556 | + $type = $tokens[$checkToken]['type']; |
|
| 557 | + echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + $adjustments[$checkToken] = $adjustments[$first]; |
|
| 561 | + |
|
| 562 | + if ($this->_debug === true) { |
|
| 563 | + $line = $tokens[$checkToken]['line']; |
|
| 564 | + $type = $tokens[$checkToken]['type']; |
|
| 565 | + echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 566 | + } |
|
| 567 | + }//end if |
|
| 568 | + }//end if |
|
| 569 | + |
|
| 570 | + // Scope closers reset the required indent to the same level as the opening condition. |
|
| 571 | + if (($checkToken !== null |
|
| 572 | + && isset($openScopes[$checkToken]) === true |
|
| 573 | + || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 574 | + && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 575 | + && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 576 | + && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 577 | + || ($checkToken === null |
|
| 578 | + && isset($openScopes[$i]) === true |
|
| 579 | + || (isset($tokens[$i]['scope_condition']) === true |
|
| 580 | + && isset($tokens[$i]['scope_closer']) === true |
|
| 581 | + && $tokens[$i]['scope_closer'] === $i |
|
| 582 | + && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 583 | + ) { |
|
| 584 | + if ($this->_debug === true) { |
|
| 585 | + if ($checkToken === null) { |
|
| 586 | + $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 587 | + $line = $tokens[$i]['line']; |
|
| 588 | + } else { |
|
| 589 | + $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 590 | + $line = $tokens[$checkToken]['line']; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + $scopeCloser = $checkToken; |
|
| 597 | + if ($scopeCloser === null) { |
|
| 598 | + $scopeCloser = $i; |
|
| 599 | + } else { |
|
| 600 | + array_pop($openScopes); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 604 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 605 | + |
|
| 606 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 607 | + if (isset($adjustments[$first]) === true) { |
|
| 608 | + $currentIndent += $adjustments[$first]; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + // Make sure it is divisible by our expected indent. |
|
| 612 | + if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 613 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + $setIndents[$scopeCloser] = $currentIndent; |
|
| 617 | + |
|
| 618 | + if ($this->_debug === true) { |
|
| 619 | + $type = $tokens[$scopeCloser]['type']; |
|
| 620 | + echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // We only check the indent of scope closers if they are |
|
| 624 | + // curly braces because other constructs tend to have different rules. |
|
| 625 | + if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 626 | + $exact = true; |
|
| 627 | + } else { |
|
| 628 | + $checkToken = null; |
|
| 629 | + } |
|
| 630 | + }//end if |
|
| 631 | + }//end if |
|
| 632 | + |
|
| 633 | + // Handle scope for JS object notation. |
|
| 634 | + if ($phpcsFile->tokenizerType === 'JS' |
|
| 635 | + && (($checkToken !== null |
|
| 636 | + && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 637 | + && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 638 | + || ($checkToken === null |
|
| 639 | + && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 640 | + && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 641 | + ) { |
|
| 642 | + if ($this->_debug === true) { |
|
| 643 | + $line = $tokens[$i]['line']; |
|
| 644 | + echo "Close JS object on line $line".PHP_EOL; |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + $scopeCloser = $checkToken; |
|
| 648 | + if ($scopeCloser === null) { |
|
| 649 | + $scopeCloser = $i; |
|
| 650 | + } else { |
|
| 651 | + array_pop($openScopes); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $parens = 0; |
|
| 655 | + if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 656 | + && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 657 | + ) { |
|
| 658 | + end($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 659 | + $parens = key($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 660 | + if ($this->_debug === true) { |
|
| 661 | + $line = $tokens[$parens]['line']; |
|
| 662 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + $condition = 0; |
|
| 667 | + if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 668 | + && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 669 | + ) { |
|
| 670 | + end($tokens[$scopeCloser]['conditions']); |
|
| 671 | + $condition = key($tokens[$scopeCloser]['conditions']); |
|
| 672 | + if ($this->_debug === true) { |
|
| 673 | + $line = $tokens[$condition]['line']; |
|
| 674 | + $type = $tokens[$condition]['type']; |
|
| 675 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + if ($parens > $condition) { |
|
| 680 | + if ($this->_debug === true) { |
|
| 681 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 685 | + $condition = 0; |
|
| 686 | + } else if ($condition > 0) { |
|
| 687 | + if ($this->_debug === true) { |
|
| 688 | + echo "\t* using condition *".PHP_EOL; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 692 | + $parens = 0; |
|
| 693 | + } else { |
|
| 694 | + if ($this->_debug === true) { |
|
| 695 | + $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 696 | + echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 700 | + }//end if |
|
| 701 | + |
|
| 702 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 703 | + if (isset($adjustments[$first]) === true) { |
|
| 704 | + $currentIndent += $adjustments[$first]; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + if ($parens > 0 || $condition > 0) { |
|
| 708 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 709 | + if (isset($adjustments[$first]) === true) { |
|
| 710 | + $checkIndent += $adjustments[$first]; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + if ($condition > 0) { |
|
| 714 | + $checkIndent += $this->indent; |
|
| 715 | + $currentIndent += $this->indent; |
|
| 716 | + $exact = true; |
|
| 717 | + } |
|
| 718 | + } else { |
|
| 719 | + $checkIndent = $currentIndent; |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + // Make sure it is divisible by our expected indent. |
|
| 723 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 724 | + $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 725 | + $setIndents[$first] = $currentIndent; |
|
| 726 | + |
|
| 727 | + if ($this->_debug === true) { |
|
| 728 | + $type = $tokens[$first]['type']; |
|
| 729 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 730 | + } |
|
| 731 | + }//end if |
|
| 732 | + |
|
| 733 | + if ($checkToken !== null |
|
| 734 | + && isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 735 | + && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false |
|
| 736 | + && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 737 | + ) { |
|
| 738 | + $exact = true; |
|
| 739 | + |
|
| 740 | + $lastOpener = null; |
|
| 741 | + if (empty($openScopes) === false) { |
|
| 742 | + end($openScopes); |
|
| 743 | + $lastOpener = current($openScopes); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + // A scope opener that shares a closer with another token (like multiple |
|
| 747 | + // CASEs using the same BREAK) needs to reduce the indent level so its |
|
| 748 | + // indent is checked correctly. It will then increase the indent again |
|
| 749 | + // (as all openers do) after being checked. |
|
| 750 | + if ($lastOpener !== null |
|
| 751 | + && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 752 | + && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 753 | + && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 754 | + ) { |
|
| 755 | + $currentIndent -= $this->indent; |
|
| 756 | + $setIndents[$lastOpener] = $currentIndent; |
|
| 757 | + if ($this->_debug === true) { |
|
| 758 | + $line = $tokens[$i]['line']; |
|
| 759 | + $type = $tokens[$lastOpener]['type']; |
|
| 760 | + echo "Shared closer found on line $line".PHP_EOL; |
|
| 761 | + echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 766 | + && $tokenIndent > $currentIndent |
|
| 767 | + ) { |
|
| 768 | + // The opener is indented more than needed, which is fine. |
|
| 769 | + // But just check that it is divisible by our expected indent. |
|
| 770 | + $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 771 | + $exact = false; |
|
| 772 | + |
|
| 773 | + if ($this->_debug === true) { |
|
| 774 | + $line = $tokens[$i]['line']; |
|
| 775 | + echo "Closure found on line $line".PHP_EOL; |
|
| 776 | + echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 777 | + } |
|
| 778 | + } |
|
| 779 | + }//end if |
|
| 780 | + |
|
| 781 | + // Method prefix indentation has to be exact or else if will break |
|
| 782 | + // the rest of the function declaration, and potentially future ones. |
|
| 783 | + if ($checkToken !== null |
|
| 784 | + && isset(PHP_CodeSniffer_Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 785 | + && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 786 | + ) { |
|
| 787 | + $exact = true; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + // JS property indentation has to be exact or else if will break |
|
| 791 | + // things like function and object indentation. |
|
| 792 | + if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 793 | + $exact = true; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + // PHP tags needs to be indented to exact column positions |
|
| 797 | + // so they don't cause problems with indent checks for the code |
|
| 798 | + // within them, but they don't need to line up with the current indent. |
|
| 799 | + if ($checkToken !== null |
|
| 800 | + && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 801 | + || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 802 | + || $tokens[$checkToken]['code'] === T_CLOSE_TAG) |
|
| 803 | + ) { |
|
| 804 | + $exact = true; |
|
| 805 | + $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 806 | + $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + // Check the line indent. |
|
| 810 | + if ($checkIndent === null) { |
|
| 811 | + $checkIndent = $currentIndent; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + $adjusted = false; |
|
| 815 | + if ($checkToken !== null |
|
| 816 | + && isset($this->_ignoreIndentationTokens[$tokens[$checkToken]['code']]) === false |
|
| 817 | + && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 818 | + || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 819 | + ) { |
|
| 820 | + $type = 'IncorrectExact'; |
|
| 821 | + $error = 'Line indented incorrectly; expected '; |
|
| 822 | + if ($exact === false) { |
|
| 823 | + $error .= 'at least '; |
|
| 824 | + $type = 'Incorrect'; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + if ($this->tabIndent === true) { |
|
| 828 | + $error .= '%s tabs, found %s'; |
|
| 829 | + $data = array( |
|
| 830 | + floor($checkIndent / $this->_tabWidth), |
|
| 831 | + floor($tokenIndent / $this->_tabWidth), |
|
| 832 | + ); |
|
| 833 | + } else { |
|
| 834 | + $error .= '%s spaces, found %s'; |
|
| 835 | + $data = array( |
|
| 836 | + $checkIndent, |
|
| 837 | + $tokenIndent, |
|
| 838 | + ); |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + if ($this->_debug === true) { |
|
| 842 | + $line = $tokens[$checkToken]['line']; |
|
| 843 | + $message = vsprintf($error, $data); |
|
| 844 | + echo "[Line $line] $message".PHP_EOL; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 848 | + if ($fix === true || $this->_debug === true) { |
|
| 849 | + $padding = ''; |
|
| 850 | + if ($this->tabIndent === true) { |
|
| 851 | + $numTabs = floor($checkIndent / $this->_tabWidth); |
|
| 852 | + if ($numTabs > 0) { |
|
| 853 | + $numSpaces = ($checkIndent - ($numTabs * $this->_tabWidth)); |
|
| 854 | + $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 855 | + } |
|
| 856 | + } else if ($checkIndent > 0) { |
|
| 857 | + $padding = str_repeat(' ', $checkIndent); |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + if ($checkToken === $i) { |
|
| 861 | + $accepted = $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 862 | + } else { |
|
| 863 | + // Easier to just replace the entire indent. |
|
| 864 | + $accepted = $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + if ($accepted === true) { |
|
| 868 | + $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 869 | + if ($this->_debug === true) { |
|
| 870 | + $line = $tokens[$checkToken]['line']; |
|
| 871 | + $type = $tokens[$checkToken]['type']; |
|
| 872 | + echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 873 | + } |
|
| 874 | + } |
|
| 875 | + } else { |
|
| 876 | + // Assume the change would be applied and continue |
|
| 877 | + // checking indents under this assumption. This gives more |
|
| 878 | + // technically accurate error messages. |
|
| 879 | + $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 880 | + }//end if |
|
| 881 | + }//end if |
|
| 882 | + |
|
| 883 | + if ($checkToken !== null) { |
|
| 884 | + $i = $checkToken; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + // Completely skip here/now docs as the indent is a part of the |
|
| 888 | + // content itself. |
|
| 889 | + if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 890 | + || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 891 | + ) { |
|
| 892 | + $i = $phpcsFile->findNext(array(T_END_HEREDOC, T_END_NOWDOC), ($i + 1)); |
|
| 893 | + continue; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + // Completely skip multi-line strings as the indent is a part of the |
|
| 897 | + // content itself. |
|
| 898 | + if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 899 | + || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 900 | + ) { |
|
| 901 | + $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 902 | + $i--; |
|
| 903 | + continue; |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + // Completely skip doc comments as they tend to have complex |
|
| 907 | + // indentation rules. |
|
| 908 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 909 | + $i = $tokens[$i]['comment_closer']; |
|
| 910 | + continue; |
|
| 911 | + } |
|
| 912 | + |
|
| 913 | + // Open tags reset the indent level. |
|
| 914 | + if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 915 | + || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 916 | + ) { |
|
| 917 | + if ($this->_debug === true) { |
|
| 918 | + $line = $tokens[$i]['line']; |
|
| 919 | + echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + if ($checkToken === null) { |
|
| 923 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 924 | + $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 925 | + } else { |
|
| 926 | + $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + $lastOpenTag = $i; |
|
| 930 | + |
|
| 931 | + if (isset($adjustments[$i]) === true) { |
|
| 932 | + $currentIndent += $adjustments[$i]; |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + // Make sure it is divisible by our expected indent. |
|
| 936 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 937 | + $setIndents[$i] = $currentIndent; |
|
| 938 | + |
|
| 939 | + if ($this->_debug === true) { |
|
| 940 | + $type = $tokens[$i]['type']; |
|
| 941 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + continue; |
|
| 945 | + }//end if |
|
| 946 | + |
|
| 947 | + // Close tags reset the indent level, unless they are closing a tag |
|
| 948 | + // opened on the same line. |
|
| 949 | + if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 950 | + if ($this->_debug === true) { |
|
| 951 | + $line = $tokens[$i]['line']; |
|
| 952 | + echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 956 | + $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 957 | + $lastCloseTag = $i; |
|
| 958 | + } else { |
|
| 959 | + if ($lastCloseTag === null) { |
|
| 960 | + $currentIndent = 0; |
|
| 961 | + } else { |
|
| 962 | + $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 963 | + } |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + if (isset($adjustments[$i]) === true) { |
|
| 967 | + $currentIndent += $adjustments[$i]; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + // Make sure it is divisible by our expected indent. |
|
| 971 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 972 | + $setIndents[$i] = $currentIndent; |
|
| 973 | + |
|
| 974 | + if ($this->_debug === true) { |
|
| 975 | + $type = $tokens[$i]['type']; |
|
| 976 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + continue; |
|
| 980 | + }//end if |
|
| 981 | + |
|
| 982 | + // Anon classes and functions set the indent based on their own indent level. |
|
| 983 | + if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 984 | + $closer = $tokens[$i]['scope_closer']; |
|
| 985 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 986 | + if ($this->_debug === true) { |
|
| 987 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 988 | + $line = $tokens[$i]['line']; |
|
| 989 | + echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + $i = $closer; |
|
| 993 | + continue; |
|
| 994 | + } |
|
| 995 | + |
|
| 996 | + if ($this->_debug === true) { |
|
| 997 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 998 | + $line = $tokens[$i]['line']; |
|
| 999 | + echo "Open $type on line $line".PHP_EOL; |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1003 | + $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1004 | + |
|
| 1005 | + if (isset($adjustments[$first]) === true) { |
|
| 1006 | + $currentIndent += $adjustments[$first]; |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + // Make sure it is divisible by our expected indent. |
|
| 1010 | + $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1011 | + $i = $tokens[$i]['scope_opener']; |
|
| 1012 | + $setIndents[$i] = $currentIndent; |
|
| 1013 | + |
|
| 1014 | + if ($this->_debug === true) { |
|
| 1015 | + $type = $tokens[$i]['type']; |
|
| 1016 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + continue; |
|
| 1020 | + }//end if |
|
| 1021 | + |
|
| 1022 | + // Scope openers increase the indent level. |
|
| 1023 | + if (isset($tokens[$i]['scope_condition']) === true |
|
| 1024 | + && isset($tokens[$i]['scope_opener']) === true |
|
| 1025 | + && $tokens[$i]['scope_opener'] === $i |
|
| 1026 | + ) { |
|
| 1027 | + $closer = $tokens[$i]['scope_closer']; |
|
| 1028 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1029 | + if ($this->_debug === true) { |
|
| 1030 | + $line = $tokens[$i]['line']; |
|
| 1031 | + $type = $tokens[$i]['type']; |
|
| 1032 | + echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + $i = $closer; |
|
| 1036 | + continue; |
|
| 1037 | + } |
|
| 1038 | + |
|
| 1039 | + $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1040 | + if (isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$condition]) === true |
|
| 1041 | + && in_array($condition, $this->nonIndentingScopes) === false |
|
| 1042 | + ) { |
|
| 1043 | + if ($this->_debug === true) { |
|
| 1044 | + $line = $tokens[$i]['line']; |
|
| 1045 | + $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1046 | + echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + $currentIndent += $this->indent; |
|
| 1050 | + $setIndents[$i] = $currentIndent; |
|
| 1051 | + $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1052 | + |
|
| 1053 | + if ($this->_debug === true) { |
|
| 1054 | + $type = $tokens[$i]['type']; |
|
| 1055 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + continue; |
|
| 1059 | + } |
|
| 1060 | + }//end if |
|
| 1061 | + |
|
| 1062 | + // JS objects set the indent level. |
|
| 1063 | + if ($phpcsFile->tokenizerType === 'JS' |
|
| 1064 | + && $tokens[$i]['code'] === T_OBJECT |
|
| 1065 | + ) { |
|
| 1066 | + $closer = $tokens[$i]['bracket_closer']; |
|
| 1067 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1068 | + if ($this->_debug === true) { |
|
| 1069 | + $line = $tokens[$i]['line']; |
|
| 1070 | + echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + $i = $closer; |
|
| 1074 | + continue; |
|
| 1075 | + } |
|
| 1076 | + |
|
| 1077 | + if ($this->_debug === true) { |
|
| 1078 | + $line = $tokens[$i]['line']; |
|
| 1079 | + echo "Open JS object on line $line".PHP_EOL; |
|
| 1080 | + } |
|
| 1081 | + |
|
| 1082 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1083 | + $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1084 | + if (isset($adjustments[$first]) === true) { |
|
| 1085 | + $currentIndent += $adjustments[$first]; |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + // Make sure it is divisible by our expected indent. |
|
| 1089 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1090 | + $setIndents[$first] = $currentIndent; |
|
| 1091 | + |
|
| 1092 | + if ($this->_debug === true) { |
|
| 1093 | + $type = $tokens[$first]['type']; |
|
| 1094 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + continue; |
|
| 1098 | + }//end if |
|
| 1099 | + |
|
| 1100 | + // Closing an anon class or function. |
|
| 1101 | + if (isset($tokens[$i]['scope_condition']) === true |
|
| 1102 | + && $tokens[$i]['scope_closer'] === $i |
|
| 1103 | + && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1104 | + || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1105 | + ) { |
|
| 1106 | + if ($this->_debug === true) { |
|
| 1107 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1108 | + $line = $tokens[$i]['line']; |
|
| 1109 | + echo "Close $type on line $line".PHP_EOL; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + $prev = false; |
|
| 1113 | + |
|
| 1114 | + $object = 0; |
|
| 1115 | + if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1116 | + $conditions = $tokens[$i]['conditions']; |
|
| 1117 | + krsort($conditions, SORT_NUMERIC); |
|
| 1118 | + foreach ($conditions as $token => $condition) { |
|
| 1119 | + if ($condition === T_OBJECT) { |
|
| 1120 | + $object = $token; |
|
| 1121 | + break; |
|
| 1122 | + } |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + if ($this->_debug === true && $object !== 0) { |
|
| 1126 | + $line = $tokens[$object]['line']; |
|
| 1127 | + echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1128 | + } |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + $parens = 0; |
|
| 1132 | + if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1133 | + && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1134 | + ) { |
|
| 1135 | + end($tokens[$i]['nested_parenthesis']); |
|
| 1136 | + $parens = key($tokens[$i]['nested_parenthesis']); |
|
| 1137 | + if ($this->_debug === true) { |
|
| 1138 | + $line = $tokens[$parens]['line']; |
|
| 1139 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1140 | + } |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + $condition = 0; |
|
| 1144 | + if (isset($tokens[$i]['conditions']) === true |
|
| 1145 | + && empty($tokens[$i]['conditions']) === false |
|
| 1146 | + ) { |
|
| 1147 | + end($tokens[$i]['conditions']); |
|
| 1148 | + $condition = key($tokens[$i]['conditions']); |
|
| 1149 | + if ($this->_debug === true) { |
|
| 1150 | + $line = $tokens[$condition]['line']; |
|
| 1151 | + $type = $tokens[$condition]['type']; |
|
| 1152 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1153 | + } |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + if ($parens > $object && $parens > $condition) { |
|
| 1157 | + if ($this->_debug === true) { |
|
| 1158 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1162 | + $object = 0; |
|
| 1163 | + $condition = 0; |
|
| 1164 | + } else if ($object > 0 && $object >= $condition) { |
|
| 1165 | + if ($this->_debug === true) { |
|
| 1166 | + echo "\t* using object *".PHP_EOL; |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + $prev = $object; |
|
| 1170 | + $parens = 0; |
|
| 1171 | + $condition = 0; |
|
| 1172 | + } else if ($condition > 0) { |
|
| 1173 | + if ($this->_debug === true) { |
|
| 1174 | + echo "\t* using condition *".PHP_EOL; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + $prev = $condition; |
|
| 1178 | + $object = 0; |
|
| 1179 | + $parens = 0; |
|
| 1180 | + }//end if |
|
| 1181 | + |
|
| 1182 | + if ($prev === false) { |
|
| 1183 | + $prev = $phpcsFile->findPrevious(array(T_EQUAL, T_RETURN), ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1184 | + if ($prev === false) { |
|
| 1185 | + $prev = $i; |
|
| 1186 | + if ($this->_debug === true) { |
|
| 1187 | + echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1188 | + } |
|
| 1189 | + } |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + if ($this->_debug === true) { |
|
| 1193 | + $line = $tokens[$prev]['line']; |
|
| 1194 | + $type = $tokens[$prev]['type']; |
|
| 1195 | + echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1199 | + if ($this->_debug === true) { |
|
| 1200 | + $line = $tokens[$first]['line']; |
|
| 1201 | + $type = $tokens[$first]['type']; |
|
| 1202 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1206 | + if ($prev !== $first) { |
|
| 1207 | + // This is not the start of the statement. |
|
| 1208 | + if ($this->_debug === true) { |
|
| 1209 | + $line = $tokens[$prev]['line']; |
|
| 1210 | + $type = $tokens[$prev]['type']; |
|
| 1211 | + echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1215 | + if ($this->_debug === true) { |
|
| 1216 | + $line = $tokens[$first]['line']; |
|
| 1217 | + $type = $tokens[$first]['type']; |
|
| 1218 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1219 | + } |
|
| 1220 | + } |
|
| 1221 | + |
|
| 1222 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1223 | + if ($object > 0 || $condition > 0) { |
|
| 1224 | + $currentIndent += $this->indent; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 1228 | + && $tokens[$first]['scope_closer'] === $first |
|
| 1229 | + ) { |
|
| 1230 | + if ($this->_debug === true) { |
|
| 1231 | + echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1235 | + $currentIndent = $setIndents[$first]; |
|
| 1236 | + } else if ($this->_debug === true) { |
|
| 1237 | + echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1238 | + } |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + // Make sure it is divisible by our expected indent. |
|
| 1242 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1243 | + $setIndents[$first] = $currentIndent; |
|
| 1244 | + |
|
| 1245 | + if ($this->_debug === true) { |
|
| 1246 | + $type = $tokens[$first]['type']; |
|
| 1247 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1248 | + } |
|
| 1249 | + }//end if |
|
| 1250 | + }//end for |
|
| 1251 | + |
|
| 1252 | + // Don't process the rest of the file. |
|
| 1253 | + return $phpcsFile->numTokens; |
|
| 1254 | + |
|
| 1255 | + }//end process() |
|
| 1256 | 1256 | |
| 1257 | 1257 | |
| 1258 | 1258 | }//end class |
@@ -121,11 +121,11 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | public function register() |
| 123 | 123 | { |
| 124 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 124 | + if ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) === true ) { |
|
| 125 | 125 | $this->_debug = false; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - return array(T_OPEN_TAG); |
|
| 128 | + return array( T_OPEN_TAG ); |
|
| 129 | 129 | |
| 130 | 130 | }//end register() |
| 131 | 131 | |
@@ -139,22 +139,22 @@ discard block |
||
| 139 | 139 | * |
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 142 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 143 | 143 | { |
| 144 | - $debug = PHP_CodeSniffer::getConfigData('scope_indent_debug'); |
|
| 145 | - if ($debug !== null) { |
|
| 144 | + $debug = PHP_CodeSniffer::getConfigData( 'scope_indent_debug' ); |
|
| 145 | + if ( $debug !== null ) { |
|
| 146 | 146 | $this->_debug = (bool) $debug; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - if ($this->_tabWidth === null) { |
|
| 149 | + if ( $this->_tabWidth === null ) { |
|
| 150 | 150 | $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
| 151 | - if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
|
| 151 | + if ( isset( $cliValues[ 'tabWidth' ] ) === false || $cliValues[ 'tabWidth' ] === 0 ) { |
|
| 152 | 152 | // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
| 153 | 153 | // It shouldn't really matter because indent checks elsewhere in the |
| 154 | 154 | // standard should fix things up. |
| 155 | 155 | $this->_tabWidth = 4; |
| 156 | 156 | } else { |
| 157 | - $this->_tabWidth = $cliValues['tabWidth']; |
|
| 157 | + $this->_tabWidth = $cliValues[ 'tabWidth' ]; |
|
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
@@ -166,39 +166,39 @@ discard block |
||
| 166 | 166 | $setIndents = array(); |
| 167 | 167 | |
| 168 | 168 | $tokens = $phpcsFile->getTokens(); |
| 169 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 170 | - $trimmed = ltrim($tokens[$first]['content']); |
|
| 171 | - if ($trimmed === '') { |
|
| 172 | - $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 169 | + $first = $phpcsFile->findFirstOnLine( T_INLINE_HTML, $stackPtr ); |
|
| 170 | + $trimmed = ltrim( $tokens[ $first ][ 'content' ] ); |
|
| 171 | + if ( $trimmed === '' ) { |
|
| 172 | + $currentIndent = ( $tokens[ $stackPtr ][ 'column' ] - 1 ); |
|
| 173 | 173 | } else { |
| 174 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 174 | + $currentIndent = ( strlen( $tokens[ $first ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - if ($this->_debug === true) { |
|
| 178 | - $line = $tokens[$stackPtr]['line']; |
|
| 179 | - echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 177 | + if ( $this->_debug === true ) { |
|
| 178 | + $line = $tokens[ $stackPtr ][ 'line' ]; |
|
| 179 | + echo "Start with token $stackPtr on line $line with indent $currentIndent" . PHP_EOL; |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | - if (empty($this->_ignoreIndentationTokens) === true) { |
|
| 183 | - $this->_ignoreIndentationTokens = array(T_INLINE_HTML => true); |
|
| 184 | - foreach ($this->ignoreIndentationTokens as $token) { |
|
| 185 | - if (is_int($token) === false) { |
|
| 186 | - if (defined($token) === false) { |
|
| 182 | + if ( empty( $this->_ignoreIndentationTokens ) === true ) { |
|
| 183 | + $this->_ignoreIndentationTokens = array( T_INLINE_HTML => true ); |
|
| 184 | + foreach ( $this->ignoreIndentationTokens as $token ) { |
|
| 185 | + if ( is_int( $token ) === false ) { |
|
| 186 | + if ( defined( $token ) === false ) { |
|
| 187 | 187 | continue; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - $token = constant($token); |
|
| 190 | + $token = constant( $token ); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - $this->_ignoreIndentationTokens[$token] = true; |
|
| 193 | + $this->_ignoreIndentationTokens[ $token ] = true; |
|
| 194 | 194 | } |
| 195 | 195 | }//end if |
| 196 | 196 | |
| 197 | 197 | $this->exact = (bool) $this->exact; |
| 198 | 198 | $this->tabIndent = (bool) $this->tabIndent; |
| 199 | 199 | |
| 200 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 201 | - if ($i === false) { |
|
| 200 | + for ( $i = ( $stackPtr + 1 ); $i < $phpcsFile->numTokens; $i++ ) { |
|
| 201 | + if ( $i === false ) { |
|
| 202 | 202 | // Something has gone very wrong; maybe a parse error. |
| 203 | 203 | break; |
| 204 | 204 | } |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | $checkIndent = null; |
| 208 | 208 | |
| 209 | 209 | $exact = (bool) $this->exact; |
| 210 | - if ($exact === true && isset($tokens[$i]['nested_parenthesis']) === true) { |
|
| 210 | + if ( $exact === true && isset( $tokens[ $i ][ 'nested_parenthesis' ] ) === true ) { |
|
| 211 | 211 | // Don't check indents exactly between parenthesis as they |
| 212 | 212 | // tend to have custom rules, such as with multi-line function calls |
| 213 | 213 | // and control structure conditions. |
@@ -215,77 +215,77 @@ discard block |
||
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | // Detect line changes and figure out where the indent is. |
| 218 | - if ($tokens[$i]['column'] === 1) { |
|
| 219 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 220 | - if ($trimmed === '') { |
|
| 221 | - if (isset($tokens[($i + 1)]) === true |
|
| 222 | - && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 218 | + if ( $tokens[ $i ][ 'column' ] === 1 ) { |
|
| 219 | + $trimmed = ltrim( $tokens[ $i ][ 'content' ] ); |
|
| 220 | + if ( $trimmed === '' ) { |
|
| 221 | + if ( isset( $tokens[ ( $i + 1 ) ] ) === true |
|
| 222 | + && $tokens[ $i ][ 'line' ] === $tokens[ ( $i + 1 ) ][ 'line' ] |
|
| 223 | 223 | ) { |
| 224 | - $checkToken = ($i + 1); |
|
| 225 | - $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 224 | + $checkToken = ( $i + 1 ); |
|
| 225 | + $tokenIndent = ( $tokens[ ( $i + 1 ) ][ 'column' ] - 1 ); |
|
| 226 | 226 | } |
| 227 | 227 | } else { |
| 228 | 228 | $checkToken = $i; |
| 229 | - $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 229 | + $tokenIndent = ( strlen( $tokens[ $i ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 230 | 230 | } |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | // Closing parenthesis should just be indented to at least |
| 234 | 234 | // the same level as where they were opened (but can be more). |
| 235 | - if (($checkToken !== null |
|
| 236 | - && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 237 | - && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 238 | - || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 239 | - && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 235 | + if ( ( $checkToken !== null |
|
| 236 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_PARENTHESIS |
|
| 237 | + && isset( $tokens[ $checkToken ][ 'parenthesis_opener' ] ) === true ) |
|
| 238 | + || ( $tokens[ $i ][ 'code' ] === T_CLOSE_PARENTHESIS |
|
| 239 | + && isset( $tokens[ $i ][ 'parenthesis_opener' ] ) === true ) |
|
| 240 | 240 | ) { |
| 241 | - if ($checkToken !== null) { |
|
| 241 | + if ( $checkToken !== null ) { |
|
| 242 | 242 | $parenCloser = $checkToken; |
| 243 | 243 | } else { |
| 244 | 244 | $parenCloser = $i; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if ($this->_debug === true) { |
|
| 248 | - $line = $tokens[$i]['line']; |
|
| 249 | - echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 247 | + if ( $this->_debug === true ) { |
|
| 248 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 249 | + echo "Closing parenthesis found on line $line" . PHP_EOL; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 253 | - if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 252 | + $parenOpener = $tokens[ $parenCloser ][ 'parenthesis_opener' ]; |
|
| 253 | + if ( $tokens[ $parenCloser ][ 'line' ] !== $tokens[ $parenOpener ][ 'line' ] ) { |
|
| 254 | 254 | $parens = 0; |
| 255 | - if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 256 | - && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 255 | + if ( isset( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ) === true |
|
| 256 | + && empty( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ) === false |
|
| 257 | 257 | ) { |
| 258 | - end($tokens[$parenCloser]['nested_parenthesis']); |
|
| 259 | - $parens = key($tokens[$parenCloser]['nested_parenthesis']); |
|
| 260 | - if ($this->_debug === true) { |
|
| 261 | - $line = $tokens[$parens]['line']; |
|
| 262 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 258 | + end( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ); |
|
| 259 | + $parens = key( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ); |
|
| 260 | + if ( $this->_debug === true ) { |
|
| 261 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 262 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 263 | 263 | } |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | $condition = 0; |
| 267 | - if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 268 | - && empty($tokens[$parenCloser]['conditions']) === false |
|
| 267 | + if ( isset( $tokens[ $parenCloser ][ 'conditions' ] ) === true |
|
| 268 | + && empty( $tokens[ $parenCloser ][ 'conditions' ] ) === false |
|
| 269 | 269 | ) { |
| 270 | - end($tokens[$parenCloser]['conditions']); |
|
| 271 | - $condition = key($tokens[$parenCloser]['conditions']); |
|
| 272 | - if ($this->_debug === true) { |
|
| 273 | - $line = $tokens[$condition]['line']; |
|
| 274 | - $type = $tokens[$condition]['type']; |
|
| 275 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 270 | + end( $tokens[ $parenCloser ][ 'conditions' ] ); |
|
| 271 | + $condition = key( $tokens[ $parenCloser ][ 'conditions' ] ); |
|
| 272 | + if ( $this->_debug === true ) { |
|
| 273 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 274 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 275 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | - if ($parens > $condition) { |
|
| 280 | - if ($this->_debug === true) { |
|
| 281 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 279 | + if ( $parens > $condition ) { |
|
| 280 | + if ( $this->_debug === true ) { |
|
| 281 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $parenOpener = $parens; |
| 285 | 285 | $condition = 0; |
| 286 | - } else if ($condition > 0) { |
|
| 287 | - if ($this->_debug === true) { |
|
| 288 | - echo "\t* using condition *".PHP_EOL; |
|
| 286 | + } else if ( $condition > 0 ) { |
|
| 287 | + if ( $this->_debug === true ) { |
|
| 288 | + echo "\t* using condition *" . PHP_EOL; |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | $parenOpener = $condition; |
@@ -294,335 +294,335 @@ discard block |
||
| 294 | 294 | |
| 295 | 295 | $exact = false; |
| 296 | 296 | |
| 297 | - $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 298 | - $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 297 | + $lastOpenTagConditions = array_keys( $tokens[ $lastOpenTag ][ 'conditions' ] ); |
|
| 298 | + $lastOpenTagCondition = array_pop( $lastOpenTagConditions ); |
|
| 299 | 299 | |
| 300 | - if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 301 | - if ($this->_debug === true) { |
|
| 302 | - echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 300 | + if ( $condition > 0 && $lastOpenTagCondition === $condition ) { |
|
| 301 | + if ( $this->_debug === true ) { |
|
| 302 | + echo "\t* open tag is inside condition; using open tag *" . PHP_EOL; |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 306 | - if (isset($adjustments[$condition]) === true) { |
|
| 307 | - $checkIndent += $adjustments[$condition]; |
|
| 305 | + $checkIndent = ( $tokens[ $lastOpenTag ][ 'column' ] - 1 ); |
|
| 306 | + if ( isset( $adjustments[ $condition ] ) === true ) { |
|
| 307 | + $checkIndent += $adjustments[ $condition ]; |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | $currentIndent = $checkIndent; |
| 311 | 311 | |
| 312 | - if ($this->_debug === true) { |
|
| 313 | - $type = $tokens[$lastOpenTag]['type']; |
|
| 314 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 312 | + if ( $this->_debug === true ) { |
|
| 313 | + $type = $tokens[ $lastOpenTag ][ 'type' ]; |
|
| 314 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)" . PHP_EOL; |
|
| 315 | 315 | } |
| 316 | - } else if ($condition > 0 |
|
| 317 | - && isset($tokens[$condition]['scope_opener']) === true |
|
| 318 | - && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 316 | + } else if ( $condition > 0 |
|
| 317 | + && isset( $tokens[ $condition ][ 'scope_opener' ] ) === true |
|
| 318 | + && isset( $setIndents[ $tokens[ $condition ][ 'scope_opener' ] ] ) === true |
|
| 319 | 319 | ) { |
| 320 | - $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 321 | - if (isset($adjustments[$condition]) === true) { |
|
| 322 | - $checkIndent += $adjustments[$condition]; |
|
| 320 | + $checkIndent = $setIndents[ $tokens[ $condition ][ 'scope_opener' ] ]; |
|
| 321 | + if ( isset( $adjustments[ $condition ] ) === true ) { |
|
| 322 | + $checkIndent += $adjustments[ $condition ]; |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | $currentIndent = $checkIndent; |
| 326 | 326 | |
| 327 | - if ($this->_debug === true) { |
|
| 328 | - $type = $tokens[$condition]['type']; |
|
| 329 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 327 | + if ( $this->_debug === true ) { |
|
| 328 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 329 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)" . PHP_EOL; |
|
| 330 | 330 | } |
| 331 | 331 | } else { |
| 332 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 332 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $parenOpener, true ); |
|
| 333 | 333 | |
| 334 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 335 | - if (isset($adjustments[$first]) === true) { |
|
| 336 | - $checkIndent += $adjustments[$first]; |
|
| 334 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 335 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 336 | + $checkIndent += $adjustments[ $first ]; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | - if ($this->_debug === true) { |
|
| 340 | - $line = $tokens[$first]['line']; |
|
| 341 | - $type = $tokens[$first]['type']; |
|
| 342 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 339 | + if ( $this->_debug === true ) { |
|
| 340 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 341 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 342 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - if ($first === $tokens[$parenCloser]['parenthesis_opener']) { |
|
| 345 | + if ( $first === $tokens[ $parenCloser ][ 'parenthesis_opener' ] ) { |
|
| 346 | 346 | // This is unlikely to be the start of the statement, so look |
| 347 | 347 | // back further to find it. |
| 348 | 348 | $first--; |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 352 | - if ($prev !== $first) { |
|
| 351 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 352 | + if ( $prev !== $first ) { |
|
| 353 | 353 | // This is not the start of the statement. |
| 354 | - if ($this->_debug === true) { |
|
| 355 | - $line = $tokens[$prev]['line']; |
|
| 356 | - $type = $tokens[$prev]['type']; |
|
| 357 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 354 | + if ( $this->_debug === true ) { |
|
| 355 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 356 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 357 | + echo "\t* previous is $type on line $line *" . PHP_EOL; |
|
| 358 | 358 | } |
| 359 | 359 | |
| 360 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 361 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 362 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 363 | - if ($this->_debug === true) { |
|
| 364 | - $line = $tokens[$first]['line']; |
|
| 365 | - $type = $tokens[$first]['type']; |
|
| 366 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 360 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 361 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 362 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 363 | + if ( $this->_debug === true ) { |
|
| 364 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 365 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 366 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 367 | 367 | } |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 371 | - && $tokens[$first]['scope_closer'] === $first |
|
| 370 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 371 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 372 | 372 | ) { |
| 373 | - if ($this->_debug === true) { |
|
| 374 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 373 | + if ( $this->_debug === true ) { |
|
| 374 | + echo "\t* first token is a scope closer *" . PHP_EOL; |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 377 | + if ( isset( $tokens[ $first ][ 'scope_condition' ] ) === true ) { |
|
| 378 | 378 | $scopeCloser = $first; |
| 379 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 379 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $tokens[ $scopeCloser ][ 'scope_condition' ], true ); |
|
| 380 | 380 | |
| 381 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 382 | - if (isset($adjustments[$first]) === true) { |
|
| 383 | - $currentIndent += $adjustments[$first]; |
|
| 381 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 382 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 383 | + $currentIndent += $adjustments[ $first ]; |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | // Make sure it is divisible by our expected indent. |
| 387 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 388 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 387 | + if ( $tokens[ $tokens[ $scopeCloser ][ 'scope_condition' ] ][ 'code' ] !== T_CLOSURE ) { |
|
| 388 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | - $setIndents[$first] = $currentIndent; |
|
| 391 | + $setIndents[ $first ] = $currentIndent; |
|
| 392 | 392 | |
| 393 | - if ($this->_debug === true) { |
|
| 394 | - $type = $tokens[$first]['type']; |
|
| 395 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 393 | + if ( $this->_debug === true ) { |
|
| 394 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 395 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 396 | 396 | } |
| 397 | 397 | }//end if |
| 398 | 398 | } else { |
| 399 | 399 | // Don't force current indent to divisible because there could be custom |
| 400 | 400 | // rules in place between parenthesis, such as with arrays. |
| 401 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 402 | - if (isset($adjustments[$first]) === true) { |
|
| 403 | - $currentIndent += $adjustments[$first]; |
|
| 401 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 402 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 403 | + $currentIndent += $adjustments[ $first ]; |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - $setIndents[$first] = $currentIndent; |
|
| 406 | + $setIndents[ $first ] = $currentIndent; |
|
| 407 | 407 | |
| 408 | - if ($this->_debug === true) { |
|
| 409 | - $type = $tokens[$first]['type']; |
|
| 410 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 408 | + if ( $this->_debug === true ) { |
|
| 409 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 410 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 411 | 411 | } |
| 412 | 412 | }//end if |
| 413 | 413 | }//end if |
| 414 | - } else if ($this->_debug === true) { |
|
| 415 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 414 | + } else if ( $this->_debug === true ) { |
|
| 415 | + echo "\t * ignoring single-line definition *" . PHP_EOL; |
|
| 416 | 416 | }//end if |
| 417 | 417 | }//end if |
| 418 | 418 | |
| 419 | 419 | // Closing short array bracket should just be indented to at least |
| 420 | 420 | // the same level as where it was opened (but can be more). |
| 421 | - if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 422 | - || ($checkToken !== null |
|
| 423 | - && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 421 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSE_SHORT_ARRAY |
|
| 422 | + || ( $checkToken !== null |
|
| 423 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_SHORT_ARRAY ) |
|
| 424 | 424 | ) { |
| 425 | - if ($checkToken !== null) { |
|
| 425 | + if ( $checkToken !== null ) { |
|
| 426 | 426 | $arrayCloser = $checkToken; |
| 427 | 427 | } else { |
| 428 | 428 | $arrayCloser = $i; |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - if ($this->_debug === true) { |
|
| 432 | - $line = $tokens[$arrayCloser]['line']; |
|
| 433 | - echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 431 | + if ( $this->_debug === true ) { |
|
| 432 | + $line = $tokens[ $arrayCloser ][ 'line' ]; |
|
| 433 | + echo "Closing short array bracket found on line $line" . PHP_EOL; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 437 | - if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 438 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 439 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 440 | - if (isset($adjustments[$first]) === true) { |
|
| 441 | - $checkIndent += $adjustments[$first]; |
|
| 436 | + $arrayOpener = $tokens[ $arrayCloser ][ 'bracket_opener' ]; |
|
| 437 | + if ( $tokens[ $arrayCloser ][ 'line' ] !== $tokens[ $arrayOpener ][ 'line' ] ) { |
|
| 438 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $arrayOpener, true ); |
|
| 439 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 440 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 441 | + $checkIndent += $adjustments[ $first ]; |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | $exact = false; |
| 445 | 445 | |
| 446 | - if ($this->_debug === true) { |
|
| 447 | - $line = $tokens[$first]['line']; |
|
| 448 | - $type = $tokens[$first]['type']; |
|
| 449 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 446 | + if ( $this->_debug === true ) { |
|
| 447 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 448 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 449 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 452 | + if ( $first === $tokens[ $arrayCloser ][ 'bracket_opener' ] ) { |
|
| 453 | 453 | // This is unlikely to be the start of the statement, so look |
| 454 | 454 | // back further to find it. |
| 455 | 455 | $first--; |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 459 | - if ($prev !== $first) { |
|
| 458 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 459 | + if ( $prev !== $first ) { |
|
| 460 | 460 | // This is not the start of the statement. |
| 461 | - if ($this->_debug === true) { |
|
| 462 | - $line = $tokens[$prev]['line']; |
|
| 463 | - $type = $tokens[$prev]['type']; |
|
| 464 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 461 | + if ( $this->_debug === true ) { |
|
| 462 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 463 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 464 | + echo "\t* previous is $type on line $line *" . PHP_EOL; |
|
| 465 | 465 | } |
| 466 | 466 | |
| 467 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 468 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 469 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 470 | - if ($this->_debug === true) { |
|
| 471 | - $line = $tokens[$first]['line']; |
|
| 472 | - $type = $tokens[$first]['type']; |
|
| 473 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 467 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 468 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 469 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 470 | + if ( $this->_debug === true ) { |
|
| 471 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 472 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 473 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 474 | 474 | } |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 478 | - && $tokens[$first]['scope_closer'] === $first |
|
| 477 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 478 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 479 | 479 | ) { |
| 480 | 480 | // The first token is a scope closer and would have already |
| 481 | 481 | // been processed and set the indent level correctly, so |
| 482 | 482 | // don't adjust it again. |
| 483 | - if ($this->_debug === true) { |
|
| 484 | - echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 483 | + if ( $this->_debug === true ) { |
|
| 484 | + echo "\t* first token is a scope closer; ignoring closing short array bracket *" . PHP_EOL; |
|
| 485 | 485 | } |
| 486 | 486 | |
| 487 | - if (isset($setIndents[$first]) === true) { |
|
| 488 | - $currentIndent = $setIndents[$first]; |
|
| 489 | - if ($this->_debug === true) { |
|
| 490 | - echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 487 | + if ( isset( $setIndents[ $first ] ) === true ) { |
|
| 488 | + $currentIndent = $setIndents[ $first ]; |
|
| 489 | + if ( $this->_debug === true ) { |
|
| 490 | + echo "\t=> indent reset to $currentIndent" . PHP_EOL; |
|
| 491 | 491 | } |
| 492 | 492 | } |
| 493 | 493 | } else { |
| 494 | 494 | // Don't force current indent to be divisible because there could be custom |
| 495 | 495 | // rules in place for arrays. |
| 496 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 497 | - if (isset($adjustments[$first]) === true) { |
|
| 498 | - $currentIndent += $adjustments[$first]; |
|
| 496 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 497 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 498 | + $currentIndent += $adjustments[ $first ]; |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | - $setIndents[$first] = $currentIndent; |
|
| 501 | + $setIndents[ $first ] = $currentIndent; |
|
| 502 | 502 | |
| 503 | - if ($this->_debug === true) { |
|
| 504 | - $type = $tokens[$first]['type']; |
|
| 505 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 503 | + if ( $this->_debug === true ) { |
|
| 504 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 505 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 506 | 506 | } |
| 507 | 507 | }//end if |
| 508 | - } else if ($this->_debug === true) { |
|
| 509 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 508 | + } else if ( $this->_debug === true ) { |
|
| 509 | + echo "\t * ignoring single-line definition *" . PHP_EOL; |
|
| 510 | 510 | }//end if |
| 511 | 511 | }//end if |
| 512 | 512 | |
| 513 | 513 | // Adjust lines within scopes while auto-fixing. |
| 514 | - if ($checkToken !== null |
|
| 514 | + if ( $checkToken !== null |
|
| 515 | 515 | && $exact === false |
| 516 | - && (empty($tokens[$checkToken]['conditions']) === false |
|
| 517 | - || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 518 | - && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 516 | + && ( empty( $tokens[ $checkToken ][ 'conditions' ] ) === false |
|
| 517 | + || ( isset( $tokens[ $checkToken ][ 'scope_opener' ] ) === true |
|
| 518 | + && $tokens[ $checkToken ][ 'scope_opener' ] === $checkToken ) ) |
|
| 519 | 519 | ) { |
| 520 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 521 | - end($tokens[$checkToken]['conditions']); |
|
| 522 | - $condition = key($tokens[$checkToken]['conditions']); |
|
| 520 | + if ( empty( $tokens[ $checkToken ][ 'conditions' ] ) === false ) { |
|
| 521 | + end( $tokens[ $checkToken ][ 'conditions' ] ); |
|
| 522 | + $condition = key( $tokens[ $checkToken ][ 'conditions' ] ); |
|
| 523 | 523 | } else { |
| 524 | - $condition = $tokens[$checkToken]['scope_condition']; |
|
| 524 | + $condition = $tokens[ $checkToken ][ 'scope_condition' ]; |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 527 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $condition, true ); |
|
| 528 | 528 | |
| 529 | - if (isset($adjustments[$first]) === true |
|
| 530 | - && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 531 | - || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 529 | + if ( isset( $adjustments[ $first ] ) === true |
|
| 530 | + && ( ( $adjustments[ $first ] < 0 && $tokenIndent > $currentIndent ) |
|
| 531 | + || ( $adjustments[ $first ] > 0 && $tokenIndent < $currentIndent ) ) |
|
| 532 | 532 | ) { |
| 533 | - $padding = ($tokenIndent + $adjustments[$first]); |
|
| 534 | - if ($padding > 0) { |
|
| 535 | - if ($this->tabIndent === true) { |
|
| 536 | - $numTabs = floor($padding / $this->_tabWidth); |
|
| 537 | - $numSpaces = ($padding - ($numTabs * $this->_tabWidth)); |
|
| 538 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 533 | + $padding = ( $tokenIndent + $adjustments[ $first ] ); |
|
| 534 | + if ( $padding > 0 ) { |
|
| 535 | + if ( $this->tabIndent === true ) { |
|
| 536 | + $numTabs = floor( $padding / $this->_tabWidth ); |
|
| 537 | + $numSpaces = ( $padding - ( $numTabs * $this->_tabWidth ) ); |
|
| 538 | + $padding = str_repeat( "\t", $numTabs ) . str_repeat( ' ', $numSpaces ); |
|
| 539 | 539 | } else { |
| 540 | - $padding = str_repeat(' ', $padding); |
|
| 540 | + $padding = str_repeat( ' ', $padding ); |
|
| 541 | 541 | } |
| 542 | 542 | } else { |
| 543 | 543 | $padding = ''; |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | - if ($checkToken === $i) { |
|
| 547 | - $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 546 | + if ( $checkToken === $i ) { |
|
| 547 | + $phpcsFile->fixer->replaceToken( $checkToken, $padding . $trimmed ); |
|
| 548 | 548 | } else { |
| 549 | 549 | // Easier to just replace the entire indent. |
| 550 | - $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 550 | + $phpcsFile->fixer->replaceToken( ( $checkToken - 1 ), $padding ); |
|
| 551 | 551 | } |
| 552 | 552 | |
| 553 | - if ($this->_debug === true) { |
|
| 554 | - $length = strlen($padding); |
|
| 555 | - $line = $tokens[$checkToken]['line']; |
|
| 556 | - $type = $tokens[$checkToken]['type']; |
|
| 557 | - echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 553 | + if ( $this->_debug === true ) { |
|
| 554 | + $length = strlen( $padding ); |
|
| 555 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 556 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 557 | + echo "Indent adjusted to $length for $type on line $line" . PHP_EOL; |
|
| 558 | 558 | } |
| 559 | 559 | |
| 560 | - $adjustments[$checkToken] = $adjustments[$first]; |
|
| 560 | + $adjustments[ $checkToken ] = $adjustments[ $first ]; |
|
| 561 | 561 | |
| 562 | - if ($this->_debug === true) { |
|
| 563 | - $line = $tokens[$checkToken]['line']; |
|
| 564 | - $type = $tokens[$checkToken]['type']; |
|
| 565 | - echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 562 | + if ( $this->_debug === true ) { |
|
| 563 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 564 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 565 | + echo "\t=> Add adjustment of " . $adjustments[ $checkToken ] . " for token $checkToken ($type) on line $line" . PHP_EOL; |
|
| 566 | 566 | } |
| 567 | 567 | }//end if |
| 568 | 568 | }//end if |
| 569 | 569 | |
| 570 | 570 | // Scope closers reset the required indent to the same level as the opening condition. |
| 571 | - if (($checkToken !== null |
|
| 572 | - && isset($openScopes[$checkToken]) === true |
|
| 573 | - || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 574 | - && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 575 | - && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 576 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 577 | - || ($checkToken === null |
|
| 578 | - && isset($openScopes[$i]) === true |
|
| 579 | - || (isset($tokens[$i]['scope_condition']) === true |
|
| 580 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 581 | - && $tokens[$i]['scope_closer'] === $i |
|
| 582 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 571 | + if ( ( $checkToken !== null |
|
| 572 | + && isset( $openScopes[ $checkToken ] ) === true |
|
| 573 | + || ( isset( $tokens[ $checkToken ][ 'scope_condition' ] ) === true |
|
| 574 | + && isset( $tokens[ $checkToken ][ 'scope_closer' ] ) === true |
|
| 575 | + && $tokens[ $checkToken ][ 'scope_closer' ] === $checkToken |
|
| 576 | + && $tokens[ $checkToken ][ 'line' ] !== $tokens[ $tokens[ $checkToken ][ 'scope_opener' ] ][ 'line' ] ) ) |
|
| 577 | + || ( $checkToken === null |
|
| 578 | + && isset( $openScopes[ $i ] ) === true |
|
| 579 | + || ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 580 | + && isset( $tokens[ $i ][ 'scope_closer' ] ) === true |
|
| 581 | + && $tokens[ $i ][ 'scope_closer' ] === $i |
|
| 582 | + && $tokens[ $i ][ 'line' ] !== $tokens[ $tokens[ $i ][ 'scope_opener' ] ][ 'line' ] ) ) |
|
| 583 | 583 | ) { |
| 584 | - if ($this->_debug === true) { |
|
| 585 | - if ($checkToken === null) { |
|
| 586 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 587 | - $line = $tokens[$i]['line']; |
|
| 584 | + if ( $this->_debug === true ) { |
|
| 585 | + if ( $checkToken === null ) { |
|
| 586 | + $type = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ]; |
|
| 587 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 588 | 588 | } else { |
| 589 | - $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 590 | - $line = $tokens[$checkToken]['line']; |
|
| 589 | + $type = $tokens[ $tokens[ $checkToken ][ 'scope_condition' ] ][ 'type' ]; |
|
| 590 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | - echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 593 | + echo "Close scope ($type) on line $line" . PHP_EOL; |
|
| 594 | 594 | } |
| 595 | 595 | |
| 596 | 596 | $scopeCloser = $checkToken; |
| 597 | - if ($scopeCloser === null) { |
|
| 597 | + if ( $scopeCloser === null ) { |
|
| 598 | 598 | $scopeCloser = $i; |
| 599 | 599 | } else { |
| 600 | - array_pop($openScopes); |
|
| 600 | + array_pop( $openScopes ); |
|
| 601 | 601 | } |
| 602 | 602 | |
| 603 | - if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 604 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 603 | + if ( isset( $tokens[ $scopeCloser ][ 'scope_condition' ] ) === true ) { |
|
| 604 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $tokens[ $scopeCloser ][ 'scope_condition' ], true ); |
|
| 605 | 605 | |
| 606 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 607 | - if (isset($adjustments[$first]) === true) { |
|
| 608 | - $currentIndent += $adjustments[$first]; |
|
| 606 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 607 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 608 | + $currentIndent += $adjustments[ $first ]; |
|
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | // Make sure it is divisible by our expected indent. |
| 612 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 613 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 612 | + if ( $tokens[ $tokens[ $scopeCloser ][ 'scope_condition' ] ][ 'code' ] !== T_CLOSURE ) { |
|
| 613 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 614 | 614 | } |
| 615 | 615 | |
| 616 | - $setIndents[$scopeCloser] = $currentIndent; |
|
| 616 | + $setIndents[ $scopeCloser ] = $currentIndent; |
|
| 617 | 617 | |
| 618 | - if ($this->_debug === true) { |
|
| 619 | - $type = $tokens[$scopeCloser]['type']; |
|
| 620 | - echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 618 | + if ( $this->_debug === true ) { |
|
| 619 | + $type = $tokens[ $scopeCloser ][ 'type' ]; |
|
| 620 | + echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)" . PHP_EOL; |
|
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | // We only check the indent of scope closers if they are |
| 624 | 624 | // curly braces because other constructs tend to have different rules. |
| 625 | - if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 625 | + if ( $tokens[ $scopeCloser ][ 'code' ] === T_CLOSE_CURLY_BRACKET ) { |
|
| 626 | 626 | $exact = true; |
| 627 | 627 | } else { |
| 628 | 628 | $checkToken = null; |
@@ -631,86 +631,86 @@ discard block |
||
| 631 | 631 | }//end if |
| 632 | 632 | |
| 633 | 633 | // Handle scope for JS object notation. |
| 634 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 635 | - && (($checkToken !== null |
|
| 636 | - && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 637 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 638 | - || ($checkToken === null |
|
| 639 | - && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 640 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 634 | + if ( $phpcsFile->tokenizerType === 'JS' |
|
| 635 | + && ( ( $checkToken !== null |
|
| 636 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_OBJECT |
|
| 637 | + && $tokens[ $checkToken ][ 'line' ] !== $tokens[ $tokens[ $checkToken ][ 'bracket_opener' ] ][ 'line' ] ) |
|
| 638 | + || ( $checkToken === null |
|
| 639 | + && $tokens[ $i ][ 'code' ] === T_CLOSE_OBJECT |
|
| 640 | + && $tokens[ $i ][ 'line' ] !== $tokens[ $tokens[ $i ][ 'bracket_opener' ] ][ 'line' ] ) ) |
|
| 641 | 641 | ) { |
| 642 | - if ($this->_debug === true) { |
|
| 643 | - $line = $tokens[$i]['line']; |
|
| 644 | - echo "Close JS object on line $line".PHP_EOL; |
|
| 642 | + if ( $this->_debug === true ) { |
|
| 643 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 644 | + echo "Close JS object on line $line" . PHP_EOL; |
|
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | $scopeCloser = $checkToken; |
| 648 | - if ($scopeCloser === null) { |
|
| 648 | + if ( $scopeCloser === null ) { |
|
| 649 | 649 | $scopeCloser = $i; |
| 650 | 650 | } else { |
| 651 | - array_pop($openScopes); |
|
| 651 | + array_pop( $openScopes ); |
|
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | $parens = 0; |
| 655 | - if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 656 | - && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 655 | + if ( isset( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ) === true |
|
| 656 | + && empty( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ) === false |
|
| 657 | 657 | ) { |
| 658 | - end($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 659 | - $parens = key($tokens[$scopeCloser]['nested_parenthesis']); |
|
| 660 | - if ($this->_debug === true) { |
|
| 661 | - $line = $tokens[$parens]['line']; |
|
| 662 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 658 | + end( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ); |
|
| 659 | + $parens = key( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ); |
|
| 660 | + if ( $this->_debug === true ) { |
|
| 661 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 662 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 663 | 663 | } |
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | $condition = 0; |
| 667 | - if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 668 | - && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 667 | + if ( isset( $tokens[ $scopeCloser ][ 'conditions' ] ) === true |
|
| 668 | + && empty( $tokens[ $scopeCloser ][ 'conditions' ] ) === false |
|
| 669 | 669 | ) { |
| 670 | - end($tokens[$scopeCloser]['conditions']); |
|
| 671 | - $condition = key($tokens[$scopeCloser]['conditions']); |
|
| 672 | - if ($this->_debug === true) { |
|
| 673 | - $line = $tokens[$condition]['line']; |
|
| 674 | - $type = $tokens[$condition]['type']; |
|
| 675 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 670 | + end( $tokens[ $scopeCloser ][ 'conditions' ] ); |
|
| 671 | + $condition = key( $tokens[ $scopeCloser ][ 'conditions' ] ); |
|
| 672 | + if ( $this->_debug === true ) { |
|
| 673 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 674 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 675 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 676 | 676 | } |
| 677 | 677 | } |
| 678 | 678 | |
| 679 | - if ($parens > $condition) { |
|
| 680 | - if ($this->_debug === true) { |
|
| 681 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 679 | + if ( $parens > $condition ) { |
|
| 680 | + if ( $this->_debug === true ) { |
|
| 681 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 682 | 682 | } |
| 683 | 683 | |
| 684 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 684 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $parens, true ); |
|
| 685 | 685 | $condition = 0; |
| 686 | - } else if ($condition > 0) { |
|
| 687 | - if ($this->_debug === true) { |
|
| 688 | - echo "\t* using condition *".PHP_EOL; |
|
| 686 | + } else if ( $condition > 0 ) { |
|
| 687 | + if ( $this->_debug === true ) { |
|
| 688 | + echo "\t* using condition *" . PHP_EOL; |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 691 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $condition, true ); |
|
| 692 | 692 | $parens = 0; |
| 693 | 693 | } else { |
| 694 | - if ($this->_debug === true) { |
|
| 695 | - $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 696 | - echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 694 | + if ( $this->_debug === true ) { |
|
| 695 | + $line = $tokens[ $tokens[ $scopeCloser ][ 'bracket_opener' ] ][ 'line' ]; |
|
| 696 | + echo "\t* token is not in parenthesis or condition; using opener on line $line *" . PHP_EOL; |
|
| 697 | 697 | } |
| 698 | 698 | |
| 699 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 699 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $tokens[ $scopeCloser ][ 'bracket_opener' ], true ); |
|
| 700 | 700 | }//end if |
| 701 | 701 | |
| 702 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 703 | - if (isset($adjustments[$first]) === true) { |
|
| 704 | - $currentIndent += $adjustments[$first]; |
|
| 702 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 703 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 704 | + $currentIndent += $adjustments[ $first ]; |
|
| 705 | 705 | } |
| 706 | 706 | |
| 707 | - if ($parens > 0 || $condition > 0) { |
|
| 708 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 709 | - if (isset($adjustments[$first]) === true) { |
|
| 710 | - $checkIndent += $adjustments[$first]; |
|
| 707 | + if ( $parens > 0 || $condition > 0 ) { |
|
| 708 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 709 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 710 | + $checkIndent += $adjustments[ $first ]; |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if ($condition > 0) { |
|
| 713 | + if ( $condition > 0 ) { |
|
| 714 | 714 | $checkIndent += $this->indent; |
| 715 | 715 | $currentIndent += $this->indent; |
| 716 | 716 | $exact = true; |
@@ -720,115 +720,115 @@ discard block |
||
| 720 | 720 | } |
| 721 | 721 | |
| 722 | 722 | // Make sure it is divisible by our expected indent. |
| 723 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 724 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 725 | - $setIndents[$first] = $currentIndent; |
|
| 723 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 724 | + $checkIndent = (int) ( ceil( $checkIndent / $this->indent ) * $this->indent ); |
|
| 725 | + $setIndents[ $first ] = $currentIndent; |
|
| 726 | 726 | |
| 727 | - if ($this->_debug === true) { |
|
| 728 | - $type = $tokens[$first]['type']; |
|
| 729 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 727 | + if ( $this->_debug === true ) { |
|
| 728 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 729 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 730 | 730 | } |
| 731 | 731 | }//end if |
| 732 | 732 | |
| 733 | - if ($checkToken !== null |
|
| 734 | - && isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 735 | - && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false |
|
| 736 | - && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 733 | + if ( $checkToken !== null |
|
| 734 | + && isset( PHP_CodeSniffer_Tokens::$scopeOpeners[ $tokens[ $checkToken ][ 'code' ] ] ) === true |
|
| 735 | + && in_array( $tokens[ $checkToken ][ 'code' ], $this->nonIndentingScopes ) === false |
|
| 736 | + && isset( $tokens[ $checkToken ][ 'scope_opener' ] ) === true |
|
| 737 | 737 | ) { |
| 738 | 738 | $exact = true; |
| 739 | 739 | |
| 740 | 740 | $lastOpener = null; |
| 741 | - if (empty($openScopes) === false) { |
|
| 742 | - end($openScopes); |
|
| 743 | - $lastOpener = current($openScopes); |
|
| 741 | + if ( empty( $openScopes ) === false ) { |
|
| 742 | + end( $openScopes ); |
|
| 743 | + $lastOpener = current( $openScopes ); |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | // A scope opener that shares a closer with another token (like multiple |
| 747 | 747 | // CASEs using the same BREAK) needs to reduce the indent level so its |
| 748 | 748 | // indent is checked correctly. It will then increase the indent again |
| 749 | 749 | // (as all openers do) after being checked. |
| 750 | - if ($lastOpener !== null |
|
| 751 | - && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 752 | - && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 753 | - && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 750 | + if ( $lastOpener !== null |
|
| 751 | + && isset( $tokens[ $lastOpener ][ 'scope_closer' ] ) === true |
|
| 752 | + && $tokens[ $lastOpener ][ 'level' ] === $tokens[ $checkToken ][ 'level' ] |
|
| 753 | + && $tokens[ $lastOpener ][ 'scope_closer' ] === $tokens[ $checkToken ][ 'scope_closer' ] |
|
| 754 | 754 | ) { |
| 755 | 755 | $currentIndent -= $this->indent; |
| 756 | - $setIndents[$lastOpener] = $currentIndent; |
|
| 757 | - if ($this->_debug === true) { |
|
| 758 | - $line = $tokens[$i]['line']; |
|
| 759 | - $type = $tokens[$lastOpener]['type']; |
|
| 760 | - echo "Shared closer found on line $line".PHP_EOL; |
|
| 761 | - echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 756 | + $setIndents[ $lastOpener ] = $currentIndent; |
|
| 757 | + if ( $this->_debug === true ) { |
|
| 758 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 759 | + $type = $tokens[ $lastOpener ][ 'type' ]; |
|
| 760 | + echo "Shared closer found on line $line" . PHP_EOL; |
|
| 761 | + echo "\t=> indent set to $currentIndent by token $lastOpener ($type)" . PHP_EOL; |
|
| 762 | 762 | } |
| 763 | 763 | } |
| 764 | 764 | |
| 765 | - if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 765 | + if ( $tokens[ $checkToken ][ 'code' ] === T_CLOSURE |
|
| 766 | 766 | && $tokenIndent > $currentIndent |
| 767 | 767 | ) { |
| 768 | 768 | // The opener is indented more than needed, which is fine. |
| 769 | 769 | // But just check that it is divisible by our expected indent. |
| 770 | - $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 770 | + $checkIndent = (int) ( ceil( $tokenIndent / $this->indent ) * $this->indent ); |
|
| 771 | 771 | $exact = false; |
| 772 | 772 | |
| 773 | - if ($this->_debug === true) { |
|
| 774 | - $line = $tokens[$i]['line']; |
|
| 775 | - echo "Closure found on line $line".PHP_EOL; |
|
| 776 | - echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 773 | + if ( $this->_debug === true ) { |
|
| 774 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 775 | + echo "Closure found on line $line" . PHP_EOL; |
|
| 776 | + echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent" . PHP_EOL; |
|
| 777 | 777 | } |
| 778 | 778 | } |
| 779 | 779 | }//end if |
| 780 | 780 | |
| 781 | 781 | // Method prefix indentation has to be exact or else if will break |
| 782 | 782 | // the rest of the function declaration, and potentially future ones. |
| 783 | - if ($checkToken !== null |
|
| 784 | - && isset(PHP_CodeSniffer_Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 785 | - && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 783 | + if ( $checkToken !== null |
|
| 784 | + && isset( PHP_CodeSniffer_Tokens::$methodPrefixes[ $tokens[ $checkToken ][ 'code' ] ] ) === true |
|
| 785 | + && $tokens[ ( $checkToken + 1 ) ][ 'code' ] !== T_DOUBLE_COLON |
|
| 786 | 786 | ) { |
| 787 | 787 | $exact = true; |
| 788 | 788 | } |
| 789 | 789 | |
| 790 | 790 | // JS property indentation has to be exact or else if will break |
| 791 | 791 | // things like function and object indentation. |
| 792 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 792 | + if ( $checkToken !== null && $tokens[ $checkToken ][ 'code' ] === T_PROPERTY ) { |
|
| 793 | 793 | $exact = true; |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | // PHP tags needs to be indented to exact column positions |
| 797 | 797 | // so they don't cause problems with indent checks for the code |
| 798 | 798 | // within them, but they don't need to line up with the current indent. |
| 799 | - if ($checkToken !== null |
|
| 800 | - && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 801 | - || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 802 | - || $tokens[$checkToken]['code'] === T_CLOSE_TAG) |
|
| 799 | + if ( $checkToken !== null |
|
| 800 | + && ( $tokens[ $checkToken ][ 'code' ] === T_OPEN_TAG |
|
| 801 | + || $tokens[ $checkToken ][ 'code' ] === T_OPEN_TAG_WITH_ECHO |
|
| 802 | + || $tokens[ $checkToken ][ 'code' ] === T_CLOSE_TAG ) |
|
| 803 | 803 | ) { |
| 804 | 804 | $exact = true; |
| 805 | - $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 806 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 805 | + $checkIndent = ( $tokens[ $checkToken ][ 'column' ] - 1 ); |
|
| 806 | + $checkIndent = (int) ( ceil( $checkIndent / $this->indent ) * $this->indent ); |
|
| 807 | 807 | } |
| 808 | 808 | |
| 809 | 809 | // Check the line indent. |
| 810 | - if ($checkIndent === null) { |
|
| 810 | + if ( $checkIndent === null ) { |
|
| 811 | 811 | $checkIndent = $currentIndent; |
| 812 | 812 | } |
| 813 | 813 | |
| 814 | 814 | $adjusted = false; |
| 815 | - if ($checkToken !== null |
|
| 816 | - && isset($this->_ignoreIndentationTokens[$tokens[$checkToken]['code']]) === false |
|
| 817 | - && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 818 | - || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 815 | + if ( $checkToken !== null |
|
| 816 | + && isset( $this->_ignoreIndentationTokens[ $tokens[ $checkToken ][ 'code' ] ] ) === false |
|
| 817 | + && ( ( $tokenIndent !== $checkIndent && $exact === true ) |
|
| 818 | + || ( $tokenIndent < $checkIndent && $exact === false ) ) |
|
| 819 | 819 | ) { |
| 820 | 820 | $type = 'IncorrectExact'; |
| 821 | 821 | $error = 'Line indented incorrectly; expected '; |
| 822 | - if ($exact === false) { |
|
| 822 | + if ( $exact === false ) { |
|
| 823 | 823 | $error .= 'at least '; |
| 824 | 824 | $type = 'Incorrect'; |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | - if ($this->tabIndent === true) { |
|
| 827 | + if ( $this->tabIndent === true ) { |
|
| 828 | 828 | $error .= '%s tabs, found %s'; |
| 829 | 829 | $data = array( |
| 830 | - floor($checkIndent / $this->_tabWidth), |
|
| 831 | - floor($tokenIndent / $this->_tabWidth), |
|
| 830 | + floor( $checkIndent / $this->_tabWidth ), |
|
| 831 | + floor( $tokenIndent / $this->_tabWidth ), |
|
| 832 | 832 | ); |
| 833 | 833 | } else { |
| 834 | 834 | $error .= '%s spaces, found %s'; |
@@ -838,107 +838,107 @@ discard block |
||
| 838 | 838 | ); |
| 839 | 839 | } |
| 840 | 840 | |
| 841 | - if ($this->_debug === true) { |
|
| 842 | - $line = $tokens[$checkToken]['line']; |
|
| 843 | - $message = vsprintf($error, $data); |
|
| 844 | - echo "[Line $line] $message".PHP_EOL; |
|
| 841 | + if ( $this->_debug === true ) { |
|
| 842 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 843 | + $message = vsprintf( $error, $data ); |
|
| 844 | + echo "[Line $line] $message" . PHP_EOL; |
|
| 845 | 845 | } |
| 846 | 846 | |
| 847 | - $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 848 | - if ($fix === true || $this->_debug === true) { |
|
| 847 | + $fix = $phpcsFile->addFixableError( $error, $checkToken, $type, $data ); |
|
| 848 | + if ( $fix === true || $this->_debug === true ) { |
|
| 849 | 849 | $padding = ''; |
| 850 | - if ($this->tabIndent === true) { |
|
| 851 | - $numTabs = floor($checkIndent / $this->_tabWidth); |
|
| 852 | - if ($numTabs > 0) { |
|
| 853 | - $numSpaces = ($checkIndent - ($numTabs * $this->_tabWidth)); |
|
| 854 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 850 | + if ( $this->tabIndent === true ) { |
|
| 851 | + $numTabs = floor( $checkIndent / $this->_tabWidth ); |
|
| 852 | + if ( $numTabs > 0 ) { |
|
| 853 | + $numSpaces = ( $checkIndent - ( $numTabs * $this->_tabWidth ) ); |
|
| 854 | + $padding = str_repeat( "\t", $numTabs ) . str_repeat( ' ', $numSpaces ); |
|
| 855 | 855 | } |
| 856 | - } else if ($checkIndent > 0) { |
|
| 857 | - $padding = str_repeat(' ', $checkIndent); |
|
| 856 | + } else if ( $checkIndent > 0 ) { |
|
| 857 | + $padding = str_repeat( ' ', $checkIndent ); |
|
| 858 | 858 | } |
| 859 | 859 | |
| 860 | - if ($checkToken === $i) { |
|
| 861 | - $accepted = $phpcsFile->fixer->replaceToken($checkToken, $padding.$trimmed); |
|
| 860 | + if ( $checkToken === $i ) { |
|
| 861 | + $accepted = $phpcsFile->fixer->replaceToken( $checkToken, $padding . $trimmed ); |
|
| 862 | 862 | } else { |
| 863 | 863 | // Easier to just replace the entire indent. |
| 864 | - $accepted = $phpcsFile->fixer->replaceToken(($checkToken - 1), $padding); |
|
| 864 | + $accepted = $phpcsFile->fixer->replaceToken( ( $checkToken - 1 ), $padding ); |
|
| 865 | 865 | } |
| 866 | 866 | |
| 867 | - if ($accepted === true) { |
|
| 868 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 869 | - if ($this->_debug === true) { |
|
| 870 | - $line = $tokens[$checkToken]['line']; |
|
| 871 | - $type = $tokens[$checkToken]['type']; |
|
| 872 | - echo "\t=> Add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 867 | + if ( $accepted === true ) { |
|
| 868 | + $adjustments[ $checkToken ] = ( $checkIndent - $tokenIndent ); |
|
| 869 | + if ( $this->_debug === true ) { |
|
| 870 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 871 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 872 | + echo "\t=> Add adjustment of " . $adjustments[ $checkToken ] . " for token $checkToken ($type) on line $line" . PHP_EOL; |
|
| 873 | 873 | } |
| 874 | 874 | } |
| 875 | 875 | } else { |
| 876 | 876 | // Assume the change would be applied and continue |
| 877 | 877 | // checking indents under this assumption. This gives more |
| 878 | 878 | // technically accurate error messages. |
| 879 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 879 | + $adjustments[ $checkToken ] = ( $checkIndent - $tokenIndent ); |
|
| 880 | 880 | }//end if |
| 881 | 881 | }//end if |
| 882 | 882 | |
| 883 | - if ($checkToken !== null) { |
|
| 883 | + if ( $checkToken !== null ) { |
|
| 884 | 884 | $i = $checkToken; |
| 885 | 885 | } |
| 886 | 886 | |
| 887 | 887 | // Completely skip here/now docs as the indent is a part of the |
| 888 | 888 | // content itself. |
| 889 | - if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 890 | - || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 889 | + if ( $tokens[ $i ][ 'code' ] === T_START_HEREDOC |
|
| 890 | + || $tokens[ $i ][ 'code' ] === T_START_NOWDOC |
|
| 891 | 891 | ) { |
| 892 | - $i = $phpcsFile->findNext(array(T_END_HEREDOC, T_END_NOWDOC), ($i + 1)); |
|
| 892 | + $i = $phpcsFile->findNext( array( T_END_HEREDOC, T_END_NOWDOC ), ( $i + 1 ) ); |
|
| 893 | 893 | continue; |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | // Completely skip multi-line strings as the indent is a part of the |
| 897 | 897 | // content itself. |
| 898 | - if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 899 | - || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 898 | + if ( $tokens[ $i ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 899 | + || $tokens[ $i ][ 'code' ] === T_DOUBLE_QUOTED_STRING |
|
| 900 | 900 | ) { |
| 901 | - $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 901 | + $i = $phpcsFile->findNext( $tokens[ $i ][ 'code' ], ( $i + 1 ), null, true ); |
|
| 902 | 902 | $i--; |
| 903 | 903 | continue; |
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | // Completely skip doc comments as they tend to have complex |
| 907 | 907 | // indentation rules. |
| 908 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 909 | - $i = $tokens[$i]['comment_closer']; |
|
| 908 | + if ( $tokens[ $i ][ 'code' ] === T_DOC_COMMENT_OPEN_TAG ) { |
|
| 909 | + $i = $tokens[ $i ][ 'comment_closer' ]; |
|
| 910 | 910 | continue; |
| 911 | 911 | } |
| 912 | 912 | |
| 913 | 913 | // Open tags reset the indent level. |
| 914 | - if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 915 | - || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 914 | + if ( $tokens[ $i ][ 'code' ] === T_OPEN_TAG |
|
| 915 | + || $tokens[ $i ][ 'code' ] === T_OPEN_TAG_WITH_ECHO |
|
| 916 | 916 | ) { |
| 917 | - if ($this->_debug === true) { |
|
| 918 | - $line = $tokens[$i]['line']; |
|
| 919 | - echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 917 | + if ( $this->_debug === true ) { |
|
| 918 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 919 | + echo "Open PHP tag found on line $line" . PHP_EOL; |
|
| 920 | 920 | } |
| 921 | 921 | |
| 922 | - if ($checkToken === null) { |
|
| 923 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 924 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 922 | + if ( $checkToken === null ) { |
|
| 923 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 924 | + $currentIndent = ( strlen( $tokens[ $first ][ 'content' ] ) - strlen( ltrim( $tokens[ $first ][ 'content' ] ) ) ); |
|
| 925 | 925 | } else { |
| 926 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 926 | + $currentIndent = ( $tokens[ $i ][ 'column' ] - 1 ); |
|
| 927 | 927 | } |
| 928 | 928 | |
| 929 | 929 | $lastOpenTag = $i; |
| 930 | 930 | |
| 931 | - if (isset($adjustments[$i]) === true) { |
|
| 932 | - $currentIndent += $adjustments[$i]; |
|
| 931 | + if ( isset( $adjustments[ $i ] ) === true ) { |
|
| 932 | + $currentIndent += $adjustments[ $i ]; |
|
| 933 | 933 | } |
| 934 | 934 | |
| 935 | 935 | // Make sure it is divisible by our expected indent. |
| 936 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 937 | - $setIndents[$i] = $currentIndent; |
|
| 936 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 937 | + $setIndents[ $i ] = $currentIndent; |
|
| 938 | 938 | |
| 939 | - if ($this->_debug === true) { |
|
| 940 | - $type = $tokens[$i]['type']; |
|
| 941 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 939 | + if ( $this->_debug === true ) { |
|
| 940 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 941 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | continue; |
@@ -946,113 +946,113 @@ discard block |
||
| 946 | 946 | |
| 947 | 947 | // Close tags reset the indent level, unless they are closing a tag |
| 948 | 948 | // opened on the same line. |
| 949 | - if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 950 | - if ($this->_debug === true) { |
|
| 951 | - $line = $tokens[$i]['line']; |
|
| 952 | - echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 949 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSE_TAG ) { |
|
| 950 | + if ( $this->_debug === true ) { |
|
| 951 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 952 | + echo "Close PHP tag found on line $line" . PHP_EOL; |
|
| 953 | 953 | } |
| 954 | 954 | |
| 955 | - if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 956 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 955 | + if ( $tokens[ $lastOpenTag ][ 'line' ] !== $tokens[ $i ][ 'line' ] ) { |
|
| 956 | + $currentIndent = ( $tokens[ $i ][ 'column' ] - 1 ); |
|
| 957 | 957 | $lastCloseTag = $i; |
| 958 | 958 | } else { |
| 959 | - if ($lastCloseTag === null) { |
|
| 959 | + if ( $lastCloseTag === null ) { |
|
| 960 | 960 | $currentIndent = 0; |
| 961 | 961 | } else { |
| 962 | - $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 962 | + $currentIndent = ( $tokens[ $lastCloseTag ][ 'column' ] - 1 ); |
|
| 963 | 963 | } |
| 964 | 964 | } |
| 965 | 965 | |
| 966 | - if (isset($adjustments[$i]) === true) { |
|
| 967 | - $currentIndent += $adjustments[$i]; |
|
| 966 | + if ( isset( $adjustments[ $i ] ) === true ) { |
|
| 967 | + $currentIndent += $adjustments[ $i ]; |
|
| 968 | 968 | } |
| 969 | 969 | |
| 970 | 970 | // Make sure it is divisible by our expected indent. |
| 971 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 972 | - $setIndents[$i] = $currentIndent; |
|
| 971 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 972 | + $setIndents[ $i ] = $currentIndent; |
|
| 973 | 973 | |
| 974 | - if ($this->_debug === true) { |
|
| 975 | - $type = $tokens[$i]['type']; |
|
| 976 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 974 | + if ( $this->_debug === true ) { |
|
| 975 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 976 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | continue; |
| 980 | 980 | }//end if |
| 981 | 981 | |
| 982 | 982 | // Anon classes and functions set the indent based on their own indent level. |
| 983 | - if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 984 | - $closer = $tokens[$i]['scope_closer']; |
|
| 985 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 986 | - if ($this->_debug === true) { |
|
| 987 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 988 | - $line = $tokens[$i]['line']; |
|
| 989 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 983 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSURE || $tokens[ $i ][ 'code' ] === T_ANON_CLASS ) { |
|
| 984 | + $closer = $tokens[ $i ][ 'scope_closer' ]; |
|
| 985 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 986 | + if ( $this->_debug === true ) { |
|
| 987 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $i ][ 'type' ], 2 ) ) ); |
|
| 988 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 989 | + echo "* ignoring single-line $type on line $line" . PHP_EOL; |
|
| 990 | 990 | } |
| 991 | 991 | |
| 992 | 992 | $i = $closer; |
| 993 | 993 | continue; |
| 994 | 994 | } |
| 995 | 995 | |
| 996 | - if ($this->_debug === true) { |
|
| 997 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 998 | - $line = $tokens[$i]['line']; |
|
| 999 | - echo "Open $type on line $line".PHP_EOL; |
|
| 996 | + if ( $this->_debug === true ) { |
|
| 997 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $i ][ 'type' ], 2 ) ) ); |
|
| 998 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 999 | + echo "Open $type on line $line" . PHP_EOL; |
|
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1003 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1002 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 1003 | + $currentIndent = ( ( $tokens[ $first ][ 'column' ] - 1 ) + $this->indent ); |
|
| 1004 | 1004 | |
| 1005 | - if (isset($adjustments[$first]) === true) { |
|
| 1006 | - $currentIndent += $adjustments[$first]; |
|
| 1005 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 1006 | + $currentIndent += $adjustments[ $first ]; |
|
| 1007 | 1007 | } |
| 1008 | 1008 | |
| 1009 | 1009 | // Make sure it is divisible by our expected indent. |
| 1010 | - $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1011 | - $i = $tokens[$i]['scope_opener']; |
|
| 1012 | - $setIndents[$i] = $currentIndent; |
|
| 1010 | + $currentIndent = (int) ( floor( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1011 | + $i = $tokens[ $i ][ 'scope_opener' ]; |
|
| 1012 | + $setIndents[ $i ] = $currentIndent; |
|
| 1013 | 1013 | |
| 1014 | - if ($this->_debug === true) { |
|
| 1015 | - $type = $tokens[$i]['type']; |
|
| 1016 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1014 | + if ( $this->_debug === true ) { |
|
| 1015 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1016 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1017 | 1017 | } |
| 1018 | 1018 | |
| 1019 | 1019 | continue; |
| 1020 | 1020 | }//end if |
| 1021 | 1021 | |
| 1022 | 1022 | // Scope openers increase the indent level. |
| 1023 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1024 | - && isset($tokens[$i]['scope_opener']) === true |
|
| 1025 | - && $tokens[$i]['scope_opener'] === $i |
|
| 1023 | + if ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 1024 | + && isset( $tokens[ $i ][ 'scope_opener' ] ) === true |
|
| 1025 | + && $tokens[ $i ][ 'scope_opener' ] === $i |
|
| 1026 | 1026 | ) { |
| 1027 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1028 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1029 | - if ($this->_debug === true) { |
|
| 1030 | - $line = $tokens[$i]['line']; |
|
| 1031 | - $type = $tokens[$i]['type']; |
|
| 1032 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1027 | + $closer = $tokens[ $i ][ 'scope_closer' ]; |
|
| 1028 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 1029 | + if ( $this->_debug === true ) { |
|
| 1030 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1031 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1032 | + echo "* ignoring single-line $type on line $line" . PHP_EOL; |
|
| 1033 | 1033 | } |
| 1034 | 1034 | |
| 1035 | 1035 | $i = $closer; |
| 1036 | 1036 | continue; |
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | - $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1040 | - if (isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$condition]) === true |
|
| 1041 | - && in_array($condition, $this->nonIndentingScopes) === false |
|
| 1039 | + $condition = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ]; |
|
| 1040 | + if ( isset( PHP_CodeSniffer_Tokens::$scopeOpeners[ $condition ] ) === true |
|
| 1041 | + && in_array( $condition, $this->nonIndentingScopes ) === false |
|
| 1042 | 1042 | ) { |
| 1043 | - if ($this->_debug === true) { |
|
| 1044 | - $line = $tokens[$i]['line']; |
|
| 1045 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1046 | - echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1043 | + if ( $this->_debug === true ) { |
|
| 1044 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1045 | + $type = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ]; |
|
| 1046 | + echo "Open scope ($type) on line $line" . PHP_EOL; |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | 1049 | $currentIndent += $this->indent; |
| 1050 | - $setIndents[$i] = $currentIndent; |
|
| 1051 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1050 | + $setIndents[ $i ] = $currentIndent; |
|
| 1051 | + $openScopes[ $tokens[ $i ][ 'scope_closer' ] ] = $tokens[ $i ][ 'scope_condition' ]; |
|
| 1052 | 1052 | |
| 1053 | - if ($this->_debug === true) { |
|
| 1054 | - $type = $tokens[$i]['type']; |
|
| 1055 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1053 | + if ( $this->_debug === true ) { |
|
| 1054 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1055 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1056 | 1056 | } |
| 1057 | 1057 | |
| 1058 | 1058 | continue; |
@@ -1060,118 +1060,118 @@ discard block |
||
| 1060 | 1060 | }//end if |
| 1061 | 1061 | |
| 1062 | 1062 | // JS objects set the indent level. |
| 1063 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 1064 | - && $tokens[$i]['code'] === T_OBJECT |
|
| 1063 | + if ( $phpcsFile->tokenizerType === 'JS' |
|
| 1064 | + && $tokens[ $i ][ 'code' ] === T_OBJECT |
|
| 1065 | 1065 | ) { |
| 1066 | - $closer = $tokens[$i]['bracket_closer']; |
|
| 1067 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1068 | - if ($this->_debug === true) { |
|
| 1069 | - $line = $tokens[$i]['line']; |
|
| 1070 | - echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1066 | + $closer = $tokens[ $i ][ 'bracket_closer' ]; |
|
| 1067 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 1068 | + if ( $this->_debug === true ) { |
|
| 1069 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1070 | + echo "* ignoring single-line JS object on line $line" . PHP_EOL; |
|
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | $i = $closer; |
| 1074 | 1074 | continue; |
| 1075 | 1075 | } |
| 1076 | 1076 | |
| 1077 | - if ($this->_debug === true) { |
|
| 1078 | - $line = $tokens[$i]['line']; |
|
| 1079 | - echo "Open JS object on line $line".PHP_EOL; |
|
| 1077 | + if ( $this->_debug === true ) { |
|
| 1078 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1079 | + echo "Open JS object on line $line" . PHP_EOL; |
|
| 1080 | 1080 | } |
| 1081 | 1081 | |
| 1082 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1083 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1084 | - if (isset($adjustments[$first]) === true) { |
|
| 1085 | - $currentIndent += $adjustments[$first]; |
|
| 1082 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 1083 | + $currentIndent = ( ( $tokens[ $first ][ 'column' ] - 1 ) + $this->indent ); |
|
| 1084 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 1085 | + $currentIndent += $adjustments[ $first ]; |
|
| 1086 | 1086 | } |
| 1087 | 1087 | |
| 1088 | 1088 | // Make sure it is divisible by our expected indent. |
| 1089 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1090 | - $setIndents[$first] = $currentIndent; |
|
| 1089 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1090 | + $setIndents[ $first ] = $currentIndent; |
|
| 1091 | 1091 | |
| 1092 | - if ($this->_debug === true) { |
|
| 1093 | - $type = $tokens[$first]['type']; |
|
| 1094 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1092 | + if ( $this->_debug === true ) { |
|
| 1093 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1094 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 1095 | 1095 | } |
| 1096 | 1096 | |
| 1097 | 1097 | continue; |
| 1098 | 1098 | }//end if |
| 1099 | 1099 | |
| 1100 | 1100 | // Closing an anon class or function. |
| 1101 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1102 | - && $tokens[$i]['scope_closer'] === $i |
|
| 1103 | - && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1104 | - || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1101 | + if ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 1102 | + && $tokens[ $i ][ 'scope_closer' ] === $i |
|
| 1103 | + && ( $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ] === T_CLOSURE |
|
| 1104 | + || $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ] === T_ANON_CLASS ) |
|
| 1105 | 1105 | ) { |
| 1106 | - if ($this->_debug === true) { |
|
| 1107 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1108 | - $line = $tokens[$i]['line']; |
|
| 1109 | - echo "Close $type on line $line".PHP_EOL; |
|
| 1106 | + if ( $this->_debug === true ) { |
|
| 1107 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ], 2 ) ) ); |
|
| 1108 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1109 | + echo "Close $type on line $line" . PHP_EOL; |
|
| 1110 | 1110 | } |
| 1111 | 1111 | |
| 1112 | 1112 | $prev = false; |
| 1113 | 1113 | |
| 1114 | 1114 | $object = 0; |
| 1115 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1116 | - $conditions = $tokens[$i]['conditions']; |
|
| 1117 | - krsort($conditions, SORT_NUMERIC); |
|
| 1118 | - foreach ($conditions as $token => $condition) { |
|
| 1119 | - if ($condition === T_OBJECT) { |
|
| 1115 | + if ( $phpcsFile->tokenizerType === 'JS' ) { |
|
| 1116 | + $conditions = $tokens[ $i ][ 'conditions' ]; |
|
| 1117 | + krsort( $conditions, SORT_NUMERIC ); |
|
| 1118 | + foreach ( $conditions as $token => $condition ) { |
|
| 1119 | + if ( $condition === T_OBJECT ) { |
|
| 1120 | 1120 | $object = $token; |
| 1121 | 1121 | break; |
| 1122 | 1122 | } |
| 1123 | 1123 | } |
| 1124 | 1124 | |
| 1125 | - if ($this->_debug === true && $object !== 0) { |
|
| 1126 | - $line = $tokens[$object]['line']; |
|
| 1127 | - echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1125 | + if ( $this->_debug === true && $object !== 0 ) { |
|
| 1126 | + $line = $tokens[ $object ][ 'line' ]; |
|
| 1127 | + echo "\t* token is inside JS object $object on line $line *" . PHP_EOL; |
|
| 1128 | 1128 | } |
| 1129 | 1129 | } |
| 1130 | 1130 | |
| 1131 | 1131 | $parens = 0; |
| 1132 | - if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1133 | - && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1132 | + if ( isset( $tokens[ $i ][ 'nested_parenthesis' ] ) === true |
|
| 1133 | + && empty( $tokens[ $i ][ 'nested_parenthesis' ] ) === false |
|
| 1134 | 1134 | ) { |
| 1135 | - end($tokens[$i]['nested_parenthesis']); |
|
| 1136 | - $parens = key($tokens[$i]['nested_parenthesis']); |
|
| 1137 | - if ($this->_debug === true) { |
|
| 1138 | - $line = $tokens[$parens]['line']; |
|
| 1139 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1135 | + end( $tokens[ $i ][ 'nested_parenthesis' ] ); |
|
| 1136 | + $parens = key( $tokens[ $i ][ 'nested_parenthesis' ] ); |
|
| 1137 | + if ( $this->_debug === true ) { |
|
| 1138 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 1139 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 1140 | 1140 | } |
| 1141 | 1141 | } |
| 1142 | 1142 | |
| 1143 | 1143 | $condition = 0; |
| 1144 | - if (isset($tokens[$i]['conditions']) === true |
|
| 1145 | - && empty($tokens[$i]['conditions']) === false |
|
| 1144 | + if ( isset( $tokens[ $i ][ 'conditions' ] ) === true |
|
| 1145 | + && empty( $tokens[ $i ][ 'conditions' ] ) === false |
|
| 1146 | 1146 | ) { |
| 1147 | - end($tokens[$i]['conditions']); |
|
| 1148 | - $condition = key($tokens[$i]['conditions']); |
|
| 1149 | - if ($this->_debug === true) { |
|
| 1150 | - $line = $tokens[$condition]['line']; |
|
| 1151 | - $type = $tokens[$condition]['type']; |
|
| 1152 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1147 | + end( $tokens[ $i ][ 'conditions' ] ); |
|
| 1148 | + $condition = key( $tokens[ $i ][ 'conditions' ] ); |
|
| 1149 | + if ( $this->_debug === true ) { |
|
| 1150 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 1151 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 1152 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 1153 | 1153 | } |
| 1154 | 1154 | } |
| 1155 | 1155 | |
| 1156 | - if ($parens > $object && $parens > $condition) { |
|
| 1157 | - if ($this->_debug === true) { |
|
| 1158 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 1156 | + if ( $parens > $object && $parens > $condition ) { |
|
| 1157 | + if ( $this->_debug === true ) { |
|
| 1158 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 1159 | 1159 | } |
| 1160 | 1160 | |
| 1161 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1161 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $parens - 1 ), null, true ); |
|
| 1162 | 1162 | $object = 0; |
| 1163 | 1163 | $condition = 0; |
| 1164 | - } else if ($object > 0 && $object >= $condition) { |
|
| 1165 | - if ($this->_debug === true) { |
|
| 1166 | - echo "\t* using object *".PHP_EOL; |
|
| 1164 | + } else if ( $object > 0 && $object >= $condition ) { |
|
| 1165 | + if ( $this->_debug === true ) { |
|
| 1166 | + echo "\t* using object *" . PHP_EOL; |
|
| 1167 | 1167 | } |
| 1168 | 1168 | |
| 1169 | 1169 | $prev = $object; |
| 1170 | 1170 | $parens = 0; |
| 1171 | 1171 | $condition = 0; |
| 1172 | - } else if ($condition > 0) { |
|
| 1173 | - if ($this->_debug === true) { |
|
| 1174 | - echo "\t* using condition *".PHP_EOL; |
|
| 1172 | + } else if ( $condition > 0 ) { |
|
| 1173 | + if ( $this->_debug === true ) { |
|
| 1174 | + echo "\t* using condition *" . PHP_EOL; |
|
| 1175 | 1175 | } |
| 1176 | 1176 | |
| 1177 | 1177 | $prev = $condition; |
@@ -1179,72 +1179,72 @@ discard block |
||
| 1179 | 1179 | $parens = 0; |
| 1180 | 1180 | }//end if |
| 1181 | 1181 | |
| 1182 | - if ($prev === false) { |
|
| 1183 | - $prev = $phpcsFile->findPrevious(array(T_EQUAL, T_RETURN), ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1184 | - if ($prev === false) { |
|
| 1182 | + if ( $prev === false ) { |
|
| 1183 | + $prev = $phpcsFile->findPrevious( array( T_EQUAL, T_RETURN ), ( $tokens[ $i ][ 'scope_condition' ] - 1 ), null, false, null, true ); |
|
| 1184 | + if ( $prev === false ) { |
|
| 1185 | 1185 | $prev = $i; |
| 1186 | - if ($this->_debug === true) { |
|
| 1187 | - echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1186 | + if ( $this->_debug === true ) { |
|
| 1187 | + echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *" . PHP_EOL; |
|
| 1188 | 1188 | } |
| 1189 | 1189 | } |
| 1190 | 1190 | } |
| 1191 | 1191 | |
| 1192 | - if ($this->_debug === true) { |
|
| 1193 | - $line = $tokens[$prev]['line']; |
|
| 1194 | - $type = $tokens[$prev]['type']; |
|
| 1195 | - echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1192 | + if ( $this->_debug === true ) { |
|
| 1193 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 1194 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 1195 | + echo "\t* previous token is $type on line $line *" . PHP_EOL; |
|
| 1196 | 1196 | } |
| 1197 | 1197 | |
| 1198 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1199 | - if ($this->_debug === true) { |
|
| 1200 | - $line = $tokens[$first]['line']; |
|
| 1201 | - $type = $tokens[$first]['type']; |
|
| 1202 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1198 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 1199 | + if ( $this->_debug === true ) { |
|
| 1200 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1201 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1202 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 1203 | 1203 | } |
| 1204 | 1204 | |
| 1205 | - $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1206 | - if ($prev !== $first) { |
|
| 1205 | + $prev = $phpcsFile->findStartOfStatement( $first ); |
|
| 1206 | + if ( $prev !== $first ) { |
|
| 1207 | 1207 | // This is not the start of the statement. |
| 1208 | - if ($this->_debug === true) { |
|
| 1209 | - $line = $tokens[$prev]['line']; |
|
| 1210 | - $type = $tokens[$prev]['type']; |
|
| 1211 | - echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1208 | + if ( $this->_debug === true ) { |
|
| 1209 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 1210 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 1211 | + echo "\t* amended previous is $type on line $line *" . PHP_EOL; |
|
| 1212 | 1212 | } |
| 1213 | 1213 | |
| 1214 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1215 | - if ($this->_debug === true) { |
|
| 1216 | - $line = $tokens[$first]['line']; |
|
| 1217 | - $type = $tokens[$first]['type']; |
|
| 1218 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1214 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 1215 | + if ( $this->_debug === true ) { |
|
| 1216 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1217 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1218 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 1219 | 1219 | } |
| 1220 | 1220 | } |
| 1221 | 1221 | |
| 1222 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1223 | - if ($object > 0 || $condition > 0) { |
|
| 1222 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 1223 | + if ( $object > 0 || $condition > 0 ) { |
|
| 1224 | 1224 | $currentIndent += $this->indent; |
| 1225 | 1225 | } |
| 1226 | 1226 | |
| 1227 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 1228 | - && $tokens[$first]['scope_closer'] === $first |
|
| 1227 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 1228 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 1229 | 1229 | ) { |
| 1230 | - if ($this->_debug === true) { |
|
| 1231 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1230 | + if ( $this->_debug === true ) { |
|
| 1231 | + echo "\t* first token is a scope closer *" . PHP_EOL; |
|
| 1232 | 1232 | } |
| 1233 | 1233 | |
| 1234 | - if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1235 | - $currentIndent = $setIndents[$first]; |
|
| 1236 | - } else if ($this->_debug === true) { |
|
| 1237 | - echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1234 | + if ( $condition === 0 || $tokens[ $condition ][ 'scope_opener' ] < $first ) { |
|
| 1235 | + $currentIndent = $setIndents[ $first ]; |
|
| 1236 | + } else if ( $this->_debug === true ) { |
|
| 1237 | + echo "\t* ignoring scope closer *" . PHP_EOL; |
|
| 1238 | 1238 | } |
| 1239 | 1239 | } |
| 1240 | 1240 | |
| 1241 | 1241 | // Make sure it is divisible by our expected indent. |
| 1242 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1243 | - $setIndents[$first] = $currentIndent; |
|
| 1242 | + $currentIndent = (int) ( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1243 | + $setIndents[ $first ] = $currentIndent; |
|
| 1244 | 1244 | |
| 1245 | - if ($this->_debug === true) { |
|
| 1246 | - $type = $tokens[$first]['type']; |
|
| 1247 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1245 | + if ( $this->_debug === true ) { |
|
| 1246 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1247 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 1248 | 1248 | } |
| 1249 | 1249 | }//end if |
| 1250 | 1250 | }//end for |
@@ -29,8 +29,7 @@ discard block |
||
| 29 | 29 | * @version Release: @package_version@ |
| 30 | 30 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 31 | 31 | */ |
| 32 | -class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff |
|
| 33 | -{ |
|
| 32 | +class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff { |
|
| 34 | 33 | |
| 35 | 34 | /** |
| 36 | 35 | * A list of tokenizers this sniff supports. |
@@ -119,8 +118,7 @@ discard block |
||
| 119 | 118 | * |
| 120 | 119 | * @return array |
| 121 | 120 | */ |
| 122 | - public function register() |
|
| 123 | - { |
|
| 121 | + public function register() { |
|
| 124 | 122 | if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
| 125 | 123 | $this->_debug = false; |
| 126 | 124 | } |
@@ -139,8 +137,7 @@ discard block |
||
| 139 | 137 | * |
| 140 | 138 | * @return void |
| 141 | 139 | */ |
| 142 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 143 | - { |
|
| 140 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 144 | 141 | $debug = PHP_CodeSniffer::getConfigData('scope_indent_debug'); |
| 145 | 142 | if ($debug !== null) { |
| 146 | 143 | $this->_debug = (bool) $debug; |
@@ -30,7 +30,7 @@ |
||
| 30 | 30 | /** |
| 31 | 31 | * Returns an array of tokens this test wants to listen for. |
| 32 | 32 | * |
| 33 | - * @return array |
|
| 33 | + * @return integer[] |
|
| 34 | 34 | */ |
| 35 | 35 | public function register() |
| 36 | 36 | { |
@@ -27,109 +27,109 @@ |
||
| 27 | 27 | { |
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns an array of tokens this test wants to listen for. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function register() |
|
| 36 | - { |
|
| 37 | - return array(T_CLASS); |
|
| 38 | - |
|
| 39 | - }//end register() |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | - * |
|
| 45 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 46 | - * @param int $stackPtr The position of the current token in |
|
| 47 | - * the stack passed in $tokens. |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 53 | - $tokens = $phpcsFile->getTokens(); |
|
| 54 | - |
|
| 55 | - // We are not interested in abstract classes. |
|
| 56 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 57 | - if ($prev !== false && $tokens[$prev]['code'] === T_ABSTRACT) { |
|
| 58 | - return; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // We are only interested in Action classes. |
|
| 62 | - $classNameToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 63 | - $className = $tokens[$classNameToken]['content']; |
|
| 64 | - if (substr($className, -7) !== 'Actions') { |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $foundFunctions = array(); |
|
| 69 | - $foundCalls = array(); |
|
| 70 | - |
|
| 71 | - // Find all static method calls in the form self::method() in the class. |
|
| 72 | - $classEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 73 | - for ($i = ($classNameToken + 1); $i < $classEnd; $i++) { |
|
| 74 | - if ($tokens[$i]['code'] !== T_DOUBLE_COLON) { |
|
| 75 | - if ($tokens[$i]['code'] === T_FUNCTION) { |
|
| 76 | - // Cache the function information. |
|
| 77 | - $funcName = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 78 | - $funcScope = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, ($i - 1)); |
|
| 79 | - |
|
| 80 | - $foundFunctions[$tokens[$funcName]['content']] = strtolower($tokens[$funcScope]['content']); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - continue; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 87 | - if ($tokens[$prevToken]['content'] !== 'self' |
|
| 88 | - && $tokens[$prevToken]['content'] !== 'static' |
|
| 89 | - ) { |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $funcNameToken = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 94 | - if ($tokens[$funcNameToken]['code'] === T_VARIABLE) { |
|
| 95 | - // We are only interested in function calls. |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $funcName = $tokens[$funcNameToken]['content']; |
|
| 100 | - |
|
| 101 | - // We've found the function, now we need to find it and see if it is |
|
| 102 | - // public, private or protected. If it starts with an underscore we |
|
| 103 | - // can assume it is private. |
|
| 104 | - if ($funcName{0} === '_') { |
|
| 105 | - continue; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $foundCalls[$i] = array( |
|
| 109 | - 'name' => $funcName, |
|
| 110 | - 'type' => strtolower($tokens[$prevToken]['content']), |
|
| 111 | - ); |
|
| 112 | - }//end for |
|
| 113 | - |
|
| 114 | - $errorClassName = substr($className, 0, -7); |
|
| 115 | - |
|
| 116 | - foreach ($foundCalls as $token => $funcData) { |
|
| 117 | - if (isset($foundFunctions[$funcData['name']]) === false) { |
|
| 118 | - // Function was not in this class, might have come from the parent. |
|
| 119 | - // Either way, we can't really check this. |
|
| 120 | - continue; |
|
| 121 | - } else if ($foundFunctions[$funcData['name']] === 'public') { |
|
| 122 | - $type = $funcData['type']; |
|
| 123 | - $error = "Static calls to public methods in Action classes must not use the $type keyword; use %s::%s() instead"; |
|
| 124 | - $data = array( |
|
| 125 | - $errorClassName, |
|
| 126 | - $funcName, |
|
| 127 | - ); |
|
| 128 | - $phpcsFile->addError($error, $token, 'Found'.ucfirst($funcData['type']), $data); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - }//end process() |
|
| 30 | + /** |
|
| 31 | + * Returns an array of tokens this test wants to listen for. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function register() |
|
| 36 | + { |
|
| 37 | + return array(T_CLASS); |
|
| 38 | + |
|
| 39 | + }//end register() |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | + * |
|
| 45 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 46 | + * @param int $stackPtr The position of the current token in |
|
| 47 | + * the stack passed in $tokens. |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | + { |
|
| 53 | + $tokens = $phpcsFile->getTokens(); |
|
| 54 | + |
|
| 55 | + // We are not interested in abstract classes. |
|
| 56 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 57 | + if ($prev !== false && $tokens[$prev]['code'] === T_ABSTRACT) { |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // We are only interested in Action classes. |
|
| 62 | + $classNameToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 63 | + $className = $tokens[$classNameToken]['content']; |
|
| 64 | + if (substr($className, -7) !== 'Actions') { |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $foundFunctions = array(); |
|
| 69 | + $foundCalls = array(); |
|
| 70 | + |
|
| 71 | + // Find all static method calls in the form self::method() in the class. |
|
| 72 | + $classEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 73 | + for ($i = ($classNameToken + 1); $i < $classEnd; $i++) { |
|
| 74 | + if ($tokens[$i]['code'] !== T_DOUBLE_COLON) { |
|
| 75 | + if ($tokens[$i]['code'] === T_FUNCTION) { |
|
| 76 | + // Cache the function information. |
|
| 77 | + $funcName = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 78 | + $funcScope = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, ($i - 1)); |
|
| 79 | + |
|
| 80 | + $foundFunctions[$tokens[$funcName]['content']] = strtolower($tokens[$funcScope]['content']); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + continue; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 87 | + if ($tokens[$prevToken]['content'] !== 'self' |
|
| 88 | + && $tokens[$prevToken]['content'] !== 'static' |
|
| 89 | + ) { |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $funcNameToken = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 94 | + if ($tokens[$funcNameToken]['code'] === T_VARIABLE) { |
|
| 95 | + // We are only interested in function calls. |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $funcName = $tokens[$funcNameToken]['content']; |
|
| 100 | + |
|
| 101 | + // We've found the function, now we need to find it and see if it is |
|
| 102 | + // public, private or protected. If it starts with an underscore we |
|
| 103 | + // can assume it is private. |
|
| 104 | + if ($funcName{0} === '_') { |
|
| 105 | + continue; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $foundCalls[$i] = array( |
|
| 109 | + 'name' => $funcName, |
|
| 110 | + 'type' => strtolower($tokens[$prevToken]['content']), |
|
| 111 | + ); |
|
| 112 | + }//end for |
|
| 113 | + |
|
| 114 | + $errorClassName = substr($className, 0, -7); |
|
| 115 | + |
|
| 116 | + foreach ($foundCalls as $token => $funcData) { |
|
| 117 | + if (isset($foundFunctions[$funcData['name']]) === false) { |
|
| 118 | + // Function was not in this class, might have come from the parent. |
|
| 119 | + // Either way, we can't really check this. |
|
| 120 | + continue; |
|
| 121 | + } else if ($foundFunctions[$funcData['name']] === 'public') { |
|
| 122 | + $type = $funcData['type']; |
|
| 123 | + $error = "Static calls to public methods in Action classes must not use the $type keyword; use %s::%s() instead"; |
|
| 124 | + $data = array( |
|
| 125 | + $errorClassName, |
|
| 126 | + $funcName, |
|
| 127 | + ); |
|
| 128 | + $phpcsFile->addError($error, $token, 'Found'.ucfirst($funcData['type']), $data); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + }//end process() |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | }//end class |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function register() |
| 36 | 36 | { |
| 37 | - return array(T_CLASS); |
|
| 37 | + return array( T_CLASS ); |
|
| 38 | 38 | |
| 39 | 39 | }//end register() |
| 40 | 40 | |
@@ -48,20 +48,20 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @return void |
| 50 | 50 | */ |
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 51 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 52 | 52 | { |
| 53 | 53 | $tokens = $phpcsFile->getTokens(); |
| 54 | 54 | |
| 55 | 55 | // We are not interested in abstract classes. |
| 56 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 57 | - if ($prev !== false && $tokens[$prev]['code'] === T_ABSTRACT) { |
|
| 56 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
| 57 | + if ( $prev !== false && $tokens[ $prev ][ 'code' ] === T_ABSTRACT ) { |
|
| 58 | 58 | return; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // We are only interested in Action classes. |
| 62 | - $classNameToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 63 | - $className = $tokens[$classNameToken]['content']; |
|
| 64 | - if (substr($className, -7) !== 'Actions') { |
|
| 62 | + $classNameToken = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
| 63 | + $className = $tokens[ $classNameToken ][ 'content' ]; |
|
| 64 | + if ( substr( $className, -7 ) !== 'Actions' ) { |
|
| 65 | 65 | return; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -69,63 +69,63 @@ discard block |
||
| 69 | 69 | $foundCalls = array(); |
| 70 | 70 | |
| 71 | 71 | // Find all static method calls in the form self::method() in the class. |
| 72 | - $classEnd = $tokens[$stackPtr]['scope_closer']; |
|
| 73 | - for ($i = ($classNameToken + 1); $i < $classEnd; $i++) { |
|
| 74 | - if ($tokens[$i]['code'] !== T_DOUBLE_COLON) { |
|
| 75 | - if ($tokens[$i]['code'] === T_FUNCTION) { |
|
| 72 | + $classEnd = $tokens[ $stackPtr ][ 'scope_closer' ]; |
|
| 73 | + for ( $i = ( $classNameToken + 1 ); $i < $classEnd; $i++ ) { |
|
| 74 | + if ( $tokens[ $i ][ 'code' ] !== T_DOUBLE_COLON ) { |
|
| 75 | + if ( $tokens[ $i ][ 'code' ] === T_FUNCTION ) { |
|
| 76 | 76 | // Cache the function information. |
| 77 | - $funcName = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 78 | - $funcScope = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, ($i - 1)); |
|
| 77 | + $funcName = $phpcsFile->findNext( T_STRING, ( $i + 1 ) ); |
|
| 78 | + $funcScope = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$scopeModifiers, ( $i - 1 ) ); |
|
| 79 | 79 | |
| 80 | - $foundFunctions[$tokens[$funcName]['content']] = strtolower($tokens[$funcScope]['content']); |
|
| 80 | + $foundFunctions[ $tokens[ $funcName ][ 'content' ] ] = strtolower( $tokens[ $funcScope ][ 'content' ] ); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | continue; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 87 | - if ($tokens[$prevToken]['content'] !== 'self' |
|
| 88 | - && $tokens[$prevToken]['content'] !== 'static' |
|
| 86 | + $prevToken = $phpcsFile->findPrevious( T_WHITESPACE, ( $i - 1 ), null, true ); |
|
| 87 | + if ( $tokens[ $prevToken ][ 'content' ] !== 'self' |
|
| 88 | + && $tokens[ $prevToken ][ 'content' ] !== 'static' |
|
| 89 | 89 | ) { |
| 90 | 90 | continue; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $funcNameToken = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 94 | - if ($tokens[$funcNameToken]['code'] === T_VARIABLE) { |
|
| 93 | + $funcNameToken = $phpcsFile->findNext( T_WHITESPACE, ( $i + 1 ), null, true ); |
|
| 94 | + if ( $tokens[ $funcNameToken ][ 'code' ] === T_VARIABLE ) { |
|
| 95 | 95 | // We are only interested in function calls. |
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $funcName = $tokens[$funcNameToken]['content']; |
|
| 99 | + $funcName = $tokens[ $funcNameToken ][ 'content' ]; |
|
| 100 | 100 | |
| 101 | 101 | // We've found the function, now we need to find it and see if it is |
| 102 | 102 | // public, private or protected. If it starts with an underscore we |
| 103 | 103 | // can assume it is private. |
| 104 | - if ($funcName{0} === '_') { |
|
| 104 | + if ( $funcName{0} === '_' ) { |
|
| 105 | 105 | continue; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $foundCalls[$i] = array( |
|
| 108 | + $foundCalls[ $i ] = array( |
|
| 109 | 109 | 'name' => $funcName, |
| 110 | - 'type' => strtolower($tokens[$prevToken]['content']), |
|
| 110 | + 'type' => strtolower( $tokens[ $prevToken ][ 'content' ] ), |
|
| 111 | 111 | ); |
| 112 | 112 | }//end for |
| 113 | 113 | |
| 114 | - $errorClassName = substr($className, 0, -7); |
|
| 114 | + $errorClassName = substr( $className, 0, -7 ); |
|
| 115 | 115 | |
| 116 | - foreach ($foundCalls as $token => $funcData) { |
|
| 117 | - if (isset($foundFunctions[$funcData['name']]) === false) { |
|
| 116 | + foreach ( $foundCalls as $token => $funcData ) { |
|
| 117 | + if ( isset( $foundFunctions[ $funcData[ 'name' ] ] ) === false ) { |
|
| 118 | 118 | // Function was not in this class, might have come from the parent. |
| 119 | 119 | // Either way, we can't really check this. |
| 120 | 120 | continue; |
| 121 | - } else if ($foundFunctions[$funcData['name']] === 'public') { |
|
| 122 | - $type = $funcData['type']; |
|
| 121 | + } else if ( $foundFunctions[ $funcData[ 'name' ] ] === 'public' ) { |
|
| 122 | + $type = $funcData[ 'type' ]; |
|
| 123 | 123 | $error = "Static calls to public methods in Action classes must not use the $type keyword; use %s::%s() instead"; |
| 124 | 124 | $data = array( |
| 125 | 125 | $errorClassName, |
| 126 | 126 | $funcName, |
| 127 | 127 | ); |
| 128 | - $phpcsFile->addError($error, $token, 'Found'.ucfirst($funcData['type']), $data); |
|
| 128 | + $phpcsFile->addError( $error, $token, 'Found' . ucfirst( $funcData[ 'type' ] ), $data ); |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
@@ -23,8 +23,7 @@ discard block |
||
| 23 | 23 | * @version Release: @package_version@ |
| 24 | 24 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 25 | 25 | */ |
| 26 | -class MySource_Sniffs_Channels_DisallowSelfActionsSniff implements PHP_CodeSniffer_Sniff |
|
| 27 | -{ |
|
| 26 | +class MySource_Sniffs_Channels_DisallowSelfActionsSniff implements PHP_CodeSniffer_Sniff { |
|
| 28 | 27 | |
| 29 | 28 | |
| 30 | 29 | /** |
@@ -32,8 +31,7 @@ discard block |
||
| 32 | 31 | * |
| 33 | 32 | * @return array |
| 34 | 33 | */ |
| 35 | - public function register() |
|
| 36 | - { |
|
| 34 | + public function register() { |
|
| 37 | 35 | return array(T_CLASS); |
| 38 | 36 | |
| 39 | 37 | }//end register() |
@@ -48,8 +46,7 @@ discard block |
||
| 48 | 46 | * |
| 49 | 47 | * @return void |
| 50 | 48 | */ |
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 49 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 53 | 50 | $tokens = $phpcsFile->getTokens(); |
| 54 | 51 | |
| 55 | 52 | // We are not interested in abstract classes. |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | /** |
| 31 | 31 | * Returns an array of tokens this test wants to listen for. |
| 32 | 32 | * |
| 33 | - * @return array |
|
| 33 | + * @return integer[] |
|
| 34 | 34 | */ |
| 35 | 35 | public function register() |
| 36 | 36 | { |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * @param int $stackPtr The position in the tokens array of the |
| 94 | 94 | * potentially included class. |
| 95 | 95 | * |
| 96 | - * @return string |
|
| 96 | + * @return boolean |
|
| 97 | 97 | */ |
| 98 | 98 | protected function getIncludedClassFromToken( |
| 99 | 99 | PHP_CodeSniffer_File $phpcsFile, |
@@ -27,83 +27,83 @@ |
||
| 27 | 27 | { |
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns an array of tokens this test wants to listen for. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function register() |
|
| 36 | - { |
|
| 37 | - return array(T_DOUBLE_COLON); |
|
| 38 | - |
|
| 39 | - }//end register() |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | - * |
|
| 45 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 46 | - * @param int $stackPtr The position of the current token in |
|
| 47 | - * the stack passed in $tokens. |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 53 | - $fileName = $phpcsFile->getFilename(); |
|
| 54 | - $matches = array(); |
|
| 55 | - if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
|
| 56 | - // Not an actions file. |
|
| 57 | - return; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $ownClass = $matches[2]; |
|
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 62 | - |
|
| 63 | - $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
|
| 64 | - $typeName = trim($tokens[$typeName]['content'], " '"); |
|
| 65 | - switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
|
| 66 | - case 'includesystem' : |
|
| 67 | - $included = strtolower($typeName); |
|
| 68 | - break; |
|
| 69 | - case 'includeasset' : |
|
| 70 | - $included = strtolower($typeName).'assettype'; |
|
| 71 | - break; |
|
| 72 | - case 'includewidget' : |
|
| 73 | - $included = strtolower($typeName).'widgettype'; |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - return; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if ($included === strtolower($ownClass)) { |
|
| 80 | - $error = "You do not need to include \"%s\" from within the system's own actions file"; |
|
| 81 | - $data = array($ownClass); |
|
| 82 | - $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - }//end process() |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Determines the included class name from given token. |
|
| 90 | - * |
|
| 91 | - * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. |
|
| 92 | - * @param array $tokens The array of file tokens. |
|
| 93 | - * @param int $stackPtr The position in the tokens array of the |
|
| 94 | - * potentially included class. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - protected function getIncludedClassFromToken( |
|
| 99 | - PHP_CodeSniffer_File $phpcsFile, |
|
| 100 | - array $tokens, |
|
| 101 | - $stackPtr |
|
| 102 | - ) { |
|
| 103 | - |
|
| 104 | - return false; |
|
| 105 | - |
|
| 106 | - }//end getIncludedClassFromToken() |
|
| 30 | + /** |
|
| 31 | + * Returns an array of tokens this test wants to listen for. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function register() |
|
| 36 | + { |
|
| 37 | + return array(T_DOUBLE_COLON); |
|
| 38 | + |
|
| 39 | + }//end register() |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | + * |
|
| 45 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 46 | + * @param int $stackPtr The position of the current token in |
|
| 47 | + * the stack passed in $tokens. |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | + { |
|
| 53 | + $fileName = $phpcsFile->getFilename(); |
|
| 54 | + $matches = array(); |
|
| 55 | + if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
|
| 56 | + // Not an actions file. |
|
| 57 | + return; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $ownClass = $matches[2]; |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | + |
|
| 63 | + $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
|
| 64 | + $typeName = trim($tokens[$typeName]['content'], " '"); |
|
| 65 | + switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
|
| 66 | + case 'includesystem' : |
|
| 67 | + $included = strtolower($typeName); |
|
| 68 | + break; |
|
| 69 | + case 'includeasset' : |
|
| 70 | + $included = strtolower($typeName).'assettype'; |
|
| 71 | + break; |
|
| 72 | + case 'includewidget' : |
|
| 73 | + $included = strtolower($typeName).'widgettype'; |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if ($included === strtolower($ownClass)) { |
|
| 80 | + $error = "You do not need to include \"%s\" from within the system's own actions file"; |
|
| 81 | + $data = array($ownClass); |
|
| 82 | + $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + }//end process() |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Determines the included class name from given token. |
|
| 90 | + * |
|
| 91 | + * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. |
|
| 92 | + * @param array $tokens The array of file tokens. |
|
| 93 | + * @param int $stackPtr The position in the tokens array of the |
|
| 94 | + * potentially included class. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + protected function getIncludedClassFromToken( |
|
| 99 | + PHP_CodeSniffer_File $phpcsFile, |
|
| 100 | + array $tokens, |
|
| 101 | + $stackPtr |
|
| 102 | + ) { |
|
| 103 | + |
|
| 104 | + return false; |
|
| 105 | + |
|
| 106 | + }//end getIncludedClassFromToken() |
|
| 107 | 107 | |
| 108 | 108 | |
| 109 | 109 | }//end class |
@@ -63,17 +63,17 @@ |
||
| 63 | 63 | $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
| 64 | 64 | $typeName = trim($tokens[$typeName]['content'], " '"); |
| 65 | 65 | switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
| 66 | - case 'includesystem' : |
|
| 67 | - $included = strtolower($typeName); |
|
| 68 | - break; |
|
| 69 | - case 'includeasset' : |
|
| 70 | - $included = strtolower($typeName).'assettype'; |
|
| 71 | - break; |
|
| 72 | - case 'includewidget' : |
|
| 73 | - $included = strtolower($typeName).'widgettype'; |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - return; |
|
| 66 | + case 'includesystem' : |
|
| 67 | + $included = strtolower($typeName); |
|
| 68 | + break; |
|
| 69 | + case 'includeasset' : |
|
| 70 | + $included = strtolower($typeName).'assettype'; |
|
| 71 | + break; |
|
| 72 | + case 'includewidget' : |
|
| 73 | + $included = strtolower($typeName).'widgettype'; |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + return; |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | if ($included === strtolower($ownClass)) { |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function register() |
| 36 | 36 | { |
| 37 | - return array(T_DOUBLE_COLON); |
|
| 37 | + return array( T_DOUBLE_COLON ); |
|
| 38 | 38 | |
| 39 | 39 | }//end register() |
| 40 | 40 | |
@@ -48,38 +48,38 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @return void |
| 50 | 50 | */ |
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 51 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 52 | 52 | { |
| 53 | 53 | $fileName = $phpcsFile->getFilename(); |
| 54 | 54 | $matches = array(); |
| 55 | - if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
|
| 55 | + if ( preg_match( '|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches ) === 0 ) { |
|
| 56 | 56 | // Not an actions file. |
| 57 | 57 | return; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $ownClass = $matches[2]; |
|
| 60 | + $ownClass = $matches[ 2 ]; |
|
| 61 | 61 | $tokens = $phpcsFile->getTokens(); |
| 62 | 62 | |
| 63 | - $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
|
| 64 | - $typeName = trim($tokens[$typeName]['content'], " '"); |
|
| 65 | - switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
|
| 63 | + $typeName = $phpcsFile->findNext( T_CONSTANT_ENCAPSED_STRING, ( $stackPtr + 2 ), null, false, true ); |
|
| 64 | + $typeName = trim( $tokens[ $typeName ][ 'content' ], " '" ); |
|
| 65 | + switch ( strtolower( $tokens[ ( $stackPtr + 1 ) ][ 'content' ] ) ) { |
|
| 66 | 66 | case 'includesystem' : |
| 67 | - $included = strtolower($typeName); |
|
| 67 | + $included = strtolower( $typeName ); |
|
| 68 | 68 | break; |
| 69 | 69 | case 'includeasset' : |
| 70 | - $included = strtolower($typeName).'assettype'; |
|
| 70 | + $included = strtolower( $typeName ) . 'assettype'; |
|
| 71 | 71 | break; |
| 72 | 72 | case 'includewidget' : |
| 73 | - $included = strtolower($typeName).'widgettype'; |
|
| 73 | + $included = strtolower( $typeName ) . 'widgettype'; |
|
| 74 | 74 | break; |
| 75 | 75 | default: |
| 76 | 76 | return; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - if ($included === strtolower($ownClass)) { |
|
| 79 | + if ( $included === strtolower( $ownClass ) ) { |
|
| 80 | 80 | $error = "You do not need to include \"%s\" from within the system's own actions file"; |
| 81 | - $data = array($ownClass); |
|
| 82 | - $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data); |
|
| 81 | + $data = array( $ownClass ); |
|
| 82 | + $phpcsFile->addError( $error, $stackPtr, 'NotRequired', $data ); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | }//end process() |
@@ -23,8 +23,7 @@ discard block |
||
| 23 | 23 | * @version Release: @package_version@ |
| 24 | 24 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 25 | 25 | */ |
| 26 | -class MySource_Sniffs_Channels_IncludeOwnSystemSniff implements PHP_CodeSniffer_Sniff |
|
| 27 | -{ |
|
| 26 | +class MySource_Sniffs_Channels_IncludeOwnSystemSniff implements PHP_CodeSniffer_Sniff { |
|
| 28 | 27 | |
| 29 | 28 | |
| 30 | 29 | /** |
@@ -32,8 +31,7 @@ discard block |
||
| 32 | 31 | * |
| 33 | 32 | * @return array |
| 34 | 33 | */ |
| 35 | - public function register() |
|
| 36 | - { |
|
| 34 | + public function register() { |
|
| 37 | 35 | return array(T_DOUBLE_COLON); |
| 38 | 36 | |
| 39 | 37 | }//end register() |
@@ -48,8 +46,7 @@ discard block |
||
| 48 | 46 | * |
| 49 | 47 | * @return void |
| 50 | 48 | */ |
| 51 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 49 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 53 | 50 | $fileName = $phpcsFile->getFilename(); |
| 54 | 51 | $matches = array(); |
| 55 | 52 | if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | /** |
| 38 | 38 | * Returns an array of tokens this test wants to listen for. |
| 39 | 39 | * |
| 40 | - * @return array |
|
| 40 | + * @return string[] |
|
| 41 | 41 | */ |
| 42 | 42 | public function register() |
| 43 | 43 | { |
@@ -26,67 +26,67 @@ |
||
| 26 | 26 | class MySource_Sniffs_Objects_AssignThisSniff implements PHP_CodeSniffer_Sniff |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * A list of tokenizers this sniff supports. |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - public $supportedTokenizers = array('JS'); |
|
| 29 | + /** |
|
| 30 | + * A list of tokenizers this sniff supports. |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + public $supportedTokenizers = array('JS'); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Returns an array of tokens this test wants to listen for. |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function register() |
|
| 43 | - { |
|
| 44 | - return array(T_THIS); |
|
| 37 | + /** |
|
| 38 | + * Returns an array of tokens this test wants to listen for. |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function register() |
|
| 43 | + { |
|
| 44 | + return array(T_THIS); |
|
| 45 | 45 | |
| 46 | - }//end register() |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | 61 | |
| 62 | - // Ignore this.something and other uses of "this" that are not |
|
| 63 | - // direct assignments. |
|
| 64 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 65 | - if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
| 66 | - if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - } |
|
| 62 | + // Ignore this.something and other uses of "this" that are not |
|
| 63 | + // direct assignments. |
|
| 64 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 65 | + if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
| 66 | + if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - // Something must be assigned to "this". |
|
| 72 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 73 | - if ($tokens[$prev]['code'] !== T_EQUAL) { |
|
| 74 | - return; |
|
| 75 | - } |
|
| 71 | + // Something must be assigned to "this". |
|
| 72 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 73 | + if ($tokens[$prev]['code'] !== T_EQUAL) { |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // A variable needs to be assigned to "this". |
|
| 78 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true); |
|
| 79 | - if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 80 | - return; |
|
| 81 | - } |
|
| 77 | + // A variable needs to be assigned to "this". |
|
| 78 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true); |
|
| 79 | + if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - // We can only assign "this" to a var called "self". |
|
| 84 | - if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') { |
|
| 85 | - $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"'; |
|
| 86 | - $phpcsFile->addError($error, $prev, 'NotSelf'); |
|
| 87 | - } |
|
| 83 | + // We can only assign "this" to a var called "self". |
|
| 84 | + if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') { |
|
| 85 | + $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"'; |
|
| 86 | + $phpcsFile->addError($error, $prev, 'NotSelf'); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - }//end process() |
|
| 89 | + }//end process() |
|
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | }//end class |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @var array |
| 33 | 33 | */ |
| 34 | - public $supportedTokenizers = array('JS'); |
|
| 34 | + public $supportedTokenizers = array( 'JS' ); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function register() |
| 43 | 43 | { |
| 44 | - return array(T_THIS); |
|
| 44 | + return array( T_THIS ); |
|
| 45 | 45 | |
| 46 | 46 | }//end register() |
| 47 | 47 | |
@@ -55,35 +55,35 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @return void |
| 57 | 57 | */ |
| 58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 58 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 59 | 59 | { |
| 60 | 60 | $tokens = $phpcsFile->getTokens(); |
| 61 | 61 | |
| 62 | 62 | // Ignore this.something and other uses of "this" that are not |
| 63 | 63 | // direct assignments. |
| 64 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 65 | - if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
| 66 | - if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 64 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
| 65 | + if ( $tokens[ $next ][ 'code' ] !== T_SEMICOLON ) { |
|
| 66 | + if ( $tokens[ $next ][ 'line' ] === $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 67 | 67 | return; |
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Something must be assigned to "this". |
| 72 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 73 | - if ($tokens[$prev]['code'] !== T_EQUAL) { |
|
| 72 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
| 73 | + if ( $tokens[ $prev ][ 'code' ] !== T_EQUAL ) { |
|
| 74 | 74 | return; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | // A variable needs to be assigned to "this". |
| 78 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true); |
|
| 79 | - if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 78 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $prev - 1 ), null, true ); |
|
| 79 | + if ( $tokens[ $prev ][ 'code' ] !== T_STRING ) { |
|
| 80 | 80 | return; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // We can only assign "this" to a var called "self". |
| 84 | - if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') { |
|
| 84 | + if ( $tokens[ $prev ][ 'content' ] !== 'self' && $tokens[ $prev ][ 'content' ] !== '_self' ) { |
|
| 85 | 85 | $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"'; |
| 86 | - $phpcsFile->addError($error, $prev, 'NotSelf'); |
|
| 86 | + $phpcsFile->addError( $error, $prev, 'NotSelf' ); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | }//end process() |
@@ -23,8 +23,7 @@ discard block |
||
| 23 | 23 | * @version Release: @package_version@ |
| 24 | 24 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 25 | 25 | */ |
| 26 | -class MySource_Sniffs_Objects_AssignThisSniff implements PHP_CodeSniffer_Sniff |
|
| 27 | -{ |
|
| 26 | +class MySource_Sniffs_Objects_AssignThisSniff implements PHP_CodeSniffer_Sniff { |
|
| 28 | 27 | |
| 29 | 28 | /** |
| 30 | 29 | * A list of tokenizers this sniff supports. |
@@ -39,8 +38,7 @@ discard block |
||
| 39 | 38 | * |
| 40 | 39 | * @return array |
| 41 | 40 | */ |
| 42 | - public function register() |
|
| 43 | - { |
|
| 41 | + public function register() { |
|
| 44 | 42 | return array(T_THIS); |
| 45 | 43 | |
| 46 | 44 | }//end register() |
@@ -55,8 +53,7 @@ discard block |
||
| 55 | 53 | * |
| 56 | 54 | * @return void |
| 57 | 55 | */ |
| 58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 60 | 57 | $tokens = $phpcsFile->getTokens(); |
| 61 | 58 | |
| 62 | 59 | // Ignore this.something and other uses of "this" that are not |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | /** |
| 38 | 38 | * Returns an array of tokens this test wants to listen for. |
| 39 | 39 | * |
| 40 | - * @return array |
|
| 40 | + * @return string[] |
|
| 41 | 41 | */ |
| 42 | 42 | public function register() |
| 43 | 43 | { |
@@ -26,68 +26,68 @@ discard block |
||
| 26 | 26 | class MySource_Sniffs_Objects_CreateWidgetTypeCallbackSniff implements PHP_CodeSniffer_Sniff |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * A list of tokenizers this sniff supports. |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - public $supportedTokenizers = array('JS'); |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns an array of tokens this test wants to listen for. |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function register() |
|
| 43 | - { |
|
| 44 | - return array(T_OBJECT); |
|
| 45 | - |
|
| 46 | - }//end register() |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 61 | - |
|
| 62 | - $className = $phpcsFile->findPrevious(T_STRING, ($stackPtr - 1)); |
|
| 63 | - if (substr(strtolower($tokens[$className]['content']), -10) !== 'widgettype') { |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - // Search for a create method. |
|
| 68 | - $create = $phpcsFile->findNext(T_PROPERTY, $stackPtr, $tokens[$stackPtr]['bracket_closer'], null, 'create'); |
|
| 69 | - if ($create === false) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $function = $phpcsFile->findNext(array(T_WHITESPACE, T_COLON), ($create + 1), null, true); |
|
| 74 | - if ($tokens[$function]['code'] !== T_FUNCTION |
|
| 75 | - && $tokens[$function]['code'] !== T_CLOSURE |
|
| 76 | - ) { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $start = ($tokens[$function]['scope_opener'] + 1); |
|
| 81 | - $end = ($tokens[$function]['scope_closer'] - 1); |
|
| 82 | - |
|
| 83 | - // Check that the first argument is called "callback". |
|
| 84 | - $arg = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$function]['parenthesis_opener'] + 1), null, true); |
|
| 85 | - if ($tokens[$arg]['content'] !== 'callback') { |
|
| 86 | - $error = 'The first argument of the create() method of a widget type must be called "callback"'; |
|
| 87 | - $phpcsFile->addError($error, $arg, 'FirstArgNotCallback'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /* |
|
| 29 | + /** |
|
| 30 | + * A list of tokenizers this sniff supports. |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + public $supportedTokenizers = array('JS'); |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns an array of tokens this test wants to listen for. |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function register() |
|
| 43 | + { |
|
| 44 | + return array(T_OBJECT); |
|
| 45 | + |
|
| 46 | + }//end register() |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | + |
|
| 62 | + $className = $phpcsFile->findPrevious(T_STRING, ($stackPtr - 1)); |
|
| 63 | + if (substr(strtolower($tokens[$className]['content']), -10) !== 'widgettype') { |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + // Search for a create method. |
|
| 68 | + $create = $phpcsFile->findNext(T_PROPERTY, $stackPtr, $tokens[$stackPtr]['bracket_closer'], null, 'create'); |
|
| 69 | + if ($create === false) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $function = $phpcsFile->findNext(array(T_WHITESPACE, T_COLON), ($create + 1), null, true); |
|
| 74 | + if ($tokens[$function]['code'] !== T_FUNCTION |
|
| 75 | + && $tokens[$function]['code'] !== T_CLOSURE |
|
| 76 | + ) { |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $start = ($tokens[$function]['scope_opener'] + 1); |
|
| 81 | + $end = ($tokens[$function]['scope_closer'] - 1); |
|
| 82 | + |
|
| 83 | + // Check that the first argument is called "callback". |
|
| 84 | + $arg = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$function]['parenthesis_opener'] + 1), null, true); |
|
| 85 | + if ($tokens[$arg]['content'] !== 'callback') { |
|
| 86 | + $error = 'The first argument of the create() method of a widget type must be called "callback"'; |
|
| 87 | + $phpcsFile->addError($error, $arg, 'FirstArgNotCallback'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /* |
|
| 91 | 91 | Look for return statements within the function. They cannot return |
| 92 | 92 | anything and must be preceded by the callback.call() line. The |
| 93 | 93 | callback itself must contain "self" or "this" as the first argument |
@@ -96,132 +96,132 @@ discard block |
||
| 96 | 96 | followed by a return statement or the end of the method. |
| 97 | 97 | */ |
| 98 | 98 | |
| 99 | - $foundCallback = false; |
|
| 100 | - $passedCallback = false; |
|
| 101 | - $nestedFunction = null; |
|
| 102 | - for ($i = $start; $i <= $end; $i++) { |
|
| 103 | - // Keep track of nested functions. |
|
| 104 | - if ($nestedFunction !== null) { |
|
| 105 | - if ($i === $nestedFunction) { |
|
| 106 | - $nestedFunction = null; |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 109 | - } else if (($tokens[$i]['code'] === T_FUNCTION |
|
| 110 | - || $tokens[$i]['code'] === T_CLOSURE) |
|
| 111 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 112 | - ) { |
|
| 113 | - $nestedFunction = $tokens[$i]['scope_closer']; |
|
| 114 | - continue; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ($nestedFunction === null && $tokens[$i]['code'] === T_RETURN) { |
|
| 118 | - // Make sure return statements are not returning anything. |
|
| 119 | - if ($tokens[($i + 1)]['code'] !== T_SEMICOLON) { |
|
| 120 | - $error = 'The create() method of a widget type must not return a value'; |
|
| 121 | - $phpcsFile->addError($error, $i, 'ReturnValue'); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - continue; |
|
| 125 | - } else if ($tokens[$i]['code'] !== T_STRING |
|
| 126 | - || $tokens[$i]['content'] !== 'callback' |
|
| 127 | - ) { |
|
| 128 | - continue; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // If this is the form "callback.call(" then it is a call |
|
| 132 | - // to the callback function. |
|
| 133 | - if ($tokens[($i + 1)]['code'] !== T_OBJECT_OPERATOR |
|
| 134 | - || $tokens[($i + 2)]['content'] !== 'call' |
|
| 135 | - || $tokens[($i + 3)]['code'] !== T_OPEN_PARENTHESIS |
|
| 136 | - ) { |
|
| 137 | - // One last chance; this might be the callback function |
|
| 138 | - // being passed to another function, like this |
|
| 139 | - // "this.init(something, callback, something)". |
|
| 140 | - if (isset($tokens[$i]['nested_parenthesis']) === false) { |
|
| 141 | - continue; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Just make sure those brackets dont belong to anyone, |
|
| 145 | - // like an IF or FOR statement. |
|
| 146 | - foreach ($tokens[$i]['nested_parenthesis'] as $bracket) { |
|
| 147 | - if (isset($tokens[$bracket]['parenthesis_owner']) === true) { |
|
| 148 | - continue(2); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Note that we use this endBracket down further when checking |
|
| 153 | - // for a RETURN statement. |
|
| 154 | - $endBracket = end($tokens[$i]['nested_parenthesis']); |
|
| 155 | - $bracket = key($tokens[$i]['nested_parenthesis']); |
|
| 156 | - |
|
| 157 | - $prev = $phpcsFile->findPrevious( |
|
| 158 | - PHP_CodeSniffer_Tokens::$emptyTokens, |
|
| 159 | - ($bracket - 1), |
|
| 160 | - null, |
|
| 161 | - true |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 165 | - // This is not a function passing the callback. |
|
| 166 | - continue; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $passedCallback = true; |
|
| 170 | - }//end if |
|
| 171 | - |
|
| 172 | - $foundCallback = true; |
|
| 173 | - |
|
| 174 | - if ($passedCallback === false) { |
|
| 175 | - // The first argument must be "this" or "self". |
|
| 176 | - $arg = $phpcsFile->findNext(T_WHITESPACE, ($i + 4), null, true); |
|
| 177 | - if ($tokens[$arg]['content'] !== 'this' |
|
| 178 | - && $tokens[$arg]['content'] !== 'self' |
|
| 179 | - ) { |
|
| 180 | - $error = 'The first argument passed to the callback function must be "this" or "self"'; |
|
| 181 | - $phpcsFile->addError($error, $arg, 'FirstArgNotSelf'); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Now it must be followed by a return statement or the end of the function. |
|
| 186 | - if ($passedCallback === false) { |
|
| 187 | - $endBracket = $tokens[($i + 3)]['parenthesis_closer']; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - for ($next = $endBracket; $next <= $end; $next++) { |
|
| 191 | - // Skip whitespace so we find the next content after the call. |
|
| 192 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$next]['code']]) === true) { |
|
| 193 | - continue; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // Skip closing braces like END IF because it is not executable code. |
|
| 197 | - if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 198 | - continue; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // We don't care about anything on the current line, like a |
|
| 202 | - // semicolon. It doesn't matter if there are other statements on the |
|
| 203 | - // line because another sniff will check for those. |
|
| 204 | - if ($tokens[$next]['line'] === $tokens[$endBracket]['line']) { |
|
| 205 | - continue; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - break; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($next !== $tokens[$function]['scope_closer'] |
|
| 212 | - && $tokens[$next]['code'] !== T_RETURN |
|
| 213 | - ) { |
|
| 214 | - $error = 'The call to the callback function must be followed by a return statement if it is not the last statement in the create() method'; |
|
| 215 | - $phpcsFile->addError($error, $i, 'NoReturn'); |
|
| 216 | - } |
|
| 217 | - }//end for |
|
| 218 | - |
|
| 219 | - if ($foundCallback === false) { |
|
| 220 | - $error = 'The create() method of a widget type must call the callback function'; |
|
| 221 | - $phpcsFile->addError($error, $create, 'CallbackNotCalled'); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - }//end process() |
|
| 99 | + $foundCallback = false; |
|
| 100 | + $passedCallback = false; |
|
| 101 | + $nestedFunction = null; |
|
| 102 | + for ($i = $start; $i <= $end; $i++) { |
|
| 103 | + // Keep track of nested functions. |
|
| 104 | + if ($nestedFunction !== null) { |
|
| 105 | + if ($i === $nestedFunction) { |
|
| 106 | + $nestedFunction = null; |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | + } else if (($tokens[$i]['code'] === T_FUNCTION |
|
| 110 | + || $tokens[$i]['code'] === T_CLOSURE) |
|
| 111 | + && isset($tokens[$i]['scope_closer']) === true |
|
| 112 | + ) { |
|
| 113 | + $nestedFunction = $tokens[$i]['scope_closer']; |
|
| 114 | + continue; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ($nestedFunction === null && $tokens[$i]['code'] === T_RETURN) { |
|
| 118 | + // Make sure return statements are not returning anything. |
|
| 119 | + if ($tokens[($i + 1)]['code'] !== T_SEMICOLON) { |
|
| 120 | + $error = 'The create() method of a widget type must not return a value'; |
|
| 121 | + $phpcsFile->addError($error, $i, 'ReturnValue'); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + continue; |
|
| 125 | + } else if ($tokens[$i]['code'] !== T_STRING |
|
| 126 | + || $tokens[$i]['content'] !== 'callback' |
|
| 127 | + ) { |
|
| 128 | + continue; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // If this is the form "callback.call(" then it is a call |
|
| 132 | + // to the callback function. |
|
| 133 | + if ($tokens[($i + 1)]['code'] !== T_OBJECT_OPERATOR |
|
| 134 | + || $tokens[($i + 2)]['content'] !== 'call' |
|
| 135 | + || $tokens[($i + 3)]['code'] !== T_OPEN_PARENTHESIS |
|
| 136 | + ) { |
|
| 137 | + // One last chance; this might be the callback function |
|
| 138 | + // being passed to another function, like this |
|
| 139 | + // "this.init(something, callback, something)". |
|
| 140 | + if (isset($tokens[$i]['nested_parenthesis']) === false) { |
|
| 141 | + continue; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Just make sure those brackets dont belong to anyone, |
|
| 145 | + // like an IF or FOR statement. |
|
| 146 | + foreach ($tokens[$i]['nested_parenthesis'] as $bracket) { |
|
| 147 | + if (isset($tokens[$bracket]['parenthesis_owner']) === true) { |
|
| 148 | + continue(2); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Note that we use this endBracket down further when checking |
|
| 153 | + // for a RETURN statement. |
|
| 154 | + $endBracket = end($tokens[$i]['nested_parenthesis']); |
|
| 155 | + $bracket = key($tokens[$i]['nested_parenthesis']); |
|
| 156 | + |
|
| 157 | + $prev = $phpcsFile->findPrevious( |
|
| 158 | + PHP_CodeSniffer_Tokens::$emptyTokens, |
|
| 159 | + ($bracket - 1), |
|
| 160 | + null, |
|
| 161 | + true |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 165 | + // This is not a function passing the callback. |
|
| 166 | + continue; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $passedCallback = true; |
|
| 170 | + }//end if |
|
| 171 | + |
|
| 172 | + $foundCallback = true; |
|
| 173 | + |
|
| 174 | + if ($passedCallback === false) { |
|
| 175 | + // The first argument must be "this" or "self". |
|
| 176 | + $arg = $phpcsFile->findNext(T_WHITESPACE, ($i + 4), null, true); |
|
| 177 | + if ($tokens[$arg]['content'] !== 'this' |
|
| 178 | + && $tokens[$arg]['content'] !== 'self' |
|
| 179 | + ) { |
|
| 180 | + $error = 'The first argument passed to the callback function must be "this" or "self"'; |
|
| 181 | + $phpcsFile->addError($error, $arg, 'FirstArgNotSelf'); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Now it must be followed by a return statement or the end of the function. |
|
| 186 | + if ($passedCallback === false) { |
|
| 187 | + $endBracket = $tokens[($i + 3)]['parenthesis_closer']; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + for ($next = $endBracket; $next <= $end; $next++) { |
|
| 191 | + // Skip whitespace so we find the next content after the call. |
|
| 192 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$next]['code']]) === true) { |
|
| 193 | + continue; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // Skip closing braces like END IF because it is not executable code. |
|
| 197 | + if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 198 | + continue; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // We don't care about anything on the current line, like a |
|
| 202 | + // semicolon. It doesn't matter if there are other statements on the |
|
| 203 | + // line because another sniff will check for those. |
|
| 204 | + if ($tokens[$next]['line'] === $tokens[$endBracket]['line']) { |
|
| 205 | + continue; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + break; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($next !== $tokens[$function]['scope_closer'] |
|
| 212 | + && $tokens[$next]['code'] !== T_RETURN |
|
| 213 | + ) { |
|
| 214 | + $error = 'The call to the callback function must be followed by a return statement if it is not the last statement in the create() method'; |
|
| 215 | + $phpcsFile->addError($error, $i, 'NoReturn'); |
|
| 216 | + } |
|
| 217 | + }//end for |
|
| 218 | + |
|
| 219 | + if ($foundCallback === false) { |
|
| 220 | + $error = 'The create() method of a widget type must call the callback function'; |
|
| 221 | + $phpcsFile->addError($error, $create, 'CallbackNotCalled'); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + }//end process() |
|
| 225 | 225 | |
| 226 | 226 | |
| 227 | 227 | }//end class |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @var array |
| 33 | 33 | */ |
| 34 | - public $supportedTokenizers = array('JS'); |
|
| 34 | + public $supportedTokenizers = array( 'JS' ); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function register() |
| 43 | 43 | { |
| 44 | - return array(T_OBJECT); |
|
| 44 | + return array( T_OBJECT ); |
|
| 45 | 45 | |
| 46 | 46 | }//end register() |
| 47 | 47 | |
@@ -55,36 +55,36 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @return void |
| 57 | 57 | */ |
| 58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 58 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 59 | 59 | { |
| 60 | 60 | $tokens = $phpcsFile->getTokens(); |
| 61 | 61 | |
| 62 | - $className = $phpcsFile->findPrevious(T_STRING, ($stackPtr - 1)); |
|
| 63 | - if (substr(strtolower($tokens[$className]['content']), -10) !== 'widgettype') { |
|
| 62 | + $className = $phpcsFile->findPrevious( T_STRING, ( $stackPtr - 1 ) ); |
|
| 63 | + if ( substr( strtolower( $tokens[ $className ][ 'content' ] ), -10 ) !== 'widgettype' ) { |
|
| 64 | 64 | return; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | // Search for a create method. |
| 68 | - $create = $phpcsFile->findNext(T_PROPERTY, $stackPtr, $tokens[$stackPtr]['bracket_closer'], null, 'create'); |
|
| 69 | - if ($create === false) { |
|
| 68 | + $create = $phpcsFile->findNext( T_PROPERTY, $stackPtr, $tokens[ $stackPtr ][ 'bracket_closer' ], null, 'create' ); |
|
| 69 | + if ( $create === false ) { |
|
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - $function = $phpcsFile->findNext(array(T_WHITESPACE, T_COLON), ($create + 1), null, true); |
|
| 74 | - if ($tokens[$function]['code'] !== T_FUNCTION |
|
| 75 | - && $tokens[$function]['code'] !== T_CLOSURE |
|
| 73 | + $function = $phpcsFile->findNext( array( T_WHITESPACE, T_COLON ), ( $create + 1 ), null, true ); |
|
| 74 | + if ( $tokens[ $function ][ 'code' ] !== T_FUNCTION |
|
| 75 | + && $tokens[ $function ][ 'code' ] !== T_CLOSURE |
|
| 76 | 76 | ) { |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $start = ($tokens[$function]['scope_opener'] + 1); |
|
| 81 | - $end = ($tokens[$function]['scope_closer'] - 1); |
|
| 80 | + $start = ( $tokens[ $function ][ 'scope_opener' ] + 1 ); |
|
| 81 | + $end = ( $tokens[ $function ][ 'scope_closer' ] - 1 ); |
|
| 82 | 82 | |
| 83 | 83 | // Check that the first argument is called "callback". |
| 84 | - $arg = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$function]['parenthesis_opener'] + 1), null, true); |
|
| 85 | - if ($tokens[$arg]['content'] !== 'callback') { |
|
| 84 | + $arg = $phpcsFile->findNext( T_WHITESPACE, ( $tokens[ $function ][ 'parenthesis_opener' ] + 1 ), null, true ); |
|
| 85 | + if ( $tokens[ $arg ][ 'content' ] !== 'callback' ) { |
|
| 86 | 86 | $error = 'The first argument of the create() method of a widget type must be called "callback"'; |
| 87 | - $phpcsFile->addError($error, $arg, 'FirstArgNotCallback'); |
|
| 87 | + $phpcsFile->addError( $error, $arg, 'FirstArgNotCallback' ); |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /* |
@@ -99,69 +99,69 @@ discard block |
||
| 99 | 99 | $foundCallback = false; |
| 100 | 100 | $passedCallback = false; |
| 101 | 101 | $nestedFunction = null; |
| 102 | - for ($i = $start; $i <= $end; $i++) { |
|
| 102 | + for ( $i = $start; $i <= $end; $i++ ) { |
|
| 103 | 103 | // Keep track of nested functions. |
| 104 | - if ($nestedFunction !== null) { |
|
| 105 | - if ($i === $nestedFunction) { |
|
| 104 | + if ( $nestedFunction !== null ) { |
|
| 105 | + if ( $i === $nestedFunction ) { |
|
| 106 | 106 | $nestedFunction = null; |
| 107 | 107 | continue; |
| 108 | 108 | } |
| 109 | - } else if (($tokens[$i]['code'] === T_FUNCTION |
|
| 110 | - || $tokens[$i]['code'] === T_CLOSURE) |
|
| 111 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 109 | + } else if ( ( $tokens[ $i ][ 'code' ] === T_FUNCTION |
|
| 110 | + || $tokens[ $i ][ 'code' ] === T_CLOSURE ) |
|
| 111 | + && isset( $tokens[ $i ][ 'scope_closer' ] ) === true |
|
| 112 | 112 | ) { |
| 113 | - $nestedFunction = $tokens[$i]['scope_closer']; |
|
| 113 | + $nestedFunction = $tokens[ $i ][ 'scope_closer' ]; |
|
| 114 | 114 | continue; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - if ($nestedFunction === null && $tokens[$i]['code'] === T_RETURN) { |
|
| 117 | + if ( $nestedFunction === null && $tokens[ $i ][ 'code' ] === T_RETURN ) { |
|
| 118 | 118 | // Make sure return statements are not returning anything. |
| 119 | - if ($tokens[($i + 1)]['code'] !== T_SEMICOLON) { |
|
| 119 | + if ( $tokens[ ( $i + 1 ) ][ 'code' ] !== T_SEMICOLON ) { |
|
| 120 | 120 | $error = 'The create() method of a widget type must not return a value'; |
| 121 | - $phpcsFile->addError($error, $i, 'ReturnValue'); |
|
| 121 | + $phpcsFile->addError( $error, $i, 'ReturnValue' ); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | continue; |
| 125 | - } else if ($tokens[$i]['code'] !== T_STRING |
|
| 126 | - || $tokens[$i]['content'] !== 'callback' |
|
| 125 | + } else if ( $tokens[ $i ][ 'code' ] !== T_STRING |
|
| 126 | + || $tokens[ $i ][ 'content' ] !== 'callback' |
|
| 127 | 127 | ) { |
| 128 | 128 | continue; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // If this is the form "callback.call(" then it is a call |
| 132 | 132 | // to the callback function. |
| 133 | - if ($tokens[($i + 1)]['code'] !== T_OBJECT_OPERATOR |
|
| 134 | - || $tokens[($i + 2)]['content'] !== 'call' |
|
| 135 | - || $tokens[($i + 3)]['code'] !== T_OPEN_PARENTHESIS |
|
| 133 | + if ( $tokens[ ( $i + 1 ) ][ 'code' ] !== T_OBJECT_OPERATOR |
|
| 134 | + || $tokens[ ( $i + 2 ) ][ 'content' ] !== 'call' |
|
| 135 | + || $tokens[ ( $i + 3 ) ][ 'code' ] !== T_OPEN_PARENTHESIS |
|
| 136 | 136 | ) { |
| 137 | 137 | // One last chance; this might be the callback function |
| 138 | 138 | // being passed to another function, like this |
| 139 | 139 | // "this.init(something, callback, something)". |
| 140 | - if (isset($tokens[$i]['nested_parenthesis']) === false) { |
|
| 140 | + if ( isset( $tokens[ $i ][ 'nested_parenthesis' ] ) === false ) { |
|
| 141 | 141 | continue; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // Just make sure those brackets dont belong to anyone, |
| 145 | 145 | // like an IF or FOR statement. |
| 146 | - foreach ($tokens[$i]['nested_parenthesis'] as $bracket) { |
|
| 147 | - if (isset($tokens[$bracket]['parenthesis_owner']) === true) { |
|
| 148 | - continue(2); |
|
| 146 | + foreach ( $tokens[ $i ][ 'nested_parenthesis' ] as $bracket ) { |
|
| 147 | + if ( isset( $tokens[ $bracket ][ 'parenthesis_owner' ] ) === true ) { |
|
| 148 | + continue( 2 ); |
|
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Note that we use this endBracket down further when checking |
| 153 | 153 | // for a RETURN statement. |
| 154 | - $endBracket = end($tokens[$i]['nested_parenthesis']); |
|
| 155 | - $bracket = key($tokens[$i]['nested_parenthesis']); |
|
| 154 | + $endBracket = end( $tokens[ $i ][ 'nested_parenthesis' ] ); |
|
| 155 | + $bracket = key( $tokens[ $i ][ 'nested_parenthesis' ] ); |
|
| 156 | 156 | |
| 157 | 157 | $prev = $phpcsFile->findPrevious( |
| 158 | 158 | PHP_CodeSniffer_Tokens::$emptyTokens, |
| 159 | - ($bracket - 1), |
|
| 159 | + ( $bracket - 1 ), |
|
| 160 | 160 | null, |
| 161 | 161 | true |
| 162 | 162 | ); |
| 163 | 163 | |
| 164 | - if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 164 | + if ( $tokens[ $prev ][ 'code' ] !== T_STRING ) { |
|
| 165 | 165 | // This is not a function passing the callback. |
| 166 | 166 | continue; |
| 167 | 167 | } |
@@ -171,54 +171,54 @@ discard block |
||
| 171 | 171 | |
| 172 | 172 | $foundCallback = true; |
| 173 | 173 | |
| 174 | - if ($passedCallback === false) { |
|
| 174 | + if ( $passedCallback === false ) { |
|
| 175 | 175 | // The first argument must be "this" or "self". |
| 176 | - $arg = $phpcsFile->findNext(T_WHITESPACE, ($i + 4), null, true); |
|
| 177 | - if ($tokens[$arg]['content'] !== 'this' |
|
| 178 | - && $tokens[$arg]['content'] !== 'self' |
|
| 176 | + $arg = $phpcsFile->findNext( T_WHITESPACE, ( $i + 4 ), null, true ); |
|
| 177 | + if ( $tokens[ $arg ][ 'content' ] !== 'this' |
|
| 178 | + && $tokens[ $arg ][ 'content' ] !== 'self' |
|
| 179 | 179 | ) { |
| 180 | 180 | $error = 'The first argument passed to the callback function must be "this" or "self"'; |
| 181 | - $phpcsFile->addError($error, $arg, 'FirstArgNotSelf'); |
|
| 181 | + $phpcsFile->addError( $error, $arg, 'FirstArgNotSelf' ); |
|
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Now it must be followed by a return statement or the end of the function. |
| 186 | - if ($passedCallback === false) { |
|
| 187 | - $endBracket = $tokens[($i + 3)]['parenthesis_closer']; |
|
| 186 | + if ( $passedCallback === false ) { |
|
| 187 | + $endBracket = $tokens[ ( $i + 3 ) ][ 'parenthesis_closer' ]; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - for ($next = $endBracket; $next <= $end; $next++) { |
|
| 190 | + for ( $next = $endBracket; $next <= $end; $next++ ) { |
|
| 191 | 191 | // Skip whitespace so we find the next content after the call. |
| 192 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$next]['code']]) === true) { |
|
| 192 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $tokens[ $next ][ 'code' ] ] ) === true ) { |
|
| 193 | 193 | continue; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | // Skip closing braces like END IF because it is not executable code. |
| 197 | - if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 197 | + if ( $tokens[ $next ][ 'code' ] === T_CLOSE_CURLY_BRACKET ) { |
|
| 198 | 198 | continue; |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // We don't care about anything on the current line, like a |
| 202 | 202 | // semicolon. It doesn't matter if there are other statements on the |
| 203 | 203 | // line because another sniff will check for those. |
| 204 | - if ($tokens[$next]['line'] === $tokens[$endBracket]['line']) { |
|
| 204 | + if ( $tokens[ $next ][ 'line' ] === $tokens[ $endBracket ][ 'line' ] ) { |
|
| 205 | 205 | continue; |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | break; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - if ($next !== $tokens[$function]['scope_closer'] |
|
| 212 | - && $tokens[$next]['code'] !== T_RETURN |
|
| 211 | + if ( $next !== $tokens[ $function ][ 'scope_closer' ] |
|
| 212 | + && $tokens[ $next ][ 'code' ] !== T_RETURN |
|
| 213 | 213 | ) { |
| 214 | 214 | $error = 'The call to the callback function must be followed by a return statement if it is not the last statement in the create() method'; |
| 215 | - $phpcsFile->addError($error, $i, 'NoReturn'); |
|
| 215 | + $phpcsFile->addError( $error, $i, 'NoReturn' ); |
|
| 216 | 216 | } |
| 217 | 217 | }//end for |
| 218 | 218 | |
| 219 | - if ($foundCallback === false) { |
|
| 219 | + if ( $foundCallback === false ) { |
|
| 220 | 220 | $error = 'The create() method of a widget type must call the callback function'; |
| 221 | - $phpcsFile->addError($error, $create, 'CallbackNotCalled'); |
|
| 221 | + $phpcsFile->addError( $error, $create, 'CallbackNotCalled' ); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | }//end process() |
@@ -23,8 +23,7 @@ discard block |
||
| 23 | 23 | * @version Release: @package_version@ |
| 24 | 24 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 25 | 25 | */ |
| 26 | -class MySource_Sniffs_Objects_CreateWidgetTypeCallbackSniff implements PHP_CodeSniffer_Sniff |
|
| 27 | -{ |
|
| 26 | +class MySource_Sniffs_Objects_CreateWidgetTypeCallbackSniff implements PHP_CodeSniffer_Sniff { |
|
| 28 | 27 | |
| 29 | 28 | /** |
| 30 | 29 | * A list of tokenizers this sniff supports. |
@@ -39,8 +38,7 @@ discard block |
||
| 39 | 38 | * |
| 40 | 39 | * @return array |
| 41 | 40 | */ |
| 42 | - public function register() |
|
| 43 | - { |
|
| 41 | + public function register() { |
|
| 44 | 42 | return array(T_OBJECT); |
| 45 | 43 | |
| 46 | 44 | }//end register() |
@@ -55,8 +53,7 @@ discard block |
||
| 55 | 53 | * |
| 56 | 54 | * @return void |
| 57 | 55 | */ |
| 58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 60 | 57 | $tokens = $phpcsFile->getTokens(); |
| 61 | 58 | |
| 62 | 59 | $className = $phpcsFile->findPrevious(T_STRING, ($stackPtr - 1)); |