@@ -301,7 +301,7 @@ |
||
| 301 | 301 | /** |
| 302 | 302 | * Get an array of the non-PHP-version array keys used in a sub-array. |
| 303 | 303 | * |
| 304 | - * @return array |
|
| 304 | + * @return string[] |
|
| 305 | 305 | */ |
| 306 | 306 | protected function getNonVersionArrayKeys() |
| 307 | 307 | { |
@@ -25,196 +25,196 @@ discard block |
||
| 25 | 25 | class NewKeywordsSniff extends AbstractNewFeatureSniff |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * A list of new keywords, not present in older versions. |
|
| 30 | - * |
|
| 31 | - * The array lists : version number with false (not present) or true (present). |
|
| 32 | - * If's sufficient to list the last version which did not contain the keyword. |
|
| 33 | - * |
|
| 34 | - * Description will be used as part of the error message. |
|
| 35 | - * Condition is the name of a callback method within this class or the parent class |
|
| 36 | - * which checks whether the token complies with a certain condition. |
|
| 37 | - * The callback function will be passed the $phpcsFile and the $stackPtr. |
|
| 38 | - * The callback function should return `true` if the condition is met and the |
|
| 39 | - * error should *not* be thrown. |
|
| 40 | - * |
|
| 41 | - * @var array(string => array(string => int|string|null)) |
|
| 42 | - */ |
|
| 43 | - protected $newKeywords = array( |
|
| 44 | - 'T_HALT_COMPILER' => array( |
|
| 45 | - '5.0' => false, |
|
| 46 | - '5.1' => true, |
|
| 47 | - 'description' => '"__halt_compiler" keyword', |
|
| 48 | - ), |
|
| 49 | - 'T_CONST' => array( |
|
| 50 | - '5.2' => false, |
|
| 51 | - '5.3' => true, |
|
| 52 | - 'description' => '"const" keyword', |
|
| 53 | - 'condition' => 'isClassConstant', // Keyword is only new when not in class context. |
|
| 54 | - ), |
|
| 55 | - 'T_CALLABLE' => array( |
|
| 56 | - '5.3' => false, |
|
| 57 | - '5.4' => true, |
|
| 58 | - 'description' => '"callable" keyword', |
|
| 59 | - 'content' => 'callable', |
|
| 60 | - ), |
|
| 61 | - 'T_DIR' => array( |
|
| 62 | - '5.2' => false, |
|
| 63 | - '5.3' => true, |
|
| 64 | - 'description' => '__DIR__ magic constant', |
|
| 65 | - 'content' => '__DIR__', |
|
| 66 | - ), |
|
| 67 | - 'T_GOTO' => array( |
|
| 68 | - '5.2' => false, |
|
| 69 | - '5.3' => true, |
|
| 70 | - 'description' => '"goto" keyword', |
|
| 71 | - 'content' => 'goto', |
|
| 72 | - ), |
|
| 73 | - 'T_INSTEADOF' => array( |
|
| 74 | - '5.3' => false, |
|
| 75 | - '5.4' => true, |
|
| 76 | - 'description' => '"insteadof" keyword (for traits)', |
|
| 77 | - 'content' => 'insteadof', |
|
| 78 | - ), |
|
| 79 | - 'T_NAMESPACE' => array( |
|
| 80 | - '5.2' => false, |
|
| 81 | - '5.3' => true, |
|
| 82 | - 'description' => '"namespace" keyword', |
|
| 83 | - 'content' => 'namespace', |
|
| 84 | - ), |
|
| 85 | - 'T_NS_C' => array( |
|
| 86 | - '5.2' => false, |
|
| 87 | - '5.3' => true, |
|
| 88 | - 'description' => '__NAMESPACE__ magic constant', |
|
| 89 | - 'content' => '__NAMESPACE__', |
|
| 90 | - ), |
|
| 91 | - 'T_USE' => array( |
|
| 92 | - '5.2' => false, |
|
| 93 | - '5.3' => true, |
|
| 94 | - 'description' => '"use" keyword (for traits/namespaces/anonymous functions)', |
|
| 95 | - ), |
|
| 96 | - 'T_START_NOWDOC' => array( |
|
| 97 | - '5.2' => false, |
|
| 98 | - '5.3' => true, |
|
| 99 | - 'description' => 'nowdoc functionality', |
|
| 100 | - ), |
|
| 101 | - 'T_END_NOWDOC' => array( |
|
| 102 | - '5.2' => false, |
|
| 103 | - '5.3' => true, |
|
| 104 | - 'description' => 'nowdoc functionality', |
|
| 105 | - ), |
|
| 106 | - 'T_START_HEREDOC' => array( |
|
| 107 | - '5.2' => false, |
|
| 108 | - '5.3' => true, |
|
| 109 | - 'description' => '(Double) quoted Heredoc identifier', |
|
| 110 | - 'condition' => 'isNotQuoted', // Heredoc is only new with quoted identifier. |
|
| 111 | - ), |
|
| 112 | - 'T_TRAIT' => array( |
|
| 113 | - '5.3' => false, |
|
| 114 | - '5.4' => true, |
|
| 115 | - 'description' => '"trait" keyword', |
|
| 116 | - 'content' => 'trait', |
|
| 117 | - ), |
|
| 118 | - 'T_TRAIT_C' => array( |
|
| 119 | - '5.3' => false, |
|
| 120 | - '5.4' => true, |
|
| 121 | - 'description' => '__TRAIT__ magic constant', |
|
| 122 | - 'content' => '__TRAIT__', |
|
| 123 | - ), |
|
| 124 | - // The specifics for distinguishing between 'yield' and 'yield from' are dealt |
|
| 125 | - // with in the translation logic. |
|
| 126 | - // This token has to be placed above the `T_YIELD` token in this array to allow for this. |
|
| 127 | - 'T_YIELD_FROM' => array( |
|
| 128 | - '5.6' => false, |
|
| 129 | - '7.0' => true, |
|
| 130 | - 'description' => '"yield from" keyword (for generators)', |
|
| 131 | - 'content' => 'yield', |
|
| 132 | - ), |
|
| 133 | - 'T_YIELD' => array( |
|
| 134 | - '5.4' => false, |
|
| 135 | - '5.5' => true, |
|
| 136 | - 'description' => '"yield" keyword (for generators)', |
|
| 137 | - 'content' => 'yield', |
|
| 138 | - ), |
|
| 139 | - 'T_FINALLY' => array( |
|
| 140 | - '5.4' => false, |
|
| 141 | - '5.5' => true, |
|
| 142 | - 'description' => '"finally" keyword (in exception handling)', |
|
| 143 | - 'content' => 'finally', |
|
| 144 | - ), |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Translation table for T_STRING tokens. |
|
| 149 | - * |
|
| 150 | - * Will be set up from the register() method. |
|
| 151 | - * |
|
| 152 | - * @var array(string => string) |
|
| 153 | - */ |
|
| 154 | - protected $translateContentToToken = array(); |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Returns an array of tokens this test wants to listen for. |
|
| 159 | - * |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function register() |
|
| 163 | - { |
|
| 164 | - $tokens = array(); |
|
| 165 | - $translate = array(); |
|
| 166 | - foreach ($this->newKeywords as $token => $versions) { |
|
| 167 | - if (\defined($token)) { |
|
| 168 | - $tokens[] = constant($token); |
|
| 169 | - } |
|
| 170 | - if (isset($versions['content'])) { |
|
| 171 | - $translate[strtolower($versions['content'])] = $token; |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /* |
|
| 28 | + /** |
|
| 29 | + * A list of new keywords, not present in older versions. |
|
| 30 | + * |
|
| 31 | + * The array lists : version number with false (not present) or true (present). |
|
| 32 | + * If's sufficient to list the last version which did not contain the keyword. |
|
| 33 | + * |
|
| 34 | + * Description will be used as part of the error message. |
|
| 35 | + * Condition is the name of a callback method within this class or the parent class |
|
| 36 | + * which checks whether the token complies with a certain condition. |
|
| 37 | + * The callback function will be passed the $phpcsFile and the $stackPtr. |
|
| 38 | + * The callback function should return `true` if the condition is met and the |
|
| 39 | + * error should *not* be thrown. |
|
| 40 | + * |
|
| 41 | + * @var array(string => array(string => int|string|null)) |
|
| 42 | + */ |
|
| 43 | + protected $newKeywords = array( |
|
| 44 | + 'T_HALT_COMPILER' => array( |
|
| 45 | + '5.0' => false, |
|
| 46 | + '5.1' => true, |
|
| 47 | + 'description' => '"__halt_compiler" keyword', |
|
| 48 | + ), |
|
| 49 | + 'T_CONST' => array( |
|
| 50 | + '5.2' => false, |
|
| 51 | + '5.3' => true, |
|
| 52 | + 'description' => '"const" keyword', |
|
| 53 | + 'condition' => 'isClassConstant', // Keyword is only new when not in class context. |
|
| 54 | + ), |
|
| 55 | + 'T_CALLABLE' => array( |
|
| 56 | + '5.3' => false, |
|
| 57 | + '5.4' => true, |
|
| 58 | + 'description' => '"callable" keyword', |
|
| 59 | + 'content' => 'callable', |
|
| 60 | + ), |
|
| 61 | + 'T_DIR' => array( |
|
| 62 | + '5.2' => false, |
|
| 63 | + '5.3' => true, |
|
| 64 | + 'description' => '__DIR__ magic constant', |
|
| 65 | + 'content' => '__DIR__', |
|
| 66 | + ), |
|
| 67 | + 'T_GOTO' => array( |
|
| 68 | + '5.2' => false, |
|
| 69 | + '5.3' => true, |
|
| 70 | + 'description' => '"goto" keyword', |
|
| 71 | + 'content' => 'goto', |
|
| 72 | + ), |
|
| 73 | + 'T_INSTEADOF' => array( |
|
| 74 | + '5.3' => false, |
|
| 75 | + '5.4' => true, |
|
| 76 | + 'description' => '"insteadof" keyword (for traits)', |
|
| 77 | + 'content' => 'insteadof', |
|
| 78 | + ), |
|
| 79 | + 'T_NAMESPACE' => array( |
|
| 80 | + '5.2' => false, |
|
| 81 | + '5.3' => true, |
|
| 82 | + 'description' => '"namespace" keyword', |
|
| 83 | + 'content' => 'namespace', |
|
| 84 | + ), |
|
| 85 | + 'T_NS_C' => array( |
|
| 86 | + '5.2' => false, |
|
| 87 | + '5.3' => true, |
|
| 88 | + 'description' => '__NAMESPACE__ magic constant', |
|
| 89 | + 'content' => '__NAMESPACE__', |
|
| 90 | + ), |
|
| 91 | + 'T_USE' => array( |
|
| 92 | + '5.2' => false, |
|
| 93 | + '5.3' => true, |
|
| 94 | + 'description' => '"use" keyword (for traits/namespaces/anonymous functions)', |
|
| 95 | + ), |
|
| 96 | + 'T_START_NOWDOC' => array( |
|
| 97 | + '5.2' => false, |
|
| 98 | + '5.3' => true, |
|
| 99 | + 'description' => 'nowdoc functionality', |
|
| 100 | + ), |
|
| 101 | + 'T_END_NOWDOC' => array( |
|
| 102 | + '5.2' => false, |
|
| 103 | + '5.3' => true, |
|
| 104 | + 'description' => 'nowdoc functionality', |
|
| 105 | + ), |
|
| 106 | + 'T_START_HEREDOC' => array( |
|
| 107 | + '5.2' => false, |
|
| 108 | + '5.3' => true, |
|
| 109 | + 'description' => '(Double) quoted Heredoc identifier', |
|
| 110 | + 'condition' => 'isNotQuoted', // Heredoc is only new with quoted identifier. |
|
| 111 | + ), |
|
| 112 | + 'T_TRAIT' => array( |
|
| 113 | + '5.3' => false, |
|
| 114 | + '5.4' => true, |
|
| 115 | + 'description' => '"trait" keyword', |
|
| 116 | + 'content' => 'trait', |
|
| 117 | + ), |
|
| 118 | + 'T_TRAIT_C' => array( |
|
| 119 | + '5.3' => false, |
|
| 120 | + '5.4' => true, |
|
| 121 | + 'description' => '__TRAIT__ magic constant', |
|
| 122 | + 'content' => '__TRAIT__', |
|
| 123 | + ), |
|
| 124 | + // The specifics for distinguishing between 'yield' and 'yield from' are dealt |
|
| 125 | + // with in the translation logic. |
|
| 126 | + // This token has to be placed above the `T_YIELD` token in this array to allow for this. |
|
| 127 | + 'T_YIELD_FROM' => array( |
|
| 128 | + '5.6' => false, |
|
| 129 | + '7.0' => true, |
|
| 130 | + 'description' => '"yield from" keyword (for generators)', |
|
| 131 | + 'content' => 'yield', |
|
| 132 | + ), |
|
| 133 | + 'T_YIELD' => array( |
|
| 134 | + '5.4' => false, |
|
| 135 | + '5.5' => true, |
|
| 136 | + 'description' => '"yield" keyword (for generators)', |
|
| 137 | + 'content' => 'yield', |
|
| 138 | + ), |
|
| 139 | + 'T_FINALLY' => array( |
|
| 140 | + '5.4' => false, |
|
| 141 | + '5.5' => true, |
|
| 142 | + 'description' => '"finally" keyword (in exception handling)', |
|
| 143 | + 'content' => 'finally', |
|
| 144 | + ), |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Translation table for T_STRING tokens. |
|
| 149 | + * |
|
| 150 | + * Will be set up from the register() method. |
|
| 151 | + * |
|
| 152 | + * @var array(string => string) |
|
| 153 | + */ |
|
| 154 | + protected $translateContentToToken = array(); |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Returns an array of tokens this test wants to listen for. |
|
| 159 | + * |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function register() |
|
| 163 | + { |
|
| 164 | + $tokens = array(); |
|
| 165 | + $translate = array(); |
|
| 166 | + foreach ($this->newKeywords as $token => $versions) { |
|
| 167 | + if (\defined($token)) { |
|
| 168 | + $tokens[] = constant($token); |
|
| 169 | + } |
|
| 170 | + if (isset($versions['content'])) { |
|
| 171 | + $translate[strtolower($versions['content'])] = $token; |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /* |
|
| 176 | 176 | * Deal with tokens not recognized by the PHP version the sniffer is run |
| 177 | 177 | * under and (not correctly) compensated for by PHPCS. |
| 178 | 178 | */ |
| 179 | - if (empty($translate) === false) { |
|
| 180 | - $this->translateContentToToken = $translate; |
|
| 181 | - $tokens[] = \T_STRING; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return $tokens; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Processes this test, when one of its tokens is encountered. |
|
| 190 | - * |
|
| 191 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 192 | - * @param int $stackPtr The position of the current token in |
|
| 193 | - * the stack passed in $tokens. |
|
| 194 | - * |
|
| 195 | - * @return void |
|
| 196 | - */ |
|
| 197 | - public function process(File $phpcsFile, $stackPtr) |
|
| 198 | - { |
|
| 199 | - $tokens = $phpcsFile->getTokens(); |
|
| 200 | - $tokenType = $tokens[$stackPtr]['type']; |
|
| 201 | - |
|
| 202 | - // Allow for dealing with multi-token keywords, like "yield from". |
|
| 203 | - $end = $stackPtr; |
|
| 204 | - |
|
| 205 | - // Translate T_STRING token if necessary. |
|
| 206 | - if ($tokens[$stackPtr]['type'] === 'T_STRING') { |
|
| 207 | - $content = strtolower($tokens[$stackPtr]['content']); |
|
| 208 | - |
|
| 209 | - if (isset($this->translateContentToToken[$content]) === false) { |
|
| 210 | - // Not one of the tokens we're looking for. |
|
| 211 | - return; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $tokenType = $this->translateContentToToken[$content]; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /* |
|
| 179 | + if (empty($translate) === false) { |
|
| 180 | + $this->translateContentToToken = $translate; |
|
| 181 | + $tokens[] = \T_STRING; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return $tokens; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Processes this test, when one of its tokens is encountered. |
|
| 190 | + * |
|
| 191 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 192 | + * @param int $stackPtr The position of the current token in |
|
| 193 | + * the stack passed in $tokens. |
|
| 194 | + * |
|
| 195 | + * @return void |
|
| 196 | + */ |
|
| 197 | + public function process(File $phpcsFile, $stackPtr) |
|
| 198 | + { |
|
| 199 | + $tokens = $phpcsFile->getTokens(); |
|
| 200 | + $tokenType = $tokens[$stackPtr]['type']; |
|
| 201 | + |
|
| 202 | + // Allow for dealing with multi-token keywords, like "yield from". |
|
| 203 | + $end = $stackPtr; |
|
| 204 | + |
|
| 205 | + // Translate T_STRING token if necessary. |
|
| 206 | + if ($tokens[$stackPtr]['type'] === 'T_STRING') { |
|
| 207 | + $content = strtolower($tokens[$stackPtr]['content']); |
|
| 208 | + |
|
| 209 | + if (isset($this->translateContentToToken[$content]) === false) { |
|
| 210 | + // Not one of the tokens we're looking for. |
|
| 211 | + return; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $tokenType = $this->translateContentToToken[$content]; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /* |
|
| 218 | 218 | * Special case: distinguish between `yield` and `yield from`. |
| 219 | 219 | * |
| 220 | 220 | * PHPCS currently (at least up to v 3.0.1) does not backfill for the |
@@ -227,140 +227,140 @@ discard block |
||
| 227 | 227 | * In PHP 7.0+ both are tokenized as their respective token, however, |
| 228 | 228 | * a multi-line "yield from" is tokenized as two tokens. |
| 229 | 229 | */ |
| 230 | - if ($tokenType === 'T_YIELD') { |
|
| 231 | - $nextToken = $phpcsFile->findNext(\T_WHITESPACE, ($end + 1), null, true); |
|
| 232 | - if ($tokens[$nextToken]['code'] === \T_STRING |
|
| 233 | - && $tokens[$nextToken]['content'] === 'from' |
|
| 234 | - ) { |
|
| 235 | - $tokenType = 'T_YIELD_FROM'; |
|
| 236 | - $end = $nextToken; |
|
| 237 | - } |
|
| 238 | - unset($nextToken); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - if ($tokenType === 'T_YIELD_FROM' && $tokens[($stackPtr - 1)]['type'] === 'T_YIELD_FROM') { |
|
| 242 | - // Multi-line "yield from", no need to report it twice. |
|
| 243 | - return; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if (isset($this->newKeywords[$tokenType]) === false) { |
|
| 247 | - return; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true); |
|
| 251 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 252 | - |
|
| 253 | - if ($prevToken !== false |
|
| 254 | - && ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON |
|
| 255 | - || $tokens[$prevToken]['code'] === \T_OBJECT_OPERATOR) |
|
| 256 | - ) { |
|
| 257 | - // Class property of the same name as one of the keywords. Ignore. |
|
| 258 | - return; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // Skip attempts to use keywords as functions or class names - the former |
|
| 262 | - // will be reported by ForbiddenNamesAsInvokedFunctionsSniff, whilst the |
|
| 263 | - // latter will be (partially) reported by the ForbiddenNames sniff. |
|
| 264 | - // Either type will result in false-positives when targetting lower versions |
|
| 265 | - // of PHP where the name was not reserved, unless we explicitly check for |
|
| 266 | - // them. |
|
| 267 | - if (($nextToken === false |
|
| 268 | - || $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS') |
|
| 269 | - && ($prevToken === false |
|
| 270 | - || $tokens[$prevToken]['type'] !== 'T_CLASS' |
|
| 271 | - || $tokens[$prevToken]['type'] !== 'T_INTERFACE') |
|
| 272 | - ) { |
|
| 273 | - // Skip based on token scope condition. |
|
| 274 | - if (isset($this->newKeywords[$tokenType]['condition']) |
|
| 275 | - && \call_user_func(array($this, $this->newKeywords[$tokenType]['condition']), $phpcsFile, $stackPtr) === true |
|
| 276 | - ) { |
|
| 277 | - return; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $itemInfo = array( |
|
| 281 | - 'name' => $tokenType, |
|
| 282 | - ); |
|
| 283 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
| 290 | - * |
|
| 291 | - * @param array $itemInfo Base information about the item. |
|
| 292 | - * |
|
| 293 | - * @return array Version and other information about the item. |
|
| 294 | - */ |
|
| 295 | - public function getItemArray(array $itemInfo) |
|
| 296 | - { |
|
| 297 | - return $this->newKeywords[$itemInfo['name']]; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Get an array of the non-PHP-version array keys used in a sub-array. |
|
| 303 | - * |
|
| 304 | - * @return array |
|
| 305 | - */ |
|
| 306 | - protected function getNonVersionArrayKeys() |
|
| 307 | - { |
|
| 308 | - return array( |
|
| 309 | - 'description', |
|
| 310 | - 'condition', |
|
| 311 | - 'content', |
|
| 312 | - ); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Retrieve the relevant detail (version) information for use in an error message. |
|
| 318 | - * |
|
| 319 | - * @param array $itemArray Version and other information about the item. |
|
| 320 | - * @param array $itemInfo Base information about the item. |
|
| 321 | - * |
|
| 322 | - * @return array |
|
| 323 | - */ |
|
| 324 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 325 | - { |
|
| 326 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 327 | - $errorInfo['description'] = $itemArray['description']; |
|
| 328 | - |
|
| 329 | - return $errorInfo; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
|
| 335 | - * |
|
| 336 | - * @param array $data The error data array which was created. |
|
| 337 | - * @param array $itemInfo Base information about the item this error message applies to. |
|
| 338 | - * @param array $errorInfo Detail information about an item this error message applies to. |
|
| 339 | - * |
|
| 340 | - * @return array |
|
| 341 | - */ |
|
| 342 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
| 343 | - { |
|
| 344 | - $data[0] = $errorInfo['description']; |
|
| 345 | - return $data; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Callback for the quoted heredoc identifier condition. |
|
| 351 | - * |
|
| 352 | - * A double quoted identifier will have the opening quote on position 3 |
|
| 353 | - * in the string: `<<<"ID"`. |
|
| 354 | - * |
|
| 355 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 356 | - * @param int $stackPtr The position of the current token in |
|
| 357 | - * the stack passed in $tokens. |
|
| 358 | - * |
|
| 359 | - * @return bool |
|
| 360 | - */ |
|
| 361 | - public function isNotQuoted(File $phpcsFile, $stackPtr) |
|
| 362 | - { |
|
| 363 | - $tokens = $phpcsFile->getTokens(); |
|
| 364 | - return ($tokens[$stackPtr]['content'][3] !== '"'); |
|
| 365 | - } |
|
| 230 | + if ($tokenType === 'T_YIELD') { |
|
| 231 | + $nextToken = $phpcsFile->findNext(\T_WHITESPACE, ($end + 1), null, true); |
|
| 232 | + if ($tokens[$nextToken]['code'] === \T_STRING |
|
| 233 | + && $tokens[$nextToken]['content'] === 'from' |
|
| 234 | + ) { |
|
| 235 | + $tokenType = 'T_YIELD_FROM'; |
|
| 236 | + $end = $nextToken; |
|
| 237 | + } |
|
| 238 | + unset($nextToken); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + if ($tokenType === 'T_YIELD_FROM' && $tokens[($stackPtr - 1)]['type'] === 'T_YIELD_FROM') { |
|
| 242 | + // Multi-line "yield from", no need to report it twice. |
|
| 243 | + return; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if (isset($this->newKeywords[$tokenType]) === false) { |
|
| 247 | + return; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true); |
|
| 251 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 252 | + |
|
| 253 | + if ($prevToken !== false |
|
| 254 | + && ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON |
|
| 255 | + || $tokens[$prevToken]['code'] === \T_OBJECT_OPERATOR) |
|
| 256 | + ) { |
|
| 257 | + // Class property of the same name as one of the keywords. Ignore. |
|
| 258 | + return; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + // Skip attempts to use keywords as functions or class names - the former |
|
| 262 | + // will be reported by ForbiddenNamesAsInvokedFunctionsSniff, whilst the |
|
| 263 | + // latter will be (partially) reported by the ForbiddenNames sniff. |
|
| 264 | + // Either type will result in false-positives when targetting lower versions |
|
| 265 | + // of PHP where the name was not reserved, unless we explicitly check for |
|
| 266 | + // them. |
|
| 267 | + if (($nextToken === false |
|
| 268 | + || $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS') |
|
| 269 | + && ($prevToken === false |
|
| 270 | + || $tokens[$prevToken]['type'] !== 'T_CLASS' |
|
| 271 | + || $tokens[$prevToken]['type'] !== 'T_INTERFACE') |
|
| 272 | + ) { |
|
| 273 | + // Skip based on token scope condition. |
|
| 274 | + if (isset($this->newKeywords[$tokenType]['condition']) |
|
| 275 | + && \call_user_func(array($this, $this->newKeywords[$tokenType]['condition']), $phpcsFile, $stackPtr) === true |
|
| 276 | + ) { |
|
| 277 | + return; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $itemInfo = array( |
|
| 281 | + 'name' => $tokenType, |
|
| 282 | + ); |
|
| 283 | + $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
| 290 | + * |
|
| 291 | + * @param array $itemInfo Base information about the item. |
|
| 292 | + * |
|
| 293 | + * @return array Version and other information about the item. |
|
| 294 | + */ |
|
| 295 | + public function getItemArray(array $itemInfo) |
|
| 296 | + { |
|
| 297 | + return $this->newKeywords[$itemInfo['name']]; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Get an array of the non-PHP-version array keys used in a sub-array. |
|
| 303 | + * |
|
| 304 | + * @return array |
|
| 305 | + */ |
|
| 306 | + protected function getNonVersionArrayKeys() |
|
| 307 | + { |
|
| 308 | + return array( |
|
| 309 | + 'description', |
|
| 310 | + 'condition', |
|
| 311 | + 'content', |
|
| 312 | + ); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Retrieve the relevant detail (version) information for use in an error message. |
|
| 318 | + * |
|
| 319 | + * @param array $itemArray Version and other information about the item. |
|
| 320 | + * @param array $itemInfo Base information about the item. |
|
| 321 | + * |
|
| 322 | + * @return array |
|
| 323 | + */ |
|
| 324 | + public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 325 | + { |
|
| 326 | + $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 327 | + $errorInfo['description'] = $itemArray['description']; |
|
| 328 | + |
|
| 329 | + return $errorInfo; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
|
| 335 | + * |
|
| 336 | + * @param array $data The error data array which was created. |
|
| 337 | + * @param array $itemInfo Base information about the item this error message applies to. |
|
| 338 | + * @param array $errorInfo Detail information about an item this error message applies to. |
|
| 339 | + * |
|
| 340 | + * @return array |
|
| 341 | + */ |
|
| 342 | + protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
| 343 | + { |
|
| 344 | + $data[0] = $errorInfo['description']; |
|
| 345 | + return $data; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Callback for the quoted heredoc identifier condition. |
|
| 351 | + * |
|
| 352 | + * A double quoted identifier will have the opening quote on position 3 |
|
| 353 | + * in the string: `<<<"ID"`. |
|
| 354 | + * |
|
| 355 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 356 | + * @param int $stackPtr The position of the current token in |
|
| 357 | + * the stack passed in $tokens. |
|
| 358 | + * |
|
| 359 | + * @return bool |
|
| 360 | + */ |
|
| 361 | + public function isNotQuoted(File $phpcsFile, $stackPtr) |
|
| 362 | + { |
|
| 363 | + $tokens = $phpcsFile->getTokens(); |
|
| 364 | + return ($tokens[$stackPtr]['content'][3] !== '"'); |
|
| 365 | + } |
|
| 366 | 366 | } |
@@ -163,12 +163,12 @@ discard block |
||
| 163 | 163 | { |
| 164 | 164 | $tokens = array(); |
| 165 | 165 | $translate = array(); |
| 166 | - foreach ($this->newKeywords as $token => $versions) { |
|
| 167 | - if (\defined($token)) { |
|
| 168 | - $tokens[] = constant($token); |
|
| 166 | + foreach ( $this->newKeywords as $token => $versions ) { |
|
| 167 | + if ( \defined( $token ) ) { |
|
| 168 | + $tokens[ ] = constant( $token ); |
|
| 169 | 169 | } |
| 170 | - if (isset($versions['content'])) { |
|
| 171 | - $translate[strtolower($versions['content'])] = $token; |
|
| 170 | + if ( isset( $versions[ 'content' ] ) ) { |
|
| 171 | + $translate[ strtolower( $versions[ 'content' ] ) ] = $token; |
|
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | |
@@ -176,9 +176,9 @@ discard block |
||
| 176 | 176 | * Deal with tokens not recognized by the PHP version the sniffer is run |
| 177 | 177 | * under and (not correctly) compensated for by PHPCS. |
| 178 | 178 | */ |
| 179 | - if (empty($translate) === false) { |
|
| 179 | + if ( empty( $translate ) === false ) { |
|
| 180 | 180 | $this->translateContentToToken = $translate; |
| 181 | - $tokens[] = \T_STRING; |
|
| 181 | + $tokens[ ] = \T_STRING; |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | return $tokens; |
@@ -194,24 +194,24 @@ discard block |
||
| 194 | 194 | * |
| 195 | 195 | * @return void |
| 196 | 196 | */ |
| 197 | - public function process(File $phpcsFile, $stackPtr) |
|
| 197 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 198 | 198 | { |
| 199 | 199 | $tokens = $phpcsFile->getTokens(); |
| 200 | - $tokenType = $tokens[$stackPtr]['type']; |
|
| 200 | + $tokenType = $tokens[ $stackPtr ][ 'type' ]; |
|
| 201 | 201 | |
| 202 | 202 | // Allow for dealing with multi-token keywords, like "yield from". |
| 203 | 203 | $end = $stackPtr; |
| 204 | 204 | |
| 205 | 205 | // Translate T_STRING token if necessary. |
| 206 | - if ($tokens[$stackPtr]['type'] === 'T_STRING') { |
|
| 207 | - $content = strtolower($tokens[$stackPtr]['content']); |
|
| 206 | + if ( $tokens[ $stackPtr ][ 'type' ] === 'T_STRING' ) { |
|
| 207 | + $content = strtolower( $tokens[ $stackPtr ][ 'content' ] ); |
|
| 208 | 208 | |
| 209 | - if (isset($this->translateContentToToken[$content]) === false) { |
|
| 209 | + if ( isset( $this->translateContentToToken[ $content ] ) === false ) { |
|
| 210 | 210 | // Not one of the tokens we're looking for. |
| 211 | 211 | return; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - $tokenType = $this->translateContentToToken[$content]; |
|
| 214 | + $tokenType = $this->translateContentToToken[ $content ]; |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /* |
@@ -227,32 +227,32 @@ discard block |
||
| 227 | 227 | * In PHP 7.0+ both are tokenized as their respective token, however, |
| 228 | 228 | * a multi-line "yield from" is tokenized as two tokens. |
| 229 | 229 | */ |
| 230 | - if ($tokenType === 'T_YIELD') { |
|
| 231 | - $nextToken = $phpcsFile->findNext(\T_WHITESPACE, ($end + 1), null, true); |
|
| 232 | - if ($tokens[$nextToken]['code'] === \T_STRING |
|
| 233 | - && $tokens[$nextToken]['content'] === 'from' |
|
| 230 | + if ( $tokenType === 'T_YIELD' ) { |
|
| 231 | + $nextToken = $phpcsFile->findNext( \T_WHITESPACE, ( $end + 1 ), null, true ); |
|
| 232 | + if ( $tokens[ $nextToken ][ 'code' ] === \T_STRING |
|
| 233 | + && $tokens[ $nextToken ][ 'content' ] === 'from' |
|
| 234 | 234 | ) { |
| 235 | 235 | $tokenType = 'T_YIELD_FROM'; |
| 236 | 236 | $end = $nextToken; |
| 237 | 237 | } |
| 238 | - unset($nextToken); |
|
| 238 | + unset( $nextToken ); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - if ($tokenType === 'T_YIELD_FROM' && $tokens[($stackPtr - 1)]['type'] === 'T_YIELD_FROM') { |
|
| 241 | + if ( $tokenType === 'T_YIELD_FROM' && $tokens[ ( $stackPtr - 1 ) ][ 'type' ] === 'T_YIELD_FROM' ) { |
|
| 242 | 242 | // Multi-line "yield from", no need to report it twice. |
| 243 | 243 | return; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - if (isset($this->newKeywords[$tokenType]) === false) { |
|
| 246 | + if ( isset( $this->newKeywords[ $tokenType ] ) === false ) { |
|
| 247 | 247 | return; |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true); |
|
| 251 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 250 | + $nextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ( $end + 1 ), null, true ); |
|
| 251 | + $prevToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); |
|
| 252 | 252 | |
| 253 | - if ($prevToken !== false |
|
| 254 | - && ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON |
|
| 255 | - || $tokens[$prevToken]['code'] === \T_OBJECT_OPERATOR) |
|
| 253 | + if ( $prevToken !== false |
|
| 254 | + && ( $tokens[ $prevToken ][ 'code' ] === \T_DOUBLE_COLON |
|
| 255 | + || $tokens[ $prevToken ][ 'code' ] === \T_OBJECT_OPERATOR ) |
|
| 256 | 256 | ) { |
| 257 | 257 | // Class property of the same name as one of the keywords. Ignore. |
| 258 | 258 | return; |
@@ -264,15 +264,15 @@ discard block |
||
| 264 | 264 | // Either type will result in false-positives when targetting lower versions |
| 265 | 265 | // of PHP where the name was not reserved, unless we explicitly check for |
| 266 | 266 | // them. |
| 267 | - if (($nextToken === false |
|
| 268 | - || $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS') |
|
| 269 | - && ($prevToken === false |
|
| 270 | - || $tokens[$prevToken]['type'] !== 'T_CLASS' |
|
| 271 | - || $tokens[$prevToken]['type'] !== 'T_INTERFACE') |
|
| 267 | + if ( ( $nextToken === false |
|
| 268 | + || $tokens[ $nextToken ][ 'type' ] !== 'T_OPEN_PARENTHESIS' ) |
|
| 269 | + && ( $prevToken === false |
|
| 270 | + || $tokens[ $prevToken ][ 'type' ] !== 'T_CLASS' |
|
| 271 | + || $tokens[ $prevToken ][ 'type' ] !== 'T_INTERFACE' ) |
|
| 272 | 272 | ) { |
| 273 | 273 | // Skip based on token scope condition. |
| 274 | - if (isset($this->newKeywords[$tokenType]['condition']) |
|
| 275 | - && \call_user_func(array($this, $this->newKeywords[$tokenType]['condition']), $phpcsFile, $stackPtr) === true |
|
| 274 | + if ( isset( $this->newKeywords[ $tokenType ][ 'condition' ] ) |
|
| 275 | + && \call_user_func( array( $this, $this->newKeywords[ $tokenType ][ 'condition' ] ), $phpcsFile, $stackPtr ) === true |
|
| 276 | 276 | ) { |
| 277 | 277 | return; |
| 278 | 278 | } |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | $itemInfo = array( |
| 281 | 281 | 'name' => $tokenType, |
| 282 | 282 | ); |
| 283 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
| 283 | + $this->handleFeature( $phpcsFile, $stackPtr, $itemInfo ); |
|
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
@@ -292,9 +292,9 @@ discard block |
||
| 292 | 292 | * |
| 293 | 293 | * @return array Version and other information about the item. |
| 294 | 294 | */ |
| 295 | - public function getItemArray(array $itemInfo) |
|
| 295 | + public function getItemArray( array $itemInfo ) |
|
| 296 | 296 | { |
| 297 | - return $this->newKeywords[$itemInfo['name']]; |
|
| 297 | + return $this->newKeywords[ $itemInfo[ 'name' ] ]; |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | |
@@ -321,10 +321,10 @@ discard block |
||
| 321 | 321 | * |
| 322 | 322 | * @return array |
| 323 | 323 | */ |
| 324 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 324 | + public function getErrorInfo( array $itemArray, array $itemInfo ) |
|
| 325 | 325 | { |
| 326 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
| 327 | - $errorInfo['description'] = $itemArray['description']; |
|
| 326 | + $errorInfo = parent::getErrorInfo( $itemArray, $itemInfo ); |
|
| 327 | + $errorInfo[ 'description' ] = $itemArray[ 'description' ]; |
|
| 328 | 328 | |
| 329 | 329 | return $errorInfo; |
| 330 | 330 | } |
@@ -339,9 +339,9 @@ discard block |
||
| 339 | 339 | * |
| 340 | 340 | * @return array |
| 341 | 341 | */ |
| 342 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
| 342 | + protected function filterErrorData( array $data, array $itemInfo, array $errorInfo ) |
|
| 343 | 343 | { |
| 344 | - $data[0] = $errorInfo['description']; |
|
| 344 | + $data[ 0 ] = $errorInfo[ 'description' ]; |
|
| 345 | 345 | return $data; |
| 346 | 346 | } |
| 347 | 347 | |
@@ -358,9 +358,9 @@ discard block |
||
| 358 | 358 | * |
| 359 | 359 | * @return bool |
| 360 | 360 | */ |
| 361 | - public function isNotQuoted(File $phpcsFile, $stackPtr) |
|
| 361 | + public function isNotQuoted( File $phpcsFile, $stackPtr ) |
|
| 362 | 362 | { |
| 363 | 363 | $tokens = $phpcsFile->getTokens(); |
| 364 | - return ($tokens[$stackPtr]['content'][3] !== '"'); |
|
| 364 | + return ( $tokens[ $stackPtr ][ 'content' ][ 3 ] !== '"' ); |
|
| 365 | 365 | } |
| 366 | 366 | } |
@@ -22,8 +22,7 @@ discard block |
||
| 22 | 22 | * @author Wim Godden <[email protected]> |
| 23 | 23 | * @copyright 2013 Cu.be Solutions bvba |
| 24 | 24 | */ |
| 25 | -class NewKeywordsSniff extends AbstractNewFeatureSniff |
|
| 26 | -{ |
|
| 25 | +class NewKeywordsSniff extends AbstractNewFeatureSniff { |
|
| 27 | 26 | |
| 28 | 27 | /** |
| 29 | 28 | * A list of new keywords, not present in older versions. |
@@ -159,8 +158,7 @@ discard block |
||
| 159 | 158 | * |
| 160 | 159 | * @return array |
| 161 | 160 | */ |
| 162 | - public function register() |
|
| 163 | - { |
|
| 161 | + public function register() { |
|
| 164 | 162 | $tokens = array(); |
| 165 | 163 | $translate = array(); |
| 166 | 164 | foreach ($this->newKeywords as $token => $versions) { |
@@ -194,8 +192,7 @@ discard block |
||
| 194 | 192 | * |
| 195 | 193 | * @return void |
| 196 | 194 | */ |
| 197 | - public function process(File $phpcsFile, $stackPtr) |
|
| 198 | - { |
|
| 195 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 199 | 196 | $tokens = $phpcsFile->getTokens(); |
| 200 | 197 | $tokenType = $tokens[$stackPtr]['type']; |
| 201 | 198 | |
@@ -292,8 +289,7 @@ discard block |
||
| 292 | 289 | * |
| 293 | 290 | * @return array Version and other information about the item. |
| 294 | 291 | */ |
| 295 | - public function getItemArray(array $itemInfo) |
|
| 296 | - { |
|
| 292 | + public function getItemArray(array $itemInfo) { |
|
| 297 | 293 | return $this->newKeywords[$itemInfo['name']]; |
| 298 | 294 | } |
| 299 | 295 | |
@@ -303,8 +299,7 @@ discard block |
||
| 303 | 299 | * |
| 304 | 300 | * @return array |
| 305 | 301 | */ |
| 306 | - protected function getNonVersionArrayKeys() |
|
| 307 | - { |
|
| 302 | + protected function getNonVersionArrayKeys() { |
|
| 308 | 303 | return array( |
| 309 | 304 | 'description', |
| 310 | 305 | 'condition', |
@@ -321,8 +316,7 @@ discard block |
||
| 321 | 316 | * |
| 322 | 317 | * @return array |
| 323 | 318 | */ |
| 324 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
| 325 | - { |
|
| 319 | + public function getErrorInfo(array $itemArray, array $itemInfo) { |
|
| 326 | 320 | $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
| 327 | 321 | $errorInfo['description'] = $itemArray['description']; |
| 328 | 322 | |
@@ -339,8 +333,7 @@ discard block |
||
| 339 | 333 | * |
| 340 | 334 | * @return array |
| 341 | 335 | */ |
| 342 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
| 343 | - { |
|
| 336 | + protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) { |
|
| 344 | 337 | $data[0] = $errorInfo['description']; |
| 345 | 338 | return $data; |
| 346 | 339 | } |
@@ -358,8 +351,7 @@ discard block |
||
| 358 | 351 | * |
| 359 | 352 | * @return bool |
| 360 | 353 | */ |
| 361 | - public function isNotQuoted(File $phpcsFile, $stackPtr) |
|
| 362 | - { |
|
| 354 | + public function isNotQuoted(File $phpcsFile, $stackPtr) { |
|
| 363 | 355 | $tokens = $phpcsFile->getTokens(); |
| 364 | 356 | return ($tokens[$stackPtr]['content'][3] !== '"'); |
| 365 | 357 | } |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | * |
| 36 | 36 | * @since 9.1.0 |
| 37 | 37 | * |
| 38 | - * @return array |
|
| 38 | + * @return integer[] |
|
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
@@ -30,43 +30,43 @@ discard block |
||
| 30 | 30 | class NewDirectCallsToCloneSniff extends Sniff |
| 31 | 31 | { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Returns an array of tokens this test wants to listen for. |
|
| 35 | - * |
|
| 36 | - * @since 9.1.0 |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - \T_DOUBLE_COLON, |
|
| 44 | - \T_OBJECT_OPERATOR, |
|
| 45 | - ); |
|
| 46 | - } |
|
| 33 | + /** |
|
| 34 | + * Returns an array of tokens this test wants to listen for. |
|
| 35 | + * |
|
| 36 | + * @since 9.1.0 |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + \T_DOUBLE_COLON, |
|
| 44 | + \T_OBJECT_OPERATOR, |
|
| 45 | + ); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Processes this test, when one of its tokens is encountered. |
|
| 50 | - * |
|
| 51 | - * @since 9.1.0 |
|
| 52 | - * |
|
| 53 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token in |
|
| 55 | - * the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - if ($this->supportsBelow('5.6') === false) { |
|
| 62 | - return; |
|
| 63 | - } |
|
| 48 | + /** |
|
| 49 | + * Processes this test, when one of its tokens is encountered. |
|
| 50 | + * |
|
| 51 | + * @since 9.1.0 |
|
| 52 | + * |
|
| 53 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token in |
|
| 55 | + * the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + if ($this->supportsBelow('5.6') === false) { |
|
| 62 | + return; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $tokens = $phpcsFile->getTokens(); |
|
| 65 | + $tokens = $phpcsFile->getTokens(); |
|
| 66 | 66 | |
| 67 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 68 | - if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) { |
|
| 69 | - /* |
|
| 67 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 68 | + if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) { |
|
| 69 | + /* |
|
| 70 | 70 | * Not a method call. |
| 71 | 71 | * |
| 72 | 72 | * Note: This disregards method calls with the method name in a variable, like: |
@@ -74,24 +74,24 @@ discard block |
||
| 74 | 74 | * $obj->$method(); |
| 75 | 75 | * However, that would be very hard to examine reliably anyway. |
| 76 | 76 | */ |
| 77 | - return; |
|
| 78 | - } |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - if (strtolower($tokens[$nextNonEmpty]['content']) !== '__clone') { |
|
| 81 | - // Not a call to the __clone() method. |
|
| 82 | - return; |
|
| 83 | - } |
|
| 80 | + if (strtolower($tokens[$nextNonEmpty]['content']) !== '__clone') { |
|
| 81 | + // Not a call to the __clone() method. |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true); |
|
| 86 | - if ($nextNextNonEmpty === false || $tokens[$nextNextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 87 | - // Not a method call. |
|
| 88 | - return; |
|
| 89 | - } |
|
| 85 | + $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true); |
|
| 86 | + if ($nextNextNonEmpty === false || $tokens[$nextNextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 87 | + // Not a method call. |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - $phpcsFile->addError( |
|
| 92 | - 'Direct calls to the __clone() magic method are not allowed in PHP 5.6 or earlier.', |
|
| 93 | - $nextNonEmpty, |
|
| 94 | - 'Found' |
|
| 95 | - ); |
|
| 96 | - } |
|
| 91 | + $phpcsFile->addError( |
|
| 92 | + 'Direct calls to the __clone() magic method are not allowed in PHP 5.6 or earlier.', |
|
| 93 | + $nextNonEmpty, |
|
| 94 | + 'Found' |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | 97 | } |
@@ -56,16 +56,16 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return void |
| 58 | 58 | */ |
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 60 | 60 | { |
| 61 | - if ($this->supportsBelow('5.6') === false) { |
|
| 61 | + if ( $this->supportsBelow( '5.6' ) === false ) { |
|
| 62 | 62 | return; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $tokens = $phpcsFile->getTokens(); |
| 66 | 66 | |
| 67 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 68 | - if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) { |
|
| 67 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
| 68 | + if ( $nextNonEmpty === false || $tokens[ $nextNonEmpty ][ 'code' ] !== \T_STRING ) { |
|
| 69 | 69 | /* |
| 70 | 70 | * Not a method call. |
| 71 | 71 | * |
@@ -77,13 +77,13 @@ discard block |
||
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if (strtolower($tokens[$nextNonEmpty]['content']) !== '__clone') { |
|
| 80 | + if ( strtolower( $tokens[ $nextNonEmpty ][ 'content' ] ) !== '__clone' ) { |
|
| 81 | 81 | // Not a call to the __clone() method. |
| 82 | 82 | return; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true); |
|
| 86 | - if ($nextNextNonEmpty === false || $tokens[$nextNextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
|
| 85 | + $nextNextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $nextNonEmpty + 1 ), null, true ); |
|
| 86 | + if ( $nextNextNonEmpty === false || $tokens[ $nextNextNonEmpty ][ 'code' ] !== \T_OPEN_PARENTHESIS ) { |
|
| 87 | 87 | // Not a method call. |
| 88 | 88 | return; |
| 89 | 89 | } |
@@ -27,8 +27,7 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @since 9.1.0 |
| 29 | 29 | */ |
| 30 | -class NewDirectCallsToCloneSniff extends Sniff |
|
| 31 | -{ |
|
| 30 | +class NewDirectCallsToCloneSniff extends Sniff { |
|
| 32 | 31 | |
| 33 | 32 | /** |
| 34 | 33 | * Returns an array of tokens this test wants to listen for. |
@@ -37,8 +36,7 @@ discard block |
||
| 37 | 36 | * |
| 38 | 37 | * @return array |
| 39 | 38 | */ |
| 40 | - public function register() |
|
| 41 | - { |
|
| 39 | + public function register() { |
|
| 42 | 40 | return array( |
| 43 | 41 | \T_DOUBLE_COLON, |
| 44 | 42 | \T_OBJECT_OPERATOR, |
@@ -56,8 +54,7 @@ discard block |
||
| 56 | 54 | * |
| 57 | 55 | * @return void |
| 58 | 56 | */ |
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 57 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 61 | 58 | if ($this->supportsBelow('5.6') === false) { |
| 62 | 59 | return; |
| 63 | 60 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @param string $class The name of the class to load. |
| 61 | 61 | * |
| 62 | - * @return bool |
|
| 62 | + * @return null|boolean |
|
| 63 | 63 | */ |
| 64 | 64 | public static function load($class) |
| 65 | 65 | { |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | /** |
| 221 | 221 | * Retrieve the namespaces and paths registered by external standards. |
| 222 | 222 | * |
| 223 | - * @return array |
|
| 223 | + * @return string[] |
|
| 224 | 224 | */ |
| 225 | 225 | public static function getSearchPaths() |
| 226 | 226 | { |
@@ -17,284 +17,284 @@ |
||
| 17 | 17 | namespace PHP_CodeSniffer; |
| 18 | 18 | |
| 19 | 19 | if (class_exists('PHP_CodeSniffer\Autoload', false) === false) { |
| 20 | - class Autoload |
|
| 21 | - { |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The composer autoloader. |
|
| 25 | - * |
|
| 26 | - * @var \Composer\Autoload\ClassLoader |
|
| 27 | - */ |
|
| 28 | - private static $composerAutoloader = null; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A mapping of file names to class names. |
|
| 32 | - * |
|
| 33 | - * @var array<string, string> |
|
| 34 | - */ |
|
| 35 | - private static $loadedClasses = []; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A mapping of class names to file names. |
|
| 39 | - * |
|
| 40 | - * @var array<string, string> |
|
| 41 | - */ |
|
| 42 | - private static $loadedFiles = []; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * A list of additional directories to search during autoloading. |
|
| 46 | - * |
|
| 47 | - * This is typically a list of coding standard directories. |
|
| 48 | - * |
|
| 49 | - * @var string[] |
|
| 50 | - */ |
|
| 51 | - private static $searchPaths = []; |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Loads a class. |
|
| 56 | - * |
|
| 57 | - * This method only loads classes that exist in the PHP_CodeSniffer namespace. |
|
| 58 | - * All other classes are ignored and loaded by subsequent autoloaders. |
|
| 59 | - * |
|
| 60 | - * @param string $class The name of the class to load. |
|
| 61 | - * |
|
| 62 | - * @return bool |
|
| 63 | - */ |
|
| 64 | - public static function load($class) |
|
| 65 | - { |
|
| 66 | - // Include the composer autoloader if there is one, but re-register it |
|
| 67 | - // so this autoloader runs before the composer one as we need to include |
|
| 68 | - // all files so we can figure out what the class/interface/trait name is. |
|
| 69 | - if (self::$composerAutoloader === null) { |
|
| 70 | - // Make sure we don't try to load any of Composer's classes |
|
| 71 | - // while the autoloader is being setup. |
|
| 72 | - if (strpos($class, 'Composer\\') === 0) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if (strpos(__DIR__, 'phar://') !== 0 |
|
| 77 | - && file_exists(__DIR__.'/../../autoload.php') === true |
|
| 78 | - ) { |
|
| 79 | - self::$composerAutoloader = include __DIR__.'/../../autoload.php'; |
|
| 80 | - if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) { |
|
| 81 | - self::$composerAutoloader->unregister(); |
|
| 82 | - self::$composerAutoloader->register(); |
|
| 83 | - } else { |
|
| 84 | - // Something went wrong, so keep going without the autoloader |
|
| 85 | - // although namespaced sniffs might error. |
|
| 86 | - self::$composerAutoloader = false; |
|
| 87 | - } |
|
| 88 | - } else { |
|
| 89 | - self::$composerAutoloader = false; |
|
| 90 | - } |
|
| 91 | - }//end if |
|
| 92 | - |
|
| 93 | - $ds = DIRECTORY_SEPARATOR; |
|
| 94 | - $path = false; |
|
| 95 | - |
|
| 96 | - if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') { |
|
| 97 | - if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') { |
|
| 98 | - $isInstalled = !is_dir(__DIR__.$ds.'tests'); |
|
| 99 | - if ($isInstalled === false) { |
|
| 100 | - $path = __DIR__.$ds.'tests'; |
|
| 101 | - } else { |
|
| 102 | - $path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer'; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php'; |
|
| 106 | - } else { |
|
| 107 | - $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php'; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // See if the composer autoloader knows where the class is. |
|
| 112 | - if ($path === false && self::$composerAutoloader !== false) { |
|
| 113 | - $path = self::$composerAutoloader->findFile($class); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // See if the class is inside one of our alternate search paths. |
|
| 117 | - if ($path === false) { |
|
| 118 | - foreach (self::$searchPaths as $searchPath => $nsPrefix) { |
|
| 119 | - $className = $class; |
|
| 120 | - if ($nsPrefix !== '' && substr($class, 0, strlen($nsPrefix)) === $nsPrefix) { |
|
| 121 | - $className = substr($class, (strlen($nsPrefix) + 1)); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $path = $searchPath.$ds.str_replace('\\', $ds, $className).'.php'; |
|
| 125 | - if (is_file($path) === true) { |
|
| 126 | - break; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $path = false; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ($path !== false && is_file($path) === true) { |
|
| 134 | - self::loadFile($path); |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return false; |
|
| 139 | - |
|
| 140 | - }//end load() |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Includes a file and tracks what class or interface was loaded as a result. |
|
| 145 | - * |
|
| 146 | - * @param string $path The path of the file to load. |
|
| 147 | - * |
|
| 148 | - * @return string The fully qualified name of the class in the loaded file. |
|
| 149 | - */ |
|
| 150 | - public static function loadFile($path) |
|
| 151 | - { |
|
| 152 | - if (strpos(__DIR__, 'phar://') !== 0) { |
|
| 153 | - $path = realpath($path); |
|
| 154 | - if ($path === false) { |
|
| 155 | - return false; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (isset(self::$loadedClasses[$path]) === true) { |
|
| 160 | - return self::$loadedClasses[$path]; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $classes = get_declared_classes(); |
|
| 164 | - $interfaces = get_declared_interfaces(); |
|
| 165 | - $traits = get_declared_traits(); |
|
| 166 | - |
|
| 167 | - include $path; |
|
| 168 | - |
|
| 169 | - $className = null; |
|
| 170 | - $newClasses = array_reverse(array_diff(get_declared_classes(), $classes)); |
|
| 171 | - foreach ($newClasses as $name) { |
|
| 172 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 173 | - $className = $name; |
|
| 174 | - break; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($className === null) { |
|
| 179 | - $newClasses = array_reverse(array_diff(get_declared_interfaces(), $interfaces)); |
|
| 180 | - foreach ($newClasses as $name) { |
|
| 181 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 182 | - $className = $name; |
|
| 183 | - break; |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if ($className === null) { |
|
| 189 | - $newClasses = array_reverse(array_diff(get_declared_traits(), $traits)); |
|
| 190 | - foreach ($newClasses as $name) { |
|
| 191 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 192 | - $className = $name; |
|
| 193 | - break; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - self::$loadedClasses[$path] = $className; |
|
| 199 | - self::$loadedFiles[$className] = $path; |
|
| 200 | - return self::$loadedClasses[$path]; |
|
| 201 | - |
|
| 202 | - }//end loadFile() |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Adds a directory to search during autoloading. |
|
| 207 | - * |
|
| 208 | - * @param string $path The path to the directory to search. |
|
| 209 | - * @param string $nsPrefix The namespace prefix used by files under this path. |
|
| 210 | - * |
|
| 211 | - * @return void |
|
| 212 | - */ |
|
| 213 | - public static function addSearchPath($path, $nsPrefix='') |
|
| 214 | - { |
|
| 215 | - self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\'); |
|
| 216 | - |
|
| 217 | - }//end addSearchPath() |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Retrieve the namespaces and paths registered by external standards. |
|
| 222 | - * |
|
| 223 | - * @return array |
|
| 224 | - */ |
|
| 225 | - public static function getSearchPaths() |
|
| 226 | - { |
|
| 227 | - return self::$searchPaths; |
|
| 228 | - |
|
| 229 | - }//end getSearchPaths() |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Gets the class name for the given file path. |
|
| 234 | - * |
|
| 235 | - * @param string $path The name of the file. |
|
| 236 | - * |
|
| 237 | - * @throws \Exception If the file path has not been loaded. |
|
| 238 | - * @return string |
|
| 239 | - */ |
|
| 240 | - public static function getLoadedClassName($path) |
|
| 241 | - { |
|
| 242 | - if (isset(self::$loadedClasses[$path]) === false) { |
|
| 243 | - throw new \Exception("Cannot get class name for $path; file has not been included"); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return self::$loadedClasses[$path]; |
|
| 247 | - |
|
| 248 | - }//end getLoadedClassName() |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Gets the file path for the given class name. |
|
| 253 | - * |
|
| 254 | - * @param string $class The name of the class. |
|
| 255 | - * |
|
| 256 | - * @throws \Exception If the class name has not been loaded |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - public static function getLoadedFileName($class) |
|
| 260 | - { |
|
| 261 | - if (isset(self::$loadedFiles[$class]) === false) { |
|
| 262 | - throw new \Exception("Cannot get file name for $class; class has not been included"); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return self::$loadedFiles[$class]; |
|
| 266 | - |
|
| 267 | - }//end getLoadedFileName() |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Gets the mapping of file names to class names. |
|
| 272 | - * |
|
| 273 | - * @return array<string, string> |
|
| 274 | - */ |
|
| 275 | - public static function getLoadedClasses() |
|
| 276 | - { |
|
| 277 | - return self::$loadedClasses; |
|
| 278 | - |
|
| 279 | - }//end getLoadedClasses() |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Gets the mapping of class names to file names. |
|
| 284 | - * |
|
| 285 | - * @return array<string, string> |
|
| 286 | - */ |
|
| 287 | - public static function getLoadedFiles() |
|
| 288 | - { |
|
| 289 | - return self::$loadedFiles; |
|
| 290 | - |
|
| 291 | - }//end getLoadedFiles() |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - }//end class |
|
| 295 | - |
|
| 296 | - // Register the autoloader before any existing autoloaders to ensure |
|
| 297 | - // it gets a chance to hear about every autoload request, and record |
|
| 298 | - // the file and class name for it. |
|
| 299 | - spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true); |
|
| 20 | + class Autoload |
|
| 21 | + { |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The composer autoloader. |
|
| 25 | + * |
|
| 26 | + * @var \Composer\Autoload\ClassLoader |
|
| 27 | + */ |
|
| 28 | + private static $composerAutoloader = null; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A mapping of file names to class names. |
|
| 32 | + * |
|
| 33 | + * @var array<string, string> |
|
| 34 | + */ |
|
| 35 | + private static $loadedClasses = []; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A mapping of class names to file names. |
|
| 39 | + * |
|
| 40 | + * @var array<string, string> |
|
| 41 | + */ |
|
| 42 | + private static $loadedFiles = []; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * A list of additional directories to search during autoloading. |
|
| 46 | + * |
|
| 47 | + * This is typically a list of coding standard directories. |
|
| 48 | + * |
|
| 49 | + * @var string[] |
|
| 50 | + */ |
|
| 51 | + private static $searchPaths = []; |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Loads a class. |
|
| 56 | + * |
|
| 57 | + * This method only loads classes that exist in the PHP_CodeSniffer namespace. |
|
| 58 | + * All other classes are ignored and loaded by subsequent autoloaders. |
|
| 59 | + * |
|
| 60 | + * @param string $class The name of the class to load. |
|
| 61 | + * |
|
| 62 | + * @return bool |
|
| 63 | + */ |
|
| 64 | + public static function load($class) |
|
| 65 | + { |
|
| 66 | + // Include the composer autoloader if there is one, but re-register it |
|
| 67 | + // so this autoloader runs before the composer one as we need to include |
|
| 68 | + // all files so we can figure out what the class/interface/trait name is. |
|
| 69 | + if (self::$composerAutoloader === null) { |
|
| 70 | + // Make sure we don't try to load any of Composer's classes |
|
| 71 | + // while the autoloader is being setup. |
|
| 72 | + if (strpos($class, 'Composer\\') === 0) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if (strpos(__DIR__, 'phar://') !== 0 |
|
| 77 | + && file_exists(__DIR__.'/../../autoload.php') === true |
|
| 78 | + ) { |
|
| 79 | + self::$composerAutoloader = include __DIR__.'/../../autoload.php'; |
|
| 80 | + if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) { |
|
| 81 | + self::$composerAutoloader->unregister(); |
|
| 82 | + self::$composerAutoloader->register(); |
|
| 83 | + } else { |
|
| 84 | + // Something went wrong, so keep going without the autoloader |
|
| 85 | + // although namespaced sniffs might error. |
|
| 86 | + self::$composerAutoloader = false; |
|
| 87 | + } |
|
| 88 | + } else { |
|
| 89 | + self::$composerAutoloader = false; |
|
| 90 | + } |
|
| 91 | + }//end if |
|
| 92 | + |
|
| 93 | + $ds = DIRECTORY_SEPARATOR; |
|
| 94 | + $path = false; |
|
| 95 | + |
|
| 96 | + if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') { |
|
| 97 | + if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') { |
|
| 98 | + $isInstalled = !is_dir(__DIR__.$ds.'tests'); |
|
| 99 | + if ($isInstalled === false) { |
|
| 100 | + $path = __DIR__.$ds.'tests'; |
|
| 101 | + } else { |
|
| 102 | + $path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer'; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php'; |
|
| 106 | + } else { |
|
| 107 | + $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php'; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // See if the composer autoloader knows where the class is. |
|
| 112 | + if ($path === false && self::$composerAutoloader !== false) { |
|
| 113 | + $path = self::$composerAutoloader->findFile($class); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // See if the class is inside one of our alternate search paths. |
|
| 117 | + if ($path === false) { |
|
| 118 | + foreach (self::$searchPaths as $searchPath => $nsPrefix) { |
|
| 119 | + $className = $class; |
|
| 120 | + if ($nsPrefix !== '' && substr($class, 0, strlen($nsPrefix)) === $nsPrefix) { |
|
| 121 | + $className = substr($class, (strlen($nsPrefix) + 1)); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $path = $searchPath.$ds.str_replace('\\', $ds, $className).'.php'; |
|
| 125 | + if (is_file($path) === true) { |
|
| 126 | + break; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $path = false; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ($path !== false && is_file($path) === true) { |
|
| 134 | + self::loadFile($path); |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return false; |
|
| 139 | + |
|
| 140 | + }//end load() |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Includes a file and tracks what class or interface was loaded as a result. |
|
| 145 | + * |
|
| 146 | + * @param string $path The path of the file to load. |
|
| 147 | + * |
|
| 148 | + * @return string The fully qualified name of the class in the loaded file. |
|
| 149 | + */ |
|
| 150 | + public static function loadFile($path) |
|
| 151 | + { |
|
| 152 | + if (strpos(__DIR__, 'phar://') !== 0) { |
|
| 153 | + $path = realpath($path); |
|
| 154 | + if ($path === false) { |
|
| 155 | + return false; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (isset(self::$loadedClasses[$path]) === true) { |
|
| 160 | + return self::$loadedClasses[$path]; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $classes = get_declared_classes(); |
|
| 164 | + $interfaces = get_declared_interfaces(); |
|
| 165 | + $traits = get_declared_traits(); |
|
| 166 | + |
|
| 167 | + include $path; |
|
| 168 | + |
|
| 169 | + $className = null; |
|
| 170 | + $newClasses = array_reverse(array_diff(get_declared_classes(), $classes)); |
|
| 171 | + foreach ($newClasses as $name) { |
|
| 172 | + if (isset(self::$loadedFiles[$name]) === false) { |
|
| 173 | + $className = $name; |
|
| 174 | + break; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($className === null) { |
|
| 179 | + $newClasses = array_reverse(array_diff(get_declared_interfaces(), $interfaces)); |
|
| 180 | + foreach ($newClasses as $name) { |
|
| 181 | + if (isset(self::$loadedFiles[$name]) === false) { |
|
| 182 | + $className = $name; |
|
| 183 | + break; |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if ($className === null) { |
|
| 189 | + $newClasses = array_reverse(array_diff(get_declared_traits(), $traits)); |
|
| 190 | + foreach ($newClasses as $name) { |
|
| 191 | + if (isset(self::$loadedFiles[$name]) === false) { |
|
| 192 | + $className = $name; |
|
| 193 | + break; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + self::$loadedClasses[$path] = $className; |
|
| 199 | + self::$loadedFiles[$className] = $path; |
|
| 200 | + return self::$loadedClasses[$path]; |
|
| 201 | + |
|
| 202 | + }//end loadFile() |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Adds a directory to search during autoloading. |
|
| 207 | + * |
|
| 208 | + * @param string $path The path to the directory to search. |
|
| 209 | + * @param string $nsPrefix The namespace prefix used by files under this path. |
|
| 210 | + * |
|
| 211 | + * @return void |
|
| 212 | + */ |
|
| 213 | + public static function addSearchPath($path, $nsPrefix='') |
|
| 214 | + { |
|
| 215 | + self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\'); |
|
| 216 | + |
|
| 217 | + }//end addSearchPath() |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Retrieve the namespaces and paths registered by external standards. |
|
| 222 | + * |
|
| 223 | + * @return array |
|
| 224 | + */ |
|
| 225 | + public static function getSearchPaths() |
|
| 226 | + { |
|
| 227 | + return self::$searchPaths; |
|
| 228 | + |
|
| 229 | + }//end getSearchPaths() |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Gets the class name for the given file path. |
|
| 234 | + * |
|
| 235 | + * @param string $path The name of the file. |
|
| 236 | + * |
|
| 237 | + * @throws \Exception If the file path has not been loaded. |
|
| 238 | + * @return string |
|
| 239 | + */ |
|
| 240 | + public static function getLoadedClassName($path) |
|
| 241 | + { |
|
| 242 | + if (isset(self::$loadedClasses[$path]) === false) { |
|
| 243 | + throw new \Exception("Cannot get class name for $path; file has not been included"); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return self::$loadedClasses[$path]; |
|
| 247 | + |
|
| 248 | + }//end getLoadedClassName() |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Gets the file path for the given class name. |
|
| 253 | + * |
|
| 254 | + * @param string $class The name of the class. |
|
| 255 | + * |
|
| 256 | + * @throws \Exception If the class name has not been loaded |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + public static function getLoadedFileName($class) |
|
| 260 | + { |
|
| 261 | + if (isset(self::$loadedFiles[$class]) === false) { |
|
| 262 | + throw new \Exception("Cannot get file name for $class; class has not been included"); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return self::$loadedFiles[$class]; |
|
| 266 | + |
|
| 267 | + }//end getLoadedFileName() |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Gets the mapping of file names to class names. |
|
| 272 | + * |
|
| 273 | + * @return array<string, string> |
|
| 274 | + */ |
|
| 275 | + public static function getLoadedClasses() |
|
| 276 | + { |
|
| 277 | + return self::$loadedClasses; |
|
| 278 | + |
|
| 279 | + }//end getLoadedClasses() |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Gets the mapping of class names to file names. |
|
| 284 | + * |
|
| 285 | + * @return array<string, string> |
|
| 286 | + */ |
|
| 287 | + public static function getLoadedFiles() |
|
| 288 | + { |
|
| 289 | + return self::$loadedFiles; |
|
| 290 | + |
|
| 291 | + }//end getLoadedFiles() |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + }//end class |
|
| 295 | + |
|
| 296 | + // Register the autoloader before any existing autoloaders to ensure |
|
| 297 | + // it gets a chance to hear about every autoload request, and record |
|
| 298 | + // the file and class name for it. |
|
| 299 | + spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true); |
|
| 300 | 300 | }//end if |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | namespace PHP_CodeSniffer; |
| 18 | 18 | |
| 19 | -if (class_exists('PHP_CodeSniffer\Autoload', false) === false) { |
|
| 19 | +if ( class_exists( 'PHP_CodeSniffer\Autoload', false ) === false ) { |
|
| 20 | 20 | class Autoload |
| 21 | 21 | { |
| 22 | 22 | |
@@ -32,14 +32,14 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @var array<string, string> |
| 34 | 34 | */ |
| 35 | - private static $loadedClasses = []; |
|
| 35 | + private static $loadedClasses = [ ]; |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * A mapping of class names to file names. |
| 39 | 39 | * |
| 40 | 40 | * @var array<string, string> |
| 41 | 41 | */ |
| 42 | - private static $loadedFiles = []; |
|
| 42 | + private static $loadedFiles = [ ]; |
|
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * A list of additional directories to search during autoloading. |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @var string[] |
| 50 | 50 | */ |
| 51 | - private static $searchPaths = []; |
|
| 51 | + private static $searchPaths = [ ]; |
|
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -61,23 +61,23 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return bool |
| 63 | 63 | */ |
| 64 | - public static function load($class) |
|
| 64 | + public static function load( $class ) |
|
| 65 | 65 | { |
| 66 | 66 | // Include the composer autoloader if there is one, but re-register it |
| 67 | 67 | // so this autoloader runs before the composer one as we need to include |
| 68 | 68 | // all files so we can figure out what the class/interface/trait name is. |
| 69 | - if (self::$composerAutoloader === null) { |
|
| 69 | + if ( self::$composerAutoloader === null ) { |
|
| 70 | 70 | // Make sure we don't try to load any of Composer's classes |
| 71 | 71 | // while the autoloader is being setup. |
| 72 | - if (strpos($class, 'Composer\\') === 0) { |
|
| 72 | + if ( strpos( $class, 'Composer\\' ) === 0 ) { |
|
| 73 | 73 | return; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - if (strpos(__DIR__, 'phar://') !== 0 |
|
| 77 | - && file_exists(__DIR__.'/../../autoload.php') === true |
|
| 76 | + if ( strpos( __DIR__, 'phar://' ) !== 0 |
|
| 77 | + && file_exists( __DIR__ . '/../../autoload.php' ) === true |
|
| 78 | 78 | ) { |
| 79 | - self::$composerAutoloader = include __DIR__.'/../../autoload.php'; |
|
| 80 | - if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) { |
|
| 79 | + self::$composerAutoloader = include __DIR__ . '/../../autoload.php'; |
|
| 80 | + if ( self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader ) { |
|
| 81 | 81 | self::$composerAutoloader->unregister(); |
| 82 | 82 | self::$composerAutoloader->register(); |
| 83 | 83 | } else { |
@@ -93,36 +93,36 @@ discard block |
||
| 93 | 93 | $ds = DIRECTORY_SEPARATOR; |
| 94 | 94 | $path = false; |
| 95 | 95 | |
| 96 | - if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') { |
|
| 97 | - if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') { |
|
| 98 | - $isInstalled = !is_dir(__DIR__.$ds.'tests'); |
|
| 99 | - if ($isInstalled === false) { |
|
| 100 | - $path = __DIR__.$ds.'tests'; |
|
| 96 | + if ( substr( $class, 0, 16 ) === 'PHP_CodeSniffer\\' ) { |
|
| 97 | + if ( substr( $class, 0, 22 ) === 'PHP_CodeSniffer\Tests\\' ) { |
|
| 98 | + $isInstalled = ! is_dir( __DIR__ . $ds . 'tests' ); |
|
| 99 | + if ( $isInstalled === false ) { |
|
| 100 | + $path = __DIR__ . $ds . 'tests'; |
|
| 101 | 101 | } else { |
| 102 | - $path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer'; |
|
| 102 | + $path = '@test_dir@' . $ds . 'PHP_CodeSniffer' . $ds . 'CodeSniffer'; |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - $path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php'; |
|
| 105 | + $path .= $ds . substr( str_replace( '\\', $ds, $class ), 22 ) . '.php'; |
|
| 106 | 106 | } else { |
| 107 | - $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php'; |
|
| 107 | + $path = __DIR__ . $ds . 'src' . $ds . substr( str_replace( '\\', $ds, $class ), 16 ) . '.php'; |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // See if the composer autoloader knows where the class is. |
| 112 | - if ($path === false && self::$composerAutoloader !== false) { |
|
| 113 | - $path = self::$composerAutoloader->findFile($class); |
|
| 112 | + if ( $path === false && self::$composerAutoloader !== false ) { |
|
| 113 | + $path = self::$composerAutoloader->findFile( $class ); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // See if the class is inside one of our alternate search paths. |
| 117 | - if ($path === false) { |
|
| 118 | - foreach (self::$searchPaths as $searchPath => $nsPrefix) { |
|
| 117 | + if ( $path === false ) { |
|
| 118 | + foreach ( self::$searchPaths as $searchPath => $nsPrefix ) { |
|
| 119 | 119 | $className = $class; |
| 120 | - if ($nsPrefix !== '' && substr($class, 0, strlen($nsPrefix)) === $nsPrefix) { |
|
| 121 | - $className = substr($class, (strlen($nsPrefix) + 1)); |
|
| 120 | + if ( $nsPrefix !== '' && substr( $class, 0, strlen( $nsPrefix ) ) === $nsPrefix ) { |
|
| 121 | + $className = substr( $class, ( strlen( $nsPrefix ) + 1 ) ); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - $path = $searchPath.$ds.str_replace('\\', $ds, $className).'.php'; |
|
| 125 | - if (is_file($path) === true) { |
|
| 124 | + $path = $searchPath . $ds . str_replace( '\\', $ds, $className ) . '.php'; |
|
| 125 | + if ( is_file( $path ) === true ) { |
|
| 126 | 126 | break; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -130,8 +130,8 @@ discard block |
||
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - if ($path !== false && is_file($path) === true) { |
|
| 134 | - self::loadFile($path); |
|
| 133 | + if ( $path !== false && is_file( $path ) === true ) { |
|
| 134 | + self::loadFile( $path ); |
|
| 135 | 135 | return true; |
| 136 | 136 | } |
| 137 | 137 | |
@@ -147,17 +147,17 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @return string The fully qualified name of the class in the loaded file. |
| 149 | 149 | */ |
| 150 | - public static function loadFile($path) |
|
| 150 | + public static function loadFile( $path ) |
|
| 151 | 151 | { |
| 152 | - if (strpos(__DIR__, 'phar://') !== 0) { |
|
| 153 | - $path = realpath($path); |
|
| 154 | - if ($path === false) { |
|
| 152 | + if ( strpos( __DIR__, 'phar://' ) !== 0 ) { |
|
| 153 | + $path = realpath( $path ); |
|
| 154 | + if ( $path === false ) { |
|
| 155 | 155 | return false; |
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if (isset(self::$loadedClasses[$path]) === true) { |
|
| 160 | - return self::$loadedClasses[$path]; |
|
| 159 | + if ( isset( self::$loadedClasses[ $path ] ) === true ) { |
|
| 160 | + return self::$loadedClasses[ $path ]; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | $classes = get_declared_classes(); |
@@ -167,37 +167,37 @@ discard block |
||
| 167 | 167 | include $path; |
| 168 | 168 | |
| 169 | 169 | $className = null; |
| 170 | - $newClasses = array_reverse(array_diff(get_declared_classes(), $classes)); |
|
| 171 | - foreach ($newClasses as $name) { |
|
| 172 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 170 | + $newClasses = array_reverse( array_diff( get_declared_classes(), $classes ) ); |
|
| 171 | + foreach ( $newClasses as $name ) { |
|
| 172 | + if ( isset( self::$loadedFiles[ $name ] ) === false ) { |
|
| 173 | 173 | $className = $name; |
| 174 | 174 | break; |
| 175 | 175 | } |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - if ($className === null) { |
|
| 179 | - $newClasses = array_reverse(array_diff(get_declared_interfaces(), $interfaces)); |
|
| 180 | - foreach ($newClasses as $name) { |
|
| 181 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 178 | + if ( $className === null ) { |
|
| 179 | + $newClasses = array_reverse( array_diff( get_declared_interfaces(), $interfaces ) ); |
|
| 180 | + foreach ( $newClasses as $name ) { |
|
| 181 | + if ( isset( self::$loadedFiles[ $name ] ) === false ) { |
|
| 182 | 182 | $className = $name; |
| 183 | 183 | break; |
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - if ($className === null) { |
|
| 189 | - $newClasses = array_reverse(array_diff(get_declared_traits(), $traits)); |
|
| 190 | - foreach ($newClasses as $name) { |
|
| 191 | - if (isset(self::$loadedFiles[$name]) === false) { |
|
| 188 | + if ( $className === null ) { |
|
| 189 | + $newClasses = array_reverse( array_diff( get_declared_traits(), $traits ) ); |
|
| 190 | + foreach ( $newClasses as $name ) { |
|
| 191 | + if ( isset( self::$loadedFiles[ $name ] ) === false ) { |
|
| 192 | 192 | $className = $name; |
| 193 | 193 | break; |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - self::$loadedClasses[$path] = $className; |
|
| 199 | - self::$loadedFiles[$className] = $path; |
|
| 200 | - return self::$loadedClasses[$path]; |
|
| 198 | + self::$loadedClasses[ $path ] = $className; |
|
| 199 | + self::$loadedFiles[ $className ] = $path; |
|
| 200 | + return self::$loadedClasses[ $path ]; |
|
| 201 | 201 | |
| 202 | 202 | }//end loadFile() |
| 203 | 203 | |
@@ -210,9 +210,9 @@ discard block |
||
| 210 | 210 | * |
| 211 | 211 | * @return void |
| 212 | 212 | */ |
| 213 | - public static function addSearchPath($path, $nsPrefix='') |
|
| 213 | + public static function addSearchPath( $path, $nsPrefix = '' ) |
|
| 214 | 214 | { |
| 215 | - self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\'); |
|
| 215 | + self::$searchPaths[ $path ] = rtrim( trim( (string)$nsPrefix ), '\\' ); |
|
| 216 | 216 | |
| 217 | 217 | }//end addSearchPath() |
| 218 | 218 | |
@@ -237,13 +237,13 @@ discard block |
||
| 237 | 237 | * @throws \Exception If the file path has not been loaded. |
| 238 | 238 | * @return string |
| 239 | 239 | */ |
| 240 | - public static function getLoadedClassName($path) |
|
| 240 | + public static function getLoadedClassName( $path ) |
|
| 241 | 241 | { |
| 242 | - if (isset(self::$loadedClasses[$path]) === false) { |
|
| 243 | - throw new \Exception("Cannot get class name for $path; file has not been included"); |
|
| 242 | + if ( isset( self::$loadedClasses[ $path ] ) === false ) { |
|
| 243 | + throw new \Exception( "Cannot get class name for $path; file has not been included" ); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - return self::$loadedClasses[$path]; |
|
| 246 | + return self::$loadedClasses[ $path ]; |
|
| 247 | 247 | |
| 248 | 248 | }//end getLoadedClassName() |
| 249 | 249 | |
@@ -256,13 +256,13 @@ discard block |
||
| 256 | 256 | * @throws \Exception If the class name has not been loaded |
| 257 | 257 | * @return string |
| 258 | 258 | */ |
| 259 | - public static function getLoadedFileName($class) |
|
| 259 | + public static function getLoadedFileName( $class ) |
|
| 260 | 260 | { |
| 261 | - if (isset(self::$loadedFiles[$class]) === false) { |
|
| 262 | - throw new \Exception("Cannot get file name for $class; class has not been included"); |
|
| 261 | + if ( isset( self::$loadedFiles[ $class ] ) === false ) { |
|
| 262 | + throw new \Exception( "Cannot get file name for $class; class has not been included" ); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - return self::$loadedFiles[$class]; |
|
| 265 | + return self::$loadedFiles[ $class ]; |
|
| 266 | 266 | |
| 267 | 267 | }//end getLoadedFileName() |
| 268 | 268 | |
@@ -296,5 +296,5 @@ discard block |
||
| 296 | 296 | // Register the autoloader before any existing autoloaders to ensure |
| 297 | 297 | // it gets a chance to hear about every autoload request, and record |
| 298 | 298 | // the file and class name for it. |
| 299 | - spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true); |
|
| 299 | + spl_autoload_register( __NAMESPACE__ . '\Autoload::load', true, true ); |
|
| 300 | 300 | }//end if |
@@ -17,8 +17,7 @@ discard block |
||
| 17 | 17 | namespace PHP_CodeSniffer; |
| 18 | 18 | |
| 19 | 19 | if (class_exists('PHP_CodeSniffer\Autoload', false) === false) { |
| 20 | - class Autoload |
|
| 21 | - { |
|
| 20 | + class Autoload { |
|
| 22 | 21 | |
| 23 | 22 | /** |
| 24 | 23 | * The composer autoloader. |
@@ -61,8 +60,7 @@ discard block |
||
| 61 | 60 | * |
| 62 | 61 | * @return bool |
| 63 | 62 | */ |
| 64 | - public static function load($class) |
|
| 65 | - { |
|
| 63 | + public static function load($class) { |
|
| 66 | 64 | // Include the composer autoloader if there is one, but re-register it |
| 67 | 65 | // so this autoloader runs before the composer one as we need to include |
| 68 | 66 | // all files so we can figure out what the class/interface/trait name is. |
@@ -147,8 +145,7 @@ discard block |
||
| 147 | 145 | * |
| 148 | 146 | * @return string The fully qualified name of the class in the loaded file. |
| 149 | 147 | */ |
| 150 | - public static function loadFile($path) |
|
| 151 | - { |
|
| 148 | + public static function loadFile($path) { |
|
| 152 | 149 | if (strpos(__DIR__, 'phar://') !== 0) { |
| 153 | 150 | $path = realpath($path); |
| 154 | 151 | if ($path === false) { |
@@ -210,8 +207,7 @@ discard block |
||
| 210 | 207 | * |
| 211 | 208 | * @return void |
| 212 | 209 | */ |
| 213 | - public static function addSearchPath($path, $nsPrefix='') |
|
| 214 | - { |
|
| 210 | + public static function addSearchPath($path, $nsPrefix='') { |
|
| 215 | 211 | self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\'); |
| 216 | 212 | |
| 217 | 213 | }//end addSearchPath() |
@@ -222,8 +218,7 @@ discard block |
||
| 222 | 218 | * |
| 223 | 219 | * @return array |
| 224 | 220 | */ |
| 225 | - public static function getSearchPaths() |
|
| 226 | - { |
|
| 221 | + public static function getSearchPaths() { |
|
| 227 | 222 | return self::$searchPaths; |
| 228 | 223 | |
| 229 | 224 | }//end getSearchPaths() |
@@ -237,8 +232,7 @@ discard block |
||
| 237 | 232 | * @throws \Exception If the file path has not been loaded. |
| 238 | 233 | * @return string |
| 239 | 234 | */ |
| 240 | - public static function getLoadedClassName($path) |
|
| 241 | - { |
|
| 235 | + public static function getLoadedClassName($path) { |
|
| 242 | 236 | if (isset(self::$loadedClasses[$path]) === false) { |
| 243 | 237 | throw new \Exception("Cannot get class name for $path; file has not been included"); |
| 244 | 238 | } |
@@ -256,8 +250,7 @@ discard block |
||
| 256 | 250 | * @throws \Exception If the class name has not been loaded |
| 257 | 251 | * @return string |
| 258 | 252 | */ |
| 259 | - public static function getLoadedFileName($class) |
|
| 260 | - { |
|
| 253 | + public static function getLoadedFileName($class) { |
|
| 261 | 254 | if (isset(self::$loadedFiles[$class]) === false) { |
| 262 | 255 | throw new \Exception("Cannot get file name for $class; class has not been included"); |
| 263 | 256 | } |
@@ -272,8 +265,7 @@ discard block |
||
| 272 | 265 | * |
| 273 | 266 | * @return array<string, string> |
| 274 | 267 | */ |
| 275 | - public static function getLoadedClasses() |
|
| 276 | - { |
|
| 268 | + public static function getLoadedClasses() { |
|
| 277 | 269 | return self::$loadedClasses; |
| 278 | 270 | |
| 279 | 271 | }//end getLoadedClasses() |
@@ -284,8 +276,7 @@ discard block |
||
| 284 | 276 | * |
| 285 | 277 | * @return array<string, string> |
| 286 | 278 | */ |
| 287 | - public static function getLoadedFiles() |
|
| 288 | - { |
|
| 279 | + public static function getLoadedFiles() { |
|
| 289 | 280 | return self::$loadedFiles; |
| 290 | 281 | |
| 291 | 282 | }//end getLoadedFiles() |
@@ -705,7 +705,7 @@ discard block |
||
| 705 | 705 | * @param string $error The error message. |
| 706 | 706 | * @param int $line The line on which the error occurred. |
| 707 | 707 | * @param string $code A violation code unique to the sniff message. |
| 708 | - * @param array $data Replacements for the error message. |
|
| 708 | + * @param string[] $data Replacements for the error message. |
|
| 709 | 709 | * @param int $severity The severity level for this error. A value of 0 |
| 710 | 710 | * will be converted into the default severity level. |
| 711 | 711 | * |
@@ -729,7 +729,7 @@ discard block |
||
| 729 | 729 | * @param string $warning The error message. |
| 730 | 730 | * @param int $line The line on which the warning occurred. |
| 731 | 731 | * @param string $code A violation code unique to the sniff message. |
| 732 | - * @param array $data Replacements for the warning message. |
|
| 732 | + * @param string[] $data Replacements for the warning message. |
|
| 733 | 733 | * @param int $severity The severity level for this warning. A value of 0 will |
| 734 | 734 | * will be converted into the default severity level. |
| 735 | 735 | * |
@@ -19,2467 +19,2467 @@ |
||
| 19 | 19 | class File |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The absolute path to the file associated with this object. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - public $path = ''; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The content of the file. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $content = ''; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The config data for the run. |
|
| 38 | - * |
|
| 39 | - * @var \PHP_CodeSniffer\Config |
|
| 40 | - */ |
|
| 41 | - public $config = null; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The ruleset used for the run. |
|
| 45 | - * |
|
| 46 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 47 | - */ |
|
| 48 | - public $ruleset = null; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * If TRUE, the entire file is being ignored. |
|
| 52 | - * |
|
| 53 | - * @var boolean |
|
| 54 | - */ |
|
| 55 | - public $ignored = false; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The EOL character this file uses. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - public $eolChar = ''; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The Fixer object to control fixing errors. |
|
| 66 | - * |
|
| 67 | - * @var \PHP_CodeSniffer\Fixer |
|
| 68 | - */ |
|
| 69 | - public $fixer = null; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The tokenizer being used for this file. |
|
| 73 | - * |
|
| 74 | - * @var \PHP_CodeSniffer\Tokenizers\Tokenizer |
|
| 75 | - */ |
|
| 76 | - public $tokenizer = null; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The name of the tokenizer being used for this file. |
|
| 80 | - * |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public $tokenizerType = 'PHP'; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Was the file loaded from cache? |
|
| 87 | - * |
|
| 88 | - * If TRUE, the file was loaded from a local cache. |
|
| 89 | - * If FALSE, the file was tokenized and processed fully. |
|
| 90 | - * |
|
| 91 | - * @var boolean |
|
| 92 | - */ |
|
| 93 | - public $fromCache = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * The number of tokens in this file. |
|
| 97 | - * |
|
| 98 | - * Stored here to save calling count() everywhere. |
|
| 99 | - * |
|
| 100 | - * @var integer |
|
| 101 | - */ |
|
| 102 | - public $numTokens = 0; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * The tokens stack map. |
|
| 106 | - * |
|
| 107 | - * @var array |
|
| 108 | - */ |
|
| 109 | - protected $tokens = []; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * The errors raised from sniffs. |
|
| 113 | - * |
|
| 114 | - * @var array |
|
| 115 | - * @see getErrors() |
|
| 116 | - */ |
|
| 117 | - protected $errors = []; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The warnings raised from sniffs. |
|
| 121 | - * |
|
| 122 | - * @var array |
|
| 123 | - * @see getWarnings() |
|
| 124 | - */ |
|
| 125 | - protected $warnings = []; |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * The metrics recorded by sniffs. |
|
| 129 | - * |
|
| 130 | - * @var array |
|
| 131 | - * @see getMetrics() |
|
| 132 | - */ |
|
| 133 | - protected $metrics = []; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * The metrics recorded for each token. |
|
| 137 | - * |
|
| 138 | - * Stops the same metric being recorded for the same token twice. |
|
| 139 | - * |
|
| 140 | - * @var array |
|
| 141 | - * @see getMetrics() |
|
| 142 | - */ |
|
| 143 | - private $metricTokens = []; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * The total number of errors raised. |
|
| 147 | - * |
|
| 148 | - * @var integer |
|
| 149 | - */ |
|
| 150 | - protected $errorCount = 0; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * The total number of warnings raised. |
|
| 154 | - * |
|
| 155 | - * @var integer |
|
| 156 | - */ |
|
| 157 | - protected $warningCount = 0; |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * The total number of errors and warnings that can be fixed. |
|
| 161 | - * |
|
| 162 | - * @var integer |
|
| 163 | - */ |
|
| 164 | - protected $fixableCount = 0; |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * The total number of errors and warnings that were fixed. |
|
| 168 | - * |
|
| 169 | - * @var integer |
|
| 170 | - */ |
|
| 171 | - protected $fixedCount = 0; |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * An array of sniffs that are being ignored. |
|
| 175 | - * |
|
| 176 | - * @var array |
|
| 177 | - */ |
|
| 178 | - protected $ignoredListeners = []; |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * An array of message codes that are being ignored. |
|
| 182 | - * |
|
| 183 | - * @var array |
|
| 184 | - */ |
|
| 185 | - protected $ignoredCodes = []; |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * An array of sniffs listening to this file's processing. |
|
| 189 | - * |
|
| 190 | - * @var \PHP_CodeSniffer\Sniffs\Sniff[] |
|
| 191 | - */ |
|
| 192 | - protected $listeners = []; |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * The class name of the sniff currently processing the file. |
|
| 196 | - * |
|
| 197 | - * @var string |
|
| 198 | - */ |
|
| 199 | - protected $activeListener = ''; |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * An array of sniffs being processed and how long they took. |
|
| 203 | - * |
|
| 204 | - * @var array |
|
| 205 | - */ |
|
| 206 | - protected $listenerTimes = []; |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * A cache of often used config settings to improve performance. |
|
| 210 | - * |
|
| 211 | - * Storing them here saves 10k+ calls to __get() in the Config class. |
|
| 212 | - * |
|
| 213 | - * @var array |
|
| 214 | - */ |
|
| 215 | - protected $configCache = []; |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Constructs a file. |
|
| 220 | - * |
|
| 221 | - * @param string $path The absolute path to the file to process. |
|
| 222 | - * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 223 | - * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 224 | - * |
|
| 225 | - * @return void |
|
| 226 | - */ |
|
| 227 | - public function __construct($path, Ruleset $ruleset, Config $config) |
|
| 228 | - { |
|
| 229 | - $this->path = $path; |
|
| 230 | - $this->ruleset = $ruleset; |
|
| 231 | - $this->config = $config; |
|
| 232 | - $this->fixer = new Fixer(); |
|
| 233 | - |
|
| 234 | - $parts = explode('.', $path); |
|
| 235 | - $extension = array_pop($parts); |
|
| 236 | - if (isset($config->extensions[$extension]) === true) { |
|
| 237 | - $this->tokenizerType = $config->extensions[$extension]; |
|
| 238 | - } else { |
|
| 239 | - // Revert to default. |
|
| 240 | - $this->tokenizerType = 'PHP'; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - $this->configCache['cache'] = $this->config->cache; |
|
| 244 | - $this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs); |
|
| 245 | - $this->configCache['exclude'] = array_map('strtolower', $this->config->exclude); |
|
| 246 | - $this->configCache['errorSeverity'] = $this->config->errorSeverity; |
|
| 247 | - $this->configCache['warningSeverity'] = $this->config->warningSeverity; |
|
| 248 | - $this->configCache['recordErrors'] = $this->config->recordErrors; |
|
| 249 | - $this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns; |
|
| 250 | - $this->configCache['includePatterns'] = $this->ruleset->includePatterns; |
|
| 251 | - |
|
| 252 | - }//end __construct() |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Set the content of the file. |
|
| 257 | - * |
|
| 258 | - * Setting the content also calculates the EOL char being used. |
|
| 259 | - * |
|
| 260 | - * @param string $content The file content. |
|
| 261 | - * |
|
| 262 | - * @return void |
|
| 263 | - */ |
|
| 264 | - public function setContent($content) |
|
| 265 | - { |
|
| 266 | - $this->content = $content; |
|
| 267 | - $this->tokens = []; |
|
| 268 | - |
|
| 269 | - try { |
|
| 270 | - $this->eolChar = Util\Common::detectLineEndings($content); |
|
| 271 | - } catch (RuntimeException $e) { |
|
| 272 | - $this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings'); |
|
| 273 | - return; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - }//end setContent() |
|
| 277 | - |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Reloads the content of the file. |
|
| 281 | - * |
|
| 282 | - * By default, we have no idea where our content comes from, |
|
| 283 | - * so we can't do anything. |
|
| 284 | - * |
|
| 285 | - * @return void |
|
| 286 | - */ |
|
| 287 | - public function reloadContent() |
|
| 288 | - { |
|
| 289 | - |
|
| 290 | - }//end reloadContent() |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Disables caching of this file. |
|
| 295 | - * |
|
| 296 | - * @return void |
|
| 297 | - */ |
|
| 298 | - public function disableCaching() |
|
| 299 | - { |
|
| 300 | - $this->configCache['cache'] = false; |
|
| 301 | - |
|
| 302 | - }//end disableCaching() |
|
| 303 | - |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Starts the stack traversal and tells listeners when tokens are found. |
|
| 307 | - * |
|
| 308 | - * @return void |
|
| 309 | - */ |
|
| 310 | - public function process() |
|
| 311 | - { |
|
| 312 | - if ($this->ignored === true) { |
|
| 313 | - return; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $this->errors = []; |
|
| 317 | - $this->warnings = []; |
|
| 318 | - $this->errorCount = 0; |
|
| 319 | - $this->warningCount = 0; |
|
| 320 | - $this->fixableCount = 0; |
|
| 321 | - |
|
| 322 | - $this->parse(); |
|
| 323 | - |
|
| 324 | - // Check if tokenizer errors cause this file to be ignored. |
|
| 325 | - if ($this->ignored === true) { |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - $this->fixer->startFile($this); |
|
| 330 | - |
|
| 331 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 332 | - echo "\t*** START TOKEN PROCESSING ***".PHP_EOL; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - $foundCode = false; |
|
| 336 | - $listenerIgnoreTo = []; |
|
| 337 | - $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); |
|
| 338 | - $checkAnnotations = $this->config->annotations; |
|
| 339 | - |
|
| 340 | - // Foreach of the listeners that have registered to listen for this |
|
| 341 | - // token, get them to process it. |
|
| 342 | - foreach ($this->tokens as $stackPtr => $token) { |
|
| 343 | - // Check for ignored lines. |
|
| 344 | - if ($checkAnnotations === true |
|
| 345 | - && ($token['code'] === T_COMMENT |
|
| 346 | - || $token['code'] === T_PHPCS_IGNORE_FILE |
|
| 347 | - || $token['code'] === T_PHPCS_SET |
|
| 348 | - || $token['code'] === T_DOC_COMMENT_STRING |
|
| 349 | - || $token['code'] === T_DOC_COMMENT_TAG |
|
| 350 | - || ($inTests === true && $token['code'] === T_INLINE_HTML)) |
|
| 351 | - ) { |
|
| 352 | - $commentText = ltrim($this->tokens[$stackPtr]['content'], ' /*'); |
|
| 353 | - $commentTextLower = strtolower($commentText); |
|
| 354 | - if (strpos($commentText, '@codingStandards') !== false) { |
|
| 355 | - if (strpos($commentText, '@codingStandardsIgnoreFile') !== false) { |
|
| 356 | - // Ignoring the whole file, just a little late. |
|
| 357 | - $this->errors = []; |
|
| 358 | - $this->warnings = []; |
|
| 359 | - $this->errorCount = 0; |
|
| 360 | - $this->warningCount = 0; |
|
| 361 | - $this->fixableCount = 0; |
|
| 362 | - return; |
|
| 363 | - } else if (strpos($commentText, '@codingStandardsChangeSetting') !== false) { |
|
| 364 | - $start = strpos($commentText, '@codingStandardsChangeSetting'); |
|
| 365 | - $comment = substr($commentText, ($start + 30)); |
|
| 366 | - $parts = explode(' ', $comment); |
|
| 367 | - if (count($parts) >= 2) { |
|
| 368 | - $sniffParts = explode('.', $parts[0]); |
|
| 369 | - if (count($sniffParts) >= 3) { |
|
| 370 | - // If the sniff code is not known to us, it has not been registered in this run. |
|
| 371 | - // But don't throw an error as it could be there for a different standard to use. |
|
| 372 | - if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) { |
|
| 373 | - $listenerCode = array_shift($parts); |
|
| 374 | - $propertyCode = array_shift($parts); |
|
| 375 | - $propertyValue = rtrim(implode(' ', $parts), " */\r\n"); |
|
| 376 | - $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 377 | - $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - }//end if |
|
| 382 | - } else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile' |
|
| 383 | - || substr($commentTextLower, 0, 17) === '@phpcs:ignorefile' |
|
| 384 | - ) { |
|
| 385 | - // Ignoring the whole file, just a little late. |
|
| 386 | - $this->errors = []; |
|
| 387 | - $this->warnings = []; |
|
| 388 | - $this->errorCount = 0; |
|
| 389 | - $this->warningCount = 0; |
|
| 390 | - $this->fixableCount = 0; |
|
| 391 | - return; |
|
| 392 | - } else if (substr($commentTextLower, 0, 9) === 'phpcs:set' |
|
| 393 | - || substr($commentTextLower, 0, 10) === '@phpcs:set' |
|
| 394 | - ) { |
|
| 395 | - if (isset($token['sniffCode']) === true) { |
|
| 396 | - $listenerCode = $token['sniffCode']; |
|
| 397 | - if (isset($this->ruleset->sniffCodes[$listenerCode]) === true) { |
|
| 398 | - $propertyCode = $token['sniffProperty']; |
|
| 399 | - $propertyValue = $token['sniffPropertyValue']; |
|
| 400 | - $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 401 | - $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - }//end if |
|
| 405 | - }//end if |
|
| 406 | - |
|
| 407 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 408 | - $type = $token['type']; |
|
| 409 | - $content = Util\Common::prepareForOutput($token['content']); |
|
| 410 | - echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ($token['code'] !== T_INLINE_HTML) { |
|
| 414 | - $foundCode = true; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - if (isset($this->ruleset->tokenListeners[$token['code']]) === false) { |
|
| 418 | - continue; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - foreach ($this->ruleset->tokenListeners[$token['code']] as $listenerData) { |
|
| 422 | - if (isset($this->ignoredListeners[$listenerData['class']]) === true |
|
| 423 | - || (isset($listenerIgnoreTo[$listenerData['class']]) === true |
|
| 424 | - && $listenerIgnoreTo[$listenerData['class']] > $stackPtr) |
|
| 425 | - ) { |
|
| 426 | - // This sniff is ignoring past this token, or the whole file. |
|
| 427 | - continue; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Make sure this sniff supports the tokenizer |
|
| 431 | - // we are currently using. |
|
| 432 | - $class = $listenerData['class']; |
|
| 433 | - |
|
| 434 | - if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) { |
|
| 435 | - continue; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - // If the file path matches one of our ignore patterns, skip it. |
|
| 439 | - // While there is support for a type of each pattern |
|
| 440 | - // (absolute or relative) we don't actually support it here. |
|
| 441 | - foreach ($listenerData['ignore'] as $pattern) { |
|
| 442 | - // We assume a / directory separator, as do the exclude rules |
|
| 443 | - // most developers write, so we need a special case for any system |
|
| 444 | - // that is different. |
|
| 445 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 446 | - $pattern = str_replace('/', '\\\\', $pattern); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $pattern = '`'.$pattern.'`i'; |
|
| 450 | - if (preg_match($pattern, $this->path) === 1) { |
|
| 451 | - $this->ignoredListeners[$class] = true; |
|
| 452 | - continue(2); |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // If the file path does not match one of our include patterns, skip it. |
|
| 457 | - // While there is support for a type of each pattern |
|
| 458 | - // (absolute or relative) we don't actually support it here. |
|
| 459 | - if (empty($listenerData['include']) === false) { |
|
| 460 | - $included = false; |
|
| 461 | - foreach ($listenerData['include'] as $pattern) { |
|
| 462 | - // We assume a / directory separator, as do the exclude rules |
|
| 463 | - // most developers write, so we need a special case for any system |
|
| 464 | - // that is different. |
|
| 465 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 466 | - $pattern = str_replace('/', '\\\\', $pattern); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - $pattern = '`'.$pattern.'`i'; |
|
| 470 | - if (preg_match($pattern, $this->path) === 1) { |
|
| 471 | - $included = true; |
|
| 472 | - break; |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - if ($included === false) { |
|
| 477 | - $this->ignoredListeners[$class] = true; |
|
| 478 | - continue; |
|
| 479 | - } |
|
| 480 | - }//end if |
|
| 481 | - |
|
| 482 | - $this->activeListener = $class; |
|
| 483 | - |
|
| 484 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 485 | - $startTime = microtime(true); |
|
| 486 | - echo "\t\t\tProcessing ".$this->activeListener.'... '; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - $ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr); |
|
| 490 | - if ($ignoreTo !== null) { |
|
| 491 | - $listenerIgnoreTo[$this->activeListener] = $ignoreTo; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 495 | - $timeTaken = (microtime(true) - $startTime); |
|
| 496 | - if (isset($this->listenerTimes[$this->activeListener]) === false) { |
|
| 497 | - $this->listenerTimes[$this->activeListener] = 0; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - $this->listenerTimes[$this->activeListener] += $timeTaken; |
|
| 501 | - |
|
| 502 | - $timeTaken = round(($timeTaken), 4); |
|
| 503 | - echo "DONE in $timeTaken seconds".PHP_EOL; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - $this->activeListener = ''; |
|
| 507 | - }//end foreach |
|
| 508 | - }//end foreach |
|
| 509 | - |
|
| 510 | - // If short open tags are off but the file being checked uses |
|
| 511 | - // short open tags, the whole content will be inline HTML |
|
| 512 | - // and nothing will be checked. So try and handle this case. |
|
| 513 | - // We don't show this error for STDIN because we can't be sure the content |
|
| 514 | - // actually came directly from the user. It could be something like |
|
| 515 | - // refs from a Git pre-push hook. |
|
| 516 | - if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') { |
|
| 517 | - $shortTags = (bool) ini_get('short_open_tag'); |
|
| 518 | - if ($shortTags === false) { |
|
| 519 | - $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.'; |
|
| 520 | - $this->addWarning($error, null, 'Internal.NoCodeFound'); |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 525 | - echo "\t*** END TOKEN PROCESSING ***".PHP_EOL; |
|
| 526 | - echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 527 | - |
|
| 528 | - asort($this->listenerTimes, SORT_NUMERIC); |
|
| 529 | - $this->listenerTimes = array_reverse($this->listenerTimes, true); |
|
| 530 | - foreach ($this->listenerTimes as $listener => $timeTaken) { |
|
| 531 | - echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - $this->fixedCount += $this->fixer->getFixCount(); |
|
| 538 | - |
|
| 539 | - }//end process() |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Tokenizes the file and prepares it for the test run. |
|
| 544 | - * |
|
| 545 | - * @return void |
|
| 546 | - */ |
|
| 547 | - public function parse() |
|
| 548 | - { |
|
| 549 | - if (empty($this->tokens) === false) { |
|
| 550 | - // File has already been parsed. |
|
| 551 | - return; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - try { |
|
| 555 | - $tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType; |
|
| 556 | - $this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar); |
|
| 557 | - $this->tokens = $this->tokenizer->getTokens(); |
|
| 558 | - } catch (TokenizerException $e) { |
|
| 559 | - $this->ignored = true; |
|
| 560 | - $this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception'); |
|
| 561 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 562 | - echo "[$this->tokenizerType => tokenizer error]... "; |
|
| 563 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 564 | - echo PHP_EOL; |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - return; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - $this->numTokens = count($this->tokens); |
|
| 572 | - |
|
| 573 | - // Check for mixed line endings as these can cause tokenizer errors and we |
|
| 574 | - // should let the user know that the results they get may be incorrect. |
|
| 575 | - // This is done by removing all backslashes, removing the newline char we |
|
| 576 | - // detected, then converting newlines chars into text. If any backslashes |
|
| 577 | - // are left at the end, we have additional newline chars in use. |
|
| 578 | - $contents = str_replace('\\', '', $this->content); |
|
| 579 | - $contents = str_replace($this->eolChar, '', $contents); |
|
| 580 | - $contents = str_replace("\n", '\n', $contents); |
|
| 581 | - $contents = str_replace("\r", '\r', $contents); |
|
| 582 | - if (strpos($contents, '\\') !== false) { |
|
| 583 | - $error = 'File has mixed line endings; this may cause incorrect results'; |
|
| 584 | - $this->addWarningOnLine($error, 1, 'Internal.LineEndings.Mixed'); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 588 | - if ($this->numTokens === 0) { |
|
| 589 | - $numLines = 0; |
|
| 590 | - } else { |
|
| 591 | - $numLines = $this->tokens[($this->numTokens - 1)]['line']; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... "; |
|
| 595 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 596 | - echo PHP_EOL; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - }//end parse() |
|
| 601 | - |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Returns the token stack for this file. |
|
| 605 | - * |
|
| 606 | - * @return array |
|
| 607 | - */ |
|
| 608 | - public function getTokens() |
|
| 609 | - { |
|
| 610 | - return $this->tokens; |
|
| 611 | - |
|
| 612 | - }//end getTokens() |
|
| 613 | - |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Remove vars stored in this file that are no longer required. |
|
| 617 | - * |
|
| 618 | - * @return void |
|
| 619 | - */ |
|
| 620 | - public function cleanUp() |
|
| 621 | - { |
|
| 622 | - $this->listenerTimes = null; |
|
| 623 | - $this->content = null; |
|
| 624 | - $this->tokens = null; |
|
| 625 | - $this->metricTokens = null; |
|
| 626 | - $this->tokenizer = null; |
|
| 627 | - $this->fixer = null; |
|
| 628 | - $this->config = null; |
|
| 629 | - $this->ruleset = null; |
|
| 630 | - |
|
| 631 | - }//end cleanUp() |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Records an error against a specific token in the file. |
|
| 636 | - * |
|
| 637 | - * @param string $error The error message. |
|
| 638 | - * @param int $stackPtr The stack position where the error occurred. |
|
| 639 | - * @param string $code A violation code unique to the sniff message. |
|
| 640 | - * @param array $data Replacements for the error message. |
|
| 641 | - * @param int $severity The severity level for this error. A value of 0 |
|
| 642 | - * will be converted into the default severity level. |
|
| 643 | - * @param boolean $fixable Can the error be fixed by the sniff? |
|
| 644 | - * |
|
| 645 | - * @return boolean |
|
| 646 | - */ |
|
| 647 | - public function addError( |
|
| 648 | - $error, |
|
| 649 | - $stackPtr, |
|
| 650 | - $code, |
|
| 651 | - $data=[], |
|
| 652 | - $severity=0, |
|
| 653 | - $fixable=false |
|
| 654 | - ) { |
|
| 655 | - if ($stackPtr === null) { |
|
| 656 | - $line = 1; |
|
| 657 | - $column = 1; |
|
| 658 | - } else { |
|
| 659 | - $line = $this->tokens[$stackPtr]['line']; |
|
| 660 | - $column = $this->tokens[$stackPtr]['column']; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - return $this->addMessage(true, $error, $line, $column, $code, $data, $severity, $fixable); |
|
| 664 | - |
|
| 665 | - }//end addError() |
|
| 666 | - |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Records a warning against a specific token in the file. |
|
| 670 | - * |
|
| 671 | - * @param string $warning The error message. |
|
| 672 | - * @param int $stackPtr The stack position where the error occurred. |
|
| 673 | - * @param string $code A violation code unique to the sniff message. |
|
| 674 | - * @param array $data Replacements for the warning message. |
|
| 675 | - * @param int $severity The severity level for this warning. A value of 0 |
|
| 676 | - * will be converted into the default severity level. |
|
| 677 | - * @param boolean $fixable Can the warning be fixed by the sniff? |
|
| 678 | - * |
|
| 679 | - * @return boolean |
|
| 680 | - */ |
|
| 681 | - public function addWarning( |
|
| 682 | - $warning, |
|
| 683 | - $stackPtr, |
|
| 684 | - $code, |
|
| 685 | - $data=[], |
|
| 686 | - $severity=0, |
|
| 687 | - $fixable=false |
|
| 688 | - ) { |
|
| 689 | - if ($stackPtr === null) { |
|
| 690 | - $line = 1; |
|
| 691 | - $column = 1; |
|
| 692 | - } else { |
|
| 693 | - $line = $this->tokens[$stackPtr]['line']; |
|
| 694 | - $column = $this->tokens[$stackPtr]['column']; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - return $this->addMessage(false, $warning, $line, $column, $code, $data, $severity, $fixable); |
|
| 698 | - |
|
| 699 | - }//end addWarning() |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Records an error against a specific line in the file. |
|
| 704 | - * |
|
| 705 | - * @param string $error The error message. |
|
| 706 | - * @param int $line The line on which the error occurred. |
|
| 707 | - * @param string $code A violation code unique to the sniff message. |
|
| 708 | - * @param array $data Replacements for the error message. |
|
| 709 | - * @param int $severity The severity level for this error. A value of 0 |
|
| 710 | - * will be converted into the default severity level. |
|
| 711 | - * |
|
| 712 | - * @return boolean |
|
| 713 | - */ |
|
| 714 | - public function addErrorOnLine( |
|
| 715 | - $error, |
|
| 716 | - $line, |
|
| 717 | - $code, |
|
| 718 | - $data=[], |
|
| 719 | - $severity=0 |
|
| 720 | - ) { |
|
| 721 | - return $this->addMessage(true, $error, $line, 1, $code, $data, $severity, false); |
|
| 722 | - |
|
| 723 | - }//end addErrorOnLine() |
|
| 724 | - |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Records a warning against a specific token in the file. |
|
| 728 | - * |
|
| 729 | - * @param string $warning The error message. |
|
| 730 | - * @param int $line The line on which the warning occurred. |
|
| 731 | - * @param string $code A violation code unique to the sniff message. |
|
| 732 | - * @param array $data Replacements for the warning message. |
|
| 733 | - * @param int $severity The severity level for this warning. A value of 0 will |
|
| 734 | - * will be converted into the default severity level. |
|
| 735 | - * |
|
| 736 | - * @return boolean |
|
| 737 | - */ |
|
| 738 | - public function addWarningOnLine( |
|
| 739 | - $warning, |
|
| 740 | - $line, |
|
| 741 | - $code, |
|
| 742 | - $data=[], |
|
| 743 | - $severity=0 |
|
| 744 | - ) { |
|
| 745 | - return $this->addMessage(false, $warning, $line, 1, $code, $data, $severity, false); |
|
| 746 | - |
|
| 747 | - }//end addWarningOnLine() |
|
| 748 | - |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Records a fixable error against a specific token in the file. |
|
| 752 | - * |
|
| 753 | - * Returns true if the error was recorded and should be fixed. |
|
| 754 | - * |
|
| 755 | - * @param string $error The error message. |
|
| 756 | - * @param int $stackPtr The stack position where the error occurred. |
|
| 757 | - * @param string $code A violation code unique to the sniff message. |
|
| 758 | - * @param array $data Replacements for the error message. |
|
| 759 | - * @param int $severity The severity level for this error. A value of 0 |
|
| 760 | - * will be converted into the default severity level. |
|
| 761 | - * |
|
| 762 | - * @return boolean |
|
| 763 | - */ |
|
| 764 | - public function addFixableError( |
|
| 765 | - $error, |
|
| 766 | - $stackPtr, |
|
| 767 | - $code, |
|
| 768 | - $data=[], |
|
| 769 | - $severity=0 |
|
| 770 | - ) { |
|
| 771 | - $recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true); |
|
| 772 | - if ($recorded === true && $this->fixer->enabled === true) { |
|
| 773 | - return true; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - return false; |
|
| 777 | - |
|
| 778 | - }//end addFixableError() |
|
| 779 | - |
|
| 780 | - |
|
| 781 | - /** |
|
| 782 | - * Records a fixable warning against a specific token in the file. |
|
| 783 | - * |
|
| 784 | - * Returns true if the warning was recorded and should be fixed. |
|
| 785 | - * |
|
| 786 | - * @param string $warning The error message. |
|
| 787 | - * @param int $stackPtr The stack position where the error occurred. |
|
| 788 | - * @param string $code A violation code unique to the sniff message. |
|
| 789 | - * @param array $data Replacements for the warning message. |
|
| 790 | - * @param int $severity The severity level for this warning. A value of 0 |
|
| 791 | - * will be converted into the default severity level. |
|
| 792 | - * |
|
| 793 | - * @return boolean |
|
| 794 | - */ |
|
| 795 | - public function addFixableWarning( |
|
| 796 | - $warning, |
|
| 797 | - $stackPtr, |
|
| 798 | - $code, |
|
| 799 | - $data=[], |
|
| 800 | - $severity=0 |
|
| 801 | - ) { |
|
| 802 | - $recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true); |
|
| 803 | - if ($recorded === true && $this->fixer->enabled === true) { |
|
| 804 | - return true; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - return false; |
|
| 808 | - |
|
| 809 | - }//end addFixableWarning() |
|
| 810 | - |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * Adds an error to the error stack. |
|
| 814 | - * |
|
| 815 | - * @param boolean $error Is this an error message? |
|
| 816 | - * @param string $message The text of the message. |
|
| 817 | - * @param int $line The line on which the message occurred. |
|
| 818 | - * @param int $column The column at which the message occurred. |
|
| 819 | - * @param string $code A violation code unique to the sniff message. |
|
| 820 | - * @param array $data Replacements for the message. |
|
| 821 | - * @param int $severity The severity level for this message. A value of 0 |
|
| 822 | - * will be converted into the default severity level. |
|
| 823 | - * @param boolean $fixable Can the problem be fixed by the sniff? |
|
| 824 | - * |
|
| 825 | - * @return boolean |
|
| 826 | - */ |
|
| 827 | - protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
|
| 828 | - { |
|
| 829 | - // Check if this line is ignoring all message codes. |
|
| 830 | - if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) { |
|
| 831 | - return false; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - // Work out which sniff generated the message. |
|
| 835 | - $parts = explode('.', $code); |
|
| 836 | - if ($parts[0] === 'Internal') { |
|
| 837 | - // An internal message. |
|
| 838 | - $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 839 | - $sniffCode = $code; |
|
| 840 | - $checkCodes = [$sniffCode]; |
|
| 841 | - } else { |
|
| 842 | - if ($parts[0] !== $code) { |
|
| 843 | - // The full message code has been passed in. |
|
| 844 | - $sniffCode = $code; |
|
| 845 | - $listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.')); |
|
| 846 | - } else { |
|
| 847 | - $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 848 | - $sniffCode = $listenerCode.'.'.$code; |
|
| 849 | - $parts = explode('.', $sniffCode); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - $checkCodes = [ |
|
| 853 | - $sniffCode, |
|
| 854 | - $parts[0].'.'.$parts[1].'.'.$parts[2], |
|
| 855 | - $parts[0].'.'.$parts[1], |
|
| 856 | - $parts[0], |
|
| 857 | - ]; |
|
| 858 | - }//end if |
|
| 859 | - |
|
| 860 | - if (isset($this->tokenizer->ignoredLines[$line]) === true) { |
|
| 861 | - // Check if this line is ignoring this specific message. |
|
| 862 | - $ignored = false; |
|
| 863 | - foreach ($checkCodes as $checkCode) { |
|
| 864 | - if (isset($this->tokenizer->ignoredLines[$line][$checkCode]) === true) { |
|
| 865 | - $ignored = true; |
|
| 866 | - break; |
|
| 867 | - } |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - // If it is ignored, make sure it's not whitelisted. |
|
| 871 | - if ($ignored === true |
|
| 872 | - && isset($this->tokenizer->ignoredLines[$line]['.except']) === true |
|
| 873 | - ) { |
|
| 874 | - foreach ($checkCodes as $checkCode) { |
|
| 875 | - if (isset($this->tokenizer->ignoredLines[$line]['.except'][$checkCode]) === true) { |
|
| 876 | - $ignored = false; |
|
| 877 | - break; |
|
| 878 | - } |
|
| 879 | - } |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - if ($ignored === true) { |
|
| 883 | - return false; |
|
| 884 | - } |
|
| 885 | - }//end if |
|
| 886 | - |
|
| 887 | - $includeAll = true; |
|
| 888 | - if ($this->configCache['cache'] === false |
|
| 889 | - || $this->configCache['recordErrors'] === false |
|
| 890 | - ) { |
|
| 891 | - $includeAll = false; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - // Filter out any messages for sniffs that shouldn't have run |
|
| 895 | - // due to the use of the --sniffs command line argument. |
|
| 896 | - if ($includeAll === false |
|
| 897 | - && ((empty($this->configCache['sniffs']) === false |
|
| 898 | - && in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false) |
|
| 899 | - || (empty($this->configCache['exclude']) === false |
|
| 900 | - && in_array(strtolower($listenerCode), $this->configCache['exclude'], true) === true)) |
|
| 901 | - ) { |
|
| 902 | - return false; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - // If we know this sniff code is being ignored for this file, return early. |
|
| 906 | - foreach ($checkCodes as $checkCode) { |
|
| 907 | - if (isset($this->ignoredCodes[$checkCode]) === true) { |
|
| 908 | - return false; |
|
| 909 | - } |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - $oppositeType = 'warning'; |
|
| 913 | - if ($error === false) { |
|
| 914 | - $oppositeType = 'error'; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - foreach ($checkCodes as $checkCode) { |
|
| 918 | - // Make sure this message type has not been set to the opposite message type. |
|
| 919 | - if (isset($this->ruleset->ruleset[$checkCode]['type']) === true |
|
| 920 | - && $this->ruleset->ruleset[$checkCode]['type'] === $oppositeType |
|
| 921 | - ) { |
|
| 922 | - $error = !$error; |
|
| 923 | - break; |
|
| 924 | - } |
|
| 925 | - } |
|
| 926 | - |
|
| 927 | - if ($error === true) { |
|
| 928 | - $configSeverity = $this->configCache['errorSeverity']; |
|
| 929 | - $messageCount = &$this->errorCount; |
|
| 930 | - $messages = &$this->errors; |
|
| 931 | - } else { |
|
| 932 | - $configSeverity = $this->configCache['warningSeverity']; |
|
| 933 | - $messageCount = &$this->warningCount; |
|
| 934 | - $messages = &$this->warnings; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - if ($includeAll === false && $configSeverity === 0) { |
|
| 938 | - // Don't bother doing any processing as these messages are just going to |
|
| 939 | - // be hidden in the reports anyway. |
|
| 940 | - return false; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - if ($severity === 0) { |
|
| 944 | - $severity = 5; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - foreach ($checkCodes as $checkCode) { |
|
| 948 | - // Make sure we are interested in this severity level. |
|
| 949 | - if (isset($this->ruleset->ruleset[$checkCode]['severity']) === true) { |
|
| 950 | - $severity = $this->ruleset->ruleset[$checkCode]['severity']; |
|
| 951 | - break; |
|
| 952 | - } |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - if ($includeAll === false && $configSeverity > $severity) { |
|
| 956 | - return false; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - // Make sure we are not ignoring this file. |
|
| 960 | - $included = null; |
|
| 961 | - foreach ($checkCodes as $checkCode) { |
|
| 962 | - $patterns = null; |
|
| 963 | - |
|
| 964 | - if (isset($this->configCache['includePatterns'][$checkCode]) === true) { |
|
| 965 | - $patterns = $this->configCache['includePatterns'][$checkCode]; |
|
| 966 | - $excluding = false; |
|
| 967 | - } else if (isset($this->configCache['ignorePatterns'][$checkCode]) === true) { |
|
| 968 | - $patterns = $this->configCache['ignorePatterns'][$checkCode]; |
|
| 969 | - $excluding = true; |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - if ($patterns === null) { |
|
| 973 | - continue; |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - foreach ($patterns as $pattern => $type) { |
|
| 977 | - // While there is support for a type of each pattern |
|
| 978 | - // (absolute or relative) we don't actually support it here. |
|
| 979 | - $replacements = [ |
|
| 980 | - '\\,' => ',', |
|
| 981 | - '*' => '.*', |
|
| 982 | - ]; |
|
| 983 | - |
|
| 984 | - // We assume a / directory separator, as do the exclude rules |
|
| 985 | - // most developers write, so we need a special case for any system |
|
| 986 | - // that is different. |
|
| 987 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 988 | - $replacements['/'] = '\\\\'; |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - $pattern = '`'.strtr($pattern, $replacements).'`i'; |
|
| 992 | - $matched = preg_match($pattern, $this->path); |
|
| 993 | - |
|
| 994 | - if ($matched === 0) { |
|
| 995 | - if ($excluding === false && $included === null) { |
|
| 996 | - // This file path is not being included. |
|
| 997 | - $included = false; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - continue; |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - if ($excluding === true) { |
|
| 1004 | - // This file path is being excluded. |
|
| 1005 | - $this->ignoredCodes[$checkCode] = true; |
|
| 1006 | - return false; |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - // This file path is being included. |
|
| 1010 | - $included = true; |
|
| 1011 | - break; |
|
| 1012 | - }//end foreach |
|
| 1013 | - }//end foreach |
|
| 1014 | - |
|
| 1015 | - if ($included === false) { |
|
| 1016 | - // There were include rules set, but this file |
|
| 1017 | - // path didn't match any of them. |
|
| 1018 | - return false; |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - $messageCount++; |
|
| 1022 | - if ($fixable === true) { |
|
| 1023 | - $this->fixableCount++; |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - if ($this->configCache['recordErrors'] === false |
|
| 1027 | - && $includeAll === false |
|
| 1028 | - ) { |
|
| 1029 | - return true; |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - // Work out the error message. |
|
| 1033 | - if (isset($this->ruleset->ruleset[$sniffCode]['message']) === true) { |
|
| 1034 | - $message = $this->ruleset->ruleset[$sniffCode]['message']; |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - if (empty($data) === false) { |
|
| 1038 | - $message = vsprintf($message, $data); |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - if (isset($messages[$line]) === false) { |
|
| 1042 | - $messages[$line] = []; |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - if (isset($messages[$line][$column]) === false) { |
|
| 1046 | - $messages[$line][$column] = []; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - $messages[$line][$column][] = [ |
|
| 1050 | - 'message' => $message, |
|
| 1051 | - 'source' => $sniffCode, |
|
| 1052 | - 'listener' => $this->activeListener, |
|
| 1053 | - 'severity' => $severity, |
|
| 1054 | - 'fixable' => $fixable, |
|
| 1055 | - ]; |
|
| 1056 | - |
|
| 1057 | - if (PHP_CODESNIFFER_VERBOSITY > 1 |
|
| 1058 | - && $this->fixer->enabled === true |
|
| 1059 | - && $fixable === true |
|
| 1060 | - ) { |
|
| 1061 | - @ob_end_clean(); |
|
| 1062 | - echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL; |
|
| 1063 | - ob_start(); |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - return true; |
|
| 1067 | - |
|
| 1068 | - }//end addMessage() |
|
| 1069 | - |
|
| 1070 | - |
|
| 1071 | - /** |
|
| 1072 | - * Record a metric about the file being examined. |
|
| 1073 | - * |
|
| 1074 | - * @param int $stackPtr The stack position where the metric was recorded. |
|
| 1075 | - * @param string $metric The name of the metric being recorded. |
|
| 1076 | - * @param string $value The value of the metric being recorded. |
|
| 1077 | - * |
|
| 1078 | - * @return boolean |
|
| 1079 | - */ |
|
| 1080 | - public function recordMetric($stackPtr, $metric, $value) |
|
| 1081 | - { |
|
| 1082 | - if (isset($this->metrics[$metric]) === false) { |
|
| 1083 | - $this->metrics[$metric] = ['values' => [$value => 1]]; |
|
| 1084 | - $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1085 | - } else if (isset($this->metricTokens[$metric][$stackPtr]) === false) { |
|
| 1086 | - $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1087 | - if (isset($this->metrics[$metric]['values'][$value]) === false) { |
|
| 1088 | - $this->metrics[$metric]['values'][$value] = 1; |
|
| 1089 | - } else { |
|
| 1090 | - $this->metrics[$metric]['values'][$value]++; |
|
| 1091 | - } |
|
| 1092 | - } |
|
| 1093 | - |
|
| 1094 | - return true; |
|
| 1095 | - |
|
| 1096 | - }//end recordMetric() |
|
| 1097 | - |
|
| 1098 | - |
|
| 1099 | - /** |
|
| 1100 | - * Returns the number of errors raised. |
|
| 1101 | - * |
|
| 1102 | - * @return int |
|
| 1103 | - */ |
|
| 1104 | - public function getErrorCount() |
|
| 1105 | - { |
|
| 1106 | - return $this->errorCount; |
|
| 1107 | - |
|
| 1108 | - }//end getErrorCount() |
|
| 1109 | - |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * Returns the number of warnings raised. |
|
| 1113 | - * |
|
| 1114 | - * @return int |
|
| 1115 | - */ |
|
| 1116 | - public function getWarningCount() |
|
| 1117 | - { |
|
| 1118 | - return $this->warningCount; |
|
| 1119 | - |
|
| 1120 | - }//end getWarningCount() |
|
| 1121 | - |
|
| 1122 | - |
|
| 1123 | - /** |
|
| 1124 | - * Returns the number of fixable errors/warnings raised. |
|
| 1125 | - * |
|
| 1126 | - * @return int |
|
| 1127 | - */ |
|
| 1128 | - public function getFixableCount() |
|
| 1129 | - { |
|
| 1130 | - return $this->fixableCount; |
|
| 1131 | - |
|
| 1132 | - }//end getFixableCount() |
|
| 1133 | - |
|
| 1134 | - |
|
| 1135 | - /** |
|
| 1136 | - * Returns the number of fixed errors/warnings. |
|
| 1137 | - * |
|
| 1138 | - * @return int |
|
| 1139 | - */ |
|
| 1140 | - public function getFixedCount() |
|
| 1141 | - { |
|
| 1142 | - return $this->fixedCount; |
|
| 1143 | - |
|
| 1144 | - }//end getFixedCount() |
|
| 1145 | - |
|
| 1146 | - |
|
| 1147 | - /** |
|
| 1148 | - * Returns the list of ignored lines. |
|
| 1149 | - * |
|
| 1150 | - * @return array |
|
| 1151 | - */ |
|
| 1152 | - public function getIgnoredLines() |
|
| 1153 | - { |
|
| 1154 | - return $this->tokenizer->ignoredLines; |
|
| 1155 | - |
|
| 1156 | - }//end getIgnoredLines() |
|
| 1157 | - |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * Returns the errors raised from processing this file. |
|
| 1161 | - * |
|
| 1162 | - * @return array |
|
| 1163 | - */ |
|
| 1164 | - public function getErrors() |
|
| 1165 | - { |
|
| 1166 | - return $this->errors; |
|
| 1167 | - |
|
| 1168 | - }//end getErrors() |
|
| 1169 | - |
|
| 1170 | - |
|
| 1171 | - /** |
|
| 1172 | - * Returns the warnings raised from processing this file. |
|
| 1173 | - * |
|
| 1174 | - * @return array |
|
| 1175 | - */ |
|
| 1176 | - public function getWarnings() |
|
| 1177 | - { |
|
| 1178 | - return $this->warnings; |
|
| 1179 | - |
|
| 1180 | - }//end getWarnings() |
|
| 1181 | - |
|
| 1182 | - |
|
| 1183 | - /** |
|
| 1184 | - * Returns the metrics found while processing this file. |
|
| 1185 | - * |
|
| 1186 | - * @return array |
|
| 1187 | - */ |
|
| 1188 | - public function getMetrics() |
|
| 1189 | - { |
|
| 1190 | - return $this->metrics; |
|
| 1191 | - |
|
| 1192 | - }//end getMetrics() |
|
| 1193 | - |
|
| 1194 | - |
|
| 1195 | - /** |
|
| 1196 | - * Returns the absolute filename of this file. |
|
| 1197 | - * |
|
| 1198 | - * @return string |
|
| 1199 | - */ |
|
| 1200 | - public function getFilename() |
|
| 1201 | - { |
|
| 1202 | - return $this->path; |
|
| 1203 | - |
|
| 1204 | - }//end getFilename() |
|
| 1205 | - |
|
| 1206 | - |
|
| 1207 | - /** |
|
| 1208 | - * Returns the declaration names for classes, interfaces, traits, and functions. |
|
| 1209 | - * |
|
| 1210 | - * @param int $stackPtr The position of the declaration token which |
|
| 1211 | - * declared the class, interface, trait, or function. |
|
| 1212 | - * |
|
| 1213 | - * @return string|null The name of the class, interface, trait, or function; |
|
| 1214 | - * or NULL if the function or class is anonymous. |
|
| 1215 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type |
|
| 1216 | - * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
|
| 1217 | - * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
|
| 1218 | - */ |
|
| 1219 | - public function getDeclarationName($stackPtr) |
|
| 1220 | - { |
|
| 1221 | - $tokenCode = $this->tokens[$stackPtr]['code']; |
|
| 1222 | - |
|
| 1223 | - if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { |
|
| 1224 | - return null; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - if ($tokenCode !== T_FUNCTION |
|
| 1228 | - && $tokenCode !== T_CLASS |
|
| 1229 | - && $tokenCode !== T_INTERFACE |
|
| 1230 | - && $tokenCode !== T_TRAIT |
|
| 1231 | - ) { |
|
| 1232 | - throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT'); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - if ($tokenCode === T_FUNCTION |
|
| 1236 | - && strtolower($this->tokens[$stackPtr]['content']) !== 'function' |
|
| 1237 | - ) { |
|
| 1238 | - // This is a function declared without the "function" keyword. |
|
| 1239 | - // So this token is the function name. |
|
| 1240 | - return $this->tokens[$stackPtr]['content']; |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - $content = null; |
|
| 1244 | - for ($i = $stackPtr; $i < $this->numTokens; $i++) { |
|
| 1245 | - if ($this->tokens[$i]['code'] === T_STRING) { |
|
| 1246 | - $content = $this->tokens[$i]['content']; |
|
| 1247 | - break; |
|
| 1248 | - } |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - return $content; |
|
| 1252 | - |
|
| 1253 | - }//end getDeclarationName() |
|
| 1254 | - |
|
| 1255 | - |
|
| 1256 | - /** |
|
| 1257 | - * Returns the method parameters for the specified function token. |
|
| 1258 | - * |
|
| 1259 | - * Each parameter is in the following format: |
|
| 1260 | - * |
|
| 1261 | - * <code> |
|
| 1262 | - * 0 => array( |
|
| 1263 | - * 'name' => '$var', // The variable name. |
|
| 1264 | - * 'token' => integer, // The stack pointer to the variable name. |
|
| 1265 | - * 'content' => string, // The full content of the variable definition. |
|
| 1266 | - * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
|
| 1267 | - * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
|
| 1268 | - * 'type_hint' => string, // The type hint for the variable. |
|
| 1269 | - * 'type_hint_token' => integer, // The stack pointer to the type hint |
|
| 1270 | - * // or false if there is no type hint. |
|
| 1271 | - * 'nullable_type' => boolean, // Is the variable using a nullable type? |
|
| 1272 | - * ) |
|
| 1273 | - * </code> |
|
| 1274 | - * |
|
| 1275 | - * Parameters with default values have an additional array index of |
|
| 1276 | - * 'default' with the value of the default as a string. |
|
| 1277 | - * |
|
| 1278 | - * @param int $stackPtr The position in the stack of the function token |
|
| 1279 | - * to acquire the parameters for. |
|
| 1280 | - * |
|
| 1281 | - * @return array |
|
| 1282 | - * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
|
| 1283 | - * type T_FUNCTION or T_CLOSURE. |
|
| 1284 | - */ |
|
| 1285 | - public function getMethodParameters($stackPtr) |
|
| 1286 | - { |
|
| 1287 | - if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1288 | - && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1289 | - ) { |
|
| 1290 | - throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - $opener = $this->tokens[$stackPtr]['parenthesis_opener']; |
|
| 1294 | - $closer = $this->tokens[$stackPtr]['parenthesis_closer']; |
|
| 1295 | - |
|
| 1296 | - $vars = []; |
|
| 1297 | - $currVar = null; |
|
| 1298 | - $paramStart = ($opener + 1); |
|
| 1299 | - $defaultStart = null; |
|
| 1300 | - $paramCount = 0; |
|
| 1301 | - $passByReference = false; |
|
| 1302 | - $variableLength = false; |
|
| 1303 | - $typeHint = ''; |
|
| 1304 | - $typeHintToken = false; |
|
| 1305 | - $nullableType = false; |
|
| 1306 | - |
|
| 1307 | - for ($i = $paramStart; $i <= $closer; $i++) { |
|
| 1308 | - // Check to see if this token has a parenthesis or bracket opener. If it does |
|
| 1309 | - // it's likely to be an array which might have arguments in it. This |
|
| 1310 | - // could cause problems in our parsing below, so lets just skip to the |
|
| 1311 | - // end of it. |
|
| 1312 | - if (isset($this->tokens[$i]['parenthesis_opener']) === true) { |
|
| 1313 | - // Don't do this if it's the close parenthesis for the method. |
|
| 1314 | - if ($i !== $this->tokens[$i]['parenthesis_closer']) { |
|
| 1315 | - $i = ($this->tokens[$i]['parenthesis_closer'] + 1); |
|
| 1316 | - } |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - if (isset($this->tokens[$i]['bracket_opener']) === true) { |
|
| 1320 | - // Don't do this if it's the close parenthesis for the method. |
|
| 1321 | - if ($i !== $this->tokens[$i]['bracket_closer']) { |
|
| 1322 | - $i = ($this->tokens[$i]['bracket_closer'] + 1); |
|
| 1323 | - } |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - switch ($this->tokens[$i]['code']) { |
|
| 1327 | - case T_BITWISE_AND: |
|
| 1328 | - if ($defaultStart === null) { |
|
| 1329 | - $passByReference = true; |
|
| 1330 | - } |
|
| 1331 | - break; |
|
| 1332 | - case T_VARIABLE: |
|
| 1333 | - $currVar = $i; |
|
| 1334 | - break; |
|
| 1335 | - case T_ELLIPSIS: |
|
| 1336 | - $variableLength = true; |
|
| 1337 | - break; |
|
| 1338 | - case T_CALLABLE: |
|
| 1339 | - if ($typeHintToken === false) { |
|
| 1340 | - $typeHintToken = $i; |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1344 | - break; |
|
| 1345 | - case T_SELF: |
|
| 1346 | - case T_PARENT: |
|
| 1347 | - case T_STATIC: |
|
| 1348 | - // Self and parent are valid, static invalid, but was probably intended as type hint. |
|
| 1349 | - if (isset($defaultStart) === false) { |
|
| 1350 | - if ($typeHintToken === false) { |
|
| 1351 | - $typeHintToken = $i; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1355 | - } |
|
| 1356 | - break; |
|
| 1357 | - case T_STRING: |
|
| 1358 | - // This is a string, so it may be a type hint, but it could |
|
| 1359 | - // also be a constant used as a default value. |
|
| 1360 | - $prevComma = false; |
|
| 1361 | - for ($t = $i; $t >= $opener; $t--) { |
|
| 1362 | - if ($this->tokens[$t]['code'] === T_COMMA) { |
|
| 1363 | - $prevComma = $t; |
|
| 1364 | - break; |
|
| 1365 | - } |
|
| 1366 | - } |
|
| 1367 | - |
|
| 1368 | - if ($prevComma !== false) { |
|
| 1369 | - $nextEquals = false; |
|
| 1370 | - for ($t = $prevComma; $t < $i; $t++) { |
|
| 1371 | - if ($this->tokens[$t]['code'] === T_EQUAL) { |
|
| 1372 | - $nextEquals = $t; |
|
| 1373 | - break; |
|
| 1374 | - } |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - if ($nextEquals !== false) { |
|
| 1378 | - break; |
|
| 1379 | - } |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - if ($defaultStart === null) { |
|
| 1383 | - if ($typeHintToken === false) { |
|
| 1384 | - $typeHintToken = $i; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1388 | - } |
|
| 1389 | - break; |
|
| 1390 | - case T_NS_SEPARATOR: |
|
| 1391 | - // Part of a type hint or default value. |
|
| 1392 | - if ($defaultStart === null) { |
|
| 1393 | - if ($typeHintToken === false) { |
|
| 1394 | - $typeHintToken = $i; |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1398 | - } |
|
| 1399 | - break; |
|
| 1400 | - case T_NULLABLE: |
|
| 1401 | - if ($defaultStart === null) { |
|
| 1402 | - $nullableType = true; |
|
| 1403 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1404 | - } |
|
| 1405 | - break; |
|
| 1406 | - case T_CLOSE_PARENTHESIS: |
|
| 1407 | - case T_COMMA: |
|
| 1408 | - // If it's null, then there must be no parameters for this |
|
| 1409 | - // method. |
|
| 1410 | - if ($currVar === null) { |
|
| 1411 | - continue 2; |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - $vars[$paramCount] = []; |
|
| 1415 | - $vars[$paramCount]['token'] = $currVar; |
|
| 1416 | - $vars[$paramCount]['name'] = $this->tokens[$currVar]['content']; |
|
| 1417 | - $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart))); |
|
| 1418 | - |
|
| 1419 | - if ($defaultStart !== null) { |
|
| 1420 | - $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart))); |
|
| 1421 | - } |
|
| 1422 | - |
|
| 1423 | - $vars[$paramCount]['pass_by_reference'] = $passByReference; |
|
| 1424 | - $vars[$paramCount]['variable_length'] = $variableLength; |
|
| 1425 | - $vars[$paramCount]['type_hint'] = $typeHint; |
|
| 1426 | - $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
|
| 1427 | - $vars[$paramCount]['nullable_type'] = $nullableType; |
|
| 1428 | - |
|
| 1429 | - // Reset the vars, as we are about to process the next parameter. |
|
| 1430 | - $defaultStart = null; |
|
| 1431 | - $paramStart = ($i + 1); |
|
| 1432 | - $passByReference = false; |
|
| 1433 | - $variableLength = false; |
|
| 1434 | - $typeHint = ''; |
|
| 1435 | - $typeHintToken = false; |
|
| 1436 | - $nullableType = false; |
|
| 1437 | - |
|
| 1438 | - $paramCount++; |
|
| 1439 | - break; |
|
| 1440 | - case T_EQUAL: |
|
| 1441 | - $defaultStart = ($i + 1); |
|
| 1442 | - break; |
|
| 1443 | - }//end switch |
|
| 1444 | - }//end for |
|
| 1445 | - |
|
| 1446 | - return $vars; |
|
| 1447 | - |
|
| 1448 | - }//end getMethodParameters() |
|
| 1449 | - |
|
| 1450 | - |
|
| 1451 | - /** |
|
| 1452 | - * Returns the visibility and implementation properties of a method. |
|
| 1453 | - * |
|
| 1454 | - * The format of the array is: |
|
| 1455 | - * <code> |
|
| 1456 | - * array( |
|
| 1457 | - * 'scope' => 'public', // public protected or protected |
|
| 1458 | - * 'scope_specified' => true, // true is scope keyword was found. |
|
| 1459 | - * 'return_type' => '', // the return type of the method. |
|
| 1460 | - * 'return_type_token' => integer, // The stack pointer to the start of the return type |
|
| 1461 | - * // or false if there is no return type. |
|
| 1462 | - * 'nullable_return_type' => false, // true if the return type is nullable. |
|
| 1463 | - * 'is_abstract' => false, // true if the abstract keyword was found. |
|
| 1464 | - * 'is_final' => false, // true if the final keyword was found. |
|
| 1465 | - * 'is_static' => false, // true if the static keyword was found. |
|
| 1466 | - * 'has_body' => false, // true if the method has a body |
|
| 1467 | - * ); |
|
| 1468 | - * </code> |
|
| 1469 | - * |
|
| 1470 | - * @param int $stackPtr The position in the stack of the function token to |
|
| 1471 | - * acquire the properties for. |
|
| 1472 | - * |
|
| 1473 | - * @return array |
|
| 1474 | - * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1475 | - * T_FUNCTION token. |
|
| 1476 | - */ |
|
| 1477 | - public function getMethodProperties($stackPtr) |
|
| 1478 | - { |
|
| 1479 | - if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1480 | - && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1481 | - ) { |
|
| 1482 | - throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1483 | - } |
|
| 1484 | - |
|
| 1485 | - if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 1486 | - $valid = [ |
|
| 1487 | - T_PUBLIC => T_PUBLIC, |
|
| 1488 | - T_PRIVATE => T_PRIVATE, |
|
| 1489 | - T_PROTECTED => T_PROTECTED, |
|
| 1490 | - T_STATIC => T_STATIC, |
|
| 1491 | - T_FINAL => T_FINAL, |
|
| 1492 | - T_ABSTRACT => T_ABSTRACT, |
|
| 1493 | - T_WHITESPACE => T_WHITESPACE, |
|
| 1494 | - T_COMMENT => T_COMMENT, |
|
| 1495 | - T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1496 | - ]; |
|
| 1497 | - } else { |
|
| 1498 | - $valid = [ |
|
| 1499 | - T_STATIC => T_STATIC, |
|
| 1500 | - T_WHITESPACE => T_WHITESPACE, |
|
| 1501 | - T_COMMENT => T_COMMENT, |
|
| 1502 | - T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1503 | - ]; |
|
| 1504 | - } |
|
| 1505 | - |
|
| 1506 | - $scope = 'public'; |
|
| 1507 | - $scopeSpecified = false; |
|
| 1508 | - $isAbstract = false; |
|
| 1509 | - $isFinal = false; |
|
| 1510 | - $isStatic = false; |
|
| 1511 | - |
|
| 1512 | - for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1513 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1514 | - break; |
|
| 1515 | - } |
|
| 1516 | - |
|
| 1517 | - switch ($this->tokens[$i]['code']) { |
|
| 1518 | - case T_PUBLIC: |
|
| 1519 | - $scope = 'public'; |
|
| 1520 | - $scopeSpecified = true; |
|
| 1521 | - break; |
|
| 1522 | - case T_PRIVATE: |
|
| 1523 | - $scope = 'private'; |
|
| 1524 | - $scopeSpecified = true; |
|
| 1525 | - break; |
|
| 1526 | - case T_PROTECTED: |
|
| 1527 | - $scope = 'protected'; |
|
| 1528 | - $scopeSpecified = true; |
|
| 1529 | - break; |
|
| 1530 | - case T_ABSTRACT: |
|
| 1531 | - $isAbstract = true; |
|
| 1532 | - break; |
|
| 1533 | - case T_FINAL: |
|
| 1534 | - $isFinal = true; |
|
| 1535 | - break; |
|
| 1536 | - case T_STATIC: |
|
| 1537 | - $isStatic = true; |
|
| 1538 | - break; |
|
| 1539 | - }//end switch |
|
| 1540 | - }//end for |
|
| 1541 | - |
|
| 1542 | - $returnType = ''; |
|
| 1543 | - $returnTypeToken = false; |
|
| 1544 | - $nullableReturnType = false; |
|
| 1545 | - $hasBody = true; |
|
| 1546 | - |
|
| 1547 | - if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 1548 | - $scopeOpener = null; |
|
| 1549 | - if (isset($this->tokens[$stackPtr]['scope_opener']) === true) { |
|
| 1550 | - $scopeOpener = $this->tokens[$stackPtr]['scope_opener']; |
|
| 1551 | - } |
|
| 1552 | - |
|
| 1553 | - $valid = [ |
|
| 1554 | - T_STRING => T_STRING, |
|
| 1555 | - T_CALLABLE => T_CALLABLE, |
|
| 1556 | - T_SELF => T_SELF, |
|
| 1557 | - T_PARENT => T_PARENT, |
|
| 1558 | - T_NS_SEPARATOR => T_NS_SEPARATOR, |
|
| 1559 | - ]; |
|
| 1560 | - |
|
| 1561 | - for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) { |
|
| 1562 | - if (($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON) |
|
| 1563 | - || ($scopeOpener !== null && $i === $scopeOpener) |
|
| 1564 | - ) { |
|
| 1565 | - // End of function definition. |
|
| 1566 | - break; |
|
| 1567 | - } |
|
| 1568 | - |
|
| 1569 | - if ($this->tokens[$i]['code'] === T_NULLABLE) { |
|
| 1570 | - $nullableReturnType = true; |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - if (isset($valid[$this->tokens[$i]['code']]) === true) { |
|
| 1574 | - if ($returnTypeToken === false) { |
|
| 1575 | - $returnTypeToken = $i; |
|
| 1576 | - } |
|
| 1577 | - |
|
| 1578 | - $returnType .= $this->tokens[$i]['content']; |
|
| 1579 | - } |
|
| 1580 | - } |
|
| 1581 | - |
|
| 1582 | - $end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']); |
|
| 1583 | - $hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET; |
|
| 1584 | - }//end if |
|
| 1585 | - |
|
| 1586 | - if ($returnType !== '' && $nullableReturnType === true) { |
|
| 1587 | - $returnType = '?'.$returnType; |
|
| 1588 | - } |
|
| 1589 | - |
|
| 1590 | - return [ |
|
| 1591 | - 'scope' => $scope, |
|
| 1592 | - 'scope_specified' => $scopeSpecified, |
|
| 1593 | - 'return_type' => $returnType, |
|
| 1594 | - 'return_type_token' => $returnTypeToken, |
|
| 1595 | - 'nullable_return_type' => $nullableReturnType, |
|
| 1596 | - 'is_abstract' => $isAbstract, |
|
| 1597 | - 'is_final' => $isFinal, |
|
| 1598 | - 'is_static' => $isStatic, |
|
| 1599 | - 'has_body' => $hasBody, |
|
| 1600 | - ]; |
|
| 1601 | - |
|
| 1602 | - }//end getMethodProperties() |
|
| 1603 | - |
|
| 1604 | - |
|
| 1605 | - /** |
|
| 1606 | - * Returns the visibility and implementation properties of the class member |
|
| 1607 | - * variable found at the specified position in the stack. |
|
| 1608 | - * |
|
| 1609 | - * The format of the array is: |
|
| 1610 | - * |
|
| 1611 | - * <code> |
|
| 1612 | - * array( |
|
| 1613 | - * 'scope' => 'public', // public protected or protected. |
|
| 1614 | - * 'scope_specified' => false, // true if the scope was explicitly specified. |
|
| 1615 | - * 'is_static' => false, // true if the static keyword was found. |
|
| 1616 | - * ); |
|
| 1617 | - * </code> |
|
| 1618 | - * |
|
| 1619 | - * @param int $stackPtr The position in the stack of the T_VARIABLE token to |
|
| 1620 | - * acquire the properties for. |
|
| 1621 | - * |
|
| 1622 | - * @return array |
|
| 1623 | - * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1624 | - * T_VARIABLE token, or if the position is not |
|
| 1625 | - * a class member variable. |
|
| 1626 | - */ |
|
| 1627 | - public function getMemberProperties($stackPtr) |
|
| 1628 | - { |
|
| 1629 | - if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) { |
|
| 1630 | - throw new TokenizerException('$stackPtr must be of type T_VARIABLE'); |
|
| 1631 | - } |
|
| 1632 | - |
|
| 1633 | - $conditions = array_keys($this->tokens[$stackPtr]['conditions']); |
|
| 1634 | - $ptr = array_pop($conditions); |
|
| 1635 | - if (isset($this->tokens[$ptr]) === false |
|
| 1636 | - || ($this->tokens[$ptr]['code'] !== T_CLASS |
|
| 1637 | - && $this->tokens[$ptr]['code'] !== T_ANON_CLASS |
|
| 1638 | - && $this->tokens[$ptr]['code'] !== T_TRAIT) |
|
| 1639 | - ) { |
|
| 1640 | - if (isset($this->tokens[$ptr]) === true |
|
| 1641 | - && $this->tokens[$ptr]['code'] === T_INTERFACE |
|
| 1642 | - ) { |
|
| 1643 | - // T_VARIABLEs in interfaces can actually be method arguments |
|
| 1644 | - // but they wont be seen as being inside the method because there |
|
| 1645 | - // are no scope openers and closers for abstract methods. If it is in |
|
| 1646 | - // parentheses, we can be pretty sure it is a method argument. |
|
| 1647 | - if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false |
|
| 1648 | - || empty($this->tokens[$stackPtr]['nested_parenthesis']) === true |
|
| 1649 | - ) { |
|
| 1650 | - $error = 'Possible parse error: interfaces may not include member vars'; |
|
| 1651 | - $this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar'); |
|
| 1652 | - return []; |
|
| 1653 | - } |
|
| 1654 | - } else { |
|
| 1655 | - throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1656 | - } |
|
| 1657 | - } |
|
| 1658 | - |
|
| 1659 | - // Make sure it's not a method parameter. |
|
| 1660 | - if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) { |
|
| 1661 | - $parenthesis = array_keys($this->tokens[$stackPtr]['nested_parenthesis']); |
|
| 1662 | - $deepestOpen = array_pop($parenthesis); |
|
| 1663 | - if ($deepestOpen > $ptr |
|
| 1664 | - && isset($this->tokens[$deepestOpen]['parenthesis_owner']) === true |
|
| 1665 | - && $this->tokens[$this->tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION |
|
| 1666 | - ) { |
|
| 1667 | - throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1668 | - } |
|
| 1669 | - } |
|
| 1670 | - |
|
| 1671 | - $valid = [ |
|
| 1672 | - T_PUBLIC => T_PUBLIC, |
|
| 1673 | - T_PRIVATE => T_PRIVATE, |
|
| 1674 | - T_PROTECTED => T_PROTECTED, |
|
| 1675 | - T_STATIC => T_STATIC, |
|
| 1676 | - T_VAR => T_VAR, |
|
| 1677 | - ]; |
|
| 1678 | - |
|
| 1679 | - $valid += Util\Tokens::$emptyTokens; |
|
| 1680 | - |
|
| 1681 | - $scope = 'public'; |
|
| 1682 | - $scopeSpecified = false; |
|
| 1683 | - $isStatic = false; |
|
| 1684 | - |
|
| 1685 | - $startOfStatement = $this->findPrevious( |
|
| 1686 | - [ |
|
| 1687 | - T_SEMICOLON, |
|
| 1688 | - T_OPEN_CURLY_BRACKET, |
|
| 1689 | - T_CLOSE_CURLY_BRACKET, |
|
| 1690 | - ], |
|
| 1691 | - ($stackPtr - 1) |
|
| 1692 | - ); |
|
| 1693 | - |
|
| 1694 | - for ($i = ($startOfStatement + 1); $i < $stackPtr; $i++) { |
|
| 1695 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1696 | - break; |
|
| 1697 | - } |
|
| 1698 | - |
|
| 1699 | - switch ($this->tokens[$i]['code']) { |
|
| 1700 | - case T_PUBLIC: |
|
| 1701 | - $scope = 'public'; |
|
| 1702 | - $scopeSpecified = true; |
|
| 1703 | - break; |
|
| 1704 | - case T_PRIVATE: |
|
| 1705 | - $scope = 'private'; |
|
| 1706 | - $scopeSpecified = true; |
|
| 1707 | - break; |
|
| 1708 | - case T_PROTECTED: |
|
| 1709 | - $scope = 'protected'; |
|
| 1710 | - $scopeSpecified = true; |
|
| 1711 | - break; |
|
| 1712 | - case T_STATIC: |
|
| 1713 | - $isStatic = true; |
|
| 1714 | - break; |
|
| 1715 | - } |
|
| 1716 | - }//end for |
|
| 1717 | - |
|
| 1718 | - return [ |
|
| 1719 | - 'scope' => $scope, |
|
| 1720 | - 'scope_specified' => $scopeSpecified, |
|
| 1721 | - 'is_static' => $isStatic, |
|
| 1722 | - ]; |
|
| 1723 | - |
|
| 1724 | - }//end getMemberProperties() |
|
| 1725 | - |
|
| 1726 | - |
|
| 1727 | - /** |
|
| 1728 | - * Returns the visibility and implementation properties of a class. |
|
| 1729 | - * |
|
| 1730 | - * The format of the array is: |
|
| 1731 | - * <code> |
|
| 1732 | - * array( |
|
| 1733 | - * 'is_abstract' => false, // true if the abstract keyword was found. |
|
| 1734 | - * 'is_final' => false, // true if the final keyword was found. |
|
| 1735 | - * ); |
|
| 1736 | - * </code> |
|
| 1737 | - * |
|
| 1738 | - * @param int $stackPtr The position in the stack of the T_CLASS token to |
|
| 1739 | - * acquire the properties for. |
|
| 1740 | - * |
|
| 1741 | - * @return array |
|
| 1742 | - * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1743 | - * T_CLASS token. |
|
| 1744 | - */ |
|
| 1745 | - public function getClassProperties($stackPtr) |
|
| 1746 | - { |
|
| 1747 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS) { |
|
| 1748 | - throw new TokenizerException('$stackPtr must be of type T_CLASS'); |
|
| 1749 | - } |
|
| 1750 | - |
|
| 1751 | - $valid = [ |
|
| 1752 | - T_FINAL => T_FINAL, |
|
| 1753 | - T_ABSTRACT => T_ABSTRACT, |
|
| 1754 | - T_WHITESPACE => T_WHITESPACE, |
|
| 1755 | - T_COMMENT => T_COMMENT, |
|
| 1756 | - T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1757 | - ]; |
|
| 1758 | - |
|
| 1759 | - $isAbstract = false; |
|
| 1760 | - $isFinal = false; |
|
| 1761 | - |
|
| 1762 | - for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1763 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1764 | - break; |
|
| 1765 | - } |
|
| 1766 | - |
|
| 1767 | - switch ($this->tokens[$i]['code']) { |
|
| 1768 | - case T_ABSTRACT: |
|
| 1769 | - $isAbstract = true; |
|
| 1770 | - break; |
|
| 1771 | - |
|
| 1772 | - case T_FINAL: |
|
| 1773 | - $isFinal = true; |
|
| 1774 | - break; |
|
| 1775 | - } |
|
| 1776 | - }//end for |
|
| 1777 | - |
|
| 1778 | - return [ |
|
| 1779 | - 'is_abstract' => $isAbstract, |
|
| 1780 | - 'is_final' => $isFinal, |
|
| 1781 | - ]; |
|
| 1782 | - |
|
| 1783 | - }//end getClassProperties() |
|
| 1784 | - |
|
| 1785 | - |
|
| 1786 | - /** |
|
| 1787 | - * Determine if the passed token is a reference operator. |
|
| 1788 | - * |
|
| 1789 | - * Returns true if the specified token position represents a reference. |
|
| 1790 | - * Returns false if the token represents a bitwise operator. |
|
| 1791 | - * |
|
| 1792 | - * @param int $stackPtr The position of the T_BITWISE_AND token. |
|
| 1793 | - * |
|
| 1794 | - * @return boolean |
|
| 1795 | - */ |
|
| 1796 | - public function isReference($stackPtr) |
|
| 1797 | - { |
|
| 1798 | - if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) { |
|
| 1799 | - return false; |
|
| 1800 | - } |
|
| 1801 | - |
|
| 1802 | - $tokenBefore = $this->findPrevious( |
|
| 1803 | - Util\Tokens::$emptyTokens, |
|
| 1804 | - ($stackPtr - 1), |
|
| 1805 | - null, |
|
| 1806 | - true |
|
| 1807 | - ); |
|
| 1808 | - |
|
| 1809 | - if ($this->tokens[$tokenBefore]['code'] === T_FUNCTION) { |
|
| 1810 | - // Function returns a reference. |
|
| 1811 | - return true; |
|
| 1812 | - } |
|
| 1813 | - |
|
| 1814 | - if ($this->tokens[$tokenBefore]['code'] === T_DOUBLE_ARROW) { |
|
| 1815 | - // Inside a foreach loop or array assignment, this is a reference. |
|
| 1816 | - return true; |
|
| 1817 | - } |
|
| 1818 | - |
|
| 1819 | - if ($this->tokens[$tokenBefore]['code'] === T_AS) { |
|
| 1820 | - // Inside a foreach loop, this is a reference. |
|
| 1821 | - return true; |
|
| 1822 | - } |
|
| 1823 | - |
|
| 1824 | - if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) { |
|
| 1825 | - // This is directly after an assignment. It's a reference. Even if |
|
| 1826 | - // it is part of an operation, the other tests will handle it. |
|
| 1827 | - return true; |
|
| 1828 | - } |
|
| 1829 | - |
|
| 1830 | - $tokenAfter = $this->findNext( |
|
| 1831 | - Util\Tokens::$emptyTokens, |
|
| 1832 | - ($stackPtr + 1), |
|
| 1833 | - null, |
|
| 1834 | - true |
|
| 1835 | - ); |
|
| 1836 | - |
|
| 1837 | - if ($this->tokens[$tokenAfter]['code'] === T_NEW) { |
|
| 1838 | - return true; |
|
| 1839 | - } |
|
| 1840 | - |
|
| 1841 | - if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1842 | - $brackets = $this->tokens[$stackPtr]['nested_parenthesis']; |
|
| 1843 | - $lastBracket = array_pop($brackets); |
|
| 1844 | - if (isset($this->tokens[$lastBracket]['parenthesis_owner']) === true) { |
|
| 1845 | - $owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']]; |
|
| 1846 | - if ($owner['code'] === T_FUNCTION |
|
| 1847 | - || $owner['code'] === T_CLOSURE |
|
| 1848 | - ) { |
|
| 1849 | - $params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']); |
|
| 1850 | - foreach ($params as $param) { |
|
| 1851 | - $varToken = $tokenAfter; |
|
| 1852 | - if ($param['variable_length'] === true) { |
|
| 1853 | - $varToken = $this->findNext( |
|
| 1854 | - (Util\Tokens::$emptyTokens + [T_ELLIPSIS]), |
|
| 1855 | - ($stackPtr + 1), |
|
| 1856 | - null, |
|
| 1857 | - true |
|
| 1858 | - ); |
|
| 1859 | - } |
|
| 1860 | - |
|
| 1861 | - if ($param['token'] === $varToken |
|
| 1862 | - && $param['pass_by_reference'] === true |
|
| 1863 | - ) { |
|
| 1864 | - // Function parameter declared to be passed by reference. |
|
| 1865 | - return true; |
|
| 1866 | - } |
|
| 1867 | - } |
|
| 1868 | - }//end if |
|
| 1869 | - } else { |
|
| 1870 | - $prev = false; |
|
| 1871 | - for ($t = ($this->tokens[$lastBracket]['parenthesis_opener'] - 1); $t >= 0; $t--) { |
|
| 1872 | - if ($this->tokens[$t]['code'] !== T_WHITESPACE) { |
|
| 1873 | - $prev = $t; |
|
| 1874 | - break; |
|
| 1875 | - } |
|
| 1876 | - } |
|
| 1877 | - |
|
| 1878 | - if ($prev !== false && $this->tokens[$prev]['code'] === T_USE) { |
|
| 1879 | - // Closure use by reference. |
|
| 1880 | - return true; |
|
| 1881 | - } |
|
| 1882 | - }//end if |
|
| 1883 | - }//end if |
|
| 1884 | - |
|
| 1885 | - // Pass by reference in function calls and assign by reference in arrays. |
|
| 1886 | - if ($this->tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS |
|
| 1887 | - || $this->tokens[$tokenBefore]['code'] === T_COMMA |
|
| 1888 | - || $this->tokens[$tokenBefore]['code'] === T_OPEN_SHORT_ARRAY |
|
| 1889 | - ) { |
|
| 1890 | - if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) { |
|
| 1891 | - return true; |
|
| 1892 | - } else { |
|
| 1893 | - $skip = Util\Tokens::$emptyTokens; |
|
| 1894 | - $skip[] = T_NS_SEPARATOR; |
|
| 1895 | - $skip[] = T_SELF; |
|
| 1896 | - $skip[] = T_PARENT; |
|
| 1897 | - $skip[] = T_STATIC; |
|
| 1898 | - $skip[] = T_STRING; |
|
| 1899 | - $skip[] = T_NAMESPACE; |
|
| 1900 | - $skip[] = T_DOUBLE_COLON; |
|
| 1901 | - |
|
| 1902 | - $nextSignificantAfter = $this->findNext( |
|
| 1903 | - $skip, |
|
| 1904 | - ($stackPtr + 1), |
|
| 1905 | - null, |
|
| 1906 | - true |
|
| 1907 | - ); |
|
| 1908 | - if ($this->tokens[$nextSignificantAfter]['code'] === T_VARIABLE) { |
|
| 1909 | - return true; |
|
| 1910 | - } |
|
| 1911 | - }//end if |
|
| 1912 | - }//end if |
|
| 1913 | - |
|
| 1914 | - return false; |
|
| 1915 | - |
|
| 1916 | - }//end isReference() |
|
| 1917 | - |
|
| 1918 | - |
|
| 1919 | - /** |
|
| 1920 | - * Returns the content of the tokens from the specified start position in |
|
| 1921 | - * the token stack for the specified length. |
|
| 1922 | - * |
|
| 1923 | - * @param int $start The position to start from in the token stack. |
|
| 1924 | - * @param int $length The length of tokens to traverse from the start pos. |
|
| 1925 | - * @param bool $origContent Whether the original content or the tab replaced |
|
| 1926 | - * content should be used. |
|
| 1927 | - * |
|
| 1928 | - * @return string The token contents. |
|
| 1929 | - */ |
|
| 1930 | - public function getTokensAsString($start, $length, $origContent=false) |
|
| 1931 | - { |
|
| 1932 | - if (is_int($start) === false || isset($this->tokens[$start]) === false) { |
|
| 1933 | - throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack'); |
|
| 1934 | - } |
|
| 1935 | - |
|
| 1936 | - if (is_int($length) === false || $length <= 0) { |
|
| 1937 | - return ''; |
|
| 1938 | - } |
|
| 1939 | - |
|
| 1940 | - $str = ''; |
|
| 1941 | - $end = ($start + $length); |
|
| 1942 | - if ($end > $this->numTokens) { |
|
| 1943 | - $end = $this->numTokens; |
|
| 1944 | - } |
|
| 1945 | - |
|
| 1946 | - for ($i = $start; $i < $end; $i++) { |
|
| 1947 | - // If tabs are being converted to spaces by the tokeniser, the |
|
| 1948 | - // original content should be used instead of the converted content. |
|
| 1949 | - if ($origContent === true && isset($this->tokens[$i]['orig_content']) === true) { |
|
| 1950 | - $str .= $this->tokens[$i]['orig_content']; |
|
| 1951 | - } else { |
|
| 1952 | - $str .= $this->tokens[$i]['content']; |
|
| 1953 | - } |
|
| 1954 | - } |
|
| 1955 | - |
|
| 1956 | - return $str; |
|
| 1957 | - |
|
| 1958 | - }//end getTokensAsString() |
|
| 1959 | - |
|
| 1960 | - |
|
| 1961 | - /** |
|
| 1962 | - * Returns the position of the previous specified token(s). |
|
| 1963 | - * |
|
| 1964 | - * If a value is specified, the previous token of the specified type(s) |
|
| 1965 | - * containing the specified value will be returned. |
|
| 1966 | - * |
|
| 1967 | - * Returns false if no token can be found. |
|
| 1968 | - * |
|
| 1969 | - * @param int|string|array $types The type(s) of tokens to search for. |
|
| 1970 | - * @param int $start The position to start searching from in the |
|
| 1971 | - * token stack. |
|
| 1972 | - * @param int $end The end position to fail if no token is found. |
|
| 1973 | - * if not specified or null, end will default to |
|
| 1974 | - * the start of the token stack. |
|
| 1975 | - * @param bool $exclude If true, find the previous token that is NOT of |
|
| 1976 | - * the types specified in $types. |
|
| 1977 | - * @param string $value The value that the token(s) must be equal to. |
|
| 1978 | - * If value is omitted, tokens with any value will |
|
| 1979 | - * be returned. |
|
| 1980 | - * @param bool $local If true, tokens outside the current statement |
|
| 1981 | - * will not be checked. IE. checking will stop |
|
| 1982 | - * at the previous semi-colon found. |
|
| 1983 | - * |
|
| 1984 | - * @return int|bool |
|
| 1985 | - * @see findNext() |
|
| 1986 | - */ |
|
| 1987 | - public function findPrevious( |
|
| 1988 | - $types, |
|
| 1989 | - $start, |
|
| 1990 | - $end=null, |
|
| 1991 | - $exclude=false, |
|
| 1992 | - $value=null, |
|
| 1993 | - $local=false |
|
| 1994 | - ) { |
|
| 1995 | - $types = (array) $types; |
|
| 1996 | - |
|
| 1997 | - if ($end === null) { |
|
| 1998 | - $end = 0; |
|
| 1999 | - } |
|
| 2000 | - |
|
| 2001 | - for ($i = $start; $i >= $end; $i--) { |
|
| 2002 | - $found = (bool) $exclude; |
|
| 2003 | - foreach ($types as $type) { |
|
| 2004 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2005 | - $found = !$exclude; |
|
| 2006 | - break; |
|
| 2007 | - } |
|
| 2008 | - } |
|
| 2009 | - |
|
| 2010 | - if ($found === true) { |
|
| 2011 | - if ($value === null) { |
|
| 2012 | - return $i; |
|
| 2013 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2014 | - return $i; |
|
| 2015 | - } |
|
| 2016 | - } |
|
| 2017 | - |
|
| 2018 | - if ($local === true) { |
|
| 2019 | - if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2020 | - && $i === $this->tokens[$i]['scope_closer'] |
|
| 2021 | - ) { |
|
| 2022 | - $i = $this->tokens[$i]['scope_opener']; |
|
| 2023 | - } else if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2024 | - && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2025 | - ) { |
|
| 2026 | - $i = $this->tokens[$i]['bracket_opener']; |
|
| 2027 | - } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2028 | - && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2029 | - ) { |
|
| 2030 | - $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2031 | - } else if ($this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2032 | - break; |
|
| 2033 | - } |
|
| 2034 | - } |
|
| 2035 | - }//end for |
|
| 2036 | - |
|
| 2037 | - return false; |
|
| 2038 | - |
|
| 2039 | - }//end findPrevious() |
|
| 2040 | - |
|
| 2041 | - |
|
| 2042 | - /** |
|
| 2043 | - * Returns the position of the next specified token(s). |
|
| 2044 | - * |
|
| 2045 | - * If a value is specified, the next token of the specified type(s) |
|
| 2046 | - * containing the specified value will be returned. |
|
| 2047 | - * |
|
| 2048 | - * Returns false if no token can be found. |
|
| 2049 | - * |
|
| 2050 | - * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2051 | - * @param int $start The position to start searching from in the |
|
| 2052 | - * token stack. |
|
| 2053 | - * @param int $end The end position to fail if no token is found. |
|
| 2054 | - * if not specified or null, end will default to |
|
| 2055 | - * the end of the token stack. |
|
| 2056 | - * @param bool $exclude If true, find the next token that is NOT of |
|
| 2057 | - * a type specified in $types. |
|
| 2058 | - * @param string $value The value that the token(s) must be equal to. |
|
| 2059 | - * If value is omitted, tokens with any value will |
|
| 2060 | - * be returned. |
|
| 2061 | - * @param bool $local If true, tokens outside the current statement |
|
| 2062 | - * will not be checked. i.e., checking will stop |
|
| 2063 | - * at the next semi-colon found. |
|
| 2064 | - * |
|
| 2065 | - * @return int|bool |
|
| 2066 | - * @see findPrevious() |
|
| 2067 | - */ |
|
| 2068 | - public function findNext( |
|
| 2069 | - $types, |
|
| 2070 | - $start, |
|
| 2071 | - $end=null, |
|
| 2072 | - $exclude=false, |
|
| 2073 | - $value=null, |
|
| 2074 | - $local=false |
|
| 2075 | - ) { |
|
| 2076 | - $types = (array) $types; |
|
| 2077 | - |
|
| 2078 | - if ($end === null || $end > $this->numTokens) { |
|
| 2079 | - $end = $this->numTokens; |
|
| 2080 | - } |
|
| 2081 | - |
|
| 2082 | - for ($i = $start; $i < $end; $i++) { |
|
| 2083 | - $found = (bool) $exclude; |
|
| 2084 | - foreach ($types as $type) { |
|
| 2085 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2086 | - $found = !$exclude; |
|
| 2087 | - break; |
|
| 2088 | - } |
|
| 2089 | - } |
|
| 2090 | - |
|
| 2091 | - if ($found === true) { |
|
| 2092 | - if ($value === null) { |
|
| 2093 | - return $i; |
|
| 2094 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2095 | - return $i; |
|
| 2096 | - } |
|
| 2097 | - } |
|
| 2098 | - |
|
| 2099 | - if ($local === true && $this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2100 | - break; |
|
| 2101 | - } |
|
| 2102 | - }//end for |
|
| 2103 | - |
|
| 2104 | - return false; |
|
| 2105 | - |
|
| 2106 | - }//end findNext() |
|
| 2107 | - |
|
| 2108 | - |
|
| 2109 | - /** |
|
| 2110 | - * Returns the position of the first non-whitespace token in a statement. |
|
| 2111 | - * |
|
| 2112 | - * @param int $start The position to start searching from in the token stack. |
|
| 2113 | - * @param int|array $ignore Token types that should not be considered stop points. |
|
| 2114 | - * |
|
| 2115 | - * @return int |
|
| 2116 | - */ |
|
| 2117 | - public function findStartOfStatement($start, $ignore=null) |
|
| 2118 | - { |
|
| 2119 | - $endTokens = Util\Tokens::$blockOpeners; |
|
| 2120 | - |
|
| 2121 | - $endTokens[T_COLON] = true; |
|
| 2122 | - $endTokens[T_COMMA] = true; |
|
| 2123 | - $endTokens[T_DOUBLE_ARROW] = true; |
|
| 2124 | - $endTokens[T_SEMICOLON] = true; |
|
| 2125 | - $endTokens[T_OPEN_TAG] = true; |
|
| 2126 | - $endTokens[T_CLOSE_TAG] = true; |
|
| 2127 | - $endTokens[T_OPEN_SHORT_ARRAY] = true; |
|
| 2128 | - |
|
| 2129 | - if ($ignore !== null) { |
|
| 2130 | - $ignore = (array) $ignore; |
|
| 2131 | - foreach ($ignore as $code) { |
|
| 2132 | - unset($endTokens[$code]); |
|
| 2133 | - } |
|
| 2134 | - } |
|
| 2135 | - |
|
| 2136 | - $lastNotEmpty = $start; |
|
| 2137 | - |
|
| 2138 | - for ($i = $start; $i >= 0; $i--) { |
|
| 2139 | - if (isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2140 | - // Found the end of the previous statement. |
|
| 2141 | - return $lastNotEmpty; |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2145 | - && $i === $this->tokens[$i]['scope_closer'] |
|
| 2146 | - ) { |
|
| 2147 | - // Found the end of the previous scope block. |
|
| 2148 | - return $lastNotEmpty; |
|
| 2149 | - } |
|
| 2150 | - |
|
| 2151 | - // Skip nested statements. |
|
| 2152 | - if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2153 | - && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2154 | - ) { |
|
| 2155 | - $i = $this->tokens[$i]['bracket_opener']; |
|
| 2156 | - } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2157 | - && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2158 | - ) { |
|
| 2159 | - $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2160 | - } |
|
| 2161 | - |
|
| 2162 | - if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2163 | - $lastNotEmpty = $i; |
|
| 2164 | - } |
|
| 2165 | - }//end for |
|
| 2166 | - |
|
| 2167 | - return 0; |
|
| 2168 | - |
|
| 2169 | - }//end findStartOfStatement() |
|
| 2170 | - |
|
| 2171 | - |
|
| 2172 | - /** |
|
| 2173 | - * Returns the position of the last non-whitespace token in a statement. |
|
| 2174 | - * |
|
| 2175 | - * @param int $start The position to start searching from in the token stack. |
|
| 2176 | - * @param int|array $ignore Token types that should not be considered stop points. |
|
| 2177 | - * |
|
| 2178 | - * @return int |
|
| 2179 | - */ |
|
| 2180 | - public function findEndOfStatement($start, $ignore=null) |
|
| 2181 | - { |
|
| 2182 | - $endTokens = [ |
|
| 2183 | - T_COLON => true, |
|
| 2184 | - T_COMMA => true, |
|
| 2185 | - T_DOUBLE_ARROW => true, |
|
| 2186 | - T_SEMICOLON => true, |
|
| 2187 | - T_CLOSE_PARENTHESIS => true, |
|
| 2188 | - T_CLOSE_SQUARE_BRACKET => true, |
|
| 2189 | - T_CLOSE_CURLY_BRACKET => true, |
|
| 2190 | - T_CLOSE_SHORT_ARRAY => true, |
|
| 2191 | - T_OPEN_TAG => true, |
|
| 2192 | - T_CLOSE_TAG => true, |
|
| 2193 | - ]; |
|
| 2194 | - |
|
| 2195 | - if ($ignore !== null) { |
|
| 2196 | - $ignore = (array) $ignore; |
|
| 2197 | - foreach ($ignore as $code) { |
|
| 2198 | - unset($endTokens[$code]); |
|
| 2199 | - } |
|
| 2200 | - } |
|
| 2201 | - |
|
| 2202 | - $lastNotEmpty = $start; |
|
| 2203 | - |
|
| 2204 | - for ($i = $start; $i < $this->numTokens; $i++) { |
|
| 2205 | - if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2206 | - // Found the end of the statement. |
|
| 2207 | - if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 2208 | - || $this->tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET |
|
| 2209 | - || $this->tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET |
|
| 2210 | - || $this->tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 2211 | - || $this->tokens[$i]['code'] === T_OPEN_TAG |
|
| 2212 | - || $this->tokens[$i]['code'] === T_CLOSE_TAG |
|
| 2213 | - ) { |
|
| 2214 | - return $lastNotEmpty; |
|
| 2215 | - } |
|
| 2216 | - |
|
| 2217 | - return $i; |
|
| 2218 | - } |
|
| 2219 | - |
|
| 2220 | - // Skip nested statements. |
|
| 2221 | - if (isset($this->tokens[$i]['scope_closer']) === true |
|
| 2222 | - && ($i === $this->tokens[$i]['scope_opener'] |
|
| 2223 | - || $i === $this->tokens[$i]['scope_condition']) |
|
| 2224 | - ) { |
|
| 2225 | - if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) { |
|
| 2226 | - return $this->tokens[$i]['scope_closer']; |
|
| 2227 | - } |
|
| 2228 | - |
|
| 2229 | - $i = $this->tokens[$i]['scope_closer']; |
|
| 2230 | - } else if (isset($this->tokens[$i]['bracket_closer']) === true |
|
| 2231 | - && $i === $this->tokens[$i]['bracket_opener'] |
|
| 2232 | - ) { |
|
| 2233 | - $i = $this->tokens[$i]['bracket_closer']; |
|
| 2234 | - } else if (isset($this->tokens[$i]['parenthesis_closer']) === true |
|
| 2235 | - && $i === $this->tokens[$i]['parenthesis_opener'] |
|
| 2236 | - ) { |
|
| 2237 | - $i = $this->tokens[$i]['parenthesis_closer']; |
|
| 2238 | - } |
|
| 2239 | - |
|
| 2240 | - if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2241 | - $lastNotEmpty = $i; |
|
| 2242 | - } |
|
| 2243 | - }//end for |
|
| 2244 | - |
|
| 2245 | - return ($this->numTokens - 1); |
|
| 2246 | - |
|
| 2247 | - }//end findEndOfStatement() |
|
| 2248 | - |
|
| 2249 | - |
|
| 2250 | - /** |
|
| 2251 | - * Returns the position of the first token on a line, matching given type. |
|
| 2252 | - * |
|
| 2253 | - * Returns false if no token can be found. |
|
| 2254 | - * |
|
| 2255 | - * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2256 | - * @param int $start The position to start searching from in the |
|
| 2257 | - * token stack. The first token matching on |
|
| 2258 | - * this line before this token will be returned. |
|
| 2259 | - * @param bool $exclude If true, find the token that is NOT of |
|
| 2260 | - * the types specified in $types. |
|
| 2261 | - * @param string $value The value that the token must be equal to. |
|
| 2262 | - * If value is omitted, tokens with any value will |
|
| 2263 | - * be returned. |
|
| 2264 | - * |
|
| 2265 | - * @return int | bool |
|
| 2266 | - */ |
|
| 2267 | - public function findFirstOnLine($types, $start, $exclude=false, $value=null) |
|
| 2268 | - { |
|
| 2269 | - if (is_array($types) === false) { |
|
| 2270 | - $types = [$types]; |
|
| 2271 | - } |
|
| 2272 | - |
|
| 2273 | - $foundToken = false; |
|
| 2274 | - |
|
| 2275 | - for ($i = $start; $i >= 0; $i--) { |
|
| 2276 | - if ($this->tokens[$i]['line'] < $this->tokens[$start]['line']) { |
|
| 2277 | - break; |
|
| 2278 | - } |
|
| 2279 | - |
|
| 2280 | - $found = $exclude; |
|
| 2281 | - foreach ($types as $type) { |
|
| 2282 | - if ($exclude === false) { |
|
| 2283 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2284 | - $found = true; |
|
| 2285 | - break; |
|
| 2286 | - } |
|
| 2287 | - } else { |
|
| 2288 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2289 | - $found = false; |
|
| 2290 | - break; |
|
| 2291 | - } |
|
| 2292 | - } |
|
| 2293 | - } |
|
| 2294 | - |
|
| 2295 | - if ($found === true) { |
|
| 2296 | - if ($value === null) { |
|
| 2297 | - $foundToken = $i; |
|
| 2298 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2299 | - $foundToken = $i; |
|
| 2300 | - } |
|
| 2301 | - } |
|
| 2302 | - }//end for |
|
| 2303 | - |
|
| 2304 | - return $foundToken; |
|
| 2305 | - |
|
| 2306 | - }//end findFirstOnLine() |
|
| 2307 | - |
|
| 2308 | - |
|
| 2309 | - /** |
|
| 2310 | - * Determine if the passed token has a condition of one of the passed types. |
|
| 2311 | - * |
|
| 2312 | - * @param int $stackPtr The position of the token we are checking. |
|
| 2313 | - * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2314 | - * |
|
| 2315 | - * @return boolean |
|
| 2316 | - */ |
|
| 2317 | - public function hasCondition($stackPtr, $types) |
|
| 2318 | - { |
|
| 2319 | - // Check for the existence of the token. |
|
| 2320 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2321 | - return false; |
|
| 2322 | - } |
|
| 2323 | - |
|
| 2324 | - // Make sure the token has conditions. |
|
| 2325 | - if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2326 | - return false; |
|
| 2327 | - } |
|
| 2328 | - |
|
| 2329 | - $types = (array) $types; |
|
| 2330 | - $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2331 | - |
|
| 2332 | - foreach ($types as $type) { |
|
| 2333 | - if (in_array($type, $conditions, true) === true) { |
|
| 2334 | - // We found a token with the required type. |
|
| 2335 | - return true; |
|
| 2336 | - } |
|
| 2337 | - } |
|
| 2338 | - |
|
| 2339 | - return false; |
|
| 2340 | - |
|
| 2341 | - }//end hasCondition() |
|
| 2342 | - |
|
| 2343 | - |
|
| 2344 | - /** |
|
| 2345 | - * Return the position of the condition for the passed token. |
|
| 2346 | - * |
|
| 2347 | - * Returns FALSE if the token does not have the condition. |
|
| 2348 | - * |
|
| 2349 | - * @param int $stackPtr The position of the token we are checking. |
|
| 2350 | - * @param int|string $type The type of token to search for. |
|
| 2351 | - * |
|
| 2352 | - * @return int |
|
| 2353 | - */ |
|
| 2354 | - public function getCondition($stackPtr, $type) |
|
| 2355 | - { |
|
| 2356 | - // Check for the existence of the token. |
|
| 2357 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2358 | - return false; |
|
| 2359 | - } |
|
| 2360 | - |
|
| 2361 | - // Make sure the token has conditions. |
|
| 2362 | - if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2363 | - return false; |
|
| 2364 | - } |
|
| 2365 | - |
|
| 2366 | - $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2367 | - foreach ($conditions as $token => $condition) { |
|
| 2368 | - if ($condition === $type) { |
|
| 2369 | - return $token; |
|
| 2370 | - } |
|
| 2371 | - } |
|
| 2372 | - |
|
| 2373 | - return false; |
|
| 2374 | - |
|
| 2375 | - }//end getCondition() |
|
| 2376 | - |
|
| 2377 | - |
|
| 2378 | - /** |
|
| 2379 | - * Returns the name of the class that the specified class extends. |
|
| 2380 | - * (works for classes, anonymous classes and interfaces) |
|
| 2381 | - * |
|
| 2382 | - * Returns FALSE on error or if there is no extended class name. |
|
| 2383 | - * |
|
| 2384 | - * @param int $stackPtr The stack position of the class. |
|
| 2385 | - * |
|
| 2386 | - * @return string|false |
|
| 2387 | - */ |
|
| 2388 | - public function findExtendedClassName($stackPtr) |
|
| 2389 | - { |
|
| 2390 | - // Check for the existence of the token. |
|
| 2391 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2392 | - return false; |
|
| 2393 | - } |
|
| 2394 | - |
|
| 2395 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2396 | - && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2397 | - && $this->tokens[$stackPtr]['code'] !== T_INTERFACE |
|
| 2398 | - ) { |
|
| 2399 | - return false; |
|
| 2400 | - } |
|
| 2401 | - |
|
| 2402 | - if (isset($this->tokens[$stackPtr]['scope_opener']) === false) { |
|
| 2403 | - return false; |
|
| 2404 | - } |
|
| 2405 | - |
|
| 2406 | - $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2407 | - $extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex); |
|
| 2408 | - if (false === $extendsIndex) { |
|
| 2409 | - return false; |
|
| 2410 | - } |
|
| 2411 | - |
|
| 2412 | - $find = [ |
|
| 2413 | - T_NS_SEPARATOR, |
|
| 2414 | - T_STRING, |
|
| 2415 | - T_WHITESPACE, |
|
| 2416 | - ]; |
|
| 2417 | - |
|
| 2418 | - $end = $this->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2419 | - $name = $this->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1)); |
|
| 2420 | - $name = trim($name); |
|
| 2421 | - |
|
| 2422 | - if ($name === '') { |
|
| 2423 | - return false; |
|
| 2424 | - } |
|
| 2425 | - |
|
| 2426 | - return $name; |
|
| 2427 | - |
|
| 2428 | - }//end findExtendedClassName() |
|
| 2429 | - |
|
| 2430 | - |
|
| 2431 | - /** |
|
| 2432 | - * Returns the names of the interfaces that the specified class implements. |
|
| 2433 | - * |
|
| 2434 | - * Returns FALSE on error or if there are no implemented interface names. |
|
| 2435 | - * |
|
| 2436 | - * @param int $stackPtr The stack position of the class. |
|
| 2437 | - * |
|
| 2438 | - * @return array|false |
|
| 2439 | - */ |
|
| 2440 | - public function findImplementedInterfaceNames($stackPtr) |
|
| 2441 | - { |
|
| 2442 | - // Check for the existence of the token. |
|
| 2443 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2444 | - return false; |
|
| 2445 | - } |
|
| 2446 | - |
|
| 2447 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2448 | - && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2449 | - ) { |
|
| 2450 | - return false; |
|
| 2451 | - } |
|
| 2452 | - |
|
| 2453 | - if (isset($this->tokens[$stackPtr]['scope_closer']) === false) { |
|
| 2454 | - return false; |
|
| 2455 | - } |
|
| 2456 | - |
|
| 2457 | - $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2458 | - $implementsIndex = $this->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
|
| 2459 | - if ($implementsIndex === false) { |
|
| 2460 | - return false; |
|
| 2461 | - } |
|
| 2462 | - |
|
| 2463 | - $find = [ |
|
| 2464 | - T_NS_SEPARATOR, |
|
| 2465 | - T_STRING, |
|
| 2466 | - T_WHITESPACE, |
|
| 2467 | - T_COMMA, |
|
| 2468 | - ]; |
|
| 2469 | - |
|
| 2470 | - $end = $this->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2471 | - $name = $this->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
|
| 2472 | - $name = trim($name); |
|
| 2473 | - |
|
| 2474 | - if ($name === '') { |
|
| 2475 | - return false; |
|
| 2476 | - } else { |
|
| 2477 | - $names = explode(',', $name); |
|
| 2478 | - $names = array_map('trim', $names); |
|
| 2479 | - return $names; |
|
| 2480 | - } |
|
| 2481 | - |
|
| 2482 | - }//end findImplementedInterfaceNames() |
|
| 22 | + /** |
|
| 23 | + * The absolute path to the file associated with this object. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + public $path = ''; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The content of the file. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $content = ''; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The config data for the run. |
|
| 38 | + * |
|
| 39 | + * @var \PHP_CodeSniffer\Config |
|
| 40 | + */ |
|
| 41 | + public $config = null; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The ruleset used for the run. |
|
| 45 | + * |
|
| 46 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 47 | + */ |
|
| 48 | + public $ruleset = null; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * If TRUE, the entire file is being ignored. |
|
| 52 | + * |
|
| 53 | + * @var boolean |
|
| 54 | + */ |
|
| 55 | + public $ignored = false; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The EOL character this file uses. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + public $eolChar = ''; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The Fixer object to control fixing errors. |
|
| 66 | + * |
|
| 67 | + * @var \PHP_CodeSniffer\Fixer |
|
| 68 | + */ |
|
| 69 | + public $fixer = null; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The tokenizer being used for this file. |
|
| 73 | + * |
|
| 74 | + * @var \PHP_CodeSniffer\Tokenizers\Tokenizer |
|
| 75 | + */ |
|
| 76 | + public $tokenizer = null; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The name of the tokenizer being used for this file. |
|
| 80 | + * |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public $tokenizerType = 'PHP'; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Was the file loaded from cache? |
|
| 87 | + * |
|
| 88 | + * If TRUE, the file was loaded from a local cache. |
|
| 89 | + * If FALSE, the file was tokenized and processed fully. |
|
| 90 | + * |
|
| 91 | + * @var boolean |
|
| 92 | + */ |
|
| 93 | + public $fromCache = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * The number of tokens in this file. |
|
| 97 | + * |
|
| 98 | + * Stored here to save calling count() everywhere. |
|
| 99 | + * |
|
| 100 | + * @var integer |
|
| 101 | + */ |
|
| 102 | + public $numTokens = 0; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * The tokens stack map. |
|
| 106 | + * |
|
| 107 | + * @var array |
|
| 108 | + */ |
|
| 109 | + protected $tokens = []; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * The errors raised from sniffs. |
|
| 113 | + * |
|
| 114 | + * @var array |
|
| 115 | + * @see getErrors() |
|
| 116 | + */ |
|
| 117 | + protected $errors = []; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The warnings raised from sniffs. |
|
| 121 | + * |
|
| 122 | + * @var array |
|
| 123 | + * @see getWarnings() |
|
| 124 | + */ |
|
| 125 | + protected $warnings = []; |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * The metrics recorded by sniffs. |
|
| 129 | + * |
|
| 130 | + * @var array |
|
| 131 | + * @see getMetrics() |
|
| 132 | + */ |
|
| 133 | + protected $metrics = []; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * The metrics recorded for each token. |
|
| 137 | + * |
|
| 138 | + * Stops the same metric being recorded for the same token twice. |
|
| 139 | + * |
|
| 140 | + * @var array |
|
| 141 | + * @see getMetrics() |
|
| 142 | + */ |
|
| 143 | + private $metricTokens = []; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * The total number of errors raised. |
|
| 147 | + * |
|
| 148 | + * @var integer |
|
| 149 | + */ |
|
| 150 | + protected $errorCount = 0; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * The total number of warnings raised. |
|
| 154 | + * |
|
| 155 | + * @var integer |
|
| 156 | + */ |
|
| 157 | + protected $warningCount = 0; |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * The total number of errors and warnings that can be fixed. |
|
| 161 | + * |
|
| 162 | + * @var integer |
|
| 163 | + */ |
|
| 164 | + protected $fixableCount = 0; |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * The total number of errors and warnings that were fixed. |
|
| 168 | + * |
|
| 169 | + * @var integer |
|
| 170 | + */ |
|
| 171 | + protected $fixedCount = 0; |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * An array of sniffs that are being ignored. |
|
| 175 | + * |
|
| 176 | + * @var array |
|
| 177 | + */ |
|
| 178 | + protected $ignoredListeners = []; |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * An array of message codes that are being ignored. |
|
| 182 | + * |
|
| 183 | + * @var array |
|
| 184 | + */ |
|
| 185 | + protected $ignoredCodes = []; |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * An array of sniffs listening to this file's processing. |
|
| 189 | + * |
|
| 190 | + * @var \PHP_CodeSniffer\Sniffs\Sniff[] |
|
| 191 | + */ |
|
| 192 | + protected $listeners = []; |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * The class name of the sniff currently processing the file. |
|
| 196 | + * |
|
| 197 | + * @var string |
|
| 198 | + */ |
|
| 199 | + protected $activeListener = ''; |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * An array of sniffs being processed and how long they took. |
|
| 203 | + * |
|
| 204 | + * @var array |
|
| 205 | + */ |
|
| 206 | + protected $listenerTimes = []; |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * A cache of often used config settings to improve performance. |
|
| 210 | + * |
|
| 211 | + * Storing them here saves 10k+ calls to __get() in the Config class. |
|
| 212 | + * |
|
| 213 | + * @var array |
|
| 214 | + */ |
|
| 215 | + protected $configCache = []; |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Constructs a file. |
|
| 220 | + * |
|
| 221 | + * @param string $path The absolute path to the file to process. |
|
| 222 | + * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 223 | + * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 224 | + * |
|
| 225 | + * @return void |
|
| 226 | + */ |
|
| 227 | + public function __construct($path, Ruleset $ruleset, Config $config) |
|
| 228 | + { |
|
| 229 | + $this->path = $path; |
|
| 230 | + $this->ruleset = $ruleset; |
|
| 231 | + $this->config = $config; |
|
| 232 | + $this->fixer = new Fixer(); |
|
| 233 | + |
|
| 234 | + $parts = explode('.', $path); |
|
| 235 | + $extension = array_pop($parts); |
|
| 236 | + if (isset($config->extensions[$extension]) === true) { |
|
| 237 | + $this->tokenizerType = $config->extensions[$extension]; |
|
| 238 | + } else { |
|
| 239 | + // Revert to default. |
|
| 240 | + $this->tokenizerType = 'PHP'; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + $this->configCache['cache'] = $this->config->cache; |
|
| 244 | + $this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs); |
|
| 245 | + $this->configCache['exclude'] = array_map('strtolower', $this->config->exclude); |
|
| 246 | + $this->configCache['errorSeverity'] = $this->config->errorSeverity; |
|
| 247 | + $this->configCache['warningSeverity'] = $this->config->warningSeverity; |
|
| 248 | + $this->configCache['recordErrors'] = $this->config->recordErrors; |
|
| 249 | + $this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns; |
|
| 250 | + $this->configCache['includePatterns'] = $this->ruleset->includePatterns; |
|
| 251 | + |
|
| 252 | + }//end __construct() |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Set the content of the file. |
|
| 257 | + * |
|
| 258 | + * Setting the content also calculates the EOL char being used. |
|
| 259 | + * |
|
| 260 | + * @param string $content The file content. |
|
| 261 | + * |
|
| 262 | + * @return void |
|
| 263 | + */ |
|
| 264 | + public function setContent($content) |
|
| 265 | + { |
|
| 266 | + $this->content = $content; |
|
| 267 | + $this->tokens = []; |
|
| 268 | + |
|
| 269 | + try { |
|
| 270 | + $this->eolChar = Util\Common::detectLineEndings($content); |
|
| 271 | + } catch (RuntimeException $e) { |
|
| 272 | + $this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings'); |
|
| 273 | + return; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + }//end setContent() |
|
| 277 | + |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Reloads the content of the file. |
|
| 281 | + * |
|
| 282 | + * By default, we have no idea where our content comes from, |
|
| 283 | + * so we can't do anything. |
|
| 284 | + * |
|
| 285 | + * @return void |
|
| 286 | + */ |
|
| 287 | + public function reloadContent() |
|
| 288 | + { |
|
| 289 | + |
|
| 290 | + }//end reloadContent() |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Disables caching of this file. |
|
| 295 | + * |
|
| 296 | + * @return void |
|
| 297 | + */ |
|
| 298 | + public function disableCaching() |
|
| 299 | + { |
|
| 300 | + $this->configCache['cache'] = false; |
|
| 301 | + |
|
| 302 | + }//end disableCaching() |
|
| 303 | + |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Starts the stack traversal and tells listeners when tokens are found. |
|
| 307 | + * |
|
| 308 | + * @return void |
|
| 309 | + */ |
|
| 310 | + public function process() |
|
| 311 | + { |
|
| 312 | + if ($this->ignored === true) { |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $this->errors = []; |
|
| 317 | + $this->warnings = []; |
|
| 318 | + $this->errorCount = 0; |
|
| 319 | + $this->warningCount = 0; |
|
| 320 | + $this->fixableCount = 0; |
|
| 321 | + |
|
| 322 | + $this->parse(); |
|
| 323 | + |
|
| 324 | + // Check if tokenizer errors cause this file to be ignored. |
|
| 325 | + if ($this->ignored === true) { |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + $this->fixer->startFile($this); |
|
| 330 | + |
|
| 331 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 332 | + echo "\t*** START TOKEN PROCESSING ***".PHP_EOL; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + $foundCode = false; |
|
| 336 | + $listenerIgnoreTo = []; |
|
| 337 | + $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); |
|
| 338 | + $checkAnnotations = $this->config->annotations; |
|
| 339 | + |
|
| 340 | + // Foreach of the listeners that have registered to listen for this |
|
| 341 | + // token, get them to process it. |
|
| 342 | + foreach ($this->tokens as $stackPtr => $token) { |
|
| 343 | + // Check for ignored lines. |
|
| 344 | + if ($checkAnnotations === true |
|
| 345 | + && ($token['code'] === T_COMMENT |
|
| 346 | + || $token['code'] === T_PHPCS_IGNORE_FILE |
|
| 347 | + || $token['code'] === T_PHPCS_SET |
|
| 348 | + || $token['code'] === T_DOC_COMMENT_STRING |
|
| 349 | + || $token['code'] === T_DOC_COMMENT_TAG |
|
| 350 | + || ($inTests === true && $token['code'] === T_INLINE_HTML)) |
|
| 351 | + ) { |
|
| 352 | + $commentText = ltrim($this->tokens[$stackPtr]['content'], ' /*'); |
|
| 353 | + $commentTextLower = strtolower($commentText); |
|
| 354 | + if (strpos($commentText, '@codingStandards') !== false) { |
|
| 355 | + if (strpos($commentText, '@codingStandardsIgnoreFile') !== false) { |
|
| 356 | + // Ignoring the whole file, just a little late. |
|
| 357 | + $this->errors = []; |
|
| 358 | + $this->warnings = []; |
|
| 359 | + $this->errorCount = 0; |
|
| 360 | + $this->warningCount = 0; |
|
| 361 | + $this->fixableCount = 0; |
|
| 362 | + return; |
|
| 363 | + } else if (strpos($commentText, '@codingStandardsChangeSetting') !== false) { |
|
| 364 | + $start = strpos($commentText, '@codingStandardsChangeSetting'); |
|
| 365 | + $comment = substr($commentText, ($start + 30)); |
|
| 366 | + $parts = explode(' ', $comment); |
|
| 367 | + if (count($parts) >= 2) { |
|
| 368 | + $sniffParts = explode('.', $parts[0]); |
|
| 369 | + if (count($sniffParts) >= 3) { |
|
| 370 | + // If the sniff code is not known to us, it has not been registered in this run. |
|
| 371 | + // But don't throw an error as it could be there for a different standard to use. |
|
| 372 | + if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) { |
|
| 373 | + $listenerCode = array_shift($parts); |
|
| 374 | + $propertyCode = array_shift($parts); |
|
| 375 | + $propertyValue = rtrim(implode(' ', $parts), " */\r\n"); |
|
| 376 | + $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 377 | + $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + }//end if |
|
| 382 | + } else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile' |
|
| 383 | + || substr($commentTextLower, 0, 17) === '@phpcs:ignorefile' |
|
| 384 | + ) { |
|
| 385 | + // Ignoring the whole file, just a little late. |
|
| 386 | + $this->errors = []; |
|
| 387 | + $this->warnings = []; |
|
| 388 | + $this->errorCount = 0; |
|
| 389 | + $this->warningCount = 0; |
|
| 390 | + $this->fixableCount = 0; |
|
| 391 | + return; |
|
| 392 | + } else if (substr($commentTextLower, 0, 9) === 'phpcs:set' |
|
| 393 | + || substr($commentTextLower, 0, 10) === '@phpcs:set' |
|
| 394 | + ) { |
|
| 395 | + if (isset($token['sniffCode']) === true) { |
|
| 396 | + $listenerCode = $token['sniffCode']; |
|
| 397 | + if (isset($this->ruleset->sniffCodes[$listenerCode]) === true) { |
|
| 398 | + $propertyCode = $token['sniffProperty']; |
|
| 399 | + $propertyValue = $token['sniffPropertyValue']; |
|
| 400 | + $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 401 | + $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + }//end if |
|
| 405 | + }//end if |
|
| 406 | + |
|
| 407 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 408 | + $type = $token['type']; |
|
| 409 | + $content = Util\Common::prepareForOutput($token['content']); |
|
| 410 | + echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ($token['code'] !== T_INLINE_HTML) { |
|
| 414 | + $foundCode = true; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + if (isset($this->ruleset->tokenListeners[$token['code']]) === false) { |
|
| 418 | + continue; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + foreach ($this->ruleset->tokenListeners[$token['code']] as $listenerData) { |
|
| 422 | + if (isset($this->ignoredListeners[$listenerData['class']]) === true |
|
| 423 | + || (isset($listenerIgnoreTo[$listenerData['class']]) === true |
|
| 424 | + && $listenerIgnoreTo[$listenerData['class']] > $stackPtr) |
|
| 425 | + ) { |
|
| 426 | + // This sniff is ignoring past this token, or the whole file. |
|
| 427 | + continue; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Make sure this sniff supports the tokenizer |
|
| 431 | + // we are currently using. |
|
| 432 | + $class = $listenerData['class']; |
|
| 433 | + |
|
| 434 | + if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) { |
|
| 435 | + continue; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + // If the file path matches one of our ignore patterns, skip it. |
|
| 439 | + // While there is support for a type of each pattern |
|
| 440 | + // (absolute or relative) we don't actually support it here. |
|
| 441 | + foreach ($listenerData['ignore'] as $pattern) { |
|
| 442 | + // We assume a / directory separator, as do the exclude rules |
|
| 443 | + // most developers write, so we need a special case for any system |
|
| 444 | + // that is different. |
|
| 445 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
| 446 | + $pattern = str_replace('/', '\\\\', $pattern); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $pattern = '`'.$pattern.'`i'; |
|
| 450 | + if (preg_match($pattern, $this->path) === 1) { |
|
| 451 | + $this->ignoredListeners[$class] = true; |
|
| 452 | + continue(2); |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // If the file path does not match one of our include patterns, skip it. |
|
| 457 | + // While there is support for a type of each pattern |
|
| 458 | + // (absolute or relative) we don't actually support it here. |
|
| 459 | + if (empty($listenerData['include']) === false) { |
|
| 460 | + $included = false; |
|
| 461 | + foreach ($listenerData['include'] as $pattern) { |
|
| 462 | + // We assume a / directory separator, as do the exclude rules |
|
| 463 | + // most developers write, so we need a special case for any system |
|
| 464 | + // that is different. |
|
| 465 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
| 466 | + $pattern = str_replace('/', '\\\\', $pattern); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + $pattern = '`'.$pattern.'`i'; |
|
| 470 | + if (preg_match($pattern, $this->path) === 1) { |
|
| 471 | + $included = true; |
|
| 472 | + break; |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + if ($included === false) { |
|
| 477 | + $this->ignoredListeners[$class] = true; |
|
| 478 | + continue; |
|
| 479 | + } |
|
| 480 | + }//end if |
|
| 481 | + |
|
| 482 | + $this->activeListener = $class; |
|
| 483 | + |
|
| 484 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 485 | + $startTime = microtime(true); |
|
| 486 | + echo "\t\t\tProcessing ".$this->activeListener.'... '; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + $ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr); |
|
| 490 | + if ($ignoreTo !== null) { |
|
| 491 | + $listenerIgnoreTo[$this->activeListener] = $ignoreTo; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 495 | + $timeTaken = (microtime(true) - $startTime); |
|
| 496 | + if (isset($this->listenerTimes[$this->activeListener]) === false) { |
|
| 497 | + $this->listenerTimes[$this->activeListener] = 0; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + $this->listenerTimes[$this->activeListener] += $timeTaken; |
|
| 501 | + |
|
| 502 | + $timeTaken = round(($timeTaken), 4); |
|
| 503 | + echo "DONE in $timeTaken seconds".PHP_EOL; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + $this->activeListener = ''; |
|
| 507 | + }//end foreach |
|
| 508 | + }//end foreach |
|
| 509 | + |
|
| 510 | + // If short open tags are off but the file being checked uses |
|
| 511 | + // short open tags, the whole content will be inline HTML |
|
| 512 | + // and nothing will be checked. So try and handle this case. |
|
| 513 | + // We don't show this error for STDIN because we can't be sure the content |
|
| 514 | + // actually came directly from the user. It could be something like |
|
| 515 | + // refs from a Git pre-push hook. |
|
| 516 | + if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') { |
|
| 517 | + $shortTags = (bool) ini_get('short_open_tag'); |
|
| 518 | + if ($shortTags === false) { |
|
| 519 | + $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.'; |
|
| 520 | + $this->addWarning($error, null, 'Internal.NoCodeFound'); |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 525 | + echo "\t*** END TOKEN PROCESSING ***".PHP_EOL; |
|
| 526 | + echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 527 | + |
|
| 528 | + asort($this->listenerTimes, SORT_NUMERIC); |
|
| 529 | + $this->listenerTimes = array_reverse($this->listenerTimes, true); |
|
| 530 | + foreach ($this->listenerTimes as $listener => $timeTaken) { |
|
| 531 | + echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + $this->fixedCount += $this->fixer->getFixCount(); |
|
| 538 | + |
|
| 539 | + }//end process() |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Tokenizes the file and prepares it for the test run. |
|
| 544 | + * |
|
| 545 | + * @return void |
|
| 546 | + */ |
|
| 547 | + public function parse() |
|
| 548 | + { |
|
| 549 | + if (empty($this->tokens) === false) { |
|
| 550 | + // File has already been parsed. |
|
| 551 | + return; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + try { |
|
| 555 | + $tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType; |
|
| 556 | + $this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar); |
|
| 557 | + $this->tokens = $this->tokenizer->getTokens(); |
|
| 558 | + } catch (TokenizerException $e) { |
|
| 559 | + $this->ignored = true; |
|
| 560 | + $this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception'); |
|
| 561 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 562 | + echo "[$this->tokenizerType => tokenizer error]... "; |
|
| 563 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 564 | + echo PHP_EOL; |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + return; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + $this->numTokens = count($this->tokens); |
|
| 572 | + |
|
| 573 | + // Check for mixed line endings as these can cause tokenizer errors and we |
|
| 574 | + // should let the user know that the results they get may be incorrect. |
|
| 575 | + // This is done by removing all backslashes, removing the newline char we |
|
| 576 | + // detected, then converting newlines chars into text. If any backslashes |
|
| 577 | + // are left at the end, we have additional newline chars in use. |
|
| 578 | + $contents = str_replace('\\', '', $this->content); |
|
| 579 | + $contents = str_replace($this->eolChar, '', $contents); |
|
| 580 | + $contents = str_replace("\n", '\n', $contents); |
|
| 581 | + $contents = str_replace("\r", '\r', $contents); |
|
| 582 | + if (strpos($contents, '\\') !== false) { |
|
| 583 | + $error = 'File has mixed line endings; this may cause incorrect results'; |
|
| 584 | + $this->addWarningOnLine($error, 1, 'Internal.LineEndings.Mixed'); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 588 | + if ($this->numTokens === 0) { |
|
| 589 | + $numLines = 0; |
|
| 590 | + } else { |
|
| 591 | + $numLines = $this->tokens[($this->numTokens - 1)]['line']; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... "; |
|
| 595 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 596 | + echo PHP_EOL; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + }//end parse() |
|
| 601 | + |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Returns the token stack for this file. |
|
| 605 | + * |
|
| 606 | + * @return array |
|
| 607 | + */ |
|
| 608 | + public function getTokens() |
|
| 609 | + { |
|
| 610 | + return $this->tokens; |
|
| 611 | + |
|
| 612 | + }//end getTokens() |
|
| 613 | + |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Remove vars stored in this file that are no longer required. |
|
| 617 | + * |
|
| 618 | + * @return void |
|
| 619 | + */ |
|
| 620 | + public function cleanUp() |
|
| 621 | + { |
|
| 622 | + $this->listenerTimes = null; |
|
| 623 | + $this->content = null; |
|
| 624 | + $this->tokens = null; |
|
| 625 | + $this->metricTokens = null; |
|
| 626 | + $this->tokenizer = null; |
|
| 627 | + $this->fixer = null; |
|
| 628 | + $this->config = null; |
|
| 629 | + $this->ruleset = null; |
|
| 630 | + |
|
| 631 | + }//end cleanUp() |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Records an error against a specific token in the file. |
|
| 636 | + * |
|
| 637 | + * @param string $error The error message. |
|
| 638 | + * @param int $stackPtr The stack position where the error occurred. |
|
| 639 | + * @param string $code A violation code unique to the sniff message. |
|
| 640 | + * @param array $data Replacements for the error message. |
|
| 641 | + * @param int $severity The severity level for this error. A value of 0 |
|
| 642 | + * will be converted into the default severity level. |
|
| 643 | + * @param boolean $fixable Can the error be fixed by the sniff? |
|
| 644 | + * |
|
| 645 | + * @return boolean |
|
| 646 | + */ |
|
| 647 | + public function addError( |
|
| 648 | + $error, |
|
| 649 | + $stackPtr, |
|
| 650 | + $code, |
|
| 651 | + $data=[], |
|
| 652 | + $severity=0, |
|
| 653 | + $fixable=false |
|
| 654 | + ) { |
|
| 655 | + if ($stackPtr === null) { |
|
| 656 | + $line = 1; |
|
| 657 | + $column = 1; |
|
| 658 | + } else { |
|
| 659 | + $line = $this->tokens[$stackPtr]['line']; |
|
| 660 | + $column = $this->tokens[$stackPtr]['column']; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + return $this->addMessage(true, $error, $line, $column, $code, $data, $severity, $fixable); |
|
| 664 | + |
|
| 665 | + }//end addError() |
|
| 666 | + |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Records a warning against a specific token in the file. |
|
| 670 | + * |
|
| 671 | + * @param string $warning The error message. |
|
| 672 | + * @param int $stackPtr The stack position where the error occurred. |
|
| 673 | + * @param string $code A violation code unique to the sniff message. |
|
| 674 | + * @param array $data Replacements for the warning message. |
|
| 675 | + * @param int $severity The severity level for this warning. A value of 0 |
|
| 676 | + * will be converted into the default severity level. |
|
| 677 | + * @param boolean $fixable Can the warning be fixed by the sniff? |
|
| 678 | + * |
|
| 679 | + * @return boolean |
|
| 680 | + */ |
|
| 681 | + public function addWarning( |
|
| 682 | + $warning, |
|
| 683 | + $stackPtr, |
|
| 684 | + $code, |
|
| 685 | + $data=[], |
|
| 686 | + $severity=0, |
|
| 687 | + $fixable=false |
|
| 688 | + ) { |
|
| 689 | + if ($stackPtr === null) { |
|
| 690 | + $line = 1; |
|
| 691 | + $column = 1; |
|
| 692 | + } else { |
|
| 693 | + $line = $this->tokens[$stackPtr]['line']; |
|
| 694 | + $column = $this->tokens[$stackPtr]['column']; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + return $this->addMessage(false, $warning, $line, $column, $code, $data, $severity, $fixable); |
|
| 698 | + |
|
| 699 | + }//end addWarning() |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Records an error against a specific line in the file. |
|
| 704 | + * |
|
| 705 | + * @param string $error The error message. |
|
| 706 | + * @param int $line The line on which the error occurred. |
|
| 707 | + * @param string $code A violation code unique to the sniff message. |
|
| 708 | + * @param array $data Replacements for the error message. |
|
| 709 | + * @param int $severity The severity level for this error. A value of 0 |
|
| 710 | + * will be converted into the default severity level. |
|
| 711 | + * |
|
| 712 | + * @return boolean |
|
| 713 | + */ |
|
| 714 | + public function addErrorOnLine( |
|
| 715 | + $error, |
|
| 716 | + $line, |
|
| 717 | + $code, |
|
| 718 | + $data=[], |
|
| 719 | + $severity=0 |
|
| 720 | + ) { |
|
| 721 | + return $this->addMessage(true, $error, $line, 1, $code, $data, $severity, false); |
|
| 722 | + |
|
| 723 | + }//end addErrorOnLine() |
|
| 724 | + |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Records a warning against a specific token in the file. |
|
| 728 | + * |
|
| 729 | + * @param string $warning The error message. |
|
| 730 | + * @param int $line The line on which the warning occurred. |
|
| 731 | + * @param string $code A violation code unique to the sniff message. |
|
| 732 | + * @param array $data Replacements for the warning message. |
|
| 733 | + * @param int $severity The severity level for this warning. A value of 0 will |
|
| 734 | + * will be converted into the default severity level. |
|
| 735 | + * |
|
| 736 | + * @return boolean |
|
| 737 | + */ |
|
| 738 | + public function addWarningOnLine( |
|
| 739 | + $warning, |
|
| 740 | + $line, |
|
| 741 | + $code, |
|
| 742 | + $data=[], |
|
| 743 | + $severity=0 |
|
| 744 | + ) { |
|
| 745 | + return $this->addMessage(false, $warning, $line, 1, $code, $data, $severity, false); |
|
| 746 | + |
|
| 747 | + }//end addWarningOnLine() |
|
| 748 | + |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Records a fixable error against a specific token in the file. |
|
| 752 | + * |
|
| 753 | + * Returns true if the error was recorded and should be fixed. |
|
| 754 | + * |
|
| 755 | + * @param string $error The error message. |
|
| 756 | + * @param int $stackPtr The stack position where the error occurred. |
|
| 757 | + * @param string $code A violation code unique to the sniff message. |
|
| 758 | + * @param array $data Replacements for the error message. |
|
| 759 | + * @param int $severity The severity level for this error. A value of 0 |
|
| 760 | + * will be converted into the default severity level. |
|
| 761 | + * |
|
| 762 | + * @return boolean |
|
| 763 | + */ |
|
| 764 | + public function addFixableError( |
|
| 765 | + $error, |
|
| 766 | + $stackPtr, |
|
| 767 | + $code, |
|
| 768 | + $data=[], |
|
| 769 | + $severity=0 |
|
| 770 | + ) { |
|
| 771 | + $recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true); |
|
| 772 | + if ($recorded === true && $this->fixer->enabled === true) { |
|
| 773 | + return true; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + return false; |
|
| 777 | + |
|
| 778 | + }//end addFixableError() |
|
| 779 | + |
|
| 780 | + |
|
| 781 | + /** |
|
| 782 | + * Records a fixable warning against a specific token in the file. |
|
| 783 | + * |
|
| 784 | + * Returns true if the warning was recorded and should be fixed. |
|
| 785 | + * |
|
| 786 | + * @param string $warning The error message. |
|
| 787 | + * @param int $stackPtr The stack position where the error occurred. |
|
| 788 | + * @param string $code A violation code unique to the sniff message. |
|
| 789 | + * @param array $data Replacements for the warning message. |
|
| 790 | + * @param int $severity The severity level for this warning. A value of 0 |
|
| 791 | + * will be converted into the default severity level. |
|
| 792 | + * |
|
| 793 | + * @return boolean |
|
| 794 | + */ |
|
| 795 | + public function addFixableWarning( |
|
| 796 | + $warning, |
|
| 797 | + $stackPtr, |
|
| 798 | + $code, |
|
| 799 | + $data=[], |
|
| 800 | + $severity=0 |
|
| 801 | + ) { |
|
| 802 | + $recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true); |
|
| 803 | + if ($recorded === true && $this->fixer->enabled === true) { |
|
| 804 | + return true; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + return false; |
|
| 808 | + |
|
| 809 | + }//end addFixableWarning() |
|
| 810 | + |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * Adds an error to the error stack. |
|
| 814 | + * |
|
| 815 | + * @param boolean $error Is this an error message? |
|
| 816 | + * @param string $message The text of the message. |
|
| 817 | + * @param int $line The line on which the message occurred. |
|
| 818 | + * @param int $column The column at which the message occurred. |
|
| 819 | + * @param string $code A violation code unique to the sniff message. |
|
| 820 | + * @param array $data Replacements for the message. |
|
| 821 | + * @param int $severity The severity level for this message. A value of 0 |
|
| 822 | + * will be converted into the default severity level. |
|
| 823 | + * @param boolean $fixable Can the problem be fixed by the sniff? |
|
| 824 | + * |
|
| 825 | + * @return boolean |
|
| 826 | + */ |
|
| 827 | + protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
|
| 828 | + { |
|
| 829 | + // Check if this line is ignoring all message codes. |
|
| 830 | + if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) { |
|
| 831 | + return false; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + // Work out which sniff generated the message. |
|
| 835 | + $parts = explode('.', $code); |
|
| 836 | + if ($parts[0] === 'Internal') { |
|
| 837 | + // An internal message. |
|
| 838 | + $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 839 | + $sniffCode = $code; |
|
| 840 | + $checkCodes = [$sniffCode]; |
|
| 841 | + } else { |
|
| 842 | + if ($parts[0] !== $code) { |
|
| 843 | + // The full message code has been passed in. |
|
| 844 | + $sniffCode = $code; |
|
| 845 | + $listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.')); |
|
| 846 | + } else { |
|
| 847 | + $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 848 | + $sniffCode = $listenerCode.'.'.$code; |
|
| 849 | + $parts = explode('.', $sniffCode); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + $checkCodes = [ |
|
| 853 | + $sniffCode, |
|
| 854 | + $parts[0].'.'.$parts[1].'.'.$parts[2], |
|
| 855 | + $parts[0].'.'.$parts[1], |
|
| 856 | + $parts[0], |
|
| 857 | + ]; |
|
| 858 | + }//end if |
|
| 859 | + |
|
| 860 | + if (isset($this->tokenizer->ignoredLines[$line]) === true) { |
|
| 861 | + // Check if this line is ignoring this specific message. |
|
| 862 | + $ignored = false; |
|
| 863 | + foreach ($checkCodes as $checkCode) { |
|
| 864 | + if (isset($this->tokenizer->ignoredLines[$line][$checkCode]) === true) { |
|
| 865 | + $ignored = true; |
|
| 866 | + break; |
|
| 867 | + } |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + // If it is ignored, make sure it's not whitelisted. |
|
| 871 | + if ($ignored === true |
|
| 872 | + && isset($this->tokenizer->ignoredLines[$line]['.except']) === true |
|
| 873 | + ) { |
|
| 874 | + foreach ($checkCodes as $checkCode) { |
|
| 875 | + if (isset($this->tokenizer->ignoredLines[$line]['.except'][$checkCode]) === true) { |
|
| 876 | + $ignored = false; |
|
| 877 | + break; |
|
| 878 | + } |
|
| 879 | + } |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + if ($ignored === true) { |
|
| 883 | + return false; |
|
| 884 | + } |
|
| 885 | + }//end if |
|
| 886 | + |
|
| 887 | + $includeAll = true; |
|
| 888 | + if ($this->configCache['cache'] === false |
|
| 889 | + || $this->configCache['recordErrors'] === false |
|
| 890 | + ) { |
|
| 891 | + $includeAll = false; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + // Filter out any messages for sniffs that shouldn't have run |
|
| 895 | + // due to the use of the --sniffs command line argument. |
|
| 896 | + if ($includeAll === false |
|
| 897 | + && ((empty($this->configCache['sniffs']) === false |
|
| 898 | + && in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false) |
|
| 899 | + || (empty($this->configCache['exclude']) === false |
|
| 900 | + && in_array(strtolower($listenerCode), $this->configCache['exclude'], true) === true)) |
|
| 901 | + ) { |
|
| 902 | + return false; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + // If we know this sniff code is being ignored for this file, return early. |
|
| 906 | + foreach ($checkCodes as $checkCode) { |
|
| 907 | + if (isset($this->ignoredCodes[$checkCode]) === true) { |
|
| 908 | + return false; |
|
| 909 | + } |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + $oppositeType = 'warning'; |
|
| 913 | + if ($error === false) { |
|
| 914 | + $oppositeType = 'error'; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + foreach ($checkCodes as $checkCode) { |
|
| 918 | + // Make sure this message type has not been set to the opposite message type. |
|
| 919 | + if (isset($this->ruleset->ruleset[$checkCode]['type']) === true |
|
| 920 | + && $this->ruleset->ruleset[$checkCode]['type'] === $oppositeType |
|
| 921 | + ) { |
|
| 922 | + $error = !$error; |
|
| 923 | + break; |
|
| 924 | + } |
|
| 925 | + } |
|
| 926 | + |
|
| 927 | + if ($error === true) { |
|
| 928 | + $configSeverity = $this->configCache['errorSeverity']; |
|
| 929 | + $messageCount = &$this->errorCount; |
|
| 930 | + $messages = &$this->errors; |
|
| 931 | + } else { |
|
| 932 | + $configSeverity = $this->configCache['warningSeverity']; |
|
| 933 | + $messageCount = &$this->warningCount; |
|
| 934 | + $messages = &$this->warnings; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + if ($includeAll === false && $configSeverity === 0) { |
|
| 938 | + // Don't bother doing any processing as these messages are just going to |
|
| 939 | + // be hidden in the reports anyway. |
|
| 940 | + return false; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + if ($severity === 0) { |
|
| 944 | + $severity = 5; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + foreach ($checkCodes as $checkCode) { |
|
| 948 | + // Make sure we are interested in this severity level. |
|
| 949 | + if (isset($this->ruleset->ruleset[$checkCode]['severity']) === true) { |
|
| 950 | + $severity = $this->ruleset->ruleset[$checkCode]['severity']; |
|
| 951 | + break; |
|
| 952 | + } |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + if ($includeAll === false && $configSeverity > $severity) { |
|
| 956 | + return false; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + // Make sure we are not ignoring this file. |
|
| 960 | + $included = null; |
|
| 961 | + foreach ($checkCodes as $checkCode) { |
|
| 962 | + $patterns = null; |
|
| 963 | + |
|
| 964 | + if (isset($this->configCache['includePatterns'][$checkCode]) === true) { |
|
| 965 | + $patterns = $this->configCache['includePatterns'][$checkCode]; |
|
| 966 | + $excluding = false; |
|
| 967 | + } else if (isset($this->configCache['ignorePatterns'][$checkCode]) === true) { |
|
| 968 | + $patterns = $this->configCache['ignorePatterns'][$checkCode]; |
|
| 969 | + $excluding = true; |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + if ($patterns === null) { |
|
| 973 | + continue; |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + foreach ($patterns as $pattern => $type) { |
|
| 977 | + // While there is support for a type of each pattern |
|
| 978 | + // (absolute or relative) we don't actually support it here. |
|
| 979 | + $replacements = [ |
|
| 980 | + '\\,' => ',', |
|
| 981 | + '*' => '.*', |
|
| 982 | + ]; |
|
| 983 | + |
|
| 984 | + // We assume a / directory separator, as do the exclude rules |
|
| 985 | + // most developers write, so we need a special case for any system |
|
| 986 | + // that is different. |
|
| 987 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
| 988 | + $replacements['/'] = '\\\\'; |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + $pattern = '`'.strtr($pattern, $replacements).'`i'; |
|
| 992 | + $matched = preg_match($pattern, $this->path); |
|
| 993 | + |
|
| 994 | + if ($matched === 0) { |
|
| 995 | + if ($excluding === false && $included === null) { |
|
| 996 | + // This file path is not being included. |
|
| 997 | + $included = false; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + continue; |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + if ($excluding === true) { |
|
| 1004 | + // This file path is being excluded. |
|
| 1005 | + $this->ignoredCodes[$checkCode] = true; |
|
| 1006 | + return false; |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + // This file path is being included. |
|
| 1010 | + $included = true; |
|
| 1011 | + break; |
|
| 1012 | + }//end foreach |
|
| 1013 | + }//end foreach |
|
| 1014 | + |
|
| 1015 | + if ($included === false) { |
|
| 1016 | + // There were include rules set, but this file |
|
| 1017 | + // path didn't match any of them. |
|
| 1018 | + return false; |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + $messageCount++; |
|
| 1022 | + if ($fixable === true) { |
|
| 1023 | + $this->fixableCount++; |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + if ($this->configCache['recordErrors'] === false |
|
| 1027 | + && $includeAll === false |
|
| 1028 | + ) { |
|
| 1029 | + return true; |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + // Work out the error message. |
|
| 1033 | + if (isset($this->ruleset->ruleset[$sniffCode]['message']) === true) { |
|
| 1034 | + $message = $this->ruleset->ruleset[$sniffCode]['message']; |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + if (empty($data) === false) { |
|
| 1038 | + $message = vsprintf($message, $data); |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + if (isset($messages[$line]) === false) { |
|
| 1042 | + $messages[$line] = []; |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + if (isset($messages[$line][$column]) === false) { |
|
| 1046 | + $messages[$line][$column] = []; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + $messages[$line][$column][] = [ |
|
| 1050 | + 'message' => $message, |
|
| 1051 | + 'source' => $sniffCode, |
|
| 1052 | + 'listener' => $this->activeListener, |
|
| 1053 | + 'severity' => $severity, |
|
| 1054 | + 'fixable' => $fixable, |
|
| 1055 | + ]; |
|
| 1056 | + |
|
| 1057 | + if (PHP_CODESNIFFER_VERBOSITY > 1 |
|
| 1058 | + && $this->fixer->enabled === true |
|
| 1059 | + && $fixable === true |
|
| 1060 | + ) { |
|
| 1061 | + @ob_end_clean(); |
|
| 1062 | + echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL; |
|
| 1063 | + ob_start(); |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + return true; |
|
| 1067 | + |
|
| 1068 | + }//end addMessage() |
|
| 1069 | + |
|
| 1070 | + |
|
| 1071 | + /** |
|
| 1072 | + * Record a metric about the file being examined. |
|
| 1073 | + * |
|
| 1074 | + * @param int $stackPtr The stack position where the metric was recorded. |
|
| 1075 | + * @param string $metric The name of the metric being recorded. |
|
| 1076 | + * @param string $value The value of the metric being recorded. |
|
| 1077 | + * |
|
| 1078 | + * @return boolean |
|
| 1079 | + */ |
|
| 1080 | + public function recordMetric($stackPtr, $metric, $value) |
|
| 1081 | + { |
|
| 1082 | + if (isset($this->metrics[$metric]) === false) { |
|
| 1083 | + $this->metrics[$metric] = ['values' => [$value => 1]]; |
|
| 1084 | + $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1085 | + } else if (isset($this->metricTokens[$metric][$stackPtr]) === false) { |
|
| 1086 | + $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1087 | + if (isset($this->metrics[$metric]['values'][$value]) === false) { |
|
| 1088 | + $this->metrics[$metric]['values'][$value] = 1; |
|
| 1089 | + } else { |
|
| 1090 | + $this->metrics[$metric]['values'][$value]++; |
|
| 1091 | + } |
|
| 1092 | + } |
|
| 1093 | + |
|
| 1094 | + return true; |
|
| 1095 | + |
|
| 1096 | + }//end recordMetric() |
|
| 1097 | + |
|
| 1098 | + |
|
| 1099 | + /** |
|
| 1100 | + * Returns the number of errors raised. |
|
| 1101 | + * |
|
| 1102 | + * @return int |
|
| 1103 | + */ |
|
| 1104 | + public function getErrorCount() |
|
| 1105 | + { |
|
| 1106 | + return $this->errorCount; |
|
| 1107 | + |
|
| 1108 | + }//end getErrorCount() |
|
| 1109 | + |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * Returns the number of warnings raised. |
|
| 1113 | + * |
|
| 1114 | + * @return int |
|
| 1115 | + */ |
|
| 1116 | + public function getWarningCount() |
|
| 1117 | + { |
|
| 1118 | + return $this->warningCount; |
|
| 1119 | + |
|
| 1120 | + }//end getWarningCount() |
|
| 1121 | + |
|
| 1122 | + |
|
| 1123 | + /** |
|
| 1124 | + * Returns the number of fixable errors/warnings raised. |
|
| 1125 | + * |
|
| 1126 | + * @return int |
|
| 1127 | + */ |
|
| 1128 | + public function getFixableCount() |
|
| 1129 | + { |
|
| 1130 | + return $this->fixableCount; |
|
| 1131 | + |
|
| 1132 | + }//end getFixableCount() |
|
| 1133 | + |
|
| 1134 | + |
|
| 1135 | + /** |
|
| 1136 | + * Returns the number of fixed errors/warnings. |
|
| 1137 | + * |
|
| 1138 | + * @return int |
|
| 1139 | + */ |
|
| 1140 | + public function getFixedCount() |
|
| 1141 | + { |
|
| 1142 | + return $this->fixedCount; |
|
| 1143 | + |
|
| 1144 | + }//end getFixedCount() |
|
| 1145 | + |
|
| 1146 | + |
|
| 1147 | + /** |
|
| 1148 | + * Returns the list of ignored lines. |
|
| 1149 | + * |
|
| 1150 | + * @return array |
|
| 1151 | + */ |
|
| 1152 | + public function getIgnoredLines() |
|
| 1153 | + { |
|
| 1154 | + return $this->tokenizer->ignoredLines; |
|
| 1155 | + |
|
| 1156 | + }//end getIgnoredLines() |
|
| 1157 | + |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * Returns the errors raised from processing this file. |
|
| 1161 | + * |
|
| 1162 | + * @return array |
|
| 1163 | + */ |
|
| 1164 | + public function getErrors() |
|
| 1165 | + { |
|
| 1166 | + return $this->errors; |
|
| 1167 | + |
|
| 1168 | + }//end getErrors() |
|
| 1169 | + |
|
| 1170 | + |
|
| 1171 | + /** |
|
| 1172 | + * Returns the warnings raised from processing this file. |
|
| 1173 | + * |
|
| 1174 | + * @return array |
|
| 1175 | + */ |
|
| 1176 | + public function getWarnings() |
|
| 1177 | + { |
|
| 1178 | + return $this->warnings; |
|
| 1179 | + |
|
| 1180 | + }//end getWarnings() |
|
| 1181 | + |
|
| 1182 | + |
|
| 1183 | + /** |
|
| 1184 | + * Returns the metrics found while processing this file. |
|
| 1185 | + * |
|
| 1186 | + * @return array |
|
| 1187 | + */ |
|
| 1188 | + public function getMetrics() |
|
| 1189 | + { |
|
| 1190 | + return $this->metrics; |
|
| 1191 | + |
|
| 1192 | + }//end getMetrics() |
|
| 1193 | + |
|
| 1194 | + |
|
| 1195 | + /** |
|
| 1196 | + * Returns the absolute filename of this file. |
|
| 1197 | + * |
|
| 1198 | + * @return string |
|
| 1199 | + */ |
|
| 1200 | + public function getFilename() |
|
| 1201 | + { |
|
| 1202 | + return $this->path; |
|
| 1203 | + |
|
| 1204 | + }//end getFilename() |
|
| 1205 | + |
|
| 1206 | + |
|
| 1207 | + /** |
|
| 1208 | + * Returns the declaration names for classes, interfaces, traits, and functions. |
|
| 1209 | + * |
|
| 1210 | + * @param int $stackPtr The position of the declaration token which |
|
| 1211 | + * declared the class, interface, trait, or function. |
|
| 1212 | + * |
|
| 1213 | + * @return string|null The name of the class, interface, trait, or function; |
|
| 1214 | + * or NULL if the function or class is anonymous. |
|
| 1215 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type |
|
| 1216 | + * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
|
| 1217 | + * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
|
| 1218 | + */ |
|
| 1219 | + public function getDeclarationName($stackPtr) |
|
| 1220 | + { |
|
| 1221 | + $tokenCode = $this->tokens[$stackPtr]['code']; |
|
| 1222 | + |
|
| 1223 | + if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { |
|
| 1224 | + return null; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + if ($tokenCode !== T_FUNCTION |
|
| 1228 | + && $tokenCode !== T_CLASS |
|
| 1229 | + && $tokenCode !== T_INTERFACE |
|
| 1230 | + && $tokenCode !== T_TRAIT |
|
| 1231 | + ) { |
|
| 1232 | + throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT'); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + if ($tokenCode === T_FUNCTION |
|
| 1236 | + && strtolower($this->tokens[$stackPtr]['content']) !== 'function' |
|
| 1237 | + ) { |
|
| 1238 | + // This is a function declared without the "function" keyword. |
|
| 1239 | + // So this token is the function name. |
|
| 1240 | + return $this->tokens[$stackPtr]['content']; |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + $content = null; |
|
| 1244 | + for ($i = $stackPtr; $i < $this->numTokens; $i++) { |
|
| 1245 | + if ($this->tokens[$i]['code'] === T_STRING) { |
|
| 1246 | + $content = $this->tokens[$i]['content']; |
|
| 1247 | + break; |
|
| 1248 | + } |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + return $content; |
|
| 1252 | + |
|
| 1253 | + }//end getDeclarationName() |
|
| 1254 | + |
|
| 1255 | + |
|
| 1256 | + /** |
|
| 1257 | + * Returns the method parameters for the specified function token. |
|
| 1258 | + * |
|
| 1259 | + * Each parameter is in the following format: |
|
| 1260 | + * |
|
| 1261 | + * <code> |
|
| 1262 | + * 0 => array( |
|
| 1263 | + * 'name' => '$var', // The variable name. |
|
| 1264 | + * 'token' => integer, // The stack pointer to the variable name. |
|
| 1265 | + * 'content' => string, // The full content of the variable definition. |
|
| 1266 | + * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
|
| 1267 | + * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
|
| 1268 | + * 'type_hint' => string, // The type hint for the variable. |
|
| 1269 | + * 'type_hint_token' => integer, // The stack pointer to the type hint |
|
| 1270 | + * // or false if there is no type hint. |
|
| 1271 | + * 'nullable_type' => boolean, // Is the variable using a nullable type? |
|
| 1272 | + * ) |
|
| 1273 | + * </code> |
|
| 1274 | + * |
|
| 1275 | + * Parameters with default values have an additional array index of |
|
| 1276 | + * 'default' with the value of the default as a string. |
|
| 1277 | + * |
|
| 1278 | + * @param int $stackPtr The position in the stack of the function token |
|
| 1279 | + * to acquire the parameters for. |
|
| 1280 | + * |
|
| 1281 | + * @return array |
|
| 1282 | + * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
|
| 1283 | + * type T_FUNCTION or T_CLOSURE. |
|
| 1284 | + */ |
|
| 1285 | + public function getMethodParameters($stackPtr) |
|
| 1286 | + { |
|
| 1287 | + if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1288 | + && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1289 | + ) { |
|
| 1290 | + throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + $opener = $this->tokens[$stackPtr]['parenthesis_opener']; |
|
| 1294 | + $closer = $this->tokens[$stackPtr]['parenthesis_closer']; |
|
| 1295 | + |
|
| 1296 | + $vars = []; |
|
| 1297 | + $currVar = null; |
|
| 1298 | + $paramStart = ($opener + 1); |
|
| 1299 | + $defaultStart = null; |
|
| 1300 | + $paramCount = 0; |
|
| 1301 | + $passByReference = false; |
|
| 1302 | + $variableLength = false; |
|
| 1303 | + $typeHint = ''; |
|
| 1304 | + $typeHintToken = false; |
|
| 1305 | + $nullableType = false; |
|
| 1306 | + |
|
| 1307 | + for ($i = $paramStart; $i <= $closer; $i++) { |
|
| 1308 | + // Check to see if this token has a parenthesis or bracket opener. If it does |
|
| 1309 | + // it's likely to be an array which might have arguments in it. This |
|
| 1310 | + // could cause problems in our parsing below, so lets just skip to the |
|
| 1311 | + // end of it. |
|
| 1312 | + if (isset($this->tokens[$i]['parenthesis_opener']) === true) { |
|
| 1313 | + // Don't do this if it's the close parenthesis for the method. |
|
| 1314 | + if ($i !== $this->tokens[$i]['parenthesis_closer']) { |
|
| 1315 | + $i = ($this->tokens[$i]['parenthesis_closer'] + 1); |
|
| 1316 | + } |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + if (isset($this->tokens[$i]['bracket_opener']) === true) { |
|
| 1320 | + // Don't do this if it's the close parenthesis for the method. |
|
| 1321 | + if ($i !== $this->tokens[$i]['bracket_closer']) { |
|
| 1322 | + $i = ($this->tokens[$i]['bracket_closer'] + 1); |
|
| 1323 | + } |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + switch ($this->tokens[$i]['code']) { |
|
| 1327 | + case T_BITWISE_AND: |
|
| 1328 | + if ($defaultStart === null) { |
|
| 1329 | + $passByReference = true; |
|
| 1330 | + } |
|
| 1331 | + break; |
|
| 1332 | + case T_VARIABLE: |
|
| 1333 | + $currVar = $i; |
|
| 1334 | + break; |
|
| 1335 | + case T_ELLIPSIS: |
|
| 1336 | + $variableLength = true; |
|
| 1337 | + break; |
|
| 1338 | + case T_CALLABLE: |
|
| 1339 | + if ($typeHintToken === false) { |
|
| 1340 | + $typeHintToken = $i; |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1344 | + break; |
|
| 1345 | + case T_SELF: |
|
| 1346 | + case T_PARENT: |
|
| 1347 | + case T_STATIC: |
|
| 1348 | + // Self and parent are valid, static invalid, but was probably intended as type hint. |
|
| 1349 | + if (isset($defaultStart) === false) { |
|
| 1350 | + if ($typeHintToken === false) { |
|
| 1351 | + $typeHintToken = $i; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1355 | + } |
|
| 1356 | + break; |
|
| 1357 | + case T_STRING: |
|
| 1358 | + // This is a string, so it may be a type hint, but it could |
|
| 1359 | + // also be a constant used as a default value. |
|
| 1360 | + $prevComma = false; |
|
| 1361 | + for ($t = $i; $t >= $opener; $t--) { |
|
| 1362 | + if ($this->tokens[$t]['code'] === T_COMMA) { |
|
| 1363 | + $prevComma = $t; |
|
| 1364 | + break; |
|
| 1365 | + } |
|
| 1366 | + } |
|
| 1367 | + |
|
| 1368 | + if ($prevComma !== false) { |
|
| 1369 | + $nextEquals = false; |
|
| 1370 | + for ($t = $prevComma; $t < $i; $t++) { |
|
| 1371 | + if ($this->tokens[$t]['code'] === T_EQUAL) { |
|
| 1372 | + $nextEquals = $t; |
|
| 1373 | + break; |
|
| 1374 | + } |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + if ($nextEquals !== false) { |
|
| 1378 | + break; |
|
| 1379 | + } |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + if ($defaultStart === null) { |
|
| 1383 | + if ($typeHintToken === false) { |
|
| 1384 | + $typeHintToken = $i; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1388 | + } |
|
| 1389 | + break; |
|
| 1390 | + case T_NS_SEPARATOR: |
|
| 1391 | + // Part of a type hint or default value. |
|
| 1392 | + if ($defaultStart === null) { |
|
| 1393 | + if ($typeHintToken === false) { |
|
| 1394 | + $typeHintToken = $i; |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1398 | + } |
|
| 1399 | + break; |
|
| 1400 | + case T_NULLABLE: |
|
| 1401 | + if ($defaultStart === null) { |
|
| 1402 | + $nullableType = true; |
|
| 1403 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1404 | + } |
|
| 1405 | + break; |
|
| 1406 | + case T_CLOSE_PARENTHESIS: |
|
| 1407 | + case T_COMMA: |
|
| 1408 | + // If it's null, then there must be no parameters for this |
|
| 1409 | + // method. |
|
| 1410 | + if ($currVar === null) { |
|
| 1411 | + continue 2; |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + $vars[$paramCount] = []; |
|
| 1415 | + $vars[$paramCount]['token'] = $currVar; |
|
| 1416 | + $vars[$paramCount]['name'] = $this->tokens[$currVar]['content']; |
|
| 1417 | + $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart))); |
|
| 1418 | + |
|
| 1419 | + if ($defaultStart !== null) { |
|
| 1420 | + $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart))); |
|
| 1421 | + } |
|
| 1422 | + |
|
| 1423 | + $vars[$paramCount]['pass_by_reference'] = $passByReference; |
|
| 1424 | + $vars[$paramCount]['variable_length'] = $variableLength; |
|
| 1425 | + $vars[$paramCount]['type_hint'] = $typeHint; |
|
| 1426 | + $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
|
| 1427 | + $vars[$paramCount]['nullable_type'] = $nullableType; |
|
| 1428 | + |
|
| 1429 | + // Reset the vars, as we are about to process the next parameter. |
|
| 1430 | + $defaultStart = null; |
|
| 1431 | + $paramStart = ($i + 1); |
|
| 1432 | + $passByReference = false; |
|
| 1433 | + $variableLength = false; |
|
| 1434 | + $typeHint = ''; |
|
| 1435 | + $typeHintToken = false; |
|
| 1436 | + $nullableType = false; |
|
| 1437 | + |
|
| 1438 | + $paramCount++; |
|
| 1439 | + break; |
|
| 1440 | + case T_EQUAL: |
|
| 1441 | + $defaultStart = ($i + 1); |
|
| 1442 | + break; |
|
| 1443 | + }//end switch |
|
| 1444 | + }//end for |
|
| 1445 | + |
|
| 1446 | + return $vars; |
|
| 1447 | + |
|
| 1448 | + }//end getMethodParameters() |
|
| 1449 | + |
|
| 1450 | + |
|
| 1451 | + /** |
|
| 1452 | + * Returns the visibility and implementation properties of a method. |
|
| 1453 | + * |
|
| 1454 | + * The format of the array is: |
|
| 1455 | + * <code> |
|
| 1456 | + * array( |
|
| 1457 | + * 'scope' => 'public', // public protected or protected |
|
| 1458 | + * 'scope_specified' => true, // true is scope keyword was found. |
|
| 1459 | + * 'return_type' => '', // the return type of the method. |
|
| 1460 | + * 'return_type_token' => integer, // The stack pointer to the start of the return type |
|
| 1461 | + * // or false if there is no return type. |
|
| 1462 | + * 'nullable_return_type' => false, // true if the return type is nullable. |
|
| 1463 | + * 'is_abstract' => false, // true if the abstract keyword was found. |
|
| 1464 | + * 'is_final' => false, // true if the final keyword was found. |
|
| 1465 | + * 'is_static' => false, // true if the static keyword was found. |
|
| 1466 | + * 'has_body' => false, // true if the method has a body |
|
| 1467 | + * ); |
|
| 1468 | + * </code> |
|
| 1469 | + * |
|
| 1470 | + * @param int $stackPtr The position in the stack of the function token to |
|
| 1471 | + * acquire the properties for. |
|
| 1472 | + * |
|
| 1473 | + * @return array |
|
| 1474 | + * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1475 | + * T_FUNCTION token. |
|
| 1476 | + */ |
|
| 1477 | + public function getMethodProperties($stackPtr) |
|
| 1478 | + { |
|
| 1479 | + if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1480 | + && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1481 | + ) { |
|
| 1482 | + throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1483 | + } |
|
| 1484 | + |
|
| 1485 | + if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 1486 | + $valid = [ |
|
| 1487 | + T_PUBLIC => T_PUBLIC, |
|
| 1488 | + T_PRIVATE => T_PRIVATE, |
|
| 1489 | + T_PROTECTED => T_PROTECTED, |
|
| 1490 | + T_STATIC => T_STATIC, |
|
| 1491 | + T_FINAL => T_FINAL, |
|
| 1492 | + T_ABSTRACT => T_ABSTRACT, |
|
| 1493 | + T_WHITESPACE => T_WHITESPACE, |
|
| 1494 | + T_COMMENT => T_COMMENT, |
|
| 1495 | + T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1496 | + ]; |
|
| 1497 | + } else { |
|
| 1498 | + $valid = [ |
|
| 1499 | + T_STATIC => T_STATIC, |
|
| 1500 | + T_WHITESPACE => T_WHITESPACE, |
|
| 1501 | + T_COMMENT => T_COMMENT, |
|
| 1502 | + T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1503 | + ]; |
|
| 1504 | + } |
|
| 1505 | + |
|
| 1506 | + $scope = 'public'; |
|
| 1507 | + $scopeSpecified = false; |
|
| 1508 | + $isAbstract = false; |
|
| 1509 | + $isFinal = false; |
|
| 1510 | + $isStatic = false; |
|
| 1511 | + |
|
| 1512 | + for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1513 | + if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1514 | + break; |
|
| 1515 | + } |
|
| 1516 | + |
|
| 1517 | + switch ($this->tokens[$i]['code']) { |
|
| 1518 | + case T_PUBLIC: |
|
| 1519 | + $scope = 'public'; |
|
| 1520 | + $scopeSpecified = true; |
|
| 1521 | + break; |
|
| 1522 | + case T_PRIVATE: |
|
| 1523 | + $scope = 'private'; |
|
| 1524 | + $scopeSpecified = true; |
|
| 1525 | + break; |
|
| 1526 | + case T_PROTECTED: |
|
| 1527 | + $scope = 'protected'; |
|
| 1528 | + $scopeSpecified = true; |
|
| 1529 | + break; |
|
| 1530 | + case T_ABSTRACT: |
|
| 1531 | + $isAbstract = true; |
|
| 1532 | + break; |
|
| 1533 | + case T_FINAL: |
|
| 1534 | + $isFinal = true; |
|
| 1535 | + break; |
|
| 1536 | + case T_STATIC: |
|
| 1537 | + $isStatic = true; |
|
| 1538 | + break; |
|
| 1539 | + }//end switch |
|
| 1540 | + }//end for |
|
| 1541 | + |
|
| 1542 | + $returnType = ''; |
|
| 1543 | + $returnTypeToken = false; |
|
| 1544 | + $nullableReturnType = false; |
|
| 1545 | + $hasBody = true; |
|
| 1546 | + |
|
| 1547 | + if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 1548 | + $scopeOpener = null; |
|
| 1549 | + if (isset($this->tokens[$stackPtr]['scope_opener']) === true) { |
|
| 1550 | + $scopeOpener = $this->tokens[$stackPtr]['scope_opener']; |
|
| 1551 | + } |
|
| 1552 | + |
|
| 1553 | + $valid = [ |
|
| 1554 | + T_STRING => T_STRING, |
|
| 1555 | + T_CALLABLE => T_CALLABLE, |
|
| 1556 | + T_SELF => T_SELF, |
|
| 1557 | + T_PARENT => T_PARENT, |
|
| 1558 | + T_NS_SEPARATOR => T_NS_SEPARATOR, |
|
| 1559 | + ]; |
|
| 1560 | + |
|
| 1561 | + for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) { |
|
| 1562 | + if (($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON) |
|
| 1563 | + || ($scopeOpener !== null && $i === $scopeOpener) |
|
| 1564 | + ) { |
|
| 1565 | + // End of function definition. |
|
| 1566 | + break; |
|
| 1567 | + } |
|
| 1568 | + |
|
| 1569 | + if ($this->tokens[$i]['code'] === T_NULLABLE) { |
|
| 1570 | + $nullableReturnType = true; |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + if (isset($valid[$this->tokens[$i]['code']]) === true) { |
|
| 1574 | + if ($returnTypeToken === false) { |
|
| 1575 | + $returnTypeToken = $i; |
|
| 1576 | + } |
|
| 1577 | + |
|
| 1578 | + $returnType .= $this->tokens[$i]['content']; |
|
| 1579 | + } |
|
| 1580 | + } |
|
| 1581 | + |
|
| 1582 | + $end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']); |
|
| 1583 | + $hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET; |
|
| 1584 | + }//end if |
|
| 1585 | + |
|
| 1586 | + if ($returnType !== '' && $nullableReturnType === true) { |
|
| 1587 | + $returnType = '?'.$returnType; |
|
| 1588 | + } |
|
| 1589 | + |
|
| 1590 | + return [ |
|
| 1591 | + 'scope' => $scope, |
|
| 1592 | + 'scope_specified' => $scopeSpecified, |
|
| 1593 | + 'return_type' => $returnType, |
|
| 1594 | + 'return_type_token' => $returnTypeToken, |
|
| 1595 | + 'nullable_return_type' => $nullableReturnType, |
|
| 1596 | + 'is_abstract' => $isAbstract, |
|
| 1597 | + 'is_final' => $isFinal, |
|
| 1598 | + 'is_static' => $isStatic, |
|
| 1599 | + 'has_body' => $hasBody, |
|
| 1600 | + ]; |
|
| 1601 | + |
|
| 1602 | + }//end getMethodProperties() |
|
| 1603 | + |
|
| 1604 | + |
|
| 1605 | + /** |
|
| 1606 | + * Returns the visibility and implementation properties of the class member |
|
| 1607 | + * variable found at the specified position in the stack. |
|
| 1608 | + * |
|
| 1609 | + * The format of the array is: |
|
| 1610 | + * |
|
| 1611 | + * <code> |
|
| 1612 | + * array( |
|
| 1613 | + * 'scope' => 'public', // public protected or protected. |
|
| 1614 | + * 'scope_specified' => false, // true if the scope was explicitly specified. |
|
| 1615 | + * 'is_static' => false, // true if the static keyword was found. |
|
| 1616 | + * ); |
|
| 1617 | + * </code> |
|
| 1618 | + * |
|
| 1619 | + * @param int $stackPtr The position in the stack of the T_VARIABLE token to |
|
| 1620 | + * acquire the properties for. |
|
| 1621 | + * |
|
| 1622 | + * @return array |
|
| 1623 | + * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1624 | + * T_VARIABLE token, or if the position is not |
|
| 1625 | + * a class member variable. |
|
| 1626 | + */ |
|
| 1627 | + public function getMemberProperties($stackPtr) |
|
| 1628 | + { |
|
| 1629 | + if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) { |
|
| 1630 | + throw new TokenizerException('$stackPtr must be of type T_VARIABLE'); |
|
| 1631 | + } |
|
| 1632 | + |
|
| 1633 | + $conditions = array_keys($this->tokens[$stackPtr]['conditions']); |
|
| 1634 | + $ptr = array_pop($conditions); |
|
| 1635 | + if (isset($this->tokens[$ptr]) === false |
|
| 1636 | + || ($this->tokens[$ptr]['code'] !== T_CLASS |
|
| 1637 | + && $this->tokens[$ptr]['code'] !== T_ANON_CLASS |
|
| 1638 | + && $this->tokens[$ptr]['code'] !== T_TRAIT) |
|
| 1639 | + ) { |
|
| 1640 | + if (isset($this->tokens[$ptr]) === true |
|
| 1641 | + && $this->tokens[$ptr]['code'] === T_INTERFACE |
|
| 1642 | + ) { |
|
| 1643 | + // T_VARIABLEs in interfaces can actually be method arguments |
|
| 1644 | + // but they wont be seen as being inside the method because there |
|
| 1645 | + // are no scope openers and closers for abstract methods. If it is in |
|
| 1646 | + // parentheses, we can be pretty sure it is a method argument. |
|
| 1647 | + if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false |
|
| 1648 | + || empty($this->tokens[$stackPtr]['nested_parenthesis']) === true |
|
| 1649 | + ) { |
|
| 1650 | + $error = 'Possible parse error: interfaces may not include member vars'; |
|
| 1651 | + $this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar'); |
|
| 1652 | + return []; |
|
| 1653 | + } |
|
| 1654 | + } else { |
|
| 1655 | + throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1656 | + } |
|
| 1657 | + } |
|
| 1658 | + |
|
| 1659 | + // Make sure it's not a method parameter. |
|
| 1660 | + if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) { |
|
| 1661 | + $parenthesis = array_keys($this->tokens[$stackPtr]['nested_parenthesis']); |
|
| 1662 | + $deepestOpen = array_pop($parenthesis); |
|
| 1663 | + if ($deepestOpen > $ptr |
|
| 1664 | + && isset($this->tokens[$deepestOpen]['parenthesis_owner']) === true |
|
| 1665 | + && $this->tokens[$this->tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION |
|
| 1666 | + ) { |
|
| 1667 | + throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1668 | + } |
|
| 1669 | + } |
|
| 1670 | + |
|
| 1671 | + $valid = [ |
|
| 1672 | + T_PUBLIC => T_PUBLIC, |
|
| 1673 | + T_PRIVATE => T_PRIVATE, |
|
| 1674 | + T_PROTECTED => T_PROTECTED, |
|
| 1675 | + T_STATIC => T_STATIC, |
|
| 1676 | + T_VAR => T_VAR, |
|
| 1677 | + ]; |
|
| 1678 | + |
|
| 1679 | + $valid += Util\Tokens::$emptyTokens; |
|
| 1680 | + |
|
| 1681 | + $scope = 'public'; |
|
| 1682 | + $scopeSpecified = false; |
|
| 1683 | + $isStatic = false; |
|
| 1684 | + |
|
| 1685 | + $startOfStatement = $this->findPrevious( |
|
| 1686 | + [ |
|
| 1687 | + T_SEMICOLON, |
|
| 1688 | + T_OPEN_CURLY_BRACKET, |
|
| 1689 | + T_CLOSE_CURLY_BRACKET, |
|
| 1690 | + ], |
|
| 1691 | + ($stackPtr - 1) |
|
| 1692 | + ); |
|
| 1693 | + |
|
| 1694 | + for ($i = ($startOfStatement + 1); $i < $stackPtr; $i++) { |
|
| 1695 | + if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1696 | + break; |
|
| 1697 | + } |
|
| 1698 | + |
|
| 1699 | + switch ($this->tokens[$i]['code']) { |
|
| 1700 | + case T_PUBLIC: |
|
| 1701 | + $scope = 'public'; |
|
| 1702 | + $scopeSpecified = true; |
|
| 1703 | + break; |
|
| 1704 | + case T_PRIVATE: |
|
| 1705 | + $scope = 'private'; |
|
| 1706 | + $scopeSpecified = true; |
|
| 1707 | + break; |
|
| 1708 | + case T_PROTECTED: |
|
| 1709 | + $scope = 'protected'; |
|
| 1710 | + $scopeSpecified = true; |
|
| 1711 | + break; |
|
| 1712 | + case T_STATIC: |
|
| 1713 | + $isStatic = true; |
|
| 1714 | + break; |
|
| 1715 | + } |
|
| 1716 | + }//end for |
|
| 1717 | + |
|
| 1718 | + return [ |
|
| 1719 | + 'scope' => $scope, |
|
| 1720 | + 'scope_specified' => $scopeSpecified, |
|
| 1721 | + 'is_static' => $isStatic, |
|
| 1722 | + ]; |
|
| 1723 | + |
|
| 1724 | + }//end getMemberProperties() |
|
| 1725 | + |
|
| 1726 | + |
|
| 1727 | + /** |
|
| 1728 | + * Returns the visibility and implementation properties of a class. |
|
| 1729 | + * |
|
| 1730 | + * The format of the array is: |
|
| 1731 | + * <code> |
|
| 1732 | + * array( |
|
| 1733 | + * 'is_abstract' => false, // true if the abstract keyword was found. |
|
| 1734 | + * 'is_final' => false, // true if the final keyword was found. |
|
| 1735 | + * ); |
|
| 1736 | + * </code> |
|
| 1737 | + * |
|
| 1738 | + * @param int $stackPtr The position in the stack of the T_CLASS token to |
|
| 1739 | + * acquire the properties for. |
|
| 1740 | + * |
|
| 1741 | + * @return array |
|
| 1742 | + * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
|
| 1743 | + * T_CLASS token. |
|
| 1744 | + */ |
|
| 1745 | + public function getClassProperties($stackPtr) |
|
| 1746 | + { |
|
| 1747 | + if ($this->tokens[$stackPtr]['code'] !== T_CLASS) { |
|
| 1748 | + throw new TokenizerException('$stackPtr must be of type T_CLASS'); |
|
| 1749 | + } |
|
| 1750 | + |
|
| 1751 | + $valid = [ |
|
| 1752 | + T_FINAL => T_FINAL, |
|
| 1753 | + T_ABSTRACT => T_ABSTRACT, |
|
| 1754 | + T_WHITESPACE => T_WHITESPACE, |
|
| 1755 | + T_COMMENT => T_COMMENT, |
|
| 1756 | + T_DOC_COMMENT => T_DOC_COMMENT, |
|
| 1757 | + ]; |
|
| 1758 | + |
|
| 1759 | + $isAbstract = false; |
|
| 1760 | + $isFinal = false; |
|
| 1761 | + |
|
| 1762 | + for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1763 | + if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1764 | + break; |
|
| 1765 | + } |
|
| 1766 | + |
|
| 1767 | + switch ($this->tokens[$i]['code']) { |
|
| 1768 | + case T_ABSTRACT: |
|
| 1769 | + $isAbstract = true; |
|
| 1770 | + break; |
|
| 1771 | + |
|
| 1772 | + case T_FINAL: |
|
| 1773 | + $isFinal = true; |
|
| 1774 | + break; |
|
| 1775 | + } |
|
| 1776 | + }//end for |
|
| 1777 | + |
|
| 1778 | + return [ |
|
| 1779 | + 'is_abstract' => $isAbstract, |
|
| 1780 | + 'is_final' => $isFinal, |
|
| 1781 | + ]; |
|
| 1782 | + |
|
| 1783 | + }//end getClassProperties() |
|
| 1784 | + |
|
| 1785 | + |
|
| 1786 | + /** |
|
| 1787 | + * Determine if the passed token is a reference operator. |
|
| 1788 | + * |
|
| 1789 | + * Returns true if the specified token position represents a reference. |
|
| 1790 | + * Returns false if the token represents a bitwise operator. |
|
| 1791 | + * |
|
| 1792 | + * @param int $stackPtr The position of the T_BITWISE_AND token. |
|
| 1793 | + * |
|
| 1794 | + * @return boolean |
|
| 1795 | + */ |
|
| 1796 | + public function isReference($stackPtr) |
|
| 1797 | + { |
|
| 1798 | + if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) { |
|
| 1799 | + return false; |
|
| 1800 | + } |
|
| 1801 | + |
|
| 1802 | + $tokenBefore = $this->findPrevious( |
|
| 1803 | + Util\Tokens::$emptyTokens, |
|
| 1804 | + ($stackPtr - 1), |
|
| 1805 | + null, |
|
| 1806 | + true |
|
| 1807 | + ); |
|
| 1808 | + |
|
| 1809 | + if ($this->tokens[$tokenBefore]['code'] === T_FUNCTION) { |
|
| 1810 | + // Function returns a reference. |
|
| 1811 | + return true; |
|
| 1812 | + } |
|
| 1813 | + |
|
| 1814 | + if ($this->tokens[$tokenBefore]['code'] === T_DOUBLE_ARROW) { |
|
| 1815 | + // Inside a foreach loop or array assignment, this is a reference. |
|
| 1816 | + return true; |
|
| 1817 | + } |
|
| 1818 | + |
|
| 1819 | + if ($this->tokens[$tokenBefore]['code'] === T_AS) { |
|
| 1820 | + // Inside a foreach loop, this is a reference. |
|
| 1821 | + return true; |
|
| 1822 | + } |
|
| 1823 | + |
|
| 1824 | + if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) { |
|
| 1825 | + // This is directly after an assignment. It's a reference. Even if |
|
| 1826 | + // it is part of an operation, the other tests will handle it. |
|
| 1827 | + return true; |
|
| 1828 | + } |
|
| 1829 | + |
|
| 1830 | + $tokenAfter = $this->findNext( |
|
| 1831 | + Util\Tokens::$emptyTokens, |
|
| 1832 | + ($stackPtr + 1), |
|
| 1833 | + null, |
|
| 1834 | + true |
|
| 1835 | + ); |
|
| 1836 | + |
|
| 1837 | + if ($this->tokens[$tokenAfter]['code'] === T_NEW) { |
|
| 1838 | + return true; |
|
| 1839 | + } |
|
| 1840 | + |
|
| 1841 | + if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1842 | + $brackets = $this->tokens[$stackPtr]['nested_parenthesis']; |
|
| 1843 | + $lastBracket = array_pop($brackets); |
|
| 1844 | + if (isset($this->tokens[$lastBracket]['parenthesis_owner']) === true) { |
|
| 1845 | + $owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']]; |
|
| 1846 | + if ($owner['code'] === T_FUNCTION |
|
| 1847 | + || $owner['code'] === T_CLOSURE |
|
| 1848 | + ) { |
|
| 1849 | + $params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']); |
|
| 1850 | + foreach ($params as $param) { |
|
| 1851 | + $varToken = $tokenAfter; |
|
| 1852 | + if ($param['variable_length'] === true) { |
|
| 1853 | + $varToken = $this->findNext( |
|
| 1854 | + (Util\Tokens::$emptyTokens + [T_ELLIPSIS]), |
|
| 1855 | + ($stackPtr + 1), |
|
| 1856 | + null, |
|
| 1857 | + true |
|
| 1858 | + ); |
|
| 1859 | + } |
|
| 1860 | + |
|
| 1861 | + if ($param['token'] === $varToken |
|
| 1862 | + && $param['pass_by_reference'] === true |
|
| 1863 | + ) { |
|
| 1864 | + // Function parameter declared to be passed by reference. |
|
| 1865 | + return true; |
|
| 1866 | + } |
|
| 1867 | + } |
|
| 1868 | + }//end if |
|
| 1869 | + } else { |
|
| 1870 | + $prev = false; |
|
| 1871 | + for ($t = ($this->tokens[$lastBracket]['parenthesis_opener'] - 1); $t >= 0; $t--) { |
|
| 1872 | + if ($this->tokens[$t]['code'] !== T_WHITESPACE) { |
|
| 1873 | + $prev = $t; |
|
| 1874 | + break; |
|
| 1875 | + } |
|
| 1876 | + } |
|
| 1877 | + |
|
| 1878 | + if ($prev !== false && $this->tokens[$prev]['code'] === T_USE) { |
|
| 1879 | + // Closure use by reference. |
|
| 1880 | + return true; |
|
| 1881 | + } |
|
| 1882 | + }//end if |
|
| 1883 | + }//end if |
|
| 1884 | + |
|
| 1885 | + // Pass by reference in function calls and assign by reference in arrays. |
|
| 1886 | + if ($this->tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS |
|
| 1887 | + || $this->tokens[$tokenBefore]['code'] === T_COMMA |
|
| 1888 | + || $this->tokens[$tokenBefore]['code'] === T_OPEN_SHORT_ARRAY |
|
| 1889 | + ) { |
|
| 1890 | + if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) { |
|
| 1891 | + return true; |
|
| 1892 | + } else { |
|
| 1893 | + $skip = Util\Tokens::$emptyTokens; |
|
| 1894 | + $skip[] = T_NS_SEPARATOR; |
|
| 1895 | + $skip[] = T_SELF; |
|
| 1896 | + $skip[] = T_PARENT; |
|
| 1897 | + $skip[] = T_STATIC; |
|
| 1898 | + $skip[] = T_STRING; |
|
| 1899 | + $skip[] = T_NAMESPACE; |
|
| 1900 | + $skip[] = T_DOUBLE_COLON; |
|
| 1901 | + |
|
| 1902 | + $nextSignificantAfter = $this->findNext( |
|
| 1903 | + $skip, |
|
| 1904 | + ($stackPtr + 1), |
|
| 1905 | + null, |
|
| 1906 | + true |
|
| 1907 | + ); |
|
| 1908 | + if ($this->tokens[$nextSignificantAfter]['code'] === T_VARIABLE) { |
|
| 1909 | + return true; |
|
| 1910 | + } |
|
| 1911 | + }//end if |
|
| 1912 | + }//end if |
|
| 1913 | + |
|
| 1914 | + return false; |
|
| 1915 | + |
|
| 1916 | + }//end isReference() |
|
| 1917 | + |
|
| 1918 | + |
|
| 1919 | + /** |
|
| 1920 | + * Returns the content of the tokens from the specified start position in |
|
| 1921 | + * the token stack for the specified length. |
|
| 1922 | + * |
|
| 1923 | + * @param int $start The position to start from in the token stack. |
|
| 1924 | + * @param int $length The length of tokens to traverse from the start pos. |
|
| 1925 | + * @param bool $origContent Whether the original content or the tab replaced |
|
| 1926 | + * content should be used. |
|
| 1927 | + * |
|
| 1928 | + * @return string The token contents. |
|
| 1929 | + */ |
|
| 1930 | + public function getTokensAsString($start, $length, $origContent=false) |
|
| 1931 | + { |
|
| 1932 | + if (is_int($start) === false || isset($this->tokens[$start]) === false) { |
|
| 1933 | + throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack'); |
|
| 1934 | + } |
|
| 1935 | + |
|
| 1936 | + if (is_int($length) === false || $length <= 0) { |
|
| 1937 | + return ''; |
|
| 1938 | + } |
|
| 1939 | + |
|
| 1940 | + $str = ''; |
|
| 1941 | + $end = ($start + $length); |
|
| 1942 | + if ($end > $this->numTokens) { |
|
| 1943 | + $end = $this->numTokens; |
|
| 1944 | + } |
|
| 1945 | + |
|
| 1946 | + for ($i = $start; $i < $end; $i++) { |
|
| 1947 | + // If tabs are being converted to spaces by the tokeniser, the |
|
| 1948 | + // original content should be used instead of the converted content. |
|
| 1949 | + if ($origContent === true && isset($this->tokens[$i]['orig_content']) === true) { |
|
| 1950 | + $str .= $this->tokens[$i]['orig_content']; |
|
| 1951 | + } else { |
|
| 1952 | + $str .= $this->tokens[$i]['content']; |
|
| 1953 | + } |
|
| 1954 | + } |
|
| 1955 | + |
|
| 1956 | + return $str; |
|
| 1957 | + |
|
| 1958 | + }//end getTokensAsString() |
|
| 1959 | + |
|
| 1960 | + |
|
| 1961 | + /** |
|
| 1962 | + * Returns the position of the previous specified token(s). |
|
| 1963 | + * |
|
| 1964 | + * If a value is specified, the previous token of the specified type(s) |
|
| 1965 | + * containing the specified value will be returned. |
|
| 1966 | + * |
|
| 1967 | + * Returns false if no token can be found. |
|
| 1968 | + * |
|
| 1969 | + * @param int|string|array $types The type(s) of tokens to search for. |
|
| 1970 | + * @param int $start The position to start searching from in the |
|
| 1971 | + * token stack. |
|
| 1972 | + * @param int $end The end position to fail if no token is found. |
|
| 1973 | + * if not specified or null, end will default to |
|
| 1974 | + * the start of the token stack. |
|
| 1975 | + * @param bool $exclude If true, find the previous token that is NOT of |
|
| 1976 | + * the types specified in $types. |
|
| 1977 | + * @param string $value The value that the token(s) must be equal to. |
|
| 1978 | + * If value is omitted, tokens with any value will |
|
| 1979 | + * be returned. |
|
| 1980 | + * @param bool $local If true, tokens outside the current statement |
|
| 1981 | + * will not be checked. IE. checking will stop |
|
| 1982 | + * at the previous semi-colon found. |
|
| 1983 | + * |
|
| 1984 | + * @return int|bool |
|
| 1985 | + * @see findNext() |
|
| 1986 | + */ |
|
| 1987 | + public function findPrevious( |
|
| 1988 | + $types, |
|
| 1989 | + $start, |
|
| 1990 | + $end=null, |
|
| 1991 | + $exclude=false, |
|
| 1992 | + $value=null, |
|
| 1993 | + $local=false |
|
| 1994 | + ) { |
|
| 1995 | + $types = (array) $types; |
|
| 1996 | + |
|
| 1997 | + if ($end === null) { |
|
| 1998 | + $end = 0; |
|
| 1999 | + } |
|
| 2000 | + |
|
| 2001 | + for ($i = $start; $i >= $end; $i--) { |
|
| 2002 | + $found = (bool) $exclude; |
|
| 2003 | + foreach ($types as $type) { |
|
| 2004 | + if ($this->tokens[$i]['code'] === $type) { |
|
| 2005 | + $found = !$exclude; |
|
| 2006 | + break; |
|
| 2007 | + } |
|
| 2008 | + } |
|
| 2009 | + |
|
| 2010 | + if ($found === true) { |
|
| 2011 | + if ($value === null) { |
|
| 2012 | + return $i; |
|
| 2013 | + } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2014 | + return $i; |
|
| 2015 | + } |
|
| 2016 | + } |
|
| 2017 | + |
|
| 2018 | + if ($local === true) { |
|
| 2019 | + if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2020 | + && $i === $this->tokens[$i]['scope_closer'] |
|
| 2021 | + ) { |
|
| 2022 | + $i = $this->tokens[$i]['scope_opener']; |
|
| 2023 | + } else if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2024 | + && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2025 | + ) { |
|
| 2026 | + $i = $this->tokens[$i]['bracket_opener']; |
|
| 2027 | + } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2028 | + && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2029 | + ) { |
|
| 2030 | + $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2031 | + } else if ($this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2032 | + break; |
|
| 2033 | + } |
|
| 2034 | + } |
|
| 2035 | + }//end for |
|
| 2036 | + |
|
| 2037 | + return false; |
|
| 2038 | + |
|
| 2039 | + }//end findPrevious() |
|
| 2040 | + |
|
| 2041 | + |
|
| 2042 | + /** |
|
| 2043 | + * Returns the position of the next specified token(s). |
|
| 2044 | + * |
|
| 2045 | + * If a value is specified, the next token of the specified type(s) |
|
| 2046 | + * containing the specified value will be returned. |
|
| 2047 | + * |
|
| 2048 | + * Returns false if no token can be found. |
|
| 2049 | + * |
|
| 2050 | + * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2051 | + * @param int $start The position to start searching from in the |
|
| 2052 | + * token stack. |
|
| 2053 | + * @param int $end The end position to fail if no token is found. |
|
| 2054 | + * if not specified or null, end will default to |
|
| 2055 | + * the end of the token stack. |
|
| 2056 | + * @param bool $exclude If true, find the next token that is NOT of |
|
| 2057 | + * a type specified in $types. |
|
| 2058 | + * @param string $value The value that the token(s) must be equal to. |
|
| 2059 | + * If value is omitted, tokens with any value will |
|
| 2060 | + * be returned. |
|
| 2061 | + * @param bool $local If true, tokens outside the current statement |
|
| 2062 | + * will not be checked. i.e., checking will stop |
|
| 2063 | + * at the next semi-colon found. |
|
| 2064 | + * |
|
| 2065 | + * @return int|bool |
|
| 2066 | + * @see findPrevious() |
|
| 2067 | + */ |
|
| 2068 | + public function findNext( |
|
| 2069 | + $types, |
|
| 2070 | + $start, |
|
| 2071 | + $end=null, |
|
| 2072 | + $exclude=false, |
|
| 2073 | + $value=null, |
|
| 2074 | + $local=false |
|
| 2075 | + ) { |
|
| 2076 | + $types = (array) $types; |
|
| 2077 | + |
|
| 2078 | + if ($end === null || $end > $this->numTokens) { |
|
| 2079 | + $end = $this->numTokens; |
|
| 2080 | + } |
|
| 2081 | + |
|
| 2082 | + for ($i = $start; $i < $end; $i++) { |
|
| 2083 | + $found = (bool) $exclude; |
|
| 2084 | + foreach ($types as $type) { |
|
| 2085 | + if ($this->tokens[$i]['code'] === $type) { |
|
| 2086 | + $found = !$exclude; |
|
| 2087 | + break; |
|
| 2088 | + } |
|
| 2089 | + } |
|
| 2090 | + |
|
| 2091 | + if ($found === true) { |
|
| 2092 | + if ($value === null) { |
|
| 2093 | + return $i; |
|
| 2094 | + } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2095 | + return $i; |
|
| 2096 | + } |
|
| 2097 | + } |
|
| 2098 | + |
|
| 2099 | + if ($local === true && $this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2100 | + break; |
|
| 2101 | + } |
|
| 2102 | + }//end for |
|
| 2103 | + |
|
| 2104 | + return false; |
|
| 2105 | + |
|
| 2106 | + }//end findNext() |
|
| 2107 | + |
|
| 2108 | + |
|
| 2109 | + /** |
|
| 2110 | + * Returns the position of the first non-whitespace token in a statement. |
|
| 2111 | + * |
|
| 2112 | + * @param int $start The position to start searching from in the token stack. |
|
| 2113 | + * @param int|array $ignore Token types that should not be considered stop points. |
|
| 2114 | + * |
|
| 2115 | + * @return int |
|
| 2116 | + */ |
|
| 2117 | + public function findStartOfStatement($start, $ignore=null) |
|
| 2118 | + { |
|
| 2119 | + $endTokens = Util\Tokens::$blockOpeners; |
|
| 2120 | + |
|
| 2121 | + $endTokens[T_COLON] = true; |
|
| 2122 | + $endTokens[T_COMMA] = true; |
|
| 2123 | + $endTokens[T_DOUBLE_ARROW] = true; |
|
| 2124 | + $endTokens[T_SEMICOLON] = true; |
|
| 2125 | + $endTokens[T_OPEN_TAG] = true; |
|
| 2126 | + $endTokens[T_CLOSE_TAG] = true; |
|
| 2127 | + $endTokens[T_OPEN_SHORT_ARRAY] = true; |
|
| 2128 | + |
|
| 2129 | + if ($ignore !== null) { |
|
| 2130 | + $ignore = (array) $ignore; |
|
| 2131 | + foreach ($ignore as $code) { |
|
| 2132 | + unset($endTokens[$code]); |
|
| 2133 | + } |
|
| 2134 | + } |
|
| 2135 | + |
|
| 2136 | + $lastNotEmpty = $start; |
|
| 2137 | + |
|
| 2138 | + for ($i = $start; $i >= 0; $i--) { |
|
| 2139 | + if (isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2140 | + // Found the end of the previous statement. |
|
| 2141 | + return $lastNotEmpty; |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2145 | + && $i === $this->tokens[$i]['scope_closer'] |
|
| 2146 | + ) { |
|
| 2147 | + // Found the end of the previous scope block. |
|
| 2148 | + return $lastNotEmpty; |
|
| 2149 | + } |
|
| 2150 | + |
|
| 2151 | + // Skip nested statements. |
|
| 2152 | + if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2153 | + && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2154 | + ) { |
|
| 2155 | + $i = $this->tokens[$i]['bracket_opener']; |
|
| 2156 | + } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2157 | + && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2158 | + ) { |
|
| 2159 | + $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2160 | + } |
|
| 2161 | + |
|
| 2162 | + if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2163 | + $lastNotEmpty = $i; |
|
| 2164 | + } |
|
| 2165 | + }//end for |
|
| 2166 | + |
|
| 2167 | + return 0; |
|
| 2168 | + |
|
| 2169 | + }//end findStartOfStatement() |
|
| 2170 | + |
|
| 2171 | + |
|
| 2172 | + /** |
|
| 2173 | + * Returns the position of the last non-whitespace token in a statement. |
|
| 2174 | + * |
|
| 2175 | + * @param int $start The position to start searching from in the token stack. |
|
| 2176 | + * @param int|array $ignore Token types that should not be considered stop points. |
|
| 2177 | + * |
|
| 2178 | + * @return int |
|
| 2179 | + */ |
|
| 2180 | + public function findEndOfStatement($start, $ignore=null) |
|
| 2181 | + { |
|
| 2182 | + $endTokens = [ |
|
| 2183 | + T_COLON => true, |
|
| 2184 | + T_COMMA => true, |
|
| 2185 | + T_DOUBLE_ARROW => true, |
|
| 2186 | + T_SEMICOLON => true, |
|
| 2187 | + T_CLOSE_PARENTHESIS => true, |
|
| 2188 | + T_CLOSE_SQUARE_BRACKET => true, |
|
| 2189 | + T_CLOSE_CURLY_BRACKET => true, |
|
| 2190 | + T_CLOSE_SHORT_ARRAY => true, |
|
| 2191 | + T_OPEN_TAG => true, |
|
| 2192 | + T_CLOSE_TAG => true, |
|
| 2193 | + ]; |
|
| 2194 | + |
|
| 2195 | + if ($ignore !== null) { |
|
| 2196 | + $ignore = (array) $ignore; |
|
| 2197 | + foreach ($ignore as $code) { |
|
| 2198 | + unset($endTokens[$code]); |
|
| 2199 | + } |
|
| 2200 | + } |
|
| 2201 | + |
|
| 2202 | + $lastNotEmpty = $start; |
|
| 2203 | + |
|
| 2204 | + for ($i = $start; $i < $this->numTokens; $i++) { |
|
| 2205 | + if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2206 | + // Found the end of the statement. |
|
| 2207 | + if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 2208 | + || $this->tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET |
|
| 2209 | + || $this->tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET |
|
| 2210 | + || $this->tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 2211 | + || $this->tokens[$i]['code'] === T_OPEN_TAG |
|
| 2212 | + || $this->tokens[$i]['code'] === T_CLOSE_TAG |
|
| 2213 | + ) { |
|
| 2214 | + return $lastNotEmpty; |
|
| 2215 | + } |
|
| 2216 | + |
|
| 2217 | + return $i; |
|
| 2218 | + } |
|
| 2219 | + |
|
| 2220 | + // Skip nested statements. |
|
| 2221 | + if (isset($this->tokens[$i]['scope_closer']) === true |
|
| 2222 | + && ($i === $this->tokens[$i]['scope_opener'] |
|
| 2223 | + || $i === $this->tokens[$i]['scope_condition']) |
|
| 2224 | + ) { |
|
| 2225 | + if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) { |
|
| 2226 | + return $this->tokens[$i]['scope_closer']; |
|
| 2227 | + } |
|
| 2228 | + |
|
| 2229 | + $i = $this->tokens[$i]['scope_closer']; |
|
| 2230 | + } else if (isset($this->tokens[$i]['bracket_closer']) === true |
|
| 2231 | + && $i === $this->tokens[$i]['bracket_opener'] |
|
| 2232 | + ) { |
|
| 2233 | + $i = $this->tokens[$i]['bracket_closer']; |
|
| 2234 | + } else if (isset($this->tokens[$i]['parenthesis_closer']) === true |
|
| 2235 | + && $i === $this->tokens[$i]['parenthesis_opener'] |
|
| 2236 | + ) { |
|
| 2237 | + $i = $this->tokens[$i]['parenthesis_closer']; |
|
| 2238 | + } |
|
| 2239 | + |
|
| 2240 | + if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2241 | + $lastNotEmpty = $i; |
|
| 2242 | + } |
|
| 2243 | + }//end for |
|
| 2244 | + |
|
| 2245 | + return ($this->numTokens - 1); |
|
| 2246 | + |
|
| 2247 | + }//end findEndOfStatement() |
|
| 2248 | + |
|
| 2249 | + |
|
| 2250 | + /** |
|
| 2251 | + * Returns the position of the first token on a line, matching given type. |
|
| 2252 | + * |
|
| 2253 | + * Returns false if no token can be found. |
|
| 2254 | + * |
|
| 2255 | + * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2256 | + * @param int $start The position to start searching from in the |
|
| 2257 | + * token stack. The first token matching on |
|
| 2258 | + * this line before this token will be returned. |
|
| 2259 | + * @param bool $exclude If true, find the token that is NOT of |
|
| 2260 | + * the types specified in $types. |
|
| 2261 | + * @param string $value The value that the token must be equal to. |
|
| 2262 | + * If value is omitted, tokens with any value will |
|
| 2263 | + * be returned. |
|
| 2264 | + * |
|
| 2265 | + * @return int | bool |
|
| 2266 | + */ |
|
| 2267 | + public function findFirstOnLine($types, $start, $exclude=false, $value=null) |
|
| 2268 | + { |
|
| 2269 | + if (is_array($types) === false) { |
|
| 2270 | + $types = [$types]; |
|
| 2271 | + } |
|
| 2272 | + |
|
| 2273 | + $foundToken = false; |
|
| 2274 | + |
|
| 2275 | + for ($i = $start; $i >= 0; $i--) { |
|
| 2276 | + if ($this->tokens[$i]['line'] < $this->tokens[$start]['line']) { |
|
| 2277 | + break; |
|
| 2278 | + } |
|
| 2279 | + |
|
| 2280 | + $found = $exclude; |
|
| 2281 | + foreach ($types as $type) { |
|
| 2282 | + if ($exclude === false) { |
|
| 2283 | + if ($this->tokens[$i]['code'] === $type) { |
|
| 2284 | + $found = true; |
|
| 2285 | + break; |
|
| 2286 | + } |
|
| 2287 | + } else { |
|
| 2288 | + if ($this->tokens[$i]['code'] === $type) { |
|
| 2289 | + $found = false; |
|
| 2290 | + break; |
|
| 2291 | + } |
|
| 2292 | + } |
|
| 2293 | + } |
|
| 2294 | + |
|
| 2295 | + if ($found === true) { |
|
| 2296 | + if ($value === null) { |
|
| 2297 | + $foundToken = $i; |
|
| 2298 | + } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2299 | + $foundToken = $i; |
|
| 2300 | + } |
|
| 2301 | + } |
|
| 2302 | + }//end for |
|
| 2303 | + |
|
| 2304 | + return $foundToken; |
|
| 2305 | + |
|
| 2306 | + }//end findFirstOnLine() |
|
| 2307 | + |
|
| 2308 | + |
|
| 2309 | + /** |
|
| 2310 | + * Determine if the passed token has a condition of one of the passed types. |
|
| 2311 | + * |
|
| 2312 | + * @param int $stackPtr The position of the token we are checking. |
|
| 2313 | + * @param int|string|array $types The type(s) of tokens to search for. |
|
| 2314 | + * |
|
| 2315 | + * @return boolean |
|
| 2316 | + */ |
|
| 2317 | + public function hasCondition($stackPtr, $types) |
|
| 2318 | + { |
|
| 2319 | + // Check for the existence of the token. |
|
| 2320 | + if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2321 | + return false; |
|
| 2322 | + } |
|
| 2323 | + |
|
| 2324 | + // Make sure the token has conditions. |
|
| 2325 | + if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2326 | + return false; |
|
| 2327 | + } |
|
| 2328 | + |
|
| 2329 | + $types = (array) $types; |
|
| 2330 | + $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2331 | + |
|
| 2332 | + foreach ($types as $type) { |
|
| 2333 | + if (in_array($type, $conditions, true) === true) { |
|
| 2334 | + // We found a token with the required type. |
|
| 2335 | + return true; |
|
| 2336 | + } |
|
| 2337 | + } |
|
| 2338 | + |
|
| 2339 | + return false; |
|
| 2340 | + |
|
| 2341 | + }//end hasCondition() |
|
| 2342 | + |
|
| 2343 | + |
|
| 2344 | + /** |
|
| 2345 | + * Return the position of the condition for the passed token. |
|
| 2346 | + * |
|
| 2347 | + * Returns FALSE if the token does not have the condition. |
|
| 2348 | + * |
|
| 2349 | + * @param int $stackPtr The position of the token we are checking. |
|
| 2350 | + * @param int|string $type The type of token to search for. |
|
| 2351 | + * |
|
| 2352 | + * @return int |
|
| 2353 | + */ |
|
| 2354 | + public function getCondition($stackPtr, $type) |
|
| 2355 | + { |
|
| 2356 | + // Check for the existence of the token. |
|
| 2357 | + if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2358 | + return false; |
|
| 2359 | + } |
|
| 2360 | + |
|
| 2361 | + // Make sure the token has conditions. |
|
| 2362 | + if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2363 | + return false; |
|
| 2364 | + } |
|
| 2365 | + |
|
| 2366 | + $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2367 | + foreach ($conditions as $token => $condition) { |
|
| 2368 | + if ($condition === $type) { |
|
| 2369 | + return $token; |
|
| 2370 | + } |
|
| 2371 | + } |
|
| 2372 | + |
|
| 2373 | + return false; |
|
| 2374 | + |
|
| 2375 | + }//end getCondition() |
|
| 2376 | + |
|
| 2377 | + |
|
| 2378 | + /** |
|
| 2379 | + * Returns the name of the class that the specified class extends. |
|
| 2380 | + * (works for classes, anonymous classes and interfaces) |
|
| 2381 | + * |
|
| 2382 | + * Returns FALSE on error or if there is no extended class name. |
|
| 2383 | + * |
|
| 2384 | + * @param int $stackPtr The stack position of the class. |
|
| 2385 | + * |
|
| 2386 | + * @return string|false |
|
| 2387 | + */ |
|
| 2388 | + public function findExtendedClassName($stackPtr) |
|
| 2389 | + { |
|
| 2390 | + // Check for the existence of the token. |
|
| 2391 | + if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2392 | + return false; |
|
| 2393 | + } |
|
| 2394 | + |
|
| 2395 | + if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2396 | + && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2397 | + && $this->tokens[$stackPtr]['code'] !== T_INTERFACE |
|
| 2398 | + ) { |
|
| 2399 | + return false; |
|
| 2400 | + } |
|
| 2401 | + |
|
| 2402 | + if (isset($this->tokens[$stackPtr]['scope_opener']) === false) { |
|
| 2403 | + return false; |
|
| 2404 | + } |
|
| 2405 | + |
|
| 2406 | + $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2407 | + $extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex); |
|
| 2408 | + if (false === $extendsIndex) { |
|
| 2409 | + return false; |
|
| 2410 | + } |
|
| 2411 | + |
|
| 2412 | + $find = [ |
|
| 2413 | + T_NS_SEPARATOR, |
|
| 2414 | + T_STRING, |
|
| 2415 | + T_WHITESPACE, |
|
| 2416 | + ]; |
|
| 2417 | + |
|
| 2418 | + $end = $this->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2419 | + $name = $this->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1)); |
|
| 2420 | + $name = trim($name); |
|
| 2421 | + |
|
| 2422 | + if ($name === '') { |
|
| 2423 | + return false; |
|
| 2424 | + } |
|
| 2425 | + |
|
| 2426 | + return $name; |
|
| 2427 | + |
|
| 2428 | + }//end findExtendedClassName() |
|
| 2429 | + |
|
| 2430 | + |
|
| 2431 | + /** |
|
| 2432 | + * Returns the names of the interfaces that the specified class implements. |
|
| 2433 | + * |
|
| 2434 | + * Returns FALSE on error or if there are no implemented interface names. |
|
| 2435 | + * |
|
| 2436 | + * @param int $stackPtr The stack position of the class. |
|
| 2437 | + * |
|
| 2438 | + * @return array|false |
|
| 2439 | + */ |
|
| 2440 | + public function findImplementedInterfaceNames($stackPtr) |
|
| 2441 | + { |
|
| 2442 | + // Check for the existence of the token. |
|
| 2443 | + if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2444 | + return false; |
|
| 2445 | + } |
|
| 2446 | + |
|
| 2447 | + if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2448 | + && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2449 | + ) { |
|
| 2450 | + return false; |
|
| 2451 | + } |
|
| 2452 | + |
|
| 2453 | + if (isset($this->tokens[$stackPtr]['scope_closer']) === false) { |
|
| 2454 | + return false; |
|
| 2455 | + } |
|
| 2456 | + |
|
| 2457 | + $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2458 | + $implementsIndex = $this->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
|
| 2459 | + if ($implementsIndex === false) { |
|
| 2460 | + return false; |
|
| 2461 | + } |
|
| 2462 | + |
|
| 2463 | + $find = [ |
|
| 2464 | + T_NS_SEPARATOR, |
|
| 2465 | + T_STRING, |
|
| 2466 | + T_WHITESPACE, |
|
| 2467 | + T_COMMA, |
|
| 2468 | + ]; |
|
| 2469 | + |
|
| 2470 | + $end = $this->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2471 | + $name = $this->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
|
| 2472 | + $name = trim($name); |
|
| 2473 | + |
|
| 2474 | + if ($name === '') { |
|
| 2475 | + return false; |
|
| 2476 | + } else { |
|
| 2477 | + $names = explode(',', $name); |
|
| 2478 | + $names = array_map('trim', $names); |
|
| 2479 | + return $names; |
|
| 2480 | + } |
|
| 2481 | + |
|
| 2482 | + }//end findImplementedInterfaceNames() |
|
| 2483 | 2483 | |
| 2484 | 2484 | |
| 2485 | 2485 | }//end class |
@@ -1324,122 +1324,122 @@ discard block |
||
| 1324 | 1324 | } |
| 1325 | 1325 | |
| 1326 | 1326 | switch ($this->tokens[$i]['code']) { |
| 1327 | - case T_BITWISE_AND: |
|
| 1328 | - if ($defaultStart === null) { |
|
| 1329 | - $passByReference = true; |
|
| 1330 | - } |
|
| 1331 | - break; |
|
| 1332 | - case T_VARIABLE: |
|
| 1333 | - $currVar = $i; |
|
| 1334 | - break; |
|
| 1335 | - case T_ELLIPSIS: |
|
| 1336 | - $variableLength = true; |
|
| 1337 | - break; |
|
| 1338 | - case T_CALLABLE: |
|
| 1339 | - if ($typeHintToken === false) { |
|
| 1340 | - $typeHintToken = $i; |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1344 | - break; |
|
| 1345 | - case T_SELF: |
|
| 1346 | - case T_PARENT: |
|
| 1347 | - case T_STATIC: |
|
| 1348 | - // Self and parent are valid, static invalid, but was probably intended as type hint. |
|
| 1349 | - if (isset($defaultStart) === false) { |
|
| 1350 | - if ($typeHintToken === false) { |
|
| 1351 | - $typeHintToken = $i; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1355 | - } |
|
| 1356 | - break; |
|
| 1357 | - case T_STRING: |
|
| 1358 | - // This is a string, so it may be a type hint, but it could |
|
| 1359 | - // also be a constant used as a default value. |
|
| 1360 | - $prevComma = false; |
|
| 1361 | - for ($t = $i; $t >= $opener; $t--) { |
|
| 1362 | - if ($this->tokens[$t]['code'] === T_COMMA) { |
|
| 1363 | - $prevComma = $t; |
|
| 1364 | - break; |
|
| 1365 | - } |
|
| 1366 | - } |
|
| 1367 | - |
|
| 1368 | - if ($prevComma !== false) { |
|
| 1369 | - $nextEquals = false; |
|
| 1370 | - for ($t = $prevComma; $t < $i; $t++) { |
|
| 1371 | - if ($this->tokens[$t]['code'] === T_EQUAL) { |
|
| 1372 | - $nextEquals = $t; |
|
| 1373 | - break; |
|
| 1374 | - } |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - if ($nextEquals !== false) { |
|
| 1378 | - break; |
|
| 1379 | - } |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - if ($defaultStart === null) { |
|
| 1383 | - if ($typeHintToken === false) { |
|
| 1384 | - $typeHintToken = $i; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1388 | - } |
|
| 1389 | - break; |
|
| 1390 | - case T_NS_SEPARATOR: |
|
| 1391 | - // Part of a type hint or default value. |
|
| 1392 | - if ($defaultStart === null) { |
|
| 1393 | - if ($typeHintToken === false) { |
|
| 1394 | - $typeHintToken = $i; |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1398 | - } |
|
| 1399 | - break; |
|
| 1400 | - case T_NULLABLE: |
|
| 1401 | - if ($defaultStart === null) { |
|
| 1402 | - $nullableType = true; |
|
| 1403 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1404 | - } |
|
| 1405 | - break; |
|
| 1406 | - case T_CLOSE_PARENTHESIS: |
|
| 1407 | - case T_COMMA: |
|
| 1408 | - // If it's null, then there must be no parameters for this |
|
| 1409 | - // method. |
|
| 1410 | - if ($currVar === null) { |
|
| 1411 | - continue 2; |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - $vars[$paramCount] = []; |
|
| 1415 | - $vars[$paramCount]['token'] = $currVar; |
|
| 1416 | - $vars[$paramCount]['name'] = $this->tokens[$currVar]['content']; |
|
| 1417 | - $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart))); |
|
| 1418 | - |
|
| 1419 | - if ($defaultStart !== null) { |
|
| 1420 | - $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart))); |
|
| 1421 | - } |
|
| 1422 | - |
|
| 1423 | - $vars[$paramCount]['pass_by_reference'] = $passByReference; |
|
| 1424 | - $vars[$paramCount]['variable_length'] = $variableLength; |
|
| 1425 | - $vars[$paramCount]['type_hint'] = $typeHint; |
|
| 1426 | - $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
|
| 1427 | - $vars[$paramCount]['nullable_type'] = $nullableType; |
|
| 1428 | - |
|
| 1429 | - // Reset the vars, as we are about to process the next parameter. |
|
| 1430 | - $defaultStart = null; |
|
| 1431 | - $paramStart = ($i + 1); |
|
| 1432 | - $passByReference = false; |
|
| 1433 | - $variableLength = false; |
|
| 1434 | - $typeHint = ''; |
|
| 1435 | - $typeHintToken = false; |
|
| 1436 | - $nullableType = false; |
|
| 1437 | - |
|
| 1438 | - $paramCount++; |
|
| 1439 | - break; |
|
| 1440 | - case T_EQUAL: |
|
| 1441 | - $defaultStart = ($i + 1); |
|
| 1442 | - break; |
|
| 1327 | + case T_BITWISE_AND: |
|
| 1328 | + if ($defaultStart === null) { |
|
| 1329 | + $passByReference = true; |
|
| 1330 | + } |
|
| 1331 | + break; |
|
| 1332 | + case T_VARIABLE: |
|
| 1333 | + $currVar = $i; |
|
| 1334 | + break; |
|
| 1335 | + case T_ELLIPSIS: |
|
| 1336 | + $variableLength = true; |
|
| 1337 | + break; |
|
| 1338 | + case T_CALLABLE: |
|
| 1339 | + if ($typeHintToken === false) { |
|
| 1340 | + $typeHintToken = $i; |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1344 | + break; |
|
| 1345 | + case T_SELF: |
|
| 1346 | + case T_PARENT: |
|
| 1347 | + case T_STATIC: |
|
| 1348 | + // Self and parent are valid, static invalid, but was probably intended as type hint. |
|
| 1349 | + if (isset($defaultStart) === false) { |
|
| 1350 | + if ($typeHintToken === false) { |
|
| 1351 | + $typeHintToken = $i; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1355 | + } |
|
| 1356 | + break; |
|
| 1357 | + case T_STRING: |
|
| 1358 | + // This is a string, so it may be a type hint, but it could |
|
| 1359 | + // also be a constant used as a default value. |
|
| 1360 | + $prevComma = false; |
|
| 1361 | + for ($t = $i; $t >= $opener; $t--) { |
|
| 1362 | + if ($this->tokens[$t]['code'] === T_COMMA) { |
|
| 1363 | + $prevComma = $t; |
|
| 1364 | + break; |
|
| 1365 | + } |
|
| 1366 | + } |
|
| 1367 | + |
|
| 1368 | + if ($prevComma !== false) { |
|
| 1369 | + $nextEquals = false; |
|
| 1370 | + for ($t = $prevComma; $t < $i; $t++) { |
|
| 1371 | + if ($this->tokens[$t]['code'] === T_EQUAL) { |
|
| 1372 | + $nextEquals = $t; |
|
| 1373 | + break; |
|
| 1374 | + } |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + if ($nextEquals !== false) { |
|
| 1378 | + break; |
|
| 1379 | + } |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + if ($defaultStart === null) { |
|
| 1383 | + if ($typeHintToken === false) { |
|
| 1384 | + $typeHintToken = $i; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1388 | + } |
|
| 1389 | + break; |
|
| 1390 | + case T_NS_SEPARATOR: |
|
| 1391 | + // Part of a type hint or default value. |
|
| 1392 | + if ($defaultStart === null) { |
|
| 1393 | + if ($typeHintToken === false) { |
|
| 1394 | + $typeHintToken = $i; |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1398 | + } |
|
| 1399 | + break; |
|
| 1400 | + case T_NULLABLE: |
|
| 1401 | + if ($defaultStart === null) { |
|
| 1402 | + $nullableType = true; |
|
| 1403 | + $typeHint .= $this->tokens[$i]['content']; |
|
| 1404 | + } |
|
| 1405 | + break; |
|
| 1406 | + case T_CLOSE_PARENTHESIS: |
|
| 1407 | + case T_COMMA: |
|
| 1408 | + // If it's null, then there must be no parameters for this |
|
| 1409 | + // method. |
|
| 1410 | + if ($currVar === null) { |
|
| 1411 | + continue 2; |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + $vars[$paramCount] = []; |
|
| 1415 | + $vars[$paramCount]['token'] = $currVar; |
|
| 1416 | + $vars[$paramCount]['name'] = $this->tokens[$currVar]['content']; |
|
| 1417 | + $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart))); |
|
| 1418 | + |
|
| 1419 | + if ($defaultStart !== null) { |
|
| 1420 | + $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart))); |
|
| 1421 | + } |
|
| 1422 | + |
|
| 1423 | + $vars[$paramCount]['pass_by_reference'] = $passByReference; |
|
| 1424 | + $vars[$paramCount]['variable_length'] = $variableLength; |
|
| 1425 | + $vars[$paramCount]['type_hint'] = $typeHint; |
|
| 1426 | + $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
|
| 1427 | + $vars[$paramCount]['nullable_type'] = $nullableType; |
|
| 1428 | + |
|
| 1429 | + // Reset the vars, as we are about to process the next parameter. |
|
| 1430 | + $defaultStart = null; |
|
| 1431 | + $paramStart = ($i + 1); |
|
| 1432 | + $passByReference = false; |
|
| 1433 | + $variableLength = false; |
|
| 1434 | + $typeHint = ''; |
|
| 1435 | + $typeHintToken = false; |
|
| 1436 | + $nullableType = false; |
|
| 1437 | + |
|
| 1438 | + $paramCount++; |
|
| 1439 | + break; |
|
| 1440 | + case T_EQUAL: |
|
| 1441 | + $defaultStart = ($i + 1); |
|
| 1442 | + break; |
|
| 1443 | 1443 | }//end switch |
| 1444 | 1444 | }//end for |
| 1445 | 1445 | |
@@ -1515,27 +1515,27 @@ discard block |
||
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | 1517 | switch ($this->tokens[$i]['code']) { |
| 1518 | - case T_PUBLIC: |
|
| 1519 | - $scope = 'public'; |
|
| 1520 | - $scopeSpecified = true; |
|
| 1521 | - break; |
|
| 1522 | - case T_PRIVATE: |
|
| 1523 | - $scope = 'private'; |
|
| 1524 | - $scopeSpecified = true; |
|
| 1525 | - break; |
|
| 1526 | - case T_PROTECTED: |
|
| 1527 | - $scope = 'protected'; |
|
| 1528 | - $scopeSpecified = true; |
|
| 1529 | - break; |
|
| 1530 | - case T_ABSTRACT: |
|
| 1531 | - $isAbstract = true; |
|
| 1532 | - break; |
|
| 1533 | - case T_FINAL: |
|
| 1534 | - $isFinal = true; |
|
| 1535 | - break; |
|
| 1536 | - case T_STATIC: |
|
| 1537 | - $isStatic = true; |
|
| 1538 | - break; |
|
| 1518 | + case T_PUBLIC: |
|
| 1519 | + $scope = 'public'; |
|
| 1520 | + $scopeSpecified = true; |
|
| 1521 | + break; |
|
| 1522 | + case T_PRIVATE: |
|
| 1523 | + $scope = 'private'; |
|
| 1524 | + $scopeSpecified = true; |
|
| 1525 | + break; |
|
| 1526 | + case T_PROTECTED: |
|
| 1527 | + $scope = 'protected'; |
|
| 1528 | + $scopeSpecified = true; |
|
| 1529 | + break; |
|
| 1530 | + case T_ABSTRACT: |
|
| 1531 | + $isAbstract = true; |
|
| 1532 | + break; |
|
| 1533 | + case T_FINAL: |
|
| 1534 | + $isFinal = true; |
|
| 1535 | + break; |
|
| 1536 | + case T_STATIC: |
|
| 1537 | + $isStatic = true; |
|
| 1538 | + break; |
|
| 1539 | 1539 | }//end switch |
| 1540 | 1540 | }//end for |
| 1541 | 1541 | |
@@ -1697,21 +1697,21 @@ discard block |
||
| 1697 | 1697 | } |
| 1698 | 1698 | |
| 1699 | 1699 | switch ($this->tokens[$i]['code']) { |
| 1700 | - case T_PUBLIC: |
|
| 1701 | - $scope = 'public'; |
|
| 1702 | - $scopeSpecified = true; |
|
| 1703 | - break; |
|
| 1704 | - case T_PRIVATE: |
|
| 1705 | - $scope = 'private'; |
|
| 1706 | - $scopeSpecified = true; |
|
| 1707 | - break; |
|
| 1708 | - case T_PROTECTED: |
|
| 1709 | - $scope = 'protected'; |
|
| 1710 | - $scopeSpecified = true; |
|
| 1711 | - break; |
|
| 1712 | - case T_STATIC: |
|
| 1713 | - $isStatic = true; |
|
| 1714 | - break; |
|
| 1700 | + case T_PUBLIC: |
|
| 1701 | + $scope = 'public'; |
|
| 1702 | + $scopeSpecified = true; |
|
| 1703 | + break; |
|
| 1704 | + case T_PRIVATE: |
|
| 1705 | + $scope = 'private'; |
|
| 1706 | + $scopeSpecified = true; |
|
| 1707 | + break; |
|
| 1708 | + case T_PROTECTED: |
|
| 1709 | + $scope = 'protected'; |
|
| 1710 | + $scopeSpecified = true; |
|
| 1711 | + break; |
|
| 1712 | + case T_STATIC: |
|
| 1713 | + $isStatic = true; |
|
| 1714 | + break; |
|
| 1715 | 1715 | } |
| 1716 | 1716 | }//end for |
| 1717 | 1717 | |
@@ -1765,13 +1765,13 @@ discard block |
||
| 1765 | 1765 | } |
| 1766 | 1766 | |
| 1767 | 1767 | switch ($this->tokens[$i]['code']) { |
| 1768 | - case T_ABSTRACT: |
|
| 1769 | - $isAbstract = true; |
|
| 1770 | - break; |
|
| 1768 | + case T_ABSTRACT: |
|
| 1769 | + $isAbstract = true; |
|
| 1770 | + break; |
|
| 1771 | 1771 | |
| 1772 | - case T_FINAL: |
|
| 1773 | - $isFinal = true; |
|
| 1774 | - break; |
|
| 1772 | + case T_FINAL: |
|
| 1773 | + $isFinal = true; |
|
| 1774 | + break; |
|
| 1775 | 1775 | } |
| 1776 | 1776 | }//end for |
| 1777 | 1777 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @var array |
| 108 | 108 | */ |
| 109 | - protected $tokens = []; |
|
| 109 | + protected $tokens = [ ]; |
|
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | 112 | * The errors raised from sniffs. |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | * @var array |
| 115 | 115 | * @see getErrors() |
| 116 | 116 | */ |
| 117 | - protected $errors = []; |
|
| 117 | + protected $errors = [ ]; |
|
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * The warnings raised from sniffs. |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * @var array |
| 123 | 123 | * @see getWarnings() |
| 124 | 124 | */ |
| 125 | - protected $warnings = []; |
|
| 125 | + protected $warnings = [ ]; |
|
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | 128 | * The metrics recorded by sniffs. |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * @var array |
| 131 | 131 | * @see getMetrics() |
| 132 | 132 | */ |
| 133 | - protected $metrics = []; |
|
| 133 | + protected $metrics = [ ]; |
|
| 134 | 134 | |
| 135 | 135 | /** |
| 136 | 136 | * The metrics recorded for each token. |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | * @var array |
| 141 | 141 | * @see getMetrics() |
| 142 | 142 | */ |
| 143 | - private $metricTokens = []; |
|
| 143 | + private $metricTokens = [ ]; |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * The total number of errors raised. |
@@ -175,21 +175,21 @@ discard block |
||
| 175 | 175 | * |
| 176 | 176 | * @var array |
| 177 | 177 | */ |
| 178 | - protected $ignoredListeners = []; |
|
| 178 | + protected $ignoredListeners = [ ]; |
|
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | 181 | * An array of message codes that are being ignored. |
| 182 | 182 | * |
| 183 | 183 | * @var array |
| 184 | 184 | */ |
| 185 | - protected $ignoredCodes = []; |
|
| 185 | + protected $ignoredCodes = [ ]; |
|
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | 188 | * An array of sniffs listening to this file's processing. |
| 189 | 189 | * |
| 190 | 190 | * @var \PHP_CodeSniffer\Sniffs\Sniff[] |
| 191 | 191 | */ |
| 192 | - protected $listeners = []; |
|
| 192 | + protected $listeners = [ ]; |
|
| 193 | 193 | |
| 194 | 194 | /** |
| 195 | 195 | * The class name of the sniff currently processing the file. |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | * |
| 204 | 204 | * @var array |
| 205 | 205 | */ |
| 206 | - protected $listenerTimes = []; |
|
| 206 | + protected $listenerTimes = [ ]; |
|
| 207 | 207 | |
| 208 | 208 | /** |
| 209 | 209 | * A cache of often used config settings to improve performance. |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @var array |
| 214 | 214 | */ |
| 215 | - protected $configCache = []; |
|
| 215 | + protected $configCache = [ ]; |
|
| 216 | 216 | |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -224,30 +224,30 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return void |
| 226 | 226 | */ |
| 227 | - public function __construct($path, Ruleset $ruleset, Config $config) |
|
| 227 | + public function __construct( $path, Ruleset $ruleset, Config $config ) |
|
| 228 | 228 | { |
| 229 | 229 | $this->path = $path; |
| 230 | 230 | $this->ruleset = $ruleset; |
| 231 | 231 | $this->config = $config; |
| 232 | 232 | $this->fixer = new Fixer(); |
| 233 | 233 | |
| 234 | - $parts = explode('.', $path); |
|
| 235 | - $extension = array_pop($parts); |
|
| 236 | - if (isset($config->extensions[$extension]) === true) { |
|
| 237 | - $this->tokenizerType = $config->extensions[$extension]; |
|
| 234 | + $parts = explode( '.', $path ); |
|
| 235 | + $extension = array_pop( $parts ); |
|
| 236 | + if ( isset( $config->extensions[ $extension ] ) === true ) { |
|
| 237 | + $this->tokenizerType = $config->extensions[ $extension ]; |
|
| 238 | 238 | } else { |
| 239 | 239 | // Revert to default. |
| 240 | 240 | $this->tokenizerType = 'PHP'; |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - $this->configCache['cache'] = $this->config->cache; |
|
| 244 | - $this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs); |
|
| 245 | - $this->configCache['exclude'] = array_map('strtolower', $this->config->exclude); |
|
| 246 | - $this->configCache['errorSeverity'] = $this->config->errorSeverity; |
|
| 247 | - $this->configCache['warningSeverity'] = $this->config->warningSeverity; |
|
| 248 | - $this->configCache['recordErrors'] = $this->config->recordErrors; |
|
| 249 | - $this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns; |
|
| 250 | - $this->configCache['includePatterns'] = $this->ruleset->includePatterns; |
|
| 243 | + $this->configCache[ 'cache' ] = $this->config->cache; |
|
| 244 | + $this->configCache[ 'sniffs' ] = array_map( 'strtolower', $this->config->sniffs ); |
|
| 245 | + $this->configCache[ 'exclude' ] = array_map( 'strtolower', $this->config->exclude ); |
|
| 246 | + $this->configCache[ 'errorSeverity' ] = $this->config->errorSeverity; |
|
| 247 | + $this->configCache[ 'warningSeverity' ] = $this->config->warningSeverity; |
|
| 248 | + $this->configCache[ 'recordErrors' ] = $this->config->recordErrors; |
|
| 249 | + $this->configCache[ 'ignorePatterns' ] = $this->ruleset->ignorePatterns; |
|
| 250 | + $this->configCache[ 'includePatterns' ] = $this->ruleset->includePatterns; |
|
| 251 | 251 | |
| 252 | 252 | }//end __construct() |
| 253 | 253 | |
@@ -261,15 +261,15 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return void |
| 263 | 263 | */ |
| 264 | - public function setContent($content) |
|
| 264 | + public function setContent( $content ) |
|
| 265 | 265 | { |
| 266 | 266 | $this->content = $content; |
| 267 | - $this->tokens = []; |
|
| 267 | + $this->tokens = [ ]; |
|
| 268 | 268 | |
| 269 | 269 | try { |
| 270 | - $this->eolChar = Util\Common::detectLineEndings($content); |
|
| 271 | - } catch (RuntimeException $e) { |
|
| 272 | - $this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings'); |
|
| 270 | + $this->eolChar = Util\Common::detectLineEndings( $content ); |
|
| 271 | + } catch ( RuntimeException $e ) { |
|
| 272 | + $this->addWarningOnLine( $e->getMessage(), 1, 'Internal.DetectLineEndings' ); |
|
| 273 | 273 | return; |
| 274 | 274 | } |
| 275 | 275 | |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | public function disableCaching() |
| 299 | 299 | { |
| 300 | - $this->configCache['cache'] = false; |
|
| 300 | + $this->configCache[ 'cache' ] = false; |
|
| 301 | 301 | |
| 302 | 302 | }//end disableCaching() |
| 303 | 303 | |
@@ -309,12 +309,12 @@ discard block |
||
| 309 | 309 | */ |
| 310 | 310 | public function process() |
| 311 | 311 | { |
| 312 | - if ($this->ignored === true) { |
|
| 312 | + if ( $this->ignored === true ) { |
|
| 313 | 313 | return; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | - $this->errors = []; |
|
| 317 | - $this->warnings = []; |
|
| 316 | + $this->errors = [ ]; |
|
| 317 | + $this->warnings = [ ]; |
|
| 318 | 318 | $this->errorCount = 0; |
| 319 | 319 | $this->warningCount = 0; |
| 320 | 320 | $this->fixableCount = 0; |
@@ -322,106 +322,106 @@ discard block |
||
| 322 | 322 | $this->parse(); |
| 323 | 323 | |
| 324 | 324 | // Check if tokenizer errors cause this file to be ignored. |
| 325 | - if ($this->ignored === true) { |
|
| 325 | + if ( $this->ignored === true ) { |
|
| 326 | 326 | return; |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | - $this->fixer->startFile($this); |
|
| 329 | + $this->fixer->startFile( $this ); |
|
| 330 | 330 | |
| 331 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 332 | - echo "\t*** START TOKEN PROCESSING ***".PHP_EOL; |
|
| 331 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 332 | + echo "\t*** START TOKEN PROCESSING ***" . PHP_EOL; |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | $foundCode = false; |
| 336 | - $listenerIgnoreTo = []; |
|
| 337 | - $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); |
|
| 336 | + $listenerIgnoreTo = [ ]; |
|
| 337 | + $inTests = defined( 'PHP_CODESNIFFER_IN_TESTS' ); |
|
| 338 | 338 | $checkAnnotations = $this->config->annotations; |
| 339 | 339 | |
| 340 | 340 | // Foreach of the listeners that have registered to listen for this |
| 341 | 341 | // token, get them to process it. |
| 342 | - foreach ($this->tokens as $stackPtr => $token) { |
|
| 342 | + foreach ( $this->tokens as $stackPtr => $token ) { |
|
| 343 | 343 | // Check for ignored lines. |
| 344 | - if ($checkAnnotations === true |
|
| 345 | - && ($token['code'] === T_COMMENT |
|
| 346 | - || $token['code'] === T_PHPCS_IGNORE_FILE |
|
| 347 | - || $token['code'] === T_PHPCS_SET |
|
| 348 | - || $token['code'] === T_DOC_COMMENT_STRING |
|
| 349 | - || $token['code'] === T_DOC_COMMENT_TAG |
|
| 350 | - || ($inTests === true && $token['code'] === T_INLINE_HTML)) |
|
| 344 | + if ( $checkAnnotations === true |
|
| 345 | + && ( $token[ 'code' ] === T_COMMENT |
|
| 346 | + || $token[ 'code' ] === T_PHPCS_IGNORE_FILE |
|
| 347 | + || $token[ 'code' ] === T_PHPCS_SET |
|
| 348 | + || $token[ 'code' ] === T_DOC_COMMENT_STRING |
|
| 349 | + || $token[ 'code' ] === T_DOC_COMMENT_TAG |
|
| 350 | + || ( $inTests === true && $token[ 'code' ] === T_INLINE_HTML ) ) |
|
| 351 | 351 | ) { |
| 352 | - $commentText = ltrim($this->tokens[$stackPtr]['content'], ' /*'); |
|
| 353 | - $commentTextLower = strtolower($commentText); |
|
| 354 | - if (strpos($commentText, '@codingStandards') !== false) { |
|
| 355 | - if (strpos($commentText, '@codingStandardsIgnoreFile') !== false) { |
|
| 352 | + $commentText = ltrim( $this->tokens[ $stackPtr ][ 'content' ], ' /*' ); |
|
| 353 | + $commentTextLower = strtolower( $commentText ); |
|
| 354 | + if ( strpos( $commentText, '@codingStandards' ) !== false ) { |
|
| 355 | + if ( strpos( $commentText, '@codingStandardsIgnoreFile' ) !== false ) { |
|
| 356 | 356 | // Ignoring the whole file, just a little late. |
| 357 | - $this->errors = []; |
|
| 358 | - $this->warnings = []; |
|
| 357 | + $this->errors = [ ]; |
|
| 358 | + $this->warnings = [ ]; |
|
| 359 | 359 | $this->errorCount = 0; |
| 360 | 360 | $this->warningCount = 0; |
| 361 | 361 | $this->fixableCount = 0; |
| 362 | 362 | return; |
| 363 | - } else if (strpos($commentText, '@codingStandardsChangeSetting') !== false) { |
|
| 364 | - $start = strpos($commentText, '@codingStandardsChangeSetting'); |
|
| 365 | - $comment = substr($commentText, ($start + 30)); |
|
| 366 | - $parts = explode(' ', $comment); |
|
| 367 | - if (count($parts) >= 2) { |
|
| 368 | - $sniffParts = explode('.', $parts[0]); |
|
| 369 | - if (count($sniffParts) >= 3) { |
|
| 363 | + } else if ( strpos( $commentText, '@codingStandardsChangeSetting' ) !== false ) { |
|
| 364 | + $start = strpos( $commentText, '@codingStandardsChangeSetting' ); |
|
| 365 | + $comment = substr( $commentText, ( $start + 30 ) ); |
|
| 366 | + $parts = explode( ' ', $comment ); |
|
| 367 | + if ( count( $parts ) >= 2 ) { |
|
| 368 | + $sniffParts = explode( '.', $parts[ 0 ] ); |
|
| 369 | + if ( count( $sniffParts ) >= 3 ) { |
|
| 370 | 370 | // If the sniff code is not known to us, it has not been registered in this run. |
| 371 | 371 | // But don't throw an error as it could be there for a different standard to use. |
| 372 | - if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) { |
|
| 373 | - $listenerCode = array_shift($parts); |
|
| 374 | - $propertyCode = array_shift($parts); |
|
| 375 | - $propertyValue = rtrim(implode(' ', $parts), " */\r\n"); |
|
| 376 | - $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 377 | - $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 372 | + if ( isset( $this->ruleset->sniffCodes[ $parts[ 0 ] ] ) === true ) { |
|
| 373 | + $listenerCode = array_shift( $parts ); |
|
| 374 | + $propertyCode = array_shift( $parts ); |
|
| 375 | + $propertyValue = rtrim( implode( ' ', $parts ), " */\r\n" ); |
|
| 376 | + $listenerClass = $this->ruleset->sniffCodes[ $listenerCode ]; |
|
| 377 | + $this->ruleset->setSniffProperty( $listenerClass, $propertyCode, $propertyValue ); |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | 381 | }//end if |
| 382 | - } else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile' |
|
| 383 | - || substr($commentTextLower, 0, 17) === '@phpcs:ignorefile' |
|
| 382 | + } else if ( substr( $commentTextLower, 0, 16 ) === 'phpcs:ignorefile' |
|
| 383 | + || substr( $commentTextLower, 0, 17 ) === '@phpcs:ignorefile' |
|
| 384 | 384 | ) { |
| 385 | 385 | // Ignoring the whole file, just a little late. |
| 386 | - $this->errors = []; |
|
| 387 | - $this->warnings = []; |
|
| 386 | + $this->errors = [ ]; |
|
| 387 | + $this->warnings = [ ]; |
|
| 388 | 388 | $this->errorCount = 0; |
| 389 | 389 | $this->warningCount = 0; |
| 390 | 390 | $this->fixableCount = 0; |
| 391 | 391 | return; |
| 392 | - } else if (substr($commentTextLower, 0, 9) === 'phpcs:set' |
|
| 393 | - || substr($commentTextLower, 0, 10) === '@phpcs:set' |
|
| 392 | + } else if ( substr( $commentTextLower, 0, 9 ) === 'phpcs:set' |
|
| 393 | + || substr( $commentTextLower, 0, 10 ) === '@phpcs:set' |
|
| 394 | 394 | ) { |
| 395 | - if (isset($token['sniffCode']) === true) { |
|
| 396 | - $listenerCode = $token['sniffCode']; |
|
| 397 | - if (isset($this->ruleset->sniffCodes[$listenerCode]) === true) { |
|
| 398 | - $propertyCode = $token['sniffProperty']; |
|
| 399 | - $propertyValue = $token['sniffPropertyValue']; |
|
| 400 | - $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; |
|
| 401 | - $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue); |
|
| 395 | + if ( isset( $token[ 'sniffCode' ] ) === true ) { |
|
| 396 | + $listenerCode = $token[ 'sniffCode' ]; |
|
| 397 | + if ( isset( $this->ruleset->sniffCodes[ $listenerCode ] ) === true ) { |
|
| 398 | + $propertyCode = $token[ 'sniffProperty' ]; |
|
| 399 | + $propertyValue = $token[ 'sniffPropertyValue' ]; |
|
| 400 | + $listenerClass = $this->ruleset->sniffCodes[ $listenerCode ]; |
|
| 401 | + $this->ruleset->setSniffProperty( $listenerClass, $propertyCode, $propertyValue ); |
|
| 402 | 402 | } |
| 403 | 403 | } |
| 404 | 404 | }//end if |
| 405 | 405 | }//end if |
| 406 | 406 | |
| 407 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 408 | - $type = $token['type']; |
|
| 409 | - $content = Util\Common::prepareForOutput($token['content']); |
|
| 410 | - echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL; |
|
| 407 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 408 | + $type = $token[ 'type' ]; |
|
| 409 | + $content = Util\Common::prepareForOutput( $token[ 'content' ] ); |
|
| 410 | + echo "\t\tProcess token $stackPtr: $type => $content" . PHP_EOL; |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | - if ($token['code'] !== T_INLINE_HTML) { |
|
| 413 | + if ( $token[ 'code' ] !== T_INLINE_HTML ) { |
|
| 414 | 414 | $foundCode = true; |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | - if (isset($this->ruleset->tokenListeners[$token['code']]) === false) { |
|
| 417 | + if ( isset( $this->ruleset->tokenListeners[ $token[ 'code' ] ] ) === false ) { |
|
| 418 | 418 | continue; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - foreach ($this->ruleset->tokenListeners[$token['code']] as $listenerData) { |
|
| 422 | - if (isset($this->ignoredListeners[$listenerData['class']]) === true |
|
| 423 | - || (isset($listenerIgnoreTo[$listenerData['class']]) === true |
|
| 424 | - && $listenerIgnoreTo[$listenerData['class']] > $stackPtr) |
|
| 421 | + foreach ( $this->ruleset->tokenListeners[ $token[ 'code' ] ] as $listenerData ) { |
|
| 422 | + if ( isset( $this->ignoredListeners[ $listenerData[ 'class' ] ] ) === true |
|
| 423 | + || ( isset( $listenerIgnoreTo[ $listenerData[ 'class' ] ] ) === true |
|
| 424 | + && $listenerIgnoreTo[ $listenerData[ 'class' ] ] > $stackPtr ) |
|
| 425 | 425 | ) { |
| 426 | 426 | // This sniff is ignoring past this token, or the whole file. |
| 427 | 427 | continue; |
@@ -429,78 +429,78 @@ discard block |
||
| 429 | 429 | |
| 430 | 430 | // Make sure this sniff supports the tokenizer |
| 431 | 431 | // we are currently using. |
| 432 | - $class = $listenerData['class']; |
|
| 432 | + $class = $listenerData[ 'class' ]; |
|
| 433 | 433 | |
| 434 | - if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) { |
|
| 434 | + if ( isset( $listenerData[ 'tokenizers' ][ $this->tokenizerType ] ) === false ) { |
|
| 435 | 435 | continue; |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | 438 | // If the file path matches one of our ignore patterns, skip it. |
| 439 | 439 | // While there is support for a type of each pattern |
| 440 | 440 | // (absolute or relative) we don't actually support it here. |
| 441 | - foreach ($listenerData['ignore'] as $pattern) { |
|
| 441 | + foreach ( $listenerData[ 'ignore' ] as $pattern ) { |
|
| 442 | 442 | // We assume a / directory separator, as do the exclude rules |
| 443 | 443 | // most developers write, so we need a special case for any system |
| 444 | 444 | // that is different. |
| 445 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 446 | - $pattern = str_replace('/', '\\\\', $pattern); |
|
| 445 | + if ( DIRECTORY_SEPARATOR === '\\' ) { |
|
| 446 | + $pattern = str_replace( '/', '\\\\', $pattern ); |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | - $pattern = '`'.$pattern.'`i'; |
|
| 450 | - if (preg_match($pattern, $this->path) === 1) { |
|
| 451 | - $this->ignoredListeners[$class] = true; |
|
| 452 | - continue(2); |
|
| 449 | + $pattern = '`' . $pattern . '`i'; |
|
| 450 | + if ( preg_match( $pattern, $this->path ) === 1 ) { |
|
| 451 | + $this->ignoredListeners[ $class ] = true; |
|
| 452 | + continue( 2 ); |
|
| 453 | 453 | } |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | // If the file path does not match one of our include patterns, skip it. |
| 457 | 457 | // While there is support for a type of each pattern |
| 458 | 458 | // (absolute or relative) we don't actually support it here. |
| 459 | - if (empty($listenerData['include']) === false) { |
|
| 459 | + if ( empty( $listenerData[ 'include' ] ) === false ) { |
|
| 460 | 460 | $included = false; |
| 461 | - foreach ($listenerData['include'] as $pattern) { |
|
| 461 | + foreach ( $listenerData[ 'include' ] as $pattern ) { |
|
| 462 | 462 | // We assume a / directory separator, as do the exclude rules |
| 463 | 463 | // most developers write, so we need a special case for any system |
| 464 | 464 | // that is different. |
| 465 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 466 | - $pattern = str_replace('/', '\\\\', $pattern); |
|
| 465 | + if ( DIRECTORY_SEPARATOR === '\\' ) { |
|
| 466 | + $pattern = str_replace( '/', '\\\\', $pattern ); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - $pattern = '`'.$pattern.'`i'; |
|
| 470 | - if (preg_match($pattern, $this->path) === 1) { |
|
| 469 | + $pattern = '`' . $pattern . '`i'; |
|
| 470 | + if ( preg_match( $pattern, $this->path ) === 1 ) { |
|
| 471 | 471 | $included = true; |
| 472 | 472 | break; |
| 473 | 473 | } |
| 474 | 474 | } |
| 475 | 475 | |
| 476 | - if ($included === false) { |
|
| 477 | - $this->ignoredListeners[$class] = true; |
|
| 476 | + if ( $included === false ) { |
|
| 477 | + $this->ignoredListeners[ $class ] = true; |
|
| 478 | 478 | continue; |
| 479 | 479 | } |
| 480 | 480 | }//end if |
| 481 | 481 | |
| 482 | 482 | $this->activeListener = $class; |
| 483 | 483 | |
| 484 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 485 | - $startTime = microtime(true); |
|
| 486 | - echo "\t\t\tProcessing ".$this->activeListener.'... '; |
|
| 484 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 485 | + $startTime = microtime( true ); |
|
| 486 | + echo "\t\t\tProcessing " . $this->activeListener . '... '; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | - $ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr); |
|
| 490 | - if ($ignoreTo !== null) { |
|
| 491 | - $listenerIgnoreTo[$this->activeListener] = $ignoreTo; |
|
| 489 | + $ignoreTo = $this->ruleset->sniffs[ $class ]->process( $this, $stackPtr ); |
|
| 490 | + if ( $ignoreTo !== null ) { |
|
| 491 | + $listenerIgnoreTo[ $this->activeListener ] = $ignoreTo; |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 495 | - $timeTaken = (microtime(true) - $startTime); |
|
| 496 | - if (isset($this->listenerTimes[$this->activeListener]) === false) { |
|
| 497 | - $this->listenerTimes[$this->activeListener] = 0; |
|
| 494 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 495 | + $timeTaken = ( microtime( true ) - $startTime ); |
|
| 496 | + if ( isset( $this->listenerTimes[ $this->activeListener ] ) === false ) { |
|
| 497 | + $this->listenerTimes[ $this->activeListener ] = 0; |
|
| 498 | 498 | } |
| 499 | 499 | |
| 500 | - $this->listenerTimes[$this->activeListener] += $timeTaken; |
|
| 500 | + $this->listenerTimes[ $this->activeListener ] += $timeTaken; |
|
| 501 | 501 | |
| 502 | - $timeTaken = round(($timeTaken), 4); |
|
| 503 | - echo "DONE in $timeTaken seconds".PHP_EOL; |
|
| 502 | + $timeTaken = round( ( $timeTaken ), 4 ); |
|
| 503 | + echo "DONE in $timeTaken seconds" . PHP_EOL; |
|
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | $this->activeListener = ''; |
@@ -513,25 +513,25 @@ discard block |
||
| 513 | 513 | // We don't show this error for STDIN because we can't be sure the content |
| 514 | 514 | // actually came directly from the user. It could be something like |
| 515 | 515 | // refs from a Git pre-push hook. |
| 516 | - if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') { |
|
| 517 | - $shortTags = (bool) ini_get('short_open_tag'); |
|
| 518 | - if ($shortTags === false) { |
|
| 516 | + if ( $foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN' ) { |
|
| 517 | + $shortTags = (bool)ini_get( 'short_open_tag' ); |
|
| 518 | + if ( $shortTags === false ) { |
|
| 519 | 519 | $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.'; |
| 520 | - $this->addWarning($error, null, 'Internal.NoCodeFound'); |
|
| 520 | + $this->addWarning( $error, null, 'Internal.NoCodeFound' ); |
|
| 521 | 521 | } |
| 522 | 522 | } |
| 523 | 523 | |
| 524 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 525 | - echo "\t*** END TOKEN PROCESSING ***".PHP_EOL; |
|
| 526 | - echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 524 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 525 | + echo "\t*** END TOKEN PROCESSING ***" . PHP_EOL; |
|
| 526 | + echo "\t*** START SNIFF PROCESSING REPORT ***" . PHP_EOL; |
|
| 527 | 527 | |
| 528 | - asort($this->listenerTimes, SORT_NUMERIC); |
|
| 529 | - $this->listenerTimes = array_reverse($this->listenerTimes, true); |
|
| 530 | - foreach ($this->listenerTimes as $listener => $timeTaken) { |
|
| 531 | - echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL; |
|
| 528 | + asort( $this->listenerTimes, SORT_NUMERIC ); |
|
| 529 | + $this->listenerTimes = array_reverse( $this->listenerTimes, true ); |
|
| 530 | + foreach ( $this->listenerTimes as $listener => $timeTaken ) { |
|
| 531 | + echo "\t$listener: " . round( ( $timeTaken ), 4 ) . ' secs' . PHP_EOL; |
|
| 532 | 532 | } |
| 533 | 533 | |
| 534 | - echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL; |
|
| 534 | + echo "\t*** END SNIFF PROCESSING REPORT ***" . PHP_EOL; |
|
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | $this->fixedCount += $this->fixer->getFixCount(); |
@@ -546,21 +546,21 @@ discard block |
||
| 546 | 546 | */ |
| 547 | 547 | public function parse() |
| 548 | 548 | { |
| 549 | - if (empty($this->tokens) === false) { |
|
| 549 | + if ( empty( $this->tokens ) === false ) { |
|
| 550 | 550 | // File has already been parsed. |
| 551 | 551 | return; |
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | try { |
| 555 | - $tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType; |
|
| 556 | - $this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar); |
|
| 555 | + $tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\' . $this->tokenizerType; |
|
| 556 | + $this->tokenizer = new $tokenizerClass( $this->content, $this->config, $this->eolChar ); |
|
| 557 | 557 | $this->tokens = $this->tokenizer->getTokens(); |
| 558 | - } catch (TokenizerException $e) { |
|
| 558 | + } catch ( TokenizerException $e ) { |
|
| 559 | 559 | $this->ignored = true; |
| 560 | - $this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception'); |
|
| 561 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 560 | + $this->addWarning( $e->getMessage(), null, 'Internal.Tokenizer.Exception' ); |
|
| 561 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 562 | 562 | echo "[$this->tokenizerType => tokenizer error]... "; |
| 563 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 563 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 564 | 564 | echo PHP_EOL; |
| 565 | 565 | } |
| 566 | 566 | } |
@@ -568,31 +568,31 @@ discard block |
||
| 568 | 568 | return; |
| 569 | 569 | } |
| 570 | 570 | |
| 571 | - $this->numTokens = count($this->tokens); |
|
| 571 | + $this->numTokens = count( $this->tokens ); |
|
| 572 | 572 | |
| 573 | 573 | // Check for mixed line endings as these can cause tokenizer errors and we |
| 574 | 574 | // should let the user know that the results they get may be incorrect. |
| 575 | 575 | // This is done by removing all backslashes, removing the newline char we |
| 576 | 576 | // detected, then converting newlines chars into text. If any backslashes |
| 577 | 577 | // are left at the end, we have additional newline chars in use. |
| 578 | - $contents = str_replace('\\', '', $this->content); |
|
| 579 | - $contents = str_replace($this->eolChar, '', $contents); |
|
| 580 | - $contents = str_replace("\n", '\n', $contents); |
|
| 581 | - $contents = str_replace("\r", '\r', $contents); |
|
| 582 | - if (strpos($contents, '\\') !== false) { |
|
| 578 | + $contents = str_replace( '\\', '', $this->content ); |
|
| 579 | + $contents = str_replace( $this->eolChar, '', $contents ); |
|
| 580 | + $contents = str_replace( "\n", '\n', $contents ); |
|
| 581 | + $contents = str_replace( "\r", '\r', $contents ); |
|
| 582 | + if ( strpos( $contents, '\\' ) !== false ) { |
|
| 583 | 583 | $error = 'File has mixed line endings; this may cause incorrect results'; |
| 584 | - $this->addWarningOnLine($error, 1, 'Internal.LineEndings.Mixed'); |
|
| 584 | + $this->addWarningOnLine( $error, 1, 'Internal.LineEndings.Mixed' ); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 588 | - if ($this->numTokens === 0) { |
|
| 587 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 588 | + if ( $this->numTokens === 0 ) { |
|
| 589 | 589 | $numLines = 0; |
| 590 | 590 | } else { |
| 591 | - $numLines = $this->tokens[($this->numTokens - 1)]['line']; |
|
| 591 | + $numLines = $this->tokens[ ( $this->numTokens - 1 ) ][ 'line' ]; |
|
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... "; |
| 595 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 595 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 596 | 596 | echo PHP_EOL; |
| 597 | 597 | } |
| 598 | 598 | } |
@@ -648,19 +648,19 @@ discard block |
||
| 648 | 648 | $error, |
| 649 | 649 | $stackPtr, |
| 650 | 650 | $code, |
| 651 | - $data=[], |
|
| 652 | - $severity=0, |
|
| 653 | - $fixable=false |
|
| 651 | + $data = [ ], |
|
| 652 | + $severity = 0, |
|
| 653 | + $fixable = false |
|
| 654 | 654 | ) { |
| 655 | - if ($stackPtr === null) { |
|
| 655 | + if ( $stackPtr === null ) { |
|
| 656 | 656 | $line = 1; |
| 657 | 657 | $column = 1; |
| 658 | 658 | } else { |
| 659 | - $line = $this->tokens[$stackPtr]['line']; |
|
| 660 | - $column = $this->tokens[$stackPtr]['column']; |
|
| 659 | + $line = $this->tokens[ $stackPtr ][ 'line' ]; |
|
| 660 | + $column = $this->tokens[ $stackPtr ][ 'column' ]; |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - return $this->addMessage(true, $error, $line, $column, $code, $data, $severity, $fixable); |
|
| 663 | + return $this->addMessage( true, $error, $line, $column, $code, $data, $severity, $fixable ); |
|
| 664 | 664 | |
| 665 | 665 | }//end addError() |
| 666 | 666 | |
@@ -682,19 +682,19 @@ discard block |
||
| 682 | 682 | $warning, |
| 683 | 683 | $stackPtr, |
| 684 | 684 | $code, |
| 685 | - $data=[], |
|
| 686 | - $severity=0, |
|
| 687 | - $fixable=false |
|
| 685 | + $data = [ ], |
|
| 686 | + $severity = 0, |
|
| 687 | + $fixable = false |
|
| 688 | 688 | ) { |
| 689 | - if ($stackPtr === null) { |
|
| 689 | + if ( $stackPtr === null ) { |
|
| 690 | 690 | $line = 1; |
| 691 | 691 | $column = 1; |
| 692 | 692 | } else { |
| 693 | - $line = $this->tokens[$stackPtr]['line']; |
|
| 694 | - $column = $this->tokens[$stackPtr]['column']; |
|
| 693 | + $line = $this->tokens[ $stackPtr ][ 'line' ]; |
|
| 694 | + $column = $this->tokens[ $stackPtr ][ 'column' ]; |
|
| 695 | 695 | } |
| 696 | 696 | |
| 697 | - return $this->addMessage(false, $warning, $line, $column, $code, $data, $severity, $fixable); |
|
| 697 | + return $this->addMessage( false, $warning, $line, $column, $code, $data, $severity, $fixable ); |
|
| 698 | 698 | |
| 699 | 699 | }//end addWarning() |
| 700 | 700 | |
@@ -715,10 +715,10 @@ discard block |
||
| 715 | 715 | $error, |
| 716 | 716 | $line, |
| 717 | 717 | $code, |
| 718 | - $data=[], |
|
| 719 | - $severity=0 |
|
| 718 | + $data = [ ], |
|
| 719 | + $severity = 0 |
|
| 720 | 720 | ) { |
| 721 | - return $this->addMessage(true, $error, $line, 1, $code, $data, $severity, false); |
|
| 721 | + return $this->addMessage( true, $error, $line, 1, $code, $data, $severity, false ); |
|
| 722 | 722 | |
| 723 | 723 | }//end addErrorOnLine() |
| 724 | 724 | |
@@ -739,10 +739,10 @@ discard block |
||
| 739 | 739 | $warning, |
| 740 | 740 | $line, |
| 741 | 741 | $code, |
| 742 | - $data=[], |
|
| 743 | - $severity=0 |
|
| 742 | + $data = [ ], |
|
| 743 | + $severity = 0 |
|
| 744 | 744 | ) { |
| 745 | - return $this->addMessage(false, $warning, $line, 1, $code, $data, $severity, false); |
|
| 745 | + return $this->addMessage( false, $warning, $line, 1, $code, $data, $severity, false ); |
|
| 746 | 746 | |
| 747 | 747 | }//end addWarningOnLine() |
| 748 | 748 | |
@@ -765,11 +765,11 @@ discard block |
||
| 765 | 765 | $error, |
| 766 | 766 | $stackPtr, |
| 767 | 767 | $code, |
| 768 | - $data=[], |
|
| 769 | - $severity=0 |
|
| 768 | + $data = [ ], |
|
| 769 | + $severity = 0 |
|
| 770 | 770 | ) { |
| 771 | - $recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true); |
|
| 772 | - if ($recorded === true && $this->fixer->enabled === true) { |
|
| 771 | + $recorded = $this->addError( $error, $stackPtr, $code, $data, $severity, true ); |
|
| 772 | + if ( $recorded === true && $this->fixer->enabled === true ) { |
|
| 773 | 773 | return true; |
| 774 | 774 | } |
| 775 | 775 | |
@@ -796,11 +796,11 @@ discard block |
||
| 796 | 796 | $warning, |
| 797 | 797 | $stackPtr, |
| 798 | 798 | $code, |
| 799 | - $data=[], |
|
| 800 | - $severity=0 |
|
| 799 | + $data = [ ], |
|
| 800 | + $severity = 0 |
|
| 801 | 801 | ) { |
| 802 | - $recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true); |
|
| 803 | - if ($recorded === true && $this->fixer->enabled === true) { |
|
| 802 | + $recorded = $this->addWarning( $warning, $stackPtr, $code, $data, $severity, true ); |
|
| 803 | + if ( $recorded === true && $this->fixer->enabled === true ) { |
|
| 804 | 804 | return true; |
| 805 | 805 | } |
| 806 | 806 | |
@@ -824,156 +824,156 @@ discard block |
||
| 824 | 824 | * |
| 825 | 825 | * @return boolean |
| 826 | 826 | */ |
| 827 | - protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
|
| 827 | + protected function addMessage( $error, $message, $line, $column, $code, $data, $severity, $fixable ) |
|
| 828 | 828 | { |
| 829 | 829 | // Check if this line is ignoring all message codes. |
| 830 | - if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) { |
|
| 830 | + if ( isset( $this->tokenizer->ignoredLines[ $line ][ '.all' ] ) === true ) { |
|
| 831 | 831 | return false; |
| 832 | 832 | } |
| 833 | 833 | |
| 834 | 834 | // Work out which sniff generated the message. |
| 835 | - $parts = explode('.', $code); |
|
| 836 | - if ($parts[0] === 'Internal') { |
|
| 835 | + $parts = explode( '.', $code ); |
|
| 836 | + if ( $parts[ 0 ] === 'Internal' ) { |
|
| 837 | 837 | // An internal message. |
| 838 | - $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 838 | + $listenerCode = Util\Common::getSniffCode( $this->activeListener ); |
|
| 839 | 839 | $sniffCode = $code; |
| 840 | - $checkCodes = [$sniffCode]; |
|
| 840 | + $checkCodes = [ $sniffCode ]; |
|
| 841 | 841 | } else { |
| 842 | - if ($parts[0] !== $code) { |
|
| 842 | + if ( $parts[ 0 ] !== $code ) { |
|
| 843 | 843 | // The full message code has been passed in. |
| 844 | 844 | $sniffCode = $code; |
| 845 | - $listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.')); |
|
| 845 | + $listenerCode = substr( $sniffCode, 0, strrpos( $sniffCode, '.' ) ); |
|
| 846 | 846 | } else { |
| 847 | - $listenerCode = Util\Common::getSniffCode($this->activeListener); |
|
| 848 | - $sniffCode = $listenerCode.'.'.$code; |
|
| 849 | - $parts = explode('.', $sniffCode); |
|
| 847 | + $listenerCode = Util\Common::getSniffCode( $this->activeListener ); |
|
| 848 | + $sniffCode = $listenerCode . '.' . $code; |
|
| 849 | + $parts = explode( '.', $sniffCode ); |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | $checkCodes = [ |
| 853 | 853 | $sniffCode, |
| 854 | - $parts[0].'.'.$parts[1].'.'.$parts[2], |
|
| 855 | - $parts[0].'.'.$parts[1], |
|
| 856 | - $parts[0], |
|
| 854 | + $parts[ 0 ] . '.' . $parts[ 1 ] . '.' . $parts[ 2 ], |
|
| 855 | + $parts[ 0 ] . '.' . $parts[ 1 ], |
|
| 856 | + $parts[ 0 ], |
|
| 857 | 857 | ]; |
| 858 | 858 | }//end if |
| 859 | 859 | |
| 860 | - if (isset($this->tokenizer->ignoredLines[$line]) === true) { |
|
| 860 | + if ( isset( $this->tokenizer->ignoredLines[ $line ] ) === true ) { |
|
| 861 | 861 | // Check if this line is ignoring this specific message. |
| 862 | 862 | $ignored = false; |
| 863 | - foreach ($checkCodes as $checkCode) { |
|
| 864 | - if (isset($this->tokenizer->ignoredLines[$line][$checkCode]) === true) { |
|
| 863 | + foreach ( $checkCodes as $checkCode ) { |
|
| 864 | + if ( isset( $this->tokenizer->ignoredLines[ $line ][ $checkCode ] ) === true ) { |
|
| 865 | 865 | $ignored = true; |
| 866 | 866 | break; |
| 867 | 867 | } |
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | // If it is ignored, make sure it's not whitelisted. |
| 871 | - if ($ignored === true |
|
| 872 | - && isset($this->tokenizer->ignoredLines[$line]['.except']) === true |
|
| 871 | + if ( $ignored === true |
|
| 872 | + && isset( $this->tokenizer->ignoredLines[ $line ][ '.except' ] ) === true |
|
| 873 | 873 | ) { |
| 874 | - foreach ($checkCodes as $checkCode) { |
|
| 875 | - if (isset($this->tokenizer->ignoredLines[$line]['.except'][$checkCode]) === true) { |
|
| 874 | + foreach ( $checkCodes as $checkCode ) { |
|
| 875 | + if ( isset( $this->tokenizer->ignoredLines[ $line ][ '.except' ][ $checkCode ] ) === true ) { |
|
| 876 | 876 | $ignored = false; |
| 877 | 877 | break; |
| 878 | 878 | } |
| 879 | 879 | } |
| 880 | 880 | } |
| 881 | 881 | |
| 882 | - if ($ignored === true) { |
|
| 882 | + if ( $ignored === true ) { |
|
| 883 | 883 | return false; |
| 884 | 884 | } |
| 885 | 885 | }//end if |
| 886 | 886 | |
| 887 | 887 | $includeAll = true; |
| 888 | - if ($this->configCache['cache'] === false |
|
| 889 | - || $this->configCache['recordErrors'] === false |
|
| 888 | + if ( $this->configCache[ 'cache' ] === false |
|
| 889 | + || $this->configCache[ 'recordErrors' ] === false |
|
| 890 | 890 | ) { |
| 891 | 891 | $includeAll = false; |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | // Filter out any messages for sniffs that shouldn't have run |
| 895 | 895 | // due to the use of the --sniffs command line argument. |
| 896 | - if ($includeAll === false |
|
| 897 | - && ((empty($this->configCache['sniffs']) === false |
|
| 898 | - && in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false) |
|
| 899 | - || (empty($this->configCache['exclude']) === false |
|
| 900 | - && in_array(strtolower($listenerCode), $this->configCache['exclude'], true) === true)) |
|
| 896 | + if ( $includeAll === false |
|
| 897 | + && ( ( empty( $this->configCache[ 'sniffs' ] ) === false |
|
| 898 | + && in_array( strtolower( $listenerCode ), $this->configCache[ 'sniffs' ], true ) === false ) |
|
| 899 | + || ( empty( $this->configCache[ 'exclude' ] ) === false |
|
| 900 | + && in_array( strtolower( $listenerCode ), $this->configCache[ 'exclude' ], true ) === true ) ) |
|
| 901 | 901 | ) { |
| 902 | 902 | return false; |
| 903 | 903 | } |
| 904 | 904 | |
| 905 | 905 | // If we know this sniff code is being ignored for this file, return early. |
| 906 | - foreach ($checkCodes as $checkCode) { |
|
| 907 | - if (isset($this->ignoredCodes[$checkCode]) === true) { |
|
| 906 | + foreach ( $checkCodes as $checkCode ) { |
|
| 907 | + if ( isset( $this->ignoredCodes[ $checkCode ] ) === true ) { |
|
| 908 | 908 | return false; |
| 909 | 909 | } |
| 910 | 910 | } |
| 911 | 911 | |
| 912 | 912 | $oppositeType = 'warning'; |
| 913 | - if ($error === false) { |
|
| 913 | + if ( $error === false ) { |
|
| 914 | 914 | $oppositeType = 'error'; |
| 915 | 915 | } |
| 916 | 916 | |
| 917 | - foreach ($checkCodes as $checkCode) { |
|
| 917 | + foreach ( $checkCodes as $checkCode ) { |
|
| 918 | 918 | // Make sure this message type has not been set to the opposite message type. |
| 919 | - if (isset($this->ruleset->ruleset[$checkCode]['type']) === true |
|
| 920 | - && $this->ruleset->ruleset[$checkCode]['type'] === $oppositeType |
|
| 919 | + if ( isset( $this->ruleset->ruleset[ $checkCode ][ 'type' ] ) === true |
|
| 920 | + && $this->ruleset->ruleset[ $checkCode ][ 'type' ] === $oppositeType |
|
| 921 | 921 | ) { |
| 922 | - $error = !$error; |
|
| 922 | + $error = ! $error; |
|
| 923 | 923 | break; |
| 924 | 924 | } |
| 925 | 925 | } |
| 926 | 926 | |
| 927 | - if ($error === true) { |
|
| 928 | - $configSeverity = $this->configCache['errorSeverity']; |
|
| 927 | + if ( $error === true ) { |
|
| 928 | + $configSeverity = $this->configCache[ 'errorSeverity' ]; |
|
| 929 | 929 | $messageCount = &$this->errorCount; |
| 930 | 930 | $messages = &$this->errors; |
| 931 | 931 | } else { |
| 932 | - $configSeverity = $this->configCache['warningSeverity']; |
|
| 932 | + $configSeverity = $this->configCache[ 'warningSeverity' ]; |
|
| 933 | 933 | $messageCount = &$this->warningCount; |
| 934 | 934 | $messages = &$this->warnings; |
| 935 | 935 | } |
| 936 | 936 | |
| 937 | - if ($includeAll === false && $configSeverity === 0) { |
|
| 937 | + if ( $includeAll === false && $configSeverity === 0 ) { |
|
| 938 | 938 | // Don't bother doing any processing as these messages are just going to |
| 939 | 939 | // be hidden in the reports anyway. |
| 940 | 940 | return false; |
| 941 | 941 | } |
| 942 | 942 | |
| 943 | - if ($severity === 0) { |
|
| 943 | + if ( $severity === 0 ) { |
|
| 944 | 944 | $severity = 5; |
| 945 | 945 | } |
| 946 | 946 | |
| 947 | - foreach ($checkCodes as $checkCode) { |
|
| 947 | + foreach ( $checkCodes as $checkCode ) { |
|
| 948 | 948 | // Make sure we are interested in this severity level. |
| 949 | - if (isset($this->ruleset->ruleset[$checkCode]['severity']) === true) { |
|
| 950 | - $severity = $this->ruleset->ruleset[$checkCode]['severity']; |
|
| 949 | + if ( isset( $this->ruleset->ruleset[ $checkCode ][ 'severity' ] ) === true ) { |
|
| 950 | + $severity = $this->ruleset->ruleset[ $checkCode ][ 'severity' ]; |
|
| 951 | 951 | break; |
| 952 | 952 | } |
| 953 | 953 | } |
| 954 | 954 | |
| 955 | - if ($includeAll === false && $configSeverity > $severity) { |
|
| 955 | + if ( $includeAll === false && $configSeverity > $severity ) { |
|
| 956 | 956 | return false; |
| 957 | 957 | } |
| 958 | 958 | |
| 959 | 959 | // Make sure we are not ignoring this file. |
| 960 | 960 | $included = null; |
| 961 | - foreach ($checkCodes as $checkCode) { |
|
| 961 | + foreach ( $checkCodes as $checkCode ) { |
|
| 962 | 962 | $patterns = null; |
| 963 | 963 | |
| 964 | - if (isset($this->configCache['includePatterns'][$checkCode]) === true) { |
|
| 965 | - $patterns = $this->configCache['includePatterns'][$checkCode]; |
|
| 964 | + if ( isset( $this->configCache[ 'includePatterns' ][ $checkCode ] ) === true ) { |
|
| 965 | + $patterns = $this->configCache[ 'includePatterns' ][ $checkCode ]; |
|
| 966 | 966 | $excluding = false; |
| 967 | - } else if (isset($this->configCache['ignorePatterns'][$checkCode]) === true) { |
|
| 968 | - $patterns = $this->configCache['ignorePatterns'][$checkCode]; |
|
| 967 | + } else if ( isset( $this->configCache[ 'ignorePatterns' ][ $checkCode ] ) === true ) { |
|
| 968 | + $patterns = $this->configCache[ 'ignorePatterns' ][ $checkCode ]; |
|
| 969 | 969 | $excluding = true; |
| 970 | 970 | } |
| 971 | 971 | |
| 972 | - if ($patterns === null) { |
|
| 972 | + if ( $patterns === null ) { |
|
| 973 | 973 | continue; |
| 974 | 974 | } |
| 975 | 975 | |
| 976 | - foreach ($patterns as $pattern => $type) { |
|
| 976 | + foreach ( $patterns as $pattern => $type ) { |
|
| 977 | 977 | // While there is support for a type of each pattern |
| 978 | 978 | // (absolute or relative) we don't actually support it here. |
| 979 | 979 | $replacements = [ |
@@ -984,15 +984,15 @@ discard block |
||
| 984 | 984 | // We assume a / directory separator, as do the exclude rules |
| 985 | 985 | // most developers write, so we need a special case for any system |
| 986 | 986 | // that is different. |
| 987 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 988 | - $replacements['/'] = '\\\\'; |
|
| 987 | + if ( DIRECTORY_SEPARATOR === '\\' ) { |
|
| 988 | + $replacements[ '/' ] = '\\\\'; |
|
| 989 | 989 | } |
| 990 | 990 | |
| 991 | - $pattern = '`'.strtr($pattern, $replacements).'`i'; |
|
| 992 | - $matched = preg_match($pattern, $this->path); |
|
| 991 | + $pattern = '`' . strtr( $pattern, $replacements ) . '`i'; |
|
| 992 | + $matched = preg_match( $pattern, $this->path ); |
|
| 993 | 993 | |
| 994 | - if ($matched === 0) { |
|
| 995 | - if ($excluding === false && $included === null) { |
|
| 994 | + if ( $matched === 0 ) { |
|
| 995 | + if ( $excluding === false && $included === null ) { |
|
| 996 | 996 | // This file path is not being included. |
| 997 | 997 | $included = false; |
| 998 | 998 | } |
@@ -1000,9 +1000,9 @@ discard block |
||
| 1000 | 1000 | continue; |
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | - if ($excluding === true) { |
|
| 1003 | + if ( $excluding === true ) { |
|
| 1004 | 1004 | // This file path is being excluded. |
| 1005 | - $this->ignoredCodes[$checkCode] = true; |
|
| 1005 | + $this->ignoredCodes[ $checkCode ] = true; |
|
| 1006 | 1006 | return false; |
| 1007 | 1007 | } |
| 1008 | 1008 | |
@@ -1012,41 +1012,41 @@ discard block |
||
| 1012 | 1012 | }//end foreach |
| 1013 | 1013 | }//end foreach |
| 1014 | 1014 | |
| 1015 | - if ($included === false) { |
|
| 1015 | + if ( $included === false ) { |
|
| 1016 | 1016 | // There were include rules set, but this file |
| 1017 | 1017 | // path didn't match any of them. |
| 1018 | 1018 | return false; |
| 1019 | 1019 | } |
| 1020 | 1020 | |
| 1021 | 1021 | $messageCount++; |
| 1022 | - if ($fixable === true) { |
|
| 1022 | + if ( $fixable === true ) { |
|
| 1023 | 1023 | $this->fixableCount++; |
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | - if ($this->configCache['recordErrors'] === false |
|
| 1026 | + if ( $this->configCache[ 'recordErrors' ] === false |
|
| 1027 | 1027 | && $includeAll === false |
| 1028 | 1028 | ) { |
| 1029 | 1029 | return true; |
| 1030 | 1030 | } |
| 1031 | 1031 | |
| 1032 | 1032 | // Work out the error message. |
| 1033 | - if (isset($this->ruleset->ruleset[$sniffCode]['message']) === true) { |
|
| 1034 | - $message = $this->ruleset->ruleset[$sniffCode]['message']; |
|
| 1033 | + if ( isset( $this->ruleset->ruleset[ $sniffCode ][ 'message' ] ) === true ) { |
|
| 1034 | + $message = $this->ruleset->ruleset[ $sniffCode ][ 'message' ]; |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | - if (empty($data) === false) { |
|
| 1038 | - $message = vsprintf($message, $data); |
|
| 1037 | + if ( empty( $data ) === false ) { |
|
| 1038 | + $message = vsprintf( $message, $data ); |
|
| 1039 | 1039 | } |
| 1040 | 1040 | |
| 1041 | - if (isset($messages[$line]) === false) { |
|
| 1042 | - $messages[$line] = []; |
|
| 1041 | + if ( isset( $messages[ $line ] ) === false ) { |
|
| 1042 | + $messages[ $line ] = [ ]; |
|
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | - if (isset($messages[$line][$column]) === false) { |
|
| 1046 | - $messages[$line][$column] = []; |
|
| 1045 | + if ( isset( $messages[ $line ][ $column ] ) === false ) { |
|
| 1046 | + $messages[ $line ][ $column ] = [ ]; |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | - $messages[$line][$column][] = [ |
|
| 1049 | + $messages[ $line ][ $column ][ ] = [ |
|
| 1050 | 1050 | 'message' => $message, |
| 1051 | 1051 | 'source' => $sniffCode, |
| 1052 | 1052 | 'listener' => $this->activeListener, |
@@ -1054,12 +1054,12 @@ discard block |
||
| 1054 | 1054 | 'fixable' => $fixable, |
| 1055 | 1055 | ]; |
| 1056 | 1056 | |
| 1057 | - if (PHP_CODESNIFFER_VERBOSITY > 1 |
|
| 1057 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 |
|
| 1058 | 1058 | && $this->fixer->enabled === true |
| 1059 | 1059 | && $fixable === true |
| 1060 | 1060 | ) { |
| 1061 | 1061 | @ob_end_clean(); |
| 1062 | - echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL; |
|
| 1062 | + echo "\tE: [Line $line] $message ($sniffCode)" . PHP_EOL; |
|
| 1063 | 1063 | ob_start(); |
| 1064 | 1064 | } |
| 1065 | 1065 | |
@@ -1077,17 +1077,17 @@ discard block |
||
| 1077 | 1077 | * |
| 1078 | 1078 | * @return boolean |
| 1079 | 1079 | */ |
| 1080 | - public function recordMetric($stackPtr, $metric, $value) |
|
| 1080 | + public function recordMetric( $stackPtr, $metric, $value ) |
|
| 1081 | 1081 | { |
| 1082 | - if (isset($this->metrics[$metric]) === false) { |
|
| 1083 | - $this->metrics[$metric] = ['values' => [$value => 1]]; |
|
| 1084 | - $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1085 | - } else if (isset($this->metricTokens[$metric][$stackPtr]) === false) { |
|
| 1086 | - $this->metricTokens[$metric][$stackPtr] = true; |
|
| 1087 | - if (isset($this->metrics[$metric]['values'][$value]) === false) { |
|
| 1088 | - $this->metrics[$metric]['values'][$value] = 1; |
|
| 1082 | + if ( isset( $this->metrics[ $metric ] ) === false ) { |
|
| 1083 | + $this->metrics[ $metric ] = [ 'values' => [ $value => 1 ] ]; |
|
| 1084 | + $this->metricTokens[ $metric ][ $stackPtr ] = true; |
|
| 1085 | + } else if ( isset( $this->metricTokens[ $metric ][ $stackPtr ] ) === false ) { |
|
| 1086 | + $this->metricTokens[ $metric ][ $stackPtr ] = true; |
|
| 1087 | + if ( isset( $this->metrics[ $metric ][ 'values' ][ $value ] ) === false ) { |
|
| 1088 | + $this->metrics[ $metric ][ 'values' ][ $value ] = 1; |
|
| 1089 | 1089 | } else { |
| 1090 | - $this->metrics[$metric]['values'][$value]++; |
|
| 1090 | + $this->metrics[ $metric ][ 'values' ][ $value ]++; |
|
| 1091 | 1091 | } |
| 1092 | 1092 | } |
| 1093 | 1093 | |
@@ -1216,34 +1216,34 @@ discard block |
||
| 1216 | 1216 | * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
| 1217 | 1217 | * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
| 1218 | 1218 | */ |
| 1219 | - public function getDeclarationName($stackPtr) |
|
| 1219 | + public function getDeclarationName( $stackPtr ) |
|
| 1220 | 1220 | { |
| 1221 | - $tokenCode = $this->tokens[$stackPtr]['code']; |
|
| 1221 | + $tokenCode = $this->tokens[ $stackPtr ][ 'code' ]; |
|
| 1222 | 1222 | |
| 1223 | - if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { |
|
| 1223 | + if ( $tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE ) { |
|
| 1224 | 1224 | return null; |
| 1225 | 1225 | } |
| 1226 | 1226 | |
| 1227 | - if ($tokenCode !== T_FUNCTION |
|
| 1227 | + if ( $tokenCode !== T_FUNCTION |
|
| 1228 | 1228 | && $tokenCode !== T_CLASS |
| 1229 | 1229 | && $tokenCode !== T_INTERFACE |
| 1230 | 1230 | && $tokenCode !== T_TRAIT |
| 1231 | 1231 | ) { |
| 1232 | - throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT'); |
|
| 1232 | + throw new RuntimeException( 'Token type "' . $this->tokens[ $stackPtr ][ 'type' ] . '" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT' ); |
|
| 1233 | 1233 | } |
| 1234 | 1234 | |
| 1235 | - if ($tokenCode === T_FUNCTION |
|
| 1236 | - && strtolower($this->tokens[$stackPtr]['content']) !== 'function' |
|
| 1235 | + if ( $tokenCode === T_FUNCTION |
|
| 1236 | + && strtolower( $this->tokens[ $stackPtr ][ 'content' ] ) !== 'function' |
|
| 1237 | 1237 | ) { |
| 1238 | 1238 | // This is a function declared without the "function" keyword. |
| 1239 | 1239 | // So this token is the function name. |
| 1240 | - return $this->tokens[$stackPtr]['content']; |
|
| 1240 | + return $this->tokens[ $stackPtr ][ 'content' ]; |
|
| 1241 | 1241 | } |
| 1242 | 1242 | |
| 1243 | 1243 | $content = null; |
| 1244 | - for ($i = $stackPtr; $i < $this->numTokens; $i++) { |
|
| 1245 | - if ($this->tokens[$i]['code'] === T_STRING) { |
|
| 1246 | - $content = $this->tokens[$i]['content']; |
|
| 1244 | + for ( $i = $stackPtr; $i < $this->numTokens; $i++ ) { |
|
| 1245 | + if ( $this->tokens[ $i ][ 'code' ] === T_STRING ) { |
|
| 1246 | + $content = $this->tokens[ $i ][ 'content' ]; |
|
| 1247 | 1247 | break; |
| 1248 | 1248 | } |
| 1249 | 1249 | } |
@@ -1282,20 +1282,20 @@ discard block |
||
| 1282 | 1282 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
| 1283 | 1283 | * type T_FUNCTION or T_CLOSURE. |
| 1284 | 1284 | */ |
| 1285 | - public function getMethodParameters($stackPtr) |
|
| 1285 | + public function getMethodParameters( $stackPtr ) |
|
| 1286 | 1286 | { |
| 1287 | - if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1288 | - && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1287 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_FUNCTION |
|
| 1288 | + && $this->tokens[ $stackPtr ][ 'code' ] !== T_CLOSURE |
|
| 1289 | 1289 | ) { |
| 1290 | - throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1290 | + throw new TokenizerException( '$stackPtr must be of type T_FUNCTION or T_CLOSURE' ); |
|
| 1291 | 1291 | } |
| 1292 | 1292 | |
| 1293 | - $opener = $this->tokens[$stackPtr]['parenthesis_opener']; |
|
| 1294 | - $closer = $this->tokens[$stackPtr]['parenthesis_closer']; |
|
| 1293 | + $opener = $this->tokens[ $stackPtr ][ 'parenthesis_opener' ]; |
|
| 1294 | + $closer = $this->tokens[ $stackPtr ][ 'parenthesis_closer' ]; |
|
| 1295 | 1295 | |
| 1296 | - $vars = []; |
|
| 1296 | + $vars = [ ]; |
|
| 1297 | 1297 | $currVar = null; |
| 1298 | - $paramStart = ($opener + 1); |
|
| 1298 | + $paramStart = ( $opener + 1 ); |
|
| 1299 | 1299 | $defaultStart = null; |
| 1300 | 1300 | $paramCount = 0; |
| 1301 | 1301 | $passByReference = false; |
@@ -1304,28 +1304,28 @@ discard block |
||
| 1304 | 1304 | $typeHintToken = false; |
| 1305 | 1305 | $nullableType = false; |
| 1306 | 1306 | |
| 1307 | - for ($i = $paramStart; $i <= $closer; $i++) { |
|
| 1307 | + for ( $i = $paramStart; $i <= $closer; $i++ ) { |
|
| 1308 | 1308 | // Check to see if this token has a parenthesis or bracket opener. If it does |
| 1309 | 1309 | // it's likely to be an array which might have arguments in it. This |
| 1310 | 1310 | // could cause problems in our parsing below, so lets just skip to the |
| 1311 | 1311 | // end of it. |
| 1312 | - if (isset($this->tokens[$i]['parenthesis_opener']) === true) { |
|
| 1312 | + if ( isset( $this->tokens[ $i ][ 'parenthesis_opener' ] ) === true ) { |
|
| 1313 | 1313 | // Don't do this if it's the close parenthesis for the method. |
| 1314 | - if ($i !== $this->tokens[$i]['parenthesis_closer']) { |
|
| 1315 | - $i = ($this->tokens[$i]['parenthesis_closer'] + 1); |
|
| 1314 | + if ( $i !== $this->tokens[ $i ][ 'parenthesis_closer' ] ) { |
|
| 1315 | + $i = ( $this->tokens[ $i ][ 'parenthesis_closer' ] + 1 ); |
|
| 1316 | 1316 | } |
| 1317 | 1317 | } |
| 1318 | 1318 | |
| 1319 | - if (isset($this->tokens[$i]['bracket_opener']) === true) { |
|
| 1319 | + if ( isset( $this->tokens[ $i ][ 'bracket_opener' ] ) === true ) { |
|
| 1320 | 1320 | // Don't do this if it's the close parenthesis for the method. |
| 1321 | - if ($i !== $this->tokens[$i]['bracket_closer']) { |
|
| 1322 | - $i = ($this->tokens[$i]['bracket_closer'] + 1); |
|
| 1321 | + if ( $i !== $this->tokens[ $i ][ 'bracket_closer' ] ) { |
|
| 1322 | + $i = ( $this->tokens[ $i ][ 'bracket_closer' ] + 1 ); |
|
| 1323 | 1323 | } |
| 1324 | 1324 | } |
| 1325 | 1325 | |
| 1326 | - switch ($this->tokens[$i]['code']) { |
|
| 1326 | + switch ( $this->tokens[ $i ][ 'code' ] ) { |
|
| 1327 | 1327 | case T_BITWISE_AND: |
| 1328 | - if ($defaultStart === null) { |
|
| 1328 | + if ( $defaultStart === null ) { |
|
| 1329 | 1329 | $passByReference = true; |
| 1330 | 1330 | } |
| 1331 | 1331 | break; |
@@ -1336,99 +1336,99 @@ discard block |
||
| 1336 | 1336 | $variableLength = true; |
| 1337 | 1337 | break; |
| 1338 | 1338 | case T_CALLABLE: |
| 1339 | - if ($typeHintToken === false) { |
|
| 1339 | + if ( $typeHintToken === false ) { |
|
| 1340 | 1340 | $typeHintToken = $i; |
| 1341 | 1341 | } |
| 1342 | 1342 | |
| 1343 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1343 | + $typeHint .= $this->tokens[ $i ][ 'content' ]; |
|
| 1344 | 1344 | break; |
| 1345 | 1345 | case T_SELF: |
| 1346 | 1346 | case T_PARENT: |
| 1347 | 1347 | case T_STATIC: |
| 1348 | 1348 | // Self and parent are valid, static invalid, but was probably intended as type hint. |
| 1349 | - if (isset($defaultStart) === false) { |
|
| 1350 | - if ($typeHintToken === false) { |
|
| 1349 | + if ( isset( $defaultStart ) === false ) { |
|
| 1350 | + if ( $typeHintToken === false ) { |
|
| 1351 | 1351 | $typeHintToken = $i; |
| 1352 | 1352 | } |
| 1353 | 1353 | |
| 1354 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1354 | + $typeHint .= $this->tokens[ $i ][ 'content' ]; |
|
| 1355 | 1355 | } |
| 1356 | 1356 | break; |
| 1357 | 1357 | case T_STRING: |
| 1358 | 1358 | // This is a string, so it may be a type hint, but it could |
| 1359 | 1359 | // also be a constant used as a default value. |
| 1360 | 1360 | $prevComma = false; |
| 1361 | - for ($t = $i; $t >= $opener; $t--) { |
|
| 1362 | - if ($this->tokens[$t]['code'] === T_COMMA) { |
|
| 1361 | + for ( $t = $i; $t >= $opener; $t-- ) { |
|
| 1362 | + if ( $this->tokens[ $t ][ 'code' ] === T_COMMA ) { |
|
| 1363 | 1363 | $prevComma = $t; |
| 1364 | 1364 | break; |
| 1365 | 1365 | } |
| 1366 | 1366 | } |
| 1367 | 1367 | |
| 1368 | - if ($prevComma !== false) { |
|
| 1368 | + if ( $prevComma !== false ) { |
|
| 1369 | 1369 | $nextEquals = false; |
| 1370 | - for ($t = $prevComma; $t < $i; $t++) { |
|
| 1371 | - if ($this->tokens[$t]['code'] === T_EQUAL) { |
|
| 1370 | + for ( $t = $prevComma; $t < $i; $t++ ) { |
|
| 1371 | + if ( $this->tokens[ $t ][ 'code' ] === T_EQUAL ) { |
|
| 1372 | 1372 | $nextEquals = $t; |
| 1373 | 1373 | break; |
| 1374 | 1374 | } |
| 1375 | 1375 | } |
| 1376 | 1376 | |
| 1377 | - if ($nextEquals !== false) { |
|
| 1377 | + if ( $nextEquals !== false ) { |
|
| 1378 | 1378 | break; |
| 1379 | 1379 | } |
| 1380 | 1380 | } |
| 1381 | 1381 | |
| 1382 | - if ($defaultStart === null) { |
|
| 1383 | - if ($typeHintToken === false) { |
|
| 1382 | + if ( $defaultStart === null ) { |
|
| 1383 | + if ( $typeHintToken === false ) { |
|
| 1384 | 1384 | $typeHintToken = $i; |
| 1385 | 1385 | } |
| 1386 | 1386 | |
| 1387 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1387 | + $typeHint .= $this->tokens[ $i ][ 'content' ]; |
|
| 1388 | 1388 | } |
| 1389 | 1389 | break; |
| 1390 | 1390 | case T_NS_SEPARATOR: |
| 1391 | 1391 | // Part of a type hint or default value. |
| 1392 | - if ($defaultStart === null) { |
|
| 1393 | - if ($typeHintToken === false) { |
|
| 1392 | + if ( $defaultStart === null ) { |
|
| 1393 | + if ( $typeHintToken === false ) { |
|
| 1394 | 1394 | $typeHintToken = $i; |
| 1395 | 1395 | } |
| 1396 | 1396 | |
| 1397 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1397 | + $typeHint .= $this->tokens[ $i ][ 'content' ]; |
|
| 1398 | 1398 | } |
| 1399 | 1399 | break; |
| 1400 | 1400 | case T_NULLABLE: |
| 1401 | - if ($defaultStart === null) { |
|
| 1401 | + if ( $defaultStart === null ) { |
|
| 1402 | 1402 | $nullableType = true; |
| 1403 | - $typeHint .= $this->tokens[$i]['content']; |
|
| 1403 | + $typeHint .= $this->tokens[ $i ][ 'content' ]; |
|
| 1404 | 1404 | } |
| 1405 | 1405 | break; |
| 1406 | 1406 | case T_CLOSE_PARENTHESIS: |
| 1407 | 1407 | case T_COMMA: |
| 1408 | 1408 | // If it's null, then there must be no parameters for this |
| 1409 | 1409 | // method. |
| 1410 | - if ($currVar === null) { |
|
| 1410 | + if ( $currVar === null ) { |
|
| 1411 | 1411 | continue 2; |
| 1412 | 1412 | } |
| 1413 | 1413 | |
| 1414 | - $vars[$paramCount] = []; |
|
| 1415 | - $vars[$paramCount]['token'] = $currVar; |
|
| 1416 | - $vars[$paramCount]['name'] = $this->tokens[$currVar]['content']; |
|
| 1417 | - $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart))); |
|
| 1414 | + $vars[ $paramCount ] = [ ]; |
|
| 1415 | + $vars[ $paramCount ][ 'token' ] = $currVar; |
|
| 1416 | + $vars[ $paramCount ][ 'name' ] = $this->tokens[ $currVar ][ 'content' ]; |
|
| 1417 | + $vars[ $paramCount ][ 'content' ] = trim( $this->getTokensAsString( $paramStart, ( $i - $paramStart ) ) ); |
|
| 1418 | 1418 | |
| 1419 | - if ($defaultStart !== null) { |
|
| 1420 | - $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart))); |
|
| 1419 | + if ( $defaultStart !== null ) { |
|
| 1420 | + $vars[ $paramCount ][ 'default' ] = trim( $this->getTokensAsString( $defaultStart, ( $i - $defaultStart ) ) ); |
|
| 1421 | 1421 | } |
| 1422 | 1422 | |
| 1423 | - $vars[$paramCount]['pass_by_reference'] = $passByReference; |
|
| 1424 | - $vars[$paramCount]['variable_length'] = $variableLength; |
|
| 1425 | - $vars[$paramCount]['type_hint'] = $typeHint; |
|
| 1426 | - $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
|
| 1427 | - $vars[$paramCount]['nullable_type'] = $nullableType; |
|
| 1423 | + $vars[ $paramCount ][ 'pass_by_reference' ] = $passByReference; |
|
| 1424 | + $vars[ $paramCount ][ 'variable_length' ] = $variableLength; |
|
| 1425 | + $vars[ $paramCount ][ 'type_hint' ] = $typeHint; |
|
| 1426 | + $vars[ $paramCount ][ 'type_hint_token' ] = $typeHintToken; |
|
| 1427 | + $vars[ $paramCount ][ 'nullable_type' ] = $nullableType; |
|
| 1428 | 1428 | |
| 1429 | 1429 | // Reset the vars, as we are about to process the next parameter. |
| 1430 | 1430 | $defaultStart = null; |
| 1431 | - $paramStart = ($i + 1); |
|
| 1431 | + $paramStart = ( $i + 1 ); |
|
| 1432 | 1432 | $passByReference = false; |
| 1433 | 1433 | $variableLength = false; |
| 1434 | 1434 | $typeHint = ''; |
@@ -1438,7 +1438,7 @@ discard block |
||
| 1438 | 1438 | $paramCount++; |
| 1439 | 1439 | break; |
| 1440 | 1440 | case T_EQUAL: |
| 1441 | - $defaultStart = ($i + 1); |
|
| 1441 | + $defaultStart = ( $i + 1 ); |
|
| 1442 | 1442 | break; |
| 1443 | 1443 | }//end switch |
| 1444 | 1444 | }//end for |
@@ -1474,15 +1474,15 @@ discard block |
||
| 1474 | 1474 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
| 1475 | 1475 | * T_FUNCTION token. |
| 1476 | 1476 | */ |
| 1477 | - public function getMethodProperties($stackPtr) |
|
| 1477 | + public function getMethodProperties( $stackPtr ) |
|
| 1478 | 1478 | { |
| 1479 | - if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 1480 | - && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
|
| 1479 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_FUNCTION |
|
| 1480 | + && $this->tokens[ $stackPtr ][ 'code' ] !== T_CLOSURE |
|
| 1481 | 1481 | ) { |
| 1482 | - throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
|
| 1482 | + throw new TokenizerException( '$stackPtr must be of type T_FUNCTION or T_CLOSURE' ); |
|
| 1483 | 1483 | } |
| 1484 | 1484 | |
| 1485 | - if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 1485 | + if ( $this->tokens[ $stackPtr ][ 'code' ] === T_FUNCTION ) { |
|
| 1486 | 1486 | $valid = [ |
| 1487 | 1487 | T_PUBLIC => T_PUBLIC, |
| 1488 | 1488 | T_PRIVATE => T_PRIVATE, |
@@ -1509,12 +1509,12 @@ discard block |
||
| 1509 | 1509 | $isFinal = false; |
| 1510 | 1510 | $isStatic = false; |
| 1511 | 1511 | |
| 1512 | - for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1513 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1512 | + for ( $i = ( $stackPtr - 1 ); $i > 0; $i-- ) { |
|
| 1513 | + if ( isset( $valid[ $this->tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 1514 | 1514 | break; |
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | - switch ($this->tokens[$i]['code']) { |
|
| 1517 | + switch ( $this->tokens[ $i ][ 'code' ] ) { |
|
| 1518 | 1518 | case T_PUBLIC: |
| 1519 | 1519 | $scope = 'public'; |
| 1520 | 1520 | $scopeSpecified = true; |
@@ -1544,10 +1544,10 @@ discard block |
||
| 1544 | 1544 | $nullableReturnType = false; |
| 1545 | 1545 | $hasBody = true; |
| 1546 | 1546 | |
| 1547 | - if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 1547 | + if ( isset( $this->tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === true ) { |
|
| 1548 | 1548 | $scopeOpener = null; |
| 1549 | - if (isset($this->tokens[$stackPtr]['scope_opener']) === true) { |
|
| 1550 | - $scopeOpener = $this->tokens[$stackPtr]['scope_opener']; |
|
| 1549 | + if ( isset( $this->tokens[ $stackPtr ][ 'scope_opener' ] ) === true ) { |
|
| 1550 | + $scopeOpener = $this->tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 1551 | 1551 | } |
| 1552 | 1552 | |
| 1553 | 1553 | $valid = [ |
@@ -1558,33 +1558,33 @@ discard block |
||
| 1558 | 1558 | T_NS_SEPARATOR => T_NS_SEPARATOR, |
| 1559 | 1559 | ]; |
| 1560 | 1560 | |
| 1561 | - for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) { |
|
| 1562 | - if (($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON) |
|
| 1563 | - || ($scopeOpener !== null && $i === $scopeOpener) |
|
| 1561 | + for ( $i = $this->tokens[ $stackPtr ][ 'parenthesis_closer' ]; $i < $this->numTokens; $i++ ) { |
|
| 1562 | + if ( ( $scopeOpener === null && $this->tokens[ $i ][ 'code' ] === T_SEMICOLON ) |
|
| 1563 | + || ( $scopeOpener !== null && $i === $scopeOpener ) |
|
| 1564 | 1564 | ) { |
| 1565 | 1565 | // End of function definition. |
| 1566 | 1566 | break; |
| 1567 | 1567 | } |
| 1568 | 1568 | |
| 1569 | - if ($this->tokens[$i]['code'] === T_NULLABLE) { |
|
| 1569 | + if ( $this->tokens[ $i ][ 'code' ] === T_NULLABLE ) { |
|
| 1570 | 1570 | $nullableReturnType = true; |
| 1571 | 1571 | } |
| 1572 | 1572 | |
| 1573 | - if (isset($valid[$this->tokens[$i]['code']]) === true) { |
|
| 1574 | - if ($returnTypeToken === false) { |
|
| 1573 | + if ( isset( $valid[ $this->tokens[ $i ][ 'code' ] ] ) === true ) { |
|
| 1574 | + if ( $returnTypeToken === false ) { |
|
| 1575 | 1575 | $returnTypeToken = $i; |
| 1576 | 1576 | } |
| 1577 | 1577 | |
| 1578 | - $returnType .= $this->tokens[$i]['content']; |
|
| 1578 | + $returnType .= $this->tokens[ $i ][ 'content' ]; |
|
| 1579 | 1579 | } |
| 1580 | 1580 | } |
| 1581 | 1581 | |
| 1582 | - $end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']); |
|
| 1583 | - $hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET; |
|
| 1582 | + $end = $this->findNext( [ T_OPEN_CURLY_BRACKET, T_SEMICOLON ], $this->tokens[ $stackPtr ][ 'parenthesis_closer' ] ); |
|
| 1583 | + $hasBody = $this->tokens[ $end ][ 'code' ] === T_OPEN_CURLY_BRACKET; |
|
| 1584 | 1584 | }//end if |
| 1585 | 1585 | |
| 1586 | - if ($returnType !== '' && $nullableReturnType === true) { |
|
| 1587 | - $returnType = '?'.$returnType; |
|
| 1586 | + if ( $returnType !== '' && $nullableReturnType === true ) { |
|
| 1587 | + $returnType = '?' . $returnType; |
|
| 1588 | 1588 | } |
| 1589 | 1589 | |
| 1590 | 1590 | return [ |
@@ -1624,47 +1624,47 @@ discard block |
||
| 1624 | 1624 | * T_VARIABLE token, or if the position is not |
| 1625 | 1625 | * a class member variable. |
| 1626 | 1626 | */ |
| 1627 | - public function getMemberProperties($stackPtr) |
|
| 1627 | + public function getMemberProperties( $stackPtr ) |
|
| 1628 | 1628 | { |
| 1629 | - if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) { |
|
| 1630 | - throw new TokenizerException('$stackPtr must be of type T_VARIABLE'); |
|
| 1629 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_VARIABLE ) { |
|
| 1630 | + throw new TokenizerException( '$stackPtr must be of type T_VARIABLE' ); |
|
| 1631 | 1631 | } |
| 1632 | 1632 | |
| 1633 | - $conditions = array_keys($this->tokens[$stackPtr]['conditions']); |
|
| 1634 | - $ptr = array_pop($conditions); |
|
| 1635 | - if (isset($this->tokens[$ptr]) === false |
|
| 1636 | - || ($this->tokens[$ptr]['code'] !== T_CLASS |
|
| 1637 | - && $this->tokens[$ptr]['code'] !== T_ANON_CLASS |
|
| 1638 | - && $this->tokens[$ptr]['code'] !== T_TRAIT) |
|
| 1633 | + $conditions = array_keys( $this->tokens[ $stackPtr ][ 'conditions' ] ); |
|
| 1634 | + $ptr = array_pop( $conditions ); |
|
| 1635 | + if ( isset( $this->tokens[ $ptr ] ) === false |
|
| 1636 | + || ( $this->tokens[ $ptr ][ 'code' ] !== T_CLASS |
|
| 1637 | + && $this->tokens[ $ptr ][ 'code' ] !== T_ANON_CLASS |
|
| 1638 | + && $this->tokens[ $ptr ][ 'code' ] !== T_TRAIT ) |
|
| 1639 | 1639 | ) { |
| 1640 | - if (isset($this->tokens[$ptr]) === true |
|
| 1641 | - && $this->tokens[$ptr]['code'] === T_INTERFACE |
|
| 1640 | + if ( isset( $this->tokens[ $ptr ] ) === true |
|
| 1641 | + && $this->tokens[ $ptr ][ 'code' ] === T_INTERFACE |
|
| 1642 | 1642 | ) { |
| 1643 | 1643 | // T_VARIABLEs in interfaces can actually be method arguments |
| 1644 | 1644 | // but they wont be seen as being inside the method because there |
| 1645 | 1645 | // are no scope openers and closers for abstract methods. If it is in |
| 1646 | 1646 | // parentheses, we can be pretty sure it is a method argument. |
| 1647 | - if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false |
|
| 1648 | - || empty($this->tokens[$stackPtr]['nested_parenthesis']) === true |
|
| 1647 | + if ( isset( $this->tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === false |
|
| 1648 | + || empty( $this->tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true |
|
| 1649 | 1649 | ) { |
| 1650 | 1650 | $error = 'Possible parse error: interfaces may not include member vars'; |
| 1651 | - $this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar'); |
|
| 1652 | - return []; |
|
| 1651 | + $this->addWarning( $error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar' ); |
|
| 1652 | + return [ ]; |
|
| 1653 | 1653 | } |
| 1654 | 1654 | } else { |
| 1655 | - throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1655 | + throw new TokenizerException( '$stackPtr is not a class member var' ); |
|
| 1656 | 1656 | } |
| 1657 | 1657 | } |
| 1658 | 1658 | |
| 1659 | 1659 | // Make sure it's not a method parameter. |
| 1660 | - if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) { |
|
| 1661 | - $parenthesis = array_keys($this->tokens[$stackPtr]['nested_parenthesis']); |
|
| 1662 | - $deepestOpen = array_pop($parenthesis); |
|
| 1663 | - if ($deepestOpen > $ptr |
|
| 1664 | - && isset($this->tokens[$deepestOpen]['parenthesis_owner']) === true |
|
| 1665 | - && $this->tokens[$this->tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION |
|
| 1660 | + if ( empty( $this->tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === false ) { |
|
| 1661 | + $parenthesis = array_keys( $this->tokens[ $stackPtr ][ 'nested_parenthesis' ] ); |
|
| 1662 | + $deepestOpen = array_pop( $parenthesis ); |
|
| 1663 | + if ( $deepestOpen > $ptr |
|
| 1664 | + && isset( $this->tokens[ $deepestOpen ][ 'parenthesis_owner' ] ) === true |
|
| 1665 | + && $this->tokens[ $this->tokens[ $deepestOpen ][ 'parenthesis_owner' ] ][ 'code' ] === T_FUNCTION |
|
| 1666 | 1666 | ) { |
| 1667 | - throw new TokenizerException('$stackPtr is not a class member var'); |
|
| 1667 | + throw new TokenizerException( '$stackPtr is not a class member var' ); |
|
| 1668 | 1668 | } |
| 1669 | 1669 | } |
| 1670 | 1670 | |
@@ -1688,15 +1688,15 @@ discard block |
||
| 1688 | 1688 | T_OPEN_CURLY_BRACKET, |
| 1689 | 1689 | T_CLOSE_CURLY_BRACKET, |
| 1690 | 1690 | ], |
| 1691 | - ($stackPtr - 1) |
|
| 1691 | + ( $stackPtr - 1 ) |
|
| 1692 | 1692 | ); |
| 1693 | 1693 | |
| 1694 | - for ($i = ($startOfStatement + 1); $i < $stackPtr; $i++) { |
|
| 1695 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1694 | + for ( $i = ( $startOfStatement + 1 ); $i < $stackPtr; $i++ ) { |
|
| 1695 | + if ( isset( $valid[ $this->tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 1696 | 1696 | break; |
| 1697 | 1697 | } |
| 1698 | 1698 | |
| 1699 | - switch ($this->tokens[$i]['code']) { |
|
| 1699 | + switch ( $this->tokens[ $i ][ 'code' ] ) { |
|
| 1700 | 1700 | case T_PUBLIC: |
| 1701 | 1701 | $scope = 'public'; |
| 1702 | 1702 | $scopeSpecified = true; |
@@ -1742,10 +1742,10 @@ discard block |
||
| 1742 | 1742 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
| 1743 | 1743 | * T_CLASS token. |
| 1744 | 1744 | */ |
| 1745 | - public function getClassProperties($stackPtr) |
|
| 1745 | + public function getClassProperties( $stackPtr ) |
|
| 1746 | 1746 | { |
| 1747 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS) { |
|
| 1748 | - throw new TokenizerException('$stackPtr must be of type T_CLASS'); |
|
| 1747 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_CLASS ) { |
|
| 1748 | + throw new TokenizerException( '$stackPtr must be of type T_CLASS' ); |
|
| 1749 | 1749 | } |
| 1750 | 1750 | |
| 1751 | 1751 | $valid = [ |
@@ -1759,12 +1759,12 @@ discard block |
||
| 1759 | 1759 | $isAbstract = false; |
| 1760 | 1760 | $isFinal = false; |
| 1761 | 1761 | |
| 1762 | - for ($i = ($stackPtr - 1); $i > 0; $i--) { |
|
| 1763 | - if (isset($valid[$this->tokens[$i]['code']]) === false) { |
|
| 1762 | + for ( $i = ( $stackPtr - 1 ); $i > 0; $i-- ) { |
|
| 1763 | + if ( isset( $valid[ $this->tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 1764 | 1764 | break; |
| 1765 | 1765 | } |
| 1766 | 1766 | |
| 1767 | - switch ($this->tokens[$i]['code']) { |
|
| 1767 | + switch ( $this->tokens[ $i ][ 'code' ] ) { |
|
| 1768 | 1768 | case T_ABSTRACT: |
| 1769 | 1769 | $isAbstract = true; |
| 1770 | 1770 | break; |
@@ -1793,35 +1793,35 @@ discard block |
||
| 1793 | 1793 | * |
| 1794 | 1794 | * @return boolean |
| 1795 | 1795 | */ |
| 1796 | - public function isReference($stackPtr) |
|
| 1796 | + public function isReference( $stackPtr ) |
|
| 1797 | 1797 | { |
| 1798 | - if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) { |
|
| 1798 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_BITWISE_AND ) { |
|
| 1799 | 1799 | return false; |
| 1800 | 1800 | } |
| 1801 | 1801 | |
| 1802 | 1802 | $tokenBefore = $this->findPrevious( |
| 1803 | 1803 | Util\Tokens::$emptyTokens, |
| 1804 | - ($stackPtr - 1), |
|
| 1804 | + ( $stackPtr - 1 ), |
|
| 1805 | 1805 | null, |
| 1806 | 1806 | true |
| 1807 | 1807 | ); |
| 1808 | 1808 | |
| 1809 | - if ($this->tokens[$tokenBefore]['code'] === T_FUNCTION) { |
|
| 1809 | + if ( $this->tokens[ $tokenBefore ][ 'code' ] === T_FUNCTION ) { |
|
| 1810 | 1810 | // Function returns a reference. |
| 1811 | 1811 | return true; |
| 1812 | 1812 | } |
| 1813 | 1813 | |
| 1814 | - if ($this->tokens[$tokenBefore]['code'] === T_DOUBLE_ARROW) { |
|
| 1814 | + if ( $this->tokens[ $tokenBefore ][ 'code' ] === T_DOUBLE_ARROW ) { |
|
| 1815 | 1815 | // Inside a foreach loop or array assignment, this is a reference. |
| 1816 | 1816 | return true; |
| 1817 | 1817 | } |
| 1818 | 1818 | |
| 1819 | - if ($this->tokens[$tokenBefore]['code'] === T_AS) { |
|
| 1819 | + if ( $this->tokens[ $tokenBefore ][ 'code' ] === T_AS ) { |
|
| 1820 | 1820 | // Inside a foreach loop, this is a reference. |
| 1821 | 1821 | return true; |
| 1822 | 1822 | } |
| 1823 | 1823 | |
| 1824 | - if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) { |
|
| 1824 | + if ( isset( Util\Tokens::$assignmentTokens[ $this->tokens[ $tokenBefore ][ 'code' ] ] ) === true ) { |
|
| 1825 | 1825 | // This is directly after an assignment. It's a reference. Even if |
| 1826 | 1826 | // it is part of an operation, the other tests will handle it. |
| 1827 | 1827 | return true; |
@@ -1829,37 +1829,37 @@ discard block |
||
| 1829 | 1829 | |
| 1830 | 1830 | $tokenAfter = $this->findNext( |
| 1831 | 1831 | Util\Tokens::$emptyTokens, |
| 1832 | - ($stackPtr + 1), |
|
| 1832 | + ( $stackPtr + 1 ), |
|
| 1833 | 1833 | null, |
| 1834 | 1834 | true |
| 1835 | 1835 | ); |
| 1836 | 1836 | |
| 1837 | - if ($this->tokens[$tokenAfter]['code'] === T_NEW) { |
|
| 1837 | + if ( $this->tokens[ $tokenAfter ][ 'code' ] === T_NEW ) { |
|
| 1838 | 1838 | return true; |
| 1839 | 1839 | } |
| 1840 | 1840 | |
| 1841 | - if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 1842 | - $brackets = $this->tokens[$stackPtr]['nested_parenthesis']; |
|
| 1843 | - $lastBracket = array_pop($brackets); |
|
| 1844 | - if (isset($this->tokens[$lastBracket]['parenthesis_owner']) === true) { |
|
| 1845 | - $owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']]; |
|
| 1846 | - if ($owner['code'] === T_FUNCTION |
|
| 1847 | - || $owner['code'] === T_CLOSURE |
|
| 1841 | + if ( isset( $this->tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true ) { |
|
| 1842 | + $brackets = $this->tokens[ $stackPtr ][ 'nested_parenthesis' ]; |
|
| 1843 | + $lastBracket = array_pop( $brackets ); |
|
| 1844 | + if ( isset( $this->tokens[ $lastBracket ][ 'parenthesis_owner' ] ) === true ) { |
|
| 1845 | + $owner = $this->tokens[ $this->tokens[ $lastBracket ][ 'parenthesis_owner' ] ]; |
|
| 1846 | + if ( $owner[ 'code' ] === T_FUNCTION |
|
| 1847 | + || $owner[ 'code' ] === T_CLOSURE |
|
| 1848 | 1848 | ) { |
| 1849 | - $params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']); |
|
| 1850 | - foreach ($params as $param) { |
|
| 1849 | + $params = $this->getMethodParameters( $this->tokens[ $lastBracket ][ 'parenthesis_owner' ] ); |
|
| 1850 | + foreach ( $params as $param ) { |
|
| 1851 | 1851 | $varToken = $tokenAfter; |
| 1852 | - if ($param['variable_length'] === true) { |
|
| 1852 | + if ( $param[ 'variable_length' ] === true ) { |
|
| 1853 | 1853 | $varToken = $this->findNext( |
| 1854 | - (Util\Tokens::$emptyTokens + [T_ELLIPSIS]), |
|
| 1855 | - ($stackPtr + 1), |
|
| 1854 | + ( Util\Tokens::$emptyTokens + [ T_ELLIPSIS ] ), |
|
| 1855 | + ( $stackPtr + 1 ), |
|
| 1856 | 1856 | null, |
| 1857 | 1857 | true |
| 1858 | 1858 | ); |
| 1859 | 1859 | } |
| 1860 | 1860 | |
| 1861 | - if ($param['token'] === $varToken |
|
| 1862 | - && $param['pass_by_reference'] === true |
|
| 1861 | + if ( $param[ 'token' ] === $varToken |
|
| 1862 | + && $param[ 'pass_by_reference' ] === true |
|
| 1863 | 1863 | ) { |
| 1864 | 1864 | // Function parameter declared to be passed by reference. |
| 1865 | 1865 | return true; |
@@ -1868,14 +1868,14 @@ discard block |
||
| 1868 | 1868 | }//end if |
| 1869 | 1869 | } else { |
| 1870 | 1870 | $prev = false; |
| 1871 | - for ($t = ($this->tokens[$lastBracket]['parenthesis_opener'] - 1); $t >= 0; $t--) { |
|
| 1872 | - if ($this->tokens[$t]['code'] !== T_WHITESPACE) { |
|
| 1871 | + for ( $t = ( $this->tokens[ $lastBracket ][ 'parenthesis_opener' ] - 1 ); $t >= 0; $t-- ) { |
|
| 1872 | + if ( $this->tokens[ $t ][ 'code' ] !== T_WHITESPACE ) { |
|
| 1873 | 1873 | $prev = $t; |
| 1874 | 1874 | break; |
| 1875 | 1875 | } |
| 1876 | 1876 | } |
| 1877 | 1877 | |
| 1878 | - if ($prev !== false && $this->tokens[$prev]['code'] === T_USE) { |
|
| 1878 | + if ( $prev !== false && $this->tokens[ $prev ][ 'code' ] === T_USE ) { |
|
| 1879 | 1879 | // Closure use by reference. |
| 1880 | 1880 | return true; |
| 1881 | 1881 | } |
@@ -1883,29 +1883,29 @@ discard block |
||
| 1883 | 1883 | }//end if |
| 1884 | 1884 | |
| 1885 | 1885 | // Pass by reference in function calls and assign by reference in arrays. |
| 1886 | - if ($this->tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS |
|
| 1887 | - || $this->tokens[$tokenBefore]['code'] === T_COMMA |
|
| 1888 | - || $this->tokens[$tokenBefore]['code'] === T_OPEN_SHORT_ARRAY |
|
| 1886 | + if ( $this->tokens[ $tokenBefore ][ 'code' ] === T_OPEN_PARENTHESIS |
|
| 1887 | + || $this->tokens[ $tokenBefore ][ 'code' ] === T_COMMA |
|
| 1888 | + || $this->tokens[ $tokenBefore ][ 'code' ] === T_OPEN_SHORT_ARRAY |
|
| 1889 | 1889 | ) { |
| 1890 | - if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) { |
|
| 1890 | + if ( $this->tokens[ $tokenAfter ][ 'code' ] === T_VARIABLE ) { |
|
| 1891 | 1891 | return true; |
| 1892 | 1892 | } else { |
| 1893 | 1893 | $skip = Util\Tokens::$emptyTokens; |
| 1894 | - $skip[] = T_NS_SEPARATOR; |
|
| 1895 | - $skip[] = T_SELF; |
|
| 1896 | - $skip[] = T_PARENT; |
|
| 1897 | - $skip[] = T_STATIC; |
|
| 1898 | - $skip[] = T_STRING; |
|
| 1899 | - $skip[] = T_NAMESPACE; |
|
| 1900 | - $skip[] = T_DOUBLE_COLON; |
|
| 1894 | + $skip[ ] = T_NS_SEPARATOR; |
|
| 1895 | + $skip[ ] = T_SELF; |
|
| 1896 | + $skip[ ] = T_PARENT; |
|
| 1897 | + $skip[ ] = T_STATIC; |
|
| 1898 | + $skip[ ] = T_STRING; |
|
| 1899 | + $skip[ ] = T_NAMESPACE; |
|
| 1900 | + $skip[ ] = T_DOUBLE_COLON; |
|
| 1901 | 1901 | |
| 1902 | 1902 | $nextSignificantAfter = $this->findNext( |
| 1903 | 1903 | $skip, |
| 1904 | - ($stackPtr + 1), |
|
| 1904 | + ( $stackPtr + 1 ), |
|
| 1905 | 1905 | null, |
| 1906 | 1906 | true |
| 1907 | 1907 | ); |
| 1908 | - if ($this->tokens[$nextSignificantAfter]['code'] === T_VARIABLE) { |
|
| 1908 | + if ( $this->tokens[ $nextSignificantAfter ][ 'code' ] === T_VARIABLE ) { |
|
| 1909 | 1909 | return true; |
| 1910 | 1910 | } |
| 1911 | 1911 | }//end if |
@@ -1927,29 +1927,29 @@ discard block |
||
| 1927 | 1927 | * |
| 1928 | 1928 | * @return string The token contents. |
| 1929 | 1929 | */ |
| 1930 | - public function getTokensAsString($start, $length, $origContent=false) |
|
| 1930 | + public function getTokensAsString( $start, $length, $origContent = false ) |
|
| 1931 | 1931 | { |
| 1932 | - if (is_int($start) === false || isset($this->tokens[$start]) === false) { |
|
| 1933 | - throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack'); |
|
| 1932 | + if ( is_int( $start ) === false || isset( $this->tokens[ $start ] ) === false ) { |
|
| 1933 | + throw new RuntimeException( 'The $start position for getTokensAsString() must exist in the token stack' ); |
|
| 1934 | 1934 | } |
| 1935 | 1935 | |
| 1936 | - if (is_int($length) === false || $length <= 0) { |
|
| 1936 | + if ( is_int( $length ) === false || $length <= 0 ) { |
|
| 1937 | 1937 | return ''; |
| 1938 | 1938 | } |
| 1939 | 1939 | |
| 1940 | 1940 | $str = ''; |
| 1941 | - $end = ($start + $length); |
|
| 1942 | - if ($end > $this->numTokens) { |
|
| 1941 | + $end = ( $start + $length ); |
|
| 1942 | + if ( $end > $this->numTokens ) { |
|
| 1943 | 1943 | $end = $this->numTokens; |
| 1944 | 1944 | } |
| 1945 | 1945 | |
| 1946 | - for ($i = $start; $i < $end; $i++) { |
|
| 1946 | + for ( $i = $start; $i < $end; $i++ ) { |
|
| 1947 | 1947 | // If tabs are being converted to spaces by the tokeniser, the |
| 1948 | 1948 | // original content should be used instead of the converted content. |
| 1949 | - if ($origContent === true && isset($this->tokens[$i]['orig_content']) === true) { |
|
| 1950 | - $str .= $this->tokens[$i]['orig_content']; |
|
| 1949 | + if ( $origContent === true && isset( $this->tokens[ $i ][ 'orig_content' ] ) === true ) { |
|
| 1950 | + $str .= $this->tokens[ $i ][ 'orig_content' ]; |
|
| 1951 | 1951 | } else { |
| 1952 | - $str .= $this->tokens[$i]['content']; |
|
| 1952 | + $str .= $this->tokens[ $i ][ 'content' ]; |
|
| 1953 | 1953 | } |
| 1954 | 1954 | } |
| 1955 | 1955 | |
@@ -1987,48 +1987,48 @@ discard block |
||
| 1987 | 1987 | public function findPrevious( |
| 1988 | 1988 | $types, |
| 1989 | 1989 | $start, |
| 1990 | - $end=null, |
|
| 1991 | - $exclude=false, |
|
| 1992 | - $value=null, |
|
| 1993 | - $local=false |
|
| 1990 | + $end = null, |
|
| 1991 | + $exclude = false, |
|
| 1992 | + $value = null, |
|
| 1993 | + $local = false |
|
| 1994 | 1994 | ) { |
| 1995 | - $types = (array) $types; |
|
| 1995 | + $types = (array)$types; |
|
| 1996 | 1996 | |
| 1997 | - if ($end === null) { |
|
| 1997 | + if ( $end === null ) { |
|
| 1998 | 1998 | $end = 0; |
| 1999 | 1999 | } |
| 2000 | 2000 | |
| 2001 | - for ($i = $start; $i >= $end; $i--) { |
|
| 2002 | - $found = (bool) $exclude; |
|
| 2003 | - foreach ($types as $type) { |
|
| 2004 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2005 | - $found = !$exclude; |
|
| 2001 | + for ( $i = $start; $i >= $end; $i-- ) { |
|
| 2002 | + $found = (bool)$exclude; |
|
| 2003 | + foreach ( $types as $type ) { |
|
| 2004 | + if ( $this->tokens[ $i ][ 'code' ] === $type ) { |
|
| 2005 | + $found = ! $exclude; |
|
| 2006 | 2006 | break; |
| 2007 | 2007 | } |
| 2008 | 2008 | } |
| 2009 | 2009 | |
| 2010 | - if ($found === true) { |
|
| 2011 | - if ($value === null) { |
|
| 2010 | + if ( $found === true ) { |
|
| 2011 | + if ( $value === null ) { |
|
| 2012 | 2012 | return $i; |
| 2013 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2013 | + } else if ( $this->tokens[ $i ][ 'content' ] === $value ) { |
|
| 2014 | 2014 | return $i; |
| 2015 | 2015 | } |
| 2016 | 2016 | } |
| 2017 | 2017 | |
| 2018 | - if ($local === true) { |
|
| 2019 | - if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2020 | - && $i === $this->tokens[$i]['scope_closer'] |
|
| 2018 | + if ( $local === true ) { |
|
| 2019 | + if ( isset( $this->tokens[ $i ][ 'scope_opener' ] ) === true |
|
| 2020 | + && $i === $this->tokens[ $i ][ 'scope_closer' ] |
|
| 2021 | 2021 | ) { |
| 2022 | - $i = $this->tokens[$i]['scope_opener']; |
|
| 2023 | - } else if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2024 | - && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2022 | + $i = $this->tokens[ $i ][ 'scope_opener' ]; |
|
| 2023 | + } else if ( isset( $this->tokens[ $i ][ 'bracket_opener' ] ) === true |
|
| 2024 | + && $i === $this->tokens[ $i ][ 'bracket_closer' ] |
|
| 2025 | 2025 | ) { |
| 2026 | - $i = $this->tokens[$i]['bracket_opener']; |
|
| 2027 | - } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2028 | - && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2026 | + $i = $this->tokens[ $i ][ 'bracket_opener' ]; |
|
| 2027 | + } else if ( isset( $this->tokens[ $i ][ 'parenthesis_opener' ] ) === true |
|
| 2028 | + && $i === $this->tokens[ $i ][ 'parenthesis_closer' ] |
|
| 2029 | 2029 | ) { |
| 2030 | - $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2031 | - } else if ($this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2030 | + $i = $this->tokens[ $i ][ 'parenthesis_opener' ]; |
|
| 2031 | + } else if ( $this->tokens[ $i ][ 'code' ] === T_SEMICOLON ) { |
|
| 2032 | 2032 | break; |
| 2033 | 2033 | } |
| 2034 | 2034 | } |
@@ -2068,35 +2068,35 @@ discard block |
||
| 2068 | 2068 | public function findNext( |
| 2069 | 2069 | $types, |
| 2070 | 2070 | $start, |
| 2071 | - $end=null, |
|
| 2072 | - $exclude=false, |
|
| 2073 | - $value=null, |
|
| 2074 | - $local=false |
|
| 2071 | + $end = null, |
|
| 2072 | + $exclude = false, |
|
| 2073 | + $value = null, |
|
| 2074 | + $local = false |
|
| 2075 | 2075 | ) { |
| 2076 | - $types = (array) $types; |
|
| 2076 | + $types = (array)$types; |
|
| 2077 | 2077 | |
| 2078 | - if ($end === null || $end > $this->numTokens) { |
|
| 2078 | + if ( $end === null || $end > $this->numTokens ) { |
|
| 2079 | 2079 | $end = $this->numTokens; |
| 2080 | 2080 | } |
| 2081 | 2081 | |
| 2082 | - for ($i = $start; $i < $end; $i++) { |
|
| 2083 | - $found = (bool) $exclude; |
|
| 2084 | - foreach ($types as $type) { |
|
| 2085 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2086 | - $found = !$exclude; |
|
| 2082 | + for ( $i = $start; $i < $end; $i++ ) { |
|
| 2083 | + $found = (bool)$exclude; |
|
| 2084 | + foreach ( $types as $type ) { |
|
| 2085 | + if ( $this->tokens[ $i ][ 'code' ] === $type ) { |
|
| 2086 | + $found = ! $exclude; |
|
| 2087 | 2087 | break; |
| 2088 | 2088 | } |
| 2089 | 2089 | } |
| 2090 | 2090 | |
| 2091 | - if ($found === true) { |
|
| 2092 | - if ($value === null) { |
|
| 2091 | + if ( $found === true ) { |
|
| 2092 | + if ( $value === null ) { |
|
| 2093 | 2093 | return $i; |
| 2094 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2094 | + } else if ( $this->tokens[ $i ][ 'content' ] === $value ) { |
|
| 2095 | 2095 | return $i; |
| 2096 | 2096 | } |
| 2097 | 2097 | } |
| 2098 | 2098 | |
| 2099 | - if ($local === true && $this->tokens[$i]['code'] === T_SEMICOLON) { |
|
| 2099 | + if ( $local === true && $this->tokens[ $i ][ 'code' ] === T_SEMICOLON ) { |
|
| 2100 | 2100 | break; |
| 2101 | 2101 | } |
| 2102 | 2102 | }//end for |
@@ -2114,52 +2114,52 @@ discard block |
||
| 2114 | 2114 | * |
| 2115 | 2115 | * @return int |
| 2116 | 2116 | */ |
| 2117 | - public function findStartOfStatement($start, $ignore=null) |
|
| 2117 | + public function findStartOfStatement( $start, $ignore = null ) |
|
| 2118 | 2118 | { |
| 2119 | 2119 | $endTokens = Util\Tokens::$blockOpeners; |
| 2120 | 2120 | |
| 2121 | - $endTokens[T_COLON] = true; |
|
| 2122 | - $endTokens[T_COMMA] = true; |
|
| 2123 | - $endTokens[T_DOUBLE_ARROW] = true; |
|
| 2124 | - $endTokens[T_SEMICOLON] = true; |
|
| 2125 | - $endTokens[T_OPEN_TAG] = true; |
|
| 2126 | - $endTokens[T_CLOSE_TAG] = true; |
|
| 2127 | - $endTokens[T_OPEN_SHORT_ARRAY] = true; |
|
| 2128 | - |
|
| 2129 | - if ($ignore !== null) { |
|
| 2130 | - $ignore = (array) $ignore; |
|
| 2131 | - foreach ($ignore as $code) { |
|
| 2132 | - unset($endTokens[$code]); |
|
| 2121 | + $endTokens[ T_COLON ] = true; |
|
| 2122 | + $endTokens[ T_COMMA ] = true; |
|
| 2123 | + $endTokens[ T_DOUBLE_ARROW ] = true; |
|
| 2124 | + $endTokens[ T_SEMICOLON ] = true; |
|
| 2125 | + $endTokens[ T_OPEN_TAG ] = true; |
|
| 2126 | + $endTokens[ T_CLOSE_TAG ] = true; |
|
| 2127 | + $endTokens[ T_OPEN_SHORT_ARRAY ] = true; |
|
| 2128 | + |
|
| 2129 | + if ( $ignore !== null ) { |
|
| 2130 | + $ignore = (array)$ignore; |
|
| 2131 | + foreach ( $ignore as $code ) { |
|
| 2132 | + unset( $endTokens[ $code ] ); |
|
| 2133 | 2133 | } |
| 2134 | 2134 | } |
| 2135 | 2135 | |
| 2136 | 2136 | $lastNotEmpty = $start; |
| 2137 | 2137 | |
| 2138 | - for ($i = $start; $i >= 0; $i--) { |
|
| 2139 | - if (isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2138 | + for ( $i = $start; $i >= 0; $i-- ) { |
|
| 2139 | + if ( isset( $endTokens[ $this->tokens[ $i ][ 'code' ] ] ) === true ) { |
|
| 2140 | 2140 | // Found the end of the previous statement. |
| 2141 | 2141 | return $lastNotEmpty; |
| 2142 | 2142 | } |
| 2143 | 2143 | |
| 2144 | - if (isset($this->tokens[$i]['scope_opener']) === true |
|
| 2145 | - && $i === $this->tokens[$i]['scope_closer'] |
|
| 2144 | + if ( isset( $this->tokens[ $i ][ 'scope_opener' ] ) === true |
|
| 2145 | + && $i === $this->tokens[ $i ][ 'scope_closer' ] |
|
| 2146 | 2146 | ) { |
| 2147 | 2147 | // Found the end of the previous scope block. |
| 2148 | 2148 | return $lastNotEmpty; |
| 2149 | 2149 | } |
| 2150 | 2150 | |
| 2151 | 2151 | // Skip nested statements. |
| 2152 | - if (isset($this->tokens[$i]['bracket_opener']) === true |
|
| 2153 | - && $i === $this->tokens[$i]['bracket_closer'] |
|
| 2152 | + if ( isset( $this->tokens[ $i ][ 'bracket_opener' ] ) === true |
|
| 2153 | + && $i === $this->tokens[ $i ][ 'bracket_closer' ] |
|
| 2154 | 2154 | ) { |
| 2155 | - $i = $this->tokens[$i]['bracket_opener']; |
|
| 2156 | - } else if (isset($this->tokens[$i]['parenthesis_opener']) === true |
|
| 2157 | - && $i === $this->tokens[$i]['parenthesis_closer'] |
|
| 2155 | + $i = $this->tokens[ $i ][ 'bracket_opener' ]; |
|
| 2156 | + } else if ( isset( $this->tokens[ $i ][ 'parenthesis_opener' ] ) === true |
|
| 2157 | + && $i === $this->tokens[ $i ][ 'parenthesis_closer' ] |
|
| 2158 | 2158 | ) { |
| 2159 | - $i = $this->tokens[$i]['parenthesis_opener']; |
|
| 2159 | + $i = $this->tokens[ $i ][ 'parenthesis_opener' ]; |
|
| 2160 | 2160 | } |
| 2161 | 2161 | |
| 2162 | - if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2162 | + if ( isset( Util\Tokens::$emptyTokens[ $this->tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 2163 | 2163 | $lastNotEmpty = $i; |
| 2164 | 2164 | } |
| 2165 | 2165 | }//end for |
@@ -2177,7 +2177,7 @@ discard block |
||
| 2177 | 2177 | * |
| 2178 | 2178 | * @return int |
| 2179 | 2179 | */ |
| 2180 | - public function findEndOfStatement($start, $ignore=null) |
|
| 2180 | + public function findEndOfStatement( $start, $ignore = null ) |
|
| 2181 | 2181 | { |
| 2182 | 2182 | $endTokens = [ |
| 2183 | 2183 | T_COLON => true, |
@@ -2192,24 +2192,24 @@ discard block |
||
| 2192 | 2192 | T_CLOSE_TAG => true, |
| 2193 | 2193 | ]; |
| 2194 | 2194 | |
| 2195 | - if ($ignore !== null) { |
|
| 2196 | - $ignore = (array) $ignore; |
|
| 2197 | - foreach ($ignore as $code) { |
|
| 2198 | - unset($endTokens[$code]); |
|
| 2195 | + if ( $ignore !== null ) { |
|
| 2196 | + $ignore = (array)$ignore; |
|
| 2197 | + foreach ( $ignore as $code ) { |
|
| 2198 | + unset( $endTokens[ $code ] ); |
|
| 2199 | 2199 | } |
| 2200 | 2200 | } |
| 2201 | 2201 | |
| 2202 | 2202 | $lastNotEmpty = $start; |
| 2203 | 2203 | |
| 2204 | - for ($i = $start; $i < $this->numTokens; $i++) { |
|
| 2205 | - if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]) === true) { |
|
| 2204 | + for ( $i = $start; $i < $this->numTokens; $i++ ) { |
|
| 2205 | + if ( $i !== $start && isset( $endTokens[ $this->tokens[ $i ][ 'code' ] ] ) === true ) { |
|
| 2206 | 2206 | // Found the end of the statement. |
| 2207 | - if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 2208 | - || $this->tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET |
|
| 2209 | - || $this->tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET |
|
| 2210 | - || $this->tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 2211 | - || $this->tokens[$i]['code'] === T_OPEN_TAG |
|
| 2212 | - || $this->tokens[$i]['code'] === T_CLOSE_TAG |
|
| 2207 | + if ( $this->tokens[ $i ][ 'code' ] === T_CLOSE_PARENTHESIS |
|
| 2208 | + || $this->tokens[ $i ][ 'code' ] === T_CLOSE_SQUARE_BRACKET |
|
| 2209 | + || $this->tokens[ $i ][ 'code' ] === T_CLOSE_CURLY_BRACKET |
|
| 2210 | + || $this->tokens[ $i ][ 'code' ] === T_CLOSE_SHORT_ARRAY |
|
| 2211 | + || $this->tokens[ $i ][ 'code' ] === T_OPEN_TAG |
|
| 2212 | + || $this->tokens[ $i ][ 'code' ] === T_CLOSE_TAG |
|
| 2213 | 2213 | ) { |
| 2214 | 2214 | return $lastNotEmpty; |
| 2215 | 2215 | } |
@@ -2218,31 +2218,31 @@ discard block |
||
| 2218 | 2218 | } |
| 2219 | 2219 | |
| 2220 | 2220 | // Skip nested statements. |
| 2221 | - if (isset($this->tokens[$i]['scope_closer']) === true |
|
| 2222 | - && ($i === $this->tokens[$i]['scope_opener'] |
|
| 2223 | - || $i === $this->tokens[$i]['scope_condition']) |
|
| 2221 | + if ( isset( $this->tokens[ $i ][ 'scope_closer' ] ) === true |
|
| 2222 | + && ( $i === $this->tokens[ $i ][ 'scope_opener' ] |
|
| 2223 | + || $i === $this->tokens[ $i ][ 'scope_condition' ] ) |
|
| 2224 | 2224 | ) { |
| 2225 | - if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) { |
|
| 2226 | - return $this->tokens[$i]['scope_closer']; |
|
| 2225 | + if ( $i === $start && isset( Util\Tokens::$scopeOpeners[ $this->tokens[ $i ][ 'code' ] ] ) === true ) { |
|
| 2226 | + return $this->tokens[ $i ][ 'scope_closer' ]; |
|
| 2227 | 2227 | } |
| 2228 | 2228 | |
| 2229 | - $i = $this->tokens[$i]['scope_closer']; |
|
| 2230 | - } else if (isset($this->tokens[$i]['bracket_closer']) === true |
|
| 2231 | - && $i === $this->tokens[$i]['bracket_opener'] |
|
| 2229 | + $i = $this->tokens[ $i ][ 'scope_closer' ]; |
|
| 2230 | + } else if ( isset( $this->tokens[ $i ][ 'bracket_closer' ] ) === true |
|
| 2231 | + && $i === $this->tokens[ $i ][ 'bracket_opener' ] |
|
| 2232 | 2232 | ) { |
| 2233 | - $i = $this->tokens[$i]['bracket_closer']; |
|
| 2234 | - } else if (isset($this->tokens[$i]['parenthesis_closer']) === true |
|
| 2235 | - && $i === $this->tokens[$i]['parenthesis_opener'] |
|
| 2233 | + $i = $this->tokens[ $i ][ 'bracket_closer' ]; |
|
| 2234 | + } else if ( isset( $this->tokens[ $i ][ 'parenthesis_closer' ] ) === true |
|
| 2235 | + && $i === $this->tokens[ $i ][ 'parenthesis_opener' ] |
|
| 2236 | 2236 | ) { |
| 2237 | - $i = $this->tokens[$i]['parenthesis_closer']; |
|
| 2237 | + $i = $this->tokens[ $i ][ 'parenthesis_closer' ]; |
|
| 2238 | 2238 | } |
| 2239 | 2239 | |
| 2240 | - if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { |
|
| 2240 | + if ( isset( Util\Tokens::$emptyTokens[ $this->tokens[ $i ][ 'code' ] ] ) === false ) { |
|
| 2241 | 2241 | $lastNotEmpty = $i; |
| 2242 | 2242 | } |
| 2243 | 2243 | }//end for |
| 2244 | 2244 | |
| 2245 | - return ($this->numTokens - 1); |
|
| 2245 | + return ( $this->numTokens - 1 ); |
|
| 2246 | 2246 | |
| 2247 | 2247 | }//end findEndOfStatement() |
| 2248 | 2248 | |
@@ -2264,38 +2264,38 @@ discard block |
||
| 2264 | 2264 | * |
| 2265 | 2265 | * @return int | bool |
| 2266 | 2266 | */ |
| 2267 | - public function findFirstOnLine($types, $start, $exclude=false, $value=null) |
|
| 2267 | + public function findFirstOnLine( $types, $start, $exclude = false, $value = null ) |
|
| 2268 | 2268 | { |
| 2269 | - if (is_array($types) === false) { |
|
| 2270 | - $types = [$types]; |
|
| 2269 | + if ( is_array( $types ) === false ) { |
|
| 2270 | + $types = [ $types ]; |
|
| 2271 | 2271 | } |
| 2272 | 2272 | |
| 2273 | 2273 | $foundToken = false; |
| 2274 | 2274 | |
| 2275 | - for ($i = $start; $i >= 0; $i--) { |
|
| 2276 | - if ($this->tokens[$i]['line'] < $this->tokens[$start]['line']) { |
|
| 2275 | + for ( $i = $start; $i >= 0; $i-- ) { |
|
| 2276 | + if ( $this->tokens[ $i ][ 'line' ] < $this->tokens[ $start ][ 'line' ] ) { |
|
| 2277 | 2277 | break; |
| 2278 | 2278 | } |
| 2279 | 2279 | |
| 2280 | 2280 | $found = $exclude; |
| 2281 | - foreach ($types as $type) { |
|
| 2282 | - if ($exclude === false) { |
|
| 2283 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2281 | + foreach ( $types as $type ) { |
|
| 2282 | + if ( $exclude === false ) { |
|
| 2283 | + if ( $this->tokens[ $i ][ 'code' ] === $type ) { |
|
| 2284 | 2284 | $found = true; |
| 2285 | 2285 | break; |
| 2286 | 2286 | } |
| 2287 | 2287 | } else { |
| 2288 | - if ($this->tokens[$i]['code'] === $type) { |
|
| 2288 | + if ( $this->tokens[ $i ][ 'code' ] === $type ) { |
|
| 2289 | 2289 | $found = false; |
| 2290 | 2290 | break; |
| 2291 | 2291 | } |
| 2292 | 2292 | } |
| 2293 | 2293 | } |
| 2294 | 2294 | |
| 2295 | - if ($found === true) { |
|
| 2296 | - if ($value === null) { |
|
| 2295 | + if ( $found === true ) { |
|
| 2296 | + if ( $value === null ) { |
|
| 2297 | 2297 | $foundToken = $i; |
| 2298 | - } else if ($this->tokens[$i]['content'] === $value) { |
|
| 2298 | + } else if ( $this->tokens[ $i ][ 'content' ] === $value ) { |
|
| 2299 | 2299 | $foundToken = $i; |
| 2300 | 2300 | } |
| 2301 | 2301 | } |
@@ -2314,23 +2314,23 @@ discard block |
||
| 2314 | 2314 | * |
| 2315 | 2315 | * @return boolean |
| 2316 | 2316 | */ |
| 2317 | - public function hasCondition($stackPtr, $types) |
|
| 2317 | + public function hasCondition( $stackPtr, $types ) |
|
| 2318 | 2318 | { |
| 2319 | 2319 | // Check for the existence of the token. |
| 2320 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2320 | + if ( isset( $this->tokens[ $stackPtr ] ) === false ) { |
|
| 2321 | 2321 | return false; |
| 2322 | 2322 | } |
| 2323 | 2323 | |
| 2324 | 2324 | // Make sure the token has conditions. |
| 2325 | - if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2325 | + if ( isset( $this->tokens[ $stackPtr ][ 'conditions' ] ) === false ) { |
|
| 2326 | 2326 | return false; |
| 2327 | 2327 | } |
| 2328 | 2328 | |
| 2329 | - $types = (array) $types; |
|
| 2330 | - $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2329 | + $types = (array)$types; |
|
| 2330 | + $conditions = $this->tokens[ $stackPtr ][ 'conditions' ]; |
|
| 2331 | 2331 | |
| 2332 | - foreach ($types as $type) { |
|
| 2333 | - if (in_array($type, $conditions, true) === true) { |
|
| 2332 | + foreach ( $types as $type ) { |
|
| 2333 | + if ( in_array( $type, $conditions, true ) === true ) { |
|
| 2334 | 2334 | // We found a token with the required type. |
| 2335 | 2335 | return true; |
| 2336 | 2336 | } |
@@ -2351,21 +2351,21 @@ discard block |
||
| 2351 | 2351 | * |
| 2352 | 2352 | * @return int |
| 2353 | 2353 | */ |
| 2354 | - public function getCondition($stackPtr, $type) |
|
| 2354 | + public function getCondition( $stackPtr, $type ) |
|
| 2355 | 2355 | { |
| 2356 | 2356 | // Check for the existence of the token. |
| 2357 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2357 | + if ( isset( $this->tokens[ $stackPtr ] ) === false ) { |
|
| 2358 | 2358 | return false; |
| 2359 | 2359 | } |
| 2360 | 2360 | |
| 2361 | 2361 | // Make sure the token has conditions. |
| 2362 | - if (isset($this->tokens[$stackPtr]['conditions']) === false) { |
|
| 2362 | + if ( isset( $this->tokens[ $stackPtr ][ 'conditions' ] ) === false ) { |
|
| 2363 | 2363 | return false; |
| 2364 | 2364 | } |
| 2365 | 2365 | |
| 2366 | - $conditions = $this->tokens[$stackPtr]['conditions']; |
|
| 2367 | - foreach ($conditions as $token => $condition) { |
|
| 2368 | - if ($condition === $type) { |
|
| 2366 | + $conditions = $this->tokens[ $stackPtr ][ 'conditions' ]; |
|
| 2367 | + foreach ( $conditions as $token => $condition ) { |
|
| 2368 | + if ( $condition === $type ) { |
|
| 2369 | 2369 | return $token; |
| 2370 | 2370 | } |
| 2371 | 2371 | } |
@@ -2385,27 +2385,27 @@ discard block |
||
| 2385 | 2385 | * |
| 2386 | 2386 | * @return string|false |
| 2387 | 2387 | */ |
| 2388 | - public function findExtendedClassName($stackPtr) |
|
| 2388 | + public function findExtendedClassName( $stackPtr ) |
|
| 2389 | 2389 | { |
| 2390 | 2390 | // Check for the existence of the token. |
| 2391 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2391 | + if ( isset( $this->tokens[ $stackPtr ] ) === false ) { |
|
| 2392 | 2392 | return false; |
| 2393 | 2393 | } |
| 2394 | 2394 | |
| 2395 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2396 | - && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2397 | - && $this->tokens[$stackPtr]['code'] !== T_INTERFACE |
|
| 2395 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_CLASS |
|
| 2396 | + && $this->tokens[ $stackPtr ][ 'code' ] !== T_ANON_CLASS |
|
| 2397 | + && $this->tokens[ $stackPtr ][ 'code' ] !== T_INTERFACE |
|
| 2398 | 2398 | ) { |
| 2399 | 2399 | return false; |
| 2400 | 2400 | } |
| 2401 | 2401 | |
| 2402 | - if (isset($this->tokens[$stackPtr]['scope_opener']) === false) { |
|
| 2402 | + if ( isset( $this->tokens[ $stackPtr ][ 'scope_opener' ] ) === false ) { |
|
| 2403 | 2403 | return false; |
| 2404 | 2404 | } |
| 2405 | 2405 | |
| 2406 | - $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2407 | - $extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex); |
|
| 2408 | - if (false === $extendsIndex) { |
|
| 2406 | + $classOpenerIndex = $this->tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 2407 | + $extendsIndex = $this->findNext( T_EXTENDS, $stackPtr, $classOpenerIndex ); |
|
| 2408 | + if ( false === $extendsIndex ) { |
|
| 2409 | 2409 | return false; |
| 2410 | 2410 | } |
| 2411 | 2411 | |
@@ -2415,11 +2415,11 @@ discard block |
||
| 2415 | 2415 | T_WHITESPACE, |
| 2416 | 2416 | ]; |
| 2417 | 2417 | |
| 2418 | - $end = $this->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2419 | - $name = $this->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1)); |
|
| 2420 | - $name = trim($name); |
|
| 2418 | + $end = $this->findNext( $find, ( $extendsIndex + 1 ), ( $classOpenerIndex + 1 ), true ); |
|
| 2419 | + $name = $this->getTokensAsString( ( $extendsIndex + 1 ), ( $end - $extendsIndex - 1 ) ); |
|
| 2420 | + $name = trim( $name ); |
|
| 2421 | 2421 | |
| 2422 | - if ($name === '') { |
|
| 2422 | + if ( $name === '' ) { |
|
| 2423 | 2423 | return false; |
| 2424 | 2424 | } |
| 2425 | 2425 | |
@@ -2437,26 +2437,26 @@ discard block |
||
| 2437 | 2437 | * |
| 2438 | 2438 | * @return array|false |
| 2439 | 2439 | */ |
| 2440 | - public function findImplementedInterfaceNames($stackPtr) |
|
| 2440 | + public function findImplementedInterfaceNames( $stackPtr ) |
|
| 2441 | 2441 | { |
| 2442 | 2442 | // Check for the existence of the token. |
| 2443 | - if (isset($this->tokens[$stackPtr]) === false) { |
|
| 2443 | + if ( isset( $this->tokens[ $stackPtr ] ) === false ) { |
|
| 2444 | 2444 | return false; |
| 2445 | 2445 | } |
| 2446 | 2446 | |
| 2447 | - if ($this->tokens[$stackPtr]['code'] !== T_CLASS |
|
| 2448 | - && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS |
|
| 2447 | + if ( $this->tokens[ $stackPtr ][ 'code' ] !== T_CLASS |
|
| 2448 | + && $this->tokens[ $stackPtr ][ 'code' ] !== T_ANON_CLASS |
|
| 2449 | 2449 | ) { |
| 2450 | 2450 | return false; |
| 2451 | 2451 | } |
| 2452 | 2452 | |
| 2453 | - if (isset($this->tokens[$stackPtr]['scope_closer']) === false) { |
|
| 2453 | + if ( isset( $this->tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
| 2454 | 2454 | return false; |
| 2455 | 2455 | } |
| 2456 | 2456 | |
| 2457 | - $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener']; |
|
| 2458 | - $implementsIndex = $this->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
|
| 2459 | - if ($implementsIndex === false) { |
|
| 2457 | + $classOpenerIndex = $this->tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 2458 | + $implementsIndex = $this->findNext( T_IMPLEMENTS, $stackPtr, $classOpenerIndex ); |
|
| 2459 | + if ( $implementsIndex === false ) { |
|
| 2460 | 2460 | return false; |
| 2461 | 2461 | } |
| 2462 | 2462 | |
@@ -2467,15 +2467,15 @@ discard block |
||
| 2467 | 2467 | T_COMMA, |
| 2468 | 2468 | ]; |
| 2469 | 2469 | |
| 2470 | - $end = $this->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
|
| 2471 | - $name = $this->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
|
| 2472 | - $name = trim($name); |
|
| 2470 | + $end = $this->findNext( $find, ( $implementsIndex + 1 ), ( $classOpenerIndex + 1 ), true ); |
|
| 2471 | + $name = $this->getTokensAsString( ( $implementsIndex + 1 ), ( $end - $implementsIndex - 1 ) ); |
|
| 2472 | + $name = trim( $name ); |
|
| 2473 | 2473 | |
| 2474 | - if ($name === '') { |
|
| 2474 | + if ( $name === '' ) { |
|
| 2475 | 2475 | return false; |
| 2476 | 2476 | } else { |
| 2477 | - $names = explode(',', $name); |
|
| 2478 | - $names = array_map('trim', $names); |
|
| 2477 | + $names = explode( ',', $name ); |
|
| 2478 | + $names = array_map( 'trim', $names ); |
|
| 2479 | 2479 | return $names; |
| 2480 | 2480 | } |
| 2481 | 2481 | |
@@ -16,8 +16,7 @@ discard block |
||
| 16 | 16 | use PHP_CodeSniffer\Exceptions\RuntimeException; |
| 17 | 17 | use PHP_CodeSniffer\Exceptions\TokenizerException; |
| 18 | 18 | |
| 19 | -class File |
|
| 20 | -{ |
|
| 19 | +class File { |
|
| 21 | 20 | |
| 22 | 21 | /** |
| 23 | 22 | * The absolute path to the file associated with this object. |
@@ -224,8 +223,7 @@ discard block |
||
| 224 | 223 | * |
| 225 | 224 | * @return void |
| 226 | 225 | */ |
| 227 | - public function __construct($path, Ruleset $ruleset, Config $config) |
|
| 228 | - { |
|
| 226 | + public function __construct($path, Ruleset $ruleset, Config $config) { |
|
| 229 | 227 | $this->path = $path; |
| 230 | 228 | $this->ruleset = $ruleset; |
| 231 | 229 | $this->config = $config; |
@@ -261,8 +259,7 @@ discard block |
||
| 261 | 259 | * |
| 262 | 260 | * @return void |
| 263 | 261 | */ |
| 264 | - public function setContent($content) |
|
| 265 | - { |
|
| 262 | + public function setContent($content) { |
|
| 266 | 263 | $this->content = $content; |
| 267 | 264 | $this->tokens = []; |
| 268 | 265 | |
@@ -284,8 +281,7 @@ discard block |
||
| 284 | 281 | * |
| 285 | 282 | * @return void |
| 286 | 283 | */ |
| 287 | - public function reloadContent() |
|
| 288 | - { |
|
| 284 | + public function reloadContent() { |
|
| 289 | 285 | |
| 290 | 286 | }//end reloadContent() |
| 291 | 287 | |
@@ -295,8 +291,7 @@ discard block |
||
| 295 | 291 | * |
| 296 | 292 | * @return void |
| 297 | 293 | */ |
| 298 | - public function disableCaching() |
|
| 299 | - { |
|
| 294 | + public function disableCaching() { |
|
| 300 | 295 | $this->configCache['cache'] = false; |
| 301 | 296 | |
| 302 | 297 | }//end disableCaching() |
@@ -307,8 +302,7 @@ discard block |
||
| 307 | 302 | * |
| 308 | 303 | * @return void |
| 309 | 304 | */ |
| 310 | - public function process() |
|
| 311 | - { |
|
| 305 | + public function process() { |
|
| 312 | 306 | if ($this->ignored === true) { |
| 313 | 307 | return; |
| 314 | 308 | } |
@@ -544,8 +538,7 @@ discard block |
||
| 544 | 538 | * |
| 545 | 539 | * @return void |
| 546 | 540 | */ |
| 547 | - public function parse() |
|
| 548 | - { |
|
| 541 | + public function parse() { |
|
| 549 | 542 | if (empty($this->tokens) === false) { |
| 550 | 543 | // File has already been parsed. |
| 551 | 544 | return; |
@@ -605,8 +598,7 @@ discard block |
||
| 605 | 598 | * |
| 606 | 599 | * @return array |
| 607 | 600 | */ |
| 608 | - public function getTokens() |
|
| 609 | - { |
|
| 601 | + public function getTokens() { |
|
| 610 | 602 | return $this->tokens; |
| 611 | 603 | |
| 612 | 604 | }//end getTokens() |
@@ -617,8 +609,7 @@ discard block |
||
| 617 | 609 | * |
| 618 | 610 | * @return void |
| 619 | 611 | */ |
| 620 | - public function cleanUp() |
|
| 621 | - { |
|
| 612 | + public function cleanUp() { |
|
| 622 | 613 | $this->listenerTimes = null; |
| 623 | 614 | $this->content = null; |
| 624 | 615 | $this->tokens = null; |
@@ -824,8 +815,7 @@ discard block |
||
| 824 | 815 | * |
| 825 | 816 | * @return boolean |
| 826 | 817 | */ |
| 827 | - protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
|
| 828 | - { |
|
| 818 | + protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) { |
|
| 829 | 819 | // Check if this line is ignoring all message codes. |
| 830 | 820 | if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) { |
| 831 | 821 | return false; |
@@ -1077,8 +1067,7 @@ discard block |
||
| 1077 | 1067 | * |
| 1078 | 1068 | * @return boolean |
| 1079 | 1069 | */ |
| 1080 | - public function recordMetric($stackPtr, $metric, $value) |
|
| 1081 | - { |
|
| 1070 | + public function recordMetric($stackPtr, $metric, $value) { |
|
| 1082 | 1071 | if (isset($this->metrics[$metric]) === false) { |
| 1083 | 1072 | $this->metrics[$metric] = ['values' => [$value => 1]]; |
| 1084 | 1073 | $this->metricTokens[$metric][$stackPtr] = true; |
@@ -1101,8 +1090,7 @@ discard block |
||
| 1101 | 1090 | * |
| 1102 | 1091 | * @return int |
| 1103 | 1092 | */ |
| 1104 | - public function getErrorCount() |
|
| 1105 | - { |
|
| 1093 | + public function getErrorCount() { |
|
| 1106 | 1094 | return $this->errorCount; |
| 1107 | 1095 | |
| 1108 | 1096 | }//end getErrorCount() |
@@ -1113,8 +1101,7 @@ discard block |
||
| 1113 | 1101 | * |
| 1114 | 1102 | * @return int |
| 1115 | 1103 | */ |
| 1116 | - public function getWarningCount() |
|
| 1117 | - { |
|
| 1104 | + public function getWarningCount() { |
|
| 1118 | 1105 | return $this->warningCount; |
| 1119 | 1106 | |
| 1120 | 1107 | }//end getWarningCount() |
@@ -1125,8 +1112,7 @@ discard block |
||
| 1125 | 1112 | * |
| 1126 | 1113 | * @return int |
| 1127 | 1114 | */ |
| 1128 | - public function getFixableCount() |
|
| 1129 | - { |
|
| 1115 | + public function getFixableCount() { |
|
| 1130 | 1116 | return $this->fixableCount; |
| 1131 | 1117 | |
| 1132 | 1118 | }//end getFixableCount() |
@@ -1137,8 +1123,7 @@ discard block |
||
| 1137 | 1123 | * |
| 1138 | 1124 | * @return int |
| 1139 | 1125 | */ |
| 1140 | - public function getFixedCount() |
|
| 1141 | - { |
|
| 1126 | + public function getFixedCount() { |
|
| 1142 | 1127 | return $this->fixedCount; |
| 1143 | 1128 | |
| 1144 | 1129 | }//end getFixedCount() |
@@ -1149,8 +1134,7 @@ discard block |
||
| 1149 | 1134 | * |
| 1150 | 1135 | * @return array |
| 1151 | 1136 | */ |
| 1152 | - public function getIgnoredLines() |
|
| 1153 | - { |
|
| 1137 | + public function getIgnoredLines() { |
|
| 1154 | 1138 | return $this->tokenizer->ignoredLines; |
| 1155 | 1139 | |
| 1156 | 1140 | }//end getIgnoredLines() |
@@ -1161,8 +1145,7 @@ discard block |
||
| 1161 | 1145 | * |
| 1162 | 1146 | * @return array |
| 1163 | 1147 | */ |
| 1164 | - public function getErrors() |
|
| 1165 | - { |
|
| 1148 | + public function getErrors() { |
|
| 1166 | 1149 | return $this->errors; |
| 1167 | 1150 | |
| 1168 | 1151 | }//end getErrors() |
@@ -1173,8 +1156,7 @@ discard block |
||
| 1173 | 1156 | * |
| 1174 | 1157 | * @return array |
| 1175 | 1158 | */ |
| 1176 | - public function getWarnings() |
|
| 1177 | - { |
|
| 1159 | + public function getWarnings() { |
|
| 1178 | 1160 | return $this->warnings; |
| 1179 | 1161 | |
| 1180 | 1162 | }//end getWarnings() |
@@ -1185,8 +1167,7 @@ discard block |
||
| 1185 | 1167 | * |
| 1186 | 1168 | * @return array |
| 1187 | 1169 | */ |
| 1188 | - public function getMetrics() |
|
| 1189 | - { |
|
| 1170 | + public function getMetrics() { |
|
| 1190 | 1171 | return $this->metrics; |
| 1191 | 1172 | |
| 1192 | 1173 | }//end getMetrics() |
@@ -1197,8 +1178,7 @@ discard block |
||
| 1197 | 1178 | * |
| 1198 | 1179 | * @return string |
| 1199 | 1180 | */ |
| 1200 | - public function getFilename() |
|
| 1201 | - { |
|
| 1181 | + public function getFilename() { |
|
| 1202 | 1182 | return $this->path; |
| 1203 | 1183 | |
| 1204 | 1184 | }//end getFilename() |
@@ -1216,8 +1196,7 @@ discard block |
||
| 1216 | 1196 | * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
| 1217 | 1197 | * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
| 1218 | 1198 | */ |
| 1219 | - public function getDeclarationName($stackPtr) |
|
| 1220 | - { |
|
| 1199 | + public function getDeclarationName($stackPtr) { |
|
| 1221 | 1200 | $tokenCode = $this->tokens[$stackPtr]['code']; |
| 1222 | 1201 | |
| 1223 | 1202 | if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { |
@@ -1282,8 +1261,7 @@ discard block |
||
| 1282 | 1261 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
| 1283 | 1262 | * type T_FUNCTION or T_CLOSURE. |
| 1284 | 1263 | */ |
| 1285 | - public function getMethodParameters($stackPtr) |
|
| 1286 | - { |
|
| 1264 | + public function getMethodParameters($stackPtr) { |
|
| 1287 | 1265 | if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
| 1288 | 1266 | && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
| 1289 | 1267 | ) { |
@@ -1474,8 +1452,7 @@ discard block |
||
| 1474 | 1452 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
| 1475 | 1453 | * T_FUNCTION token. |
| 1476 | 1454 | */ |
| 1477 | - public function getMethodProperties($stackPtr) |
|
| 1478 | - { |
|
| 1455 | + public function getMethodProperties($stackPtr) { |
|
| 1479 | 1456 | if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION |
| 1480 | 1457 | && $this->tokens[$stackPtr]['code'] !== T_CLOSURE |
| 1481 | 1458 | ) { |
@@ -1624,8 +1601,7 @@ discard block |
||
| 1624 | 1601 | * T_VARIABLE token, or if the position is not |
| 1625 | 1602 | * a class member variable. |
| 1626 | 1603 | */ |
| 1627 | - public function getMemberProperties($stackPtr) |
|
| 1628 | - { |
|
| 1604 | + public function getMemberProperties($stackPtr) { |
|
| 1629 | 1605 | if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) { |
| 1630 | 1606 | throw new TokenizerException('$stackPtr must be of type T_VARIABLE'); |
| 1631 | 1607 | } |
@@ -1742,8 +1718,7 @@ discard block |
||
| 1742 | 1718 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
| 1743 | 1719 | * T_CLASS token. |
| 1744 | 1720 | */ |
| 1745 | - public function getClassProperties($stackPtr) |
|
| 1746 | - { |
|
| 1721 | + public function getClassProperties($stackPtr) { |
|
| 1747 | 1722 | if ($this->tokens[$stackPtr]['code'] !== T_CLASS) { |
| 1748 | 1723 | throw new TokenizerException('$stackPtr must be of type T_CLASS'); |
| 1749 | 1724 | } |
@@ -1793,8 +1768,7 @@ discard block |
||
| 1793 | 1768 | * |
| 1794 | 1769 | * @return boolean |
| 1795 | 1770 | */ |
| 1796 | - public function isReference($stackPtr) |
|
| 1797 | - { |
|
| 1771 | + public function isReference($stackPtr) { |
|
| 1798 | 1772 | if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) { |
| 1799 | 1773 | return false; |
| 1800 | 1774 | } |
@@ -1927,8 +1901,7 @@ discard block |
||
| 1927 | 1901 | * |
| 1928 | 1902 | * @return string The token contents. |
| 1929 | 1903 | */ |
| 1930 | - public function getTokensAsString($start, $length, $origContent=false) |
|
| 1931 | - { |
|
| 1904 | + public function getTokensAsString($start, $length, $origContent=false) { |
|
| 1932 | 1905 | if (is_int($start) === false || isset($this->tokens[$start]) === false) { |
| 1933 | 1906 | throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack'); |
| 1934 | 1907 | } |
@@ -2114,8 +2087,7 @@ discard block |
||
| 2114 | 2087 | * |
| 2115 | 2088 | * @return int |
| 2116 | 2089 | */ |
| 2117 | - public function findStartOfStatement($start, $ignore=null) |
|
| 2118 | - { |
|
| 2090 | + public function findStartOfStatement($start, $ignore=null) { |
|
| 2119 | 2091 | $endTokens = Util\Tokens::$blockOpeners; |
| 2120 | 2092 | |
| 2121 | 2093 | $endTokens[T_COLON] = true; |
@@ -2177,8 +2149,7 @@ discard block |
||
| 2177 | 2149 | * |
| 2178 | 2150 | * @return int |
| 2179 | 2151 | */ |
| 2180 | - public function findEndOfStatement($start, $ignore=null) |
|
| 2181 | - { |
|
| 2152 | + public function findEndOfStatement($start, $ignore=null) { |
|
| 2182 | 2153 | $endTokens = [ |
| 2183 | 2154 | T_COLON => true, |
| 2184 | 2155 | T_COMMA => true, |
@@ -2264,8 +2235,7 @@ discard block |
||
| 2264 | 2235 | * |
| 2265 | 2236 | * @return int | bool |
| 2266 | 2237 | */ |
| 2267 | - public function findFirstOnLine($types, $start, $exclude=false, $value=null) |
|
| 2268 | - { |
|
| 2238 | + public function findFirstOnLine($types, $start, $exclude=false, $value=null) { |
|
| 2269 | 2239 | if (is_array($types) === false) { |
| 2270 | 2240 | $types = [$types]; |
| 2271 | 2241 | } |
@@ -2314,8 +2284,7 @@ discard block |
||
| 2314 | 2284 | * |
| 2315 | 2285 | * @return boolean |
| 2316 | 2286 | */ |
| 2317 | - public function hasCondition($stackPtr, $types) |
|
| 2318 | - { |
|
| 2287 | + public function hasCondition($stackPtr, $types) { |
|
| 2319 | 2288 | // Check for the existence of the token. |
| 2320 | 2289 | if (isset($this->tokens[$stackPtr]) === false) { |
| 2321 | 2290 | return false; |
@@ -2351,8 +2320,7 @@ discard block |
||
| 2351 | 2320 | * |
| 2352 | 2321 | * @return int |
| 2353 | 2322 | */ |
| 2354 | - public function getCondition($stackPtr, $type) |
|
| 2355 | - { |
|
| 2323 | + public function getCondition($stackPtr, $type) { |
|
| 2356 | 2324 | // Check for the existence of the token. |
| 2357 | 2325 | if (isset($this->tokens[$stackPtr]) === false) { |
| 2358 | 2326 | return false; |
@@ -2385,8 +2353,7 @@ discard block |
||
| 2385 | 2353 | * |
| 2386 | 2354 | * @return string|false |
| 2387 | 2355 | */ |
| 2388 | - public function findExtendedClassName($stackPtr) |
|
| 2389 | - { |
|
| 2356 | + public function findExtendedClassName($stackPtr) { |
|
| 2390 | 2357 | // Check for the existence of the token. |
| 2391 | 2358 | if (isset($this->tokens[$stackPtr]) === false) { |
| 2392 | 2359 | return false; |
@@ -2437,8 +2404,7 @@ discard block |
||
| 2437 | 2404 | * |
| 2438 | 2405 | * @return array|false |
| 2439 | 2406 | */ |
| 2440 | - public function findImplementedInterfaceNames($stackPtr) |
|
| 2441 | - { |
|
| 2407 | + public function findImplementedInterfaceNames($stackPtr) { |
|
| 2442 | 2408 | // Check for the existence of the token. |
| 2443 | 2409 | if (isset($this->tokens[$stackPtr]) === false) { |
| 2444 | 2410 | return false; |
@@ -195,7 +195,7 @@ |
||
| 195 | 195 | /** |
| 196 | 196 | * Return the file path of the current file being processed. |
| 197 | 197 | * |
| 198 | - * @return void |
|
| 198 | + * @return string |
|
| 199 | 199 | */ |
| 200 | 200 | public function key() |
| 201 | 201 | { |
@@ -20,228 +20,228 @@ |
||
| 20 | 20 | class FileList implements \Iterator, \Countable |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A list of file paths that are included in the list. |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $files = []; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The number of files in the list. |
|
| 32 | - * |
|
| 33 | - * @var integer |
|
| 34 | - */ |
|
| 35 | - private $numFiles = 0; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The config data for the run. |
|
| 39 | - * |
|
| 40 | - * @var \PHP_CodeSniffer\Config |
|
| 41 | - */ |
|
| 42 | - public $config = null; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The ruleset used for the run. |
|
| 46 | - * |
|
| 47 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 48 | - */ |
|
| 49 | - public $ruleset = null; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * An array of patterns to use for skipping files. |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $ignorePatterns = []; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Constructs a file list and loads in an array of file paths to process. |
|
| 61 | - * |
|
| 62 | - * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 63 | - * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function __construct(Config $config, Ruleset $ruleset) |
|
| 68 | - { |
|
| 69 | - $this->ruleset = $ruleset; |
|
| 70 | - $this->config = $config; |
|
| 71 | - |
|
| 72 | - $paths = $config->files; |
|
| 73 | - foreach ($paths as $path) { |
|
| 74 | - $isPharFile = Util\Common::isPharFile($path); |
|
| 75 | - if (is_dir($path) === true || $isPharFile === true) { |
|
| 76 | - if ($isPharFile === true) { |
|
| 77 | - $path = 'phar://'.$path; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $filterClass = $this->getFilterClass(); |
|
| 81 | - |
|
| 82 | - $di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)); |
|
| 83 | - $filter = new $filterClass($di, $path, $config, $ruleset); |
|
| 84 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 85 | - |
|
| 86 | - foreach ($iterator as $file) { |
|
| 87 | - $this->files[$file->getPathname()] = null; |
|
| 88 | - $this->numFiles++; |
|
| 89 | - } |
|
| 90 | - } else { |
|
| 91 | - $this->addFile($path); |
|
| 92 | - }//end if |
|
| 93 | - }//end foreach |
|
| 94 | - |
|
| 95 | - reset($this->files); |
|
| 96 | - |
|
| 97 | - }//end __construct() |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Add a file to the list. |
|
| 102 | - * |
|
| 103 | - * If a file object has already been created, it can be passed here. |
|
| 104 | - * If it is left NULL, it will be created when accessed. |
|
| 105 | - * |
|
| 106 | - * @param string $path The path to the file being added. |
|
| 107 | - * @param \PHP_CodeSniffer\Files\File $file The file being added. |
|
| 108 | - * |
|
| 109 | - * @return void |
|
| 110 | - */ |
|
| 111 | - public function addFile($path, $file=null) |
|
| 112 | - { |
|
| 113 | - // No filtering is done for STDIN when the filename |
|
| 114 | - // has not been specified. |
|
| 115 | - if ($path === 'STDIN') { |
|
| 116 | - $this->files[$path] = $file; |
|
| 117 | - $this->numFiles++; |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $filterClass = $this->getFilterClass(); |
|
| 122 | - |
|
| 123 | - $di = new \RecursiveArrayIterator([$path]); |
|
| 124 | - $filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
|
| 125 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 126 | - |
|
| 127 | - foreach ($iterator as $path) { |
|
| 128 | - $this->files[$path] = $file; |
|
| 129 | - $this->numFiles++; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - }//end addFile() |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Get the class name of the filter being used for the run. |
|
| 137 | - * |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - private function getFilterClass() |
|
| 141 | - { |
|
| 142 | - $filterType = $this->config->filter; |
|
| 143 | - |
|
| 144 | - if ($filterType === null) { |
|
| 145 | - $filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
|
| 146 | - } else { |
|
| 147 | - if (strpos($filterType, '.') !== false) { |
|
| 148 | - // This is a path to a custom filter class. |
|
| 149 | - $filename = realpath($filterType); |
|
| 150 | - if ($filename === false) { |
|
| 151 | - $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
|
| 152 | - throw new DeepExitException($error, 3); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $filterClass = Autoload::loadFile($filename); |
|
| 156 | - } else { |
|
| 157 | - $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return $filterClass; |
|
| 162 | - |
|
| 163 | - }//end getFilterClass() |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Rewind the iterator to the first file. |
|
| 168 | - * |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public function rewind() |
|
| 172 | - { |
|
| 173 | - reset($this->files); |
|
| 174 | - |
|
| 175 | - }//end rewind() |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get the file that is currently being processed. |
|
| 180 | - * |
|
| 181 | - * @return \PHP_CodeSniffer\Files\File |
|
| 182 | - */ |
|
| 183 | - public function current() |
|
| 184 | - { |
|
| 185 | - $path = key($this->files); |
|
| 186 | - if ($this->files[$path] === null) { |
|
| 187 | - $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $this->files[$path]; |
|
| 191 | - |
|
| 192 | - }//end current() |
|
| 193 | - |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Return the file path of the current file being processed. |
|
| 197 | - * |
|
| 198 | - * @return void |
|
| 199 | - */ |
|
| 200 | - public function key() |
|
| 201 | - { |
|
| 202 | - return key($this->files); |
|
| 203 | - |
|
| 204 | - }//end key() |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Move forward to the next file. |
|
| 209 | - * |
|
| 210 | - * @return void |
|
| 211 | - */ |
|
| 212 | - public function next() |
|
| 213 | - { |
|
| 214 | - next($this->files); |
|
| 215 | - |
|
| 216 | - }//end next() |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Checks if current position is valid. |
|
| 221 | - * |
|
| 222 | - * @return boolean |
|
| 223 | - */ |
|
| 224 | - public function valid() |
|
| 225 | - { |
|
| 226 | - if (current($this->files) === false) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return true; |
|
| 231 | - |
|
| 232 | - }//end valid() |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Return the number of files in the list. |
|
| 237 | - * |
|
| 238 | - * @return integer |
|
| 239 | - */ |
|
| 240 | - public function count() |
|
| 241 | - { |
|
| 242 | - return $this->numFiles; |
|
| 243 | - |
|
| 244 | - }//end count() |
|
| 23 | + /** |
|
| 24 | + * A list of file paths that are included in the list. |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $files = []; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The number of files in the list. |
|
| 32 | + * |
|
| 33 | + * @var integer |
|
| 34 | + */ |
|
| 35 | + private $numFiles = 0; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The config data for the run. |
|
| 39 | + * |
|
| 40 | + * @var \PHP_CodeSniffer\Config |
|
| 41 | + */ |
|
| 42 | + public $config = null; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The ruleset used for the run. |
|
| 46 | + * |
|
| 47 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 48 | + */ |
|
| 49 | + public $ruleset = null; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * An array of patterns to use for skipping files. |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $ignorePatterns = []; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Constructs a file list and loads in an array of file paths to process. |
|
| 61 | + * |
|
| 62 | + * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 63 | + * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function __construct(Config $config, Ruleset $ruleset) |
|
| 68 | + { |
|
| 69 | + $this->ruleset = $ruleset; |
|
| 70 | + $this->config = $config; |
|
| 71 | + |
|
| 72 | + $paths = $config->files; |
|
| 73 | + foreach ($paths as $path) { |
|
| 74 | + $isPharFile = Util\Common::isPharFile($path); |
|
| 75 | + if (is_dir($path) === true || $isPharFile === true) { |
|
| 76 | + if ($isPharFile === true) { |
|
| 77 | + $path = 'phar://'.$path; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $filterClass = $this->getFilterClass(); |
|
| 81 | + |
|
| 82 | + $di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)); |
|
| 83 | + $filter = new $filterClass($di, $path, $config, $ruleset); |
|
| 84 | + $iterator = new \RecursiveIteratorIterator($filter); |
|
| 85 | + |
|
| 86 | + foreach ($iterator as $file) { |
|
| 87 | + $this->files[$file->getPathname()] = null; |
|
| 88 | + $this->numFiles++; |
|
| 89 | + } |
|
| 90 | + } else { |
|
| 91 | + $this->addFile($path); |
|
| 92 | + }//end if |
|
| 93 | + }//end foreach |
|
| 94 | + |
|
| 95 | + reset($this->files); |
|
| 96 | + |
|
| 97 | + }//end __construct() |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Add a file to the list. |
|
| 102 | + * |
|
| 103 | + * If a file object has already been created, it can be passed here. |
|
| 104 | + * If it is left NULL, it will be created when accessed. |
|
| 105 | + * |
|
| 106 | + * @param string $path The path to the file being added. |
|
| 107 | + * @param \PHP_CodeSniffer\Files\File $file The file being added. |
|
| 108 | + * |
|
| 109 | + * @return void |
|
| 110 | + */ |
|
| 111 | + public function addFile($path, $file=null) |
|
| 112 | + { |
|
| 113 | + // No filtering is done for STDIN when the filename |
|
| 114 | + // has not been specified. |
|
| 115 | + if ($path === 'STDIN') { |
|
| 116 | + $this->files[$path] = $file; |
|
| 117 | + $this->numFiles++; |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $filterClass = $this->getFilterClass(); |
|
| 122 | + |
|
| 123 | + $di = new \RecursiveArrayIterator([$path]); |
|
| 124 | + $filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
|
| 125 | + $iterator = new \RecursiveIteratorIterator($filter); |
|
| 126 | + |
|
| 127 | + foreach ($iterator as $path) { |
|
| 128 | + $this->files[$path] = $file; |
|
| 129 | + $this->numFiles++; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + }//end addFile() |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Get the class name of the filter being used for the run. |
|
| 137 | + * |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + private function getFilterClass() |
|
| 141 | + { |
|
| 142 | + $filterType = $this->config->filter; |
|
| 143 | + |
|
| 144 | + if ($filterType === null) { |
|
| 145 | + $filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
|
| 146 | + } else { |
|
| 147 | + if (strpos($filterType, '.') !== false) { |
|
| 148 | + // This is a path to a custom filter class. |
|
| 149 | + $filename = realpath($filterType); |
|
| 150 | + if ($filename === false) { |
|
| 151 | + $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
|
| 152 | + throw new DeepExitException($error, 3); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $filterClass = Autoload::loadFile($filename); |
|
| 156 | + } else { |
|
| 157 | + $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return $filterClass; |
|
| 162 | + |
|
| 163 | + }//end getFilterClass() |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Rewind the iterator to the first file. |
|
| 168 | + * |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public function rewind() |
|
| 172 | + { |
|
| 173 | + reset($this->files); |
|
| 174 | + |
|
| 175 | + }//end rewind() |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get the file that is currently being processed. |
|
| 180 | + * |
|
| 181 | + * @return \PHP_CodeSniffer\Files\File |
|
| 182 | + */ |
|
| 183 | + public function current() |
|
| 184 | + { |
|
| 185 | + $path = key($this->files); |
|
| 186 | + if ($this->files[$path] === null) { |
|
| 187 | + $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $this->files[$path]; |
|
| 191 | + |
|
| 192 | + }//end current() |
|
| 193 | + |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Return the file path of the current file being processed. |
|
| 197 | + * |
|
| 198 | + * @return void |
|
| 199 | + */ |
|
| 200 | + public function key() |
|
| 201 | + { |
|
| 202 | + return key($this->files); |
|
| 203 | + |
|
| 204 | + }//end key() |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Move forward to the next file. |
|
| 209 | + * |
|
| 210 | + * @return void |
|
| 211 | + */ |
|
| 212 | + public function next() |
|
| 213 | + { |
|
| 214 | + next($this->files); |
|
| 215 | + |
|
| 216 | + }//end next() |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Checks if current position is valid. |
|
| 221 | + * |
|
| 222 | + * @return boolean |
|
| 223 | + */ |
|
| 224 | + public function valid() |
|
| 225 | + { |
|
| 226 | + if (current($this->files) === false) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return true; |
|
| 231 | + |
|
| 232 | + }//end valid() |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Return the number of files in the list. |
|
| 237 | + * |
|
| 238 | + * @return integer |
|
| 239 | + */ |
|
| 240 | + public function count() |
|
| 241 | + { |
|
| 242 | + return $this->numFiles; |
|
| 243 | + |
|
| 244 | + }//end count() |
|
| 245 | 245 | |
| 246 | 246 | |
| 247 | 247 | }//end class |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @var array |
| 27 | 27 | */ |
| 28 | - private $files = []; |
|
| 28 | + private $files = [ ]; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * The number of files in the list. |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @var array |
| 55 | 55 | */ |
| 56 | - protected $ignorePatterns = []; |
|
| 56 | + protected $ignorePatterns = [ ]; |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -64,35 +64,35 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return void |
| 66 | 66 | */ |
| 67 | - public function __construct(Config $config, Ruleset $ruleset) |
|
| 67 | + public function __construct( Config $config, Ruleset $ruleset ) |
|
| 68 | 68 | { |
| 69 | 69 | $this->ruleset = $ruleset; |
| 70 | 70 | $this->config = $config; |
| 71 | 71 | |
| 72 | 72 | $paths = $config->files; |
| 73 | - foreach ($paths as $path) { |
|
| 74 | - $isPharFile = Util\Common::isPharFile($path); |
|
| 75 | - if (is_dir($path) === true || $isPharFile === true) { |
|
| 76 | - if ($isPharFile === true) { |
|
| 77 | - $path = 'phar://'.$path; |
|
| 73 | + foreach ( $paths as $path ) { |
|
| 74 | + $isPharFile = Util\Common::isPharFile( $path ); |
|
| 75 | + if ( is_dir( $path ) === true || $isPharFile === true ) { |
|
| 76 | + if ( $isPharFile === true ) { |
|
| 77 | + $path = 'phar://' . $path; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | $filterClass = $this->getFilterClass(); |
| 81 | 81 | |
| 82 | - $di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)); |
|
| 83 | - $filter = new $filterClass($di, $path, $config, $ruleset); |
|
| 84 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 82 | + $di = new \RecursiveDirectoryIterator( $path, ( \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ) ); |
|
| 83 | + $filter = new $filterClass( $di, $path, $config, $ruleset ); |
|
| 84 | + $iterator = new \RecursiveIteratorIterator( $filter ); |
|
| 85 | 85 | |
| 86 | - foreach ($iterator as $file) { |
|
| 87 | - $this->files[$file->getPathname()] = null; |
|
| 86 | + foreach ( $iterator as $file ) { |
|
| 87 | + $this->files[ $file->getPathname() ] = null; |
|
| 88 | 88 | $this->numFiles++; |
| 89 | 89 | } |
| 90 | 90 | } else { |
| 91 | - $this->addFile($path); |
|
| 91 | + $this->addFile( $path ); |
|
| 92 | 92 | }//end if |
| 93 | 93 | }//end foreach |
| 94 | 94 | |
| 95 | - reset($this->files); |
|
| 95 | + reset( $this->files ); |
|
| 96 | 96 | |
| 97 | 97 | }//end __construct() |
| 98 | 98 | |
@@ -108,24 +108,24 @@ discard block |
||
| 108 | 108 | * |
| 109 | 109 | * @return void |
| 110 | 110 | */ |
| 111 | - public function addFile($path, $file=null) |
|
| 111 | + public function addFile( $path, $file = null ) |
|
| 112 | 112 | { |
| 113 | 113 | // No filtering is done for STDIN when the filename |
| 114 | 114 | // has not been specified. |
| 115 | - if ($path === 'STDIN') { |
|
| 116 | - $this->files[$path] = $file; |
|
| 115 | + if ( $path === 'STDIN' ) { |
|
| 116 | + $this->files[ $path ] = $file; |
|
| 117 | 117 | $this->numFiles++; |
| 118 | 118 | return; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | $filterClass = $this->getFilterClass(); |
| 122 | 122 | |
| 123 | - $di = new \RecursiveArrayIterator([$path]); |
|
| 124 | - $filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
|
| 125 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 123 | + $di = new \RecursiveArrayIterator( [ $path ] ); |
|
| 124 | + $filter = new $filterClass( $di, $path, $this->config, $this->ruleset ); |
|
| 125 | + $iterator = new \RecursiveIteratorIterator( $filter ); |
|
| 126 | 126 | |
| 127 | - foreach ($iterator as $path) { |
|
| 128 | - $this->files[$path] = $file; |
|
| 127 | + foreach ( $iterator as $path ) { |
|
| 128 | + $this->files[ $path ] = $file; |
|
| 129 | 129 | $this->numFiles++; |
| 130 | 130 | } |
| 131 | 131 | |
@@ -141,20 +141,20 @@ discard block |
||
| 141 | 141 | { |
| 142 | 142 | $filterType = $this->config->filter; |
| 143 | 143 | |
| 144 | - if ($filterType === null) { |
|
| 144 | + if ( $filterType === null ) { |
|
| 145 | 145 | $filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
| 146 | 146 | } else { |
| 147 | - if (strpos($filterType, '.') !== false) { |
|
| 147 | + if ( strpos( $filterType, '.' ) !== false ) { |
|
| 148 | 148 | // This is a path to a custom filter class. |
| 149 | - $filename = realpath($filterType); |
|
| 150 | - if ($filename === false) { |
|
| 151 | - $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
|
| 152 | - throw new DeepExitException($error, 3); |
|
| 149 | + $filename = realpath( $filterType ); |
|
| 150 | + if ( $filename === false ) { |
|
| 151 | + $error = "ERROR: Custom filter \"$filterType\" not found" . PHP_EOL; |
|
| 152 | + throw new DeepExitException( $error, 3 ); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $filterClass = Autoload::loadFile($filename); |
|
| 155 | + $filterClass = Autoload::loadFile( $filename ); |
|
| 156 | 156 | } else { |
| 157 | - $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
|
| 157 | + $filterClass = '\PHP_CodeSniffer\Filters\\' . $filterType; |
|
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | public function rewind() |
| 172 | 172 | { |
| 173 | - reset($this->files); |
|
| 173 | + reset( $this->files ); |
|
| 174 | 174 | |
| 175 | 175 | }//end rewind() |
| 176 | 176 | |
@@ -182,12 +182,12 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | public function current() |
| 184 | 184 | { |
| 185 | - $path = key($this->files); |
|
| 186 | - if ($this->files[$path] === null) { |
|
| 187 | - $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
|
| 185 | + $path = key( $this->files ); |
|
| 186 | + if ( $this->files[ $path ] === null ) { |
|
| 187 | + $this->files[ $path ] = new LocalFile( $path, $this->ruleset, $this->config ); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - return $this->files[$path]; |
|
| 190 | + return $this->files[ $path ]; |
|
| 191 | 191 | |
| 192 | 192 | }//end current() |
| 193 | 193 | |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | public function key() |
| 201 | 201 | { |
| 202 | - return key($this->files); |
|
| 202 | + return key( $this->files ); |
|
| 203 | 203 | |
| 204 | 204 | }//end key() |
| 205 | 205 | |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | public function next() |
| 213 | 213 | { |
| 214 | - next($this->files); |
|
| 214 | + next( $this->files ); |
|
| 215 | 215 | |
| 216 | 216 | }//end next() |
| 217 | 217 | |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | public function valid() |
| 225 | 225 | { |
| 226 | - if (current($this->files) === false) { |
|
| 226 | + if ( current( $this->files ) === false ) { |
|
| 227 | 227 | return false; |
| 228 | 228 | } |
| 229 | 229 | |
@@ -64,8 +64,7 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return void |
| 66 | 66 | */ |
| 67 | - public function __construct(Config $config, Ruleset $ruleset) |
|
| 68 | - { |
|
| 67 | + public function __construct(Config $config, Ruleset $ruleset) { |
|
| 69 | 68 | $this->ruleset = $ruleset; |
| 70 | 69 | $this->config = $config; |
| 71 | 70 | |
@@ -108,8 +107,7 @@ discard block |
||
| 108 | 107 | * |
| 109 | 108 | * @return void |
| 110 | 109 | */ |
| 111 | - public function addFile($path, $file=null) |
|
| 112 | - { |
|
| 110 | + public function addFile($path, $file=null) { |
|
| 113 | 111 | // No filtering is done for STDIN when the filename |
| 114 | 112 | // has not been specified. |
| 115 | 113 | if ($path === 'STDIN') { |
@@ -137,8 +135,7 @@ discard block |
||
| 137 | 135 | * |
| 138 | 136 | * @return string |
| 139 | 137 | */ |
| 140 | - private function getFilterClass() |
|
| 141 | - { |
|
| 138 | + private function getFilterClass() { |
|
| 142 | 139 | $filterType = $this->config->filter; |
| 143 | 140 | |
| 144 | 141 | if ($filterType === null) { |
@@ -168,8 +165,7 @@ discard block |
||
| 168 | 165 | * |
| 169 | 166 | * @return void |
| 170 | 167 | */ |
| 171 | - public function rewind() |
|
| 172 | - { |
|
| 168 | + public function rewind() { |
|
| 173 | 169 | reset($this->files); |
| 174 | 170 | |
| 175 | 171 | }//end rewind() |
@@ -180,8 +176,7 @@ discard block |
||
| 180 | 176 | * |
| 181 | 177 | * @return \PHP_CodeSniffer\Files\File |
| 182 | 178 | */ |
| 183 | - public function current() |
|
| 184 | - { |
|
| 179 | + public function current() { |
|
| 185 | 180 | $path = key($this->files); |
| 186 | 181 | if ($this->files[$path] === null) { |
| 187 | 182 | $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
@@ -197,8 +192,7 @@ discard block |
||
| 197 | 192 | * |
| 198 | 193 | * @return void |
| 199 | 194 | */ |
| 200 | - public function key() |
|
| 201 | - { |
|
| 195 | + public function key() { |
|
| 202 | 196 | return key($this->files); |
| 203 | 197 | |
| 204 | 198 | }//end key() |
@@ -209,8 +203,7 @@ discard block |
||
| 209 | 203 | * |
| 210 | 204 | * @return void |
| 211 | 205 | */ |
| 212 | - public function next() |
|
| 213 | - { |
|
| 206 | + public function next() { |
|
| 214 | 207 | next($this->files); |
| 215 | 208 | |
| 216 | 209 | }//end next() |
@@ -221,8 +214,7 @@ discard block |
||
| 221 | 214 | * |
| 222 | 215 | * @return boolean |
| 223 | 216 | */ |
| 224 | - public function valid() |
|
| 225 | - { |
|
| 217 | + public function valid() { |
|
| 226 | 218 | if (current($this->files) === false) { |
| 227 | 219 | return false; |
| 228 | 220 | } |
@@ -237,8 +229,7 @@ discard block |
||
| 237 | 229 | * |
| 238 | 230 | * @return integer |
| 239 | 231 | */ |
| 240 | - public function count() |
|
| 241 | - { |
|
| 232 | + public function count() { |
|
| 242 | 233 | return $this->numFiles; |
| 243 | 234 | |
| 244 | 235 | }//end count() |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | /** |
| 343 | 343 | * Start recording actions for a changeset. |
| 344 | 344 | * |
| 345 | - * @return void |
|
| 345 | + * @return false|null |
|
| 346 | 346 | */ |
| 347 | 347 | public function beginChangeset() |
| 348 | 348 | { |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | /** |
| 370 | 370 | * Stop recording actions for a changeset, and apply logged changes. |
| 371 | 371 | * |
| 372 | - * @return boolean |
|
| 372 | + * @return false|null |
|
| 373 | 373 | */ |
| 374 | 374 | public function endChangeset() |
| 375 | 375 | { |
@@ -18,706 +18,706 @@ |
||
| 18 | 18 | class Fixer |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Is the fixer enabled and fixing a file? |
|
| 23 | - * |
|
| 24 | - * Sniffs should check this value to ensure they are not |
|
| 25 | - * doing extra processing to prepare for a fix when fixing is |
|
| 26 | - * not required. |
|
| 27 | - * |
|
| 28 | - * @var boolean |
|
| 29 | - */ |
|
| 30 | - public $enabled = false; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The number of times we have looped over a file. |
|
| 34 | - * |
|
| 35 | - * @var integer |
|
| 36 | - */ |
|
| 37 | - public $loops = 0; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The file being fixed. |
|
| 41 | - * |
|
| 42 | - * @var \PHP_CodeSniffer\Files\File |
|
| 43 | - */ |
|
| 44 | - private $currentFile = null; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The list of tokens that make up the file contents. |
|
| 48 | - * |
|
| 49 | - * This is a simplified list which just contains the token content and nothing |
|
| 50 | - * else. This is the array that is updated as fixes are made, not the file's |
|
| 51 | - * token array. Imploding this array will give you the file content back. |
|
| 52 | - * |
|
| 53 | - * @var array<int, string> |
|
| 54 | - */ |
|
| 55 | - private $tokens = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * A list of tokens that have already been fixed. |
|
| 59 | - * |
|
| 60 | - * We don't allow the same token to be fixed more than once each time |
|
| 61 | - * through a file as this can easily cause conflicts between sniffs. |
|
| 62 | - * |
|
| 63 | - * @var int[] |
|
| 64 | - */ |
|
| 65 | - private $fixedTokens = []; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The last value of each fixed token. |
|
| 69 | - * |
|
| 70 | - * If a token is being "fixed" back to its last value, the fix is |
|
| 71 | - * probably conflicting with another. |
|
| 72 | - * |
|
| 73 | - * @var array<int, string> |
|
| 74 | - */ |
|
| 75 | - private $oldTokenValues = []; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * A list of tokens that have been fixed during a changeset. |
|
| 79 | - * |
|
| 80 | - * All changes in changeset must be able to be applied, or else |
|
| 81 | - * the entire changeset is rejected. |
|
| 82 | - * |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - private $changeset = []; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Is there an open changeset. |
|
| 89 | - * |
|
| 90 | - * @var boolean |
|
| 91 | - */ |
|
| 92 | - private $inChangeset = false; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Is the current fixing loop in conflict? |
|
| 96 | - * |
|
| 97 | - * @var boolean |
|
| 98 | - */ |
|
| 99 | - private $inConflict = false; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * The number of fixes that have been performed. |
|
| 103 | - * |
|
| 104 | - * @var integer |
|
| 105 | - */ |
|
| 106 | - private $numFixes = 0; |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Starts fixing a new file. |
|
| 111 | - * |
|
| 112 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being fixed. |
|
| 113 | - * |
|
| 114 | - * @return void |
|
| 115 | - */ |
|
| 116 | - public function startFile(File $phpcsFile) |
|
| 117 | - { |
|
| 118 | - $this->currentFile = $phpcsFile; |
|
| 119 | - $this->numFixes = 0; |
|
| 120 | - $this->fixedTokens = []; |
|
| 121 | - |
|
| 122 | - $tokens = $phpcsFile->getTokens(); |
|
| 123 | - $this->tokens = []; |
|
| 124 | - foreach ($tokens as $index => $token) { |
|
| 125 | - if (isset($token['orig_content']) === true) { |
|
| 126 | - $this->tokens[$index] = $token['orig_content']; |
|
| 127 | - } else { |
|
| 128 | - $this->tokens[$index] = $token['content']; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - }//end startFile() |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Attempt to fix the file by processing it until no fixes are made. |
|
| 137 | - * |
|
| 138 | - * @return boolean |
|
| 139 | - */ |
|
| 140 | - public function fixFile() |
|
| 141 | - { |
|
| 142 | - $fixable = $this->currentFile->getFixableCount(); |
|
| 143 | - if ($fixable === 0) { |
|
| 144 | - // Nothing to fix. |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $this->enabled = true; |
|
| 149 | - |
|
| 150 | - $this->loops = 0; |
|
| 151 | - while ($this->loops < 50) { |
|
| 152 | - ob_start(); |
|
| 153 | - |
|
| 154 | - // Only needed once file content has changed. |
|
| 155 | - $contents = $this->getContents(); |
|
| 156 | - |
|
| 157 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 158 | - @ob_end_clean(); |
|
| 159 | - echo '---START FILE CONTENT---'.PHP_EOL; |
|
| 160 | - $lines = explode($this->currentFile->eolChar, $contents); |
|
| 161 | - $max = strlen(count($lines)); |
|
| 162 | - foreach ($lines as $lineNum => $line) { |
|
| 163 | - $lineNum++; |
|
| 164 | - echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
| 168 | - ob_start(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->inConflict = false; |
|
| 172 | - $this->currentFile->ruleset->populateTokenListeners(); |
|
| 173 | - $this->currentFile->setContent($contents); |
|
| 174 | - $this->currentFile->process(); |
|
| 175 | - ob_end_clean(); |
|
| 176 | - |
|
| 177 | - $this->loops++; |
|
| 178 | - |
|
| 179 | - if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 180 | - echo "\r".str_repeat(' ', 80)."\r"; |
|
| 181 | - echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass"; |
|
| 182 | - if ($this->loops > 1) { |
|
| 183 | - echo 'es'; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - echo ']... '; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if ($this->numFixes === 0 && $this->inConflict === false) { |
|
| 190 | - // Nothing left to do. |
|
| 191 | - break; |
|
| 192 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 193 | - echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
| 194 | - } |
|
| 195 | - }//end while |
|
| 196 | - |
|
| 197 | - $this->enabled = false; |
|
| 198 | - |
|
| 199 | - if ($this->numFixes > 0) { |
|
| 200 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 201 | - if (ob_get_level() > 0) { |
|
| 202 | - ob_end_clean(); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL; |
|
| 206 | - ob_start(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return true; |
|
| 213 | - |
|
| 214 | - }//end fixFile() |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Generates a text diff of the original file and the new content. |
|
| 219 | - * |
|
| 220 | - * @param string $filePath Optional file path to diff the file against. |
|
| 221 | - * If not specified, the original version of the |
|
| 222 | - * file will be used. |
|
| 223 | - * @param boolean $colors Print colored output or not. |
|
| 224 | - * |
|
| 225 | - * @return string |
|
| 226 | - */ |
|
| 227 | - public function generateDiff($filePath=null, $colors=true) |
|
| 228 | - { |
|
| 229 | - if ($filePath === null) { |
|
| 230 | - $filePath = $this->currentFile->getFilename(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
| 234 | - if (strpos($filePath, $cwd) === 0) { |
|
| 235 | - $filename = substr($filePath, strlen($cwd)); |
|
| 236 | - } else { |
|
| 237 | - $filename = $filePath; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $contents = $this->getContents(); |
|
| 241 | - |
|
| 242 | - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
| 243 | - $fixedFile = fopen($tempName, 'w'); |
|
| 244 | - fwrite($fixedFile, $contents); |
|
| 245 | - |
|
| 246 | - // We must use something like shell_exec() because whitespace at the end |
|
| 247 | - // of lines is critical to diff files. |
|
| 248 | - $filename = escapeshellarg($filename); |
|
| 249 | - $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; |
|
| 250 | - |
|
| 251 | - $diff = shell_exec($cmd); |
|
| 252 | - |
|
| 253 | - fclose($fixedFile); |
|
| 254 | - if (is_file($tempName) === true) { |
|
| 255 | - unlink($tempName); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ($colors === false) { |
|
| 259 | - return $diff; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - $diffLines = explode(PHP_EOL, $diff); |
|
| 263 | - if (count($diffLines) === 1) { |
|
| 264 | - // Seems to be required for cygwin. |
|
| 265 | - $diffLines = explode("\n", $diff); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $diff = []; |
|
| 269 | - foreach ($diffLines as $line) { |
|
| 270 | - if (isset($line[0]) === true) { |
|
| 271 | - switch ($line[0]) { |
|
| 272 | - case '-': |
|
| 273 | - $diff[] = "\033[31m$line\033[0m"; |
|
| 274 | - break; |
|
| 275 | - case '+': |
|
| 276 | - $diff[] = "\033[32m$line\033[0m"; |
|
| 277 | - break; |
|
| 278 | - default: |
|
| 279 | - $diff[] = $line; |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $diff = implode(PHP_EOL, $diff); |
|
| 285 | - |
|
| 286 | - return $diff; |
|
| 287 | - |
|
| 288 | - }//end generateDiff() |
|
| 289 | - |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get a count of fixes that have been performed on the file. |
|
| 293 | - * |
|
| 294 | - * This value is reset every time a new file is started, or an existing |
|
| 295 | - * file is restarted. |
|
| 296 | - * |
|
| 297 | - * @return int |
|
| 298 | - */ |
|
| 299 | - public function getFixCount() |
|
| 300 | - { |
|
| 301 | - return $this->numFixes; |
|
| 302 | - |
|
| 303 | - }//end getFixCount() |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get the current content of the file, as a string. |
|
| 308 | - * |
|
| 309 | - * @return string |
|
| 310 | - */ |
|
| 311 | - public function getContents() |
|
| 312 | - { |
|
| 313 | - $contents = implode($this->tokens); |
|
| 314 | - return $contents; |
|
| 315 | - |
|
| 316 | - }//end getContents() |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the current fixed content of a token. |
|
| 321 | - * |
|
| 322 | - * This function takes changesets into account so should be used |
|
| 323 | - * instead of directly accessing the token array. |
|
| 324 | - * |
|
| 325 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 326 | - * |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - public function getTokenContent($stackPtr) |
|
| 330 | - { |
|
| 331 | - if ($this->inChangeset === true |
|
| 332 | - && isset($this->changeset[$stackPtr]) === true |
|
| 333 | - ) { |
|
| 334 | - return $this->changeset[$stackPtr]; |
|
| 335 | - } else { |
|
| 336 | - return $this->tokens[$stackPtr]; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - }//end getTokenContent() |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Start recording actions for a changeset. |
|
| 344 | - * |
|
| 345 | - * @return void |
|
| 346 | - */ |
|
| 347 | - public function beginChangeset() |
|
| 348 | - { |
|
| 349 | - if ($this->inConflict === true) { |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 354 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 355 | - $sniff = $bt[1]['class']; |
|
| 356 | - $line = $bt[0]['line']; |
|
| 357 | - |
|
| 358 | - @ob_end_clean(); |
|
| 359 | - echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
| 360 | - ob_start(); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $this->changeset = []; |
|
| 364 | - $this->inChangeset = true; |
|
| 365 | - |
|
| 366 | - }//end beginChangeset() |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Stop recording actions for a changeset, and apply logged changes. |
|
| 371 | - * |
|
| 372 | - * @return boolean |
|
| 373 | - */ |
|
| 374 | - public function endChangeset() |
|
| 375 | - { |
|
| 376 | - if ($this->inConflict === true) { |
|
| 377 | - return false; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - $this->inChangeset = false; |
|
| 381 | - |
|
| 382 | - $success = true; |
|
| 383 | - $applied = []; |
|
| 384 | - foreach ($this->changeset as $stackPtr => $content) { |
|
| 385 | - $success = $this->replaceToken($stackPtr, $content); |
|
| 386 | - if ($success === false) { |
|
| 387 | - break; |
|
| 388 | - } else { |
|
| 389 | - $applied[] = $stackPtr; |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - if ($success === false) { |
|
| 394 | - // Rolling back all changes. |
|
| 395 | - foreach ($applied as $stackPtr) { |
|
| 396 | - $this->revertToken($stackPtr); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 400 | - @ob_end_clean(); |
|
| 401 | - echo "\t=> Changeset failed to apply".PHP_EOL; |
|
| 402 | - ob_start(); |
|
| 403 | - } |
|
| 404 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 405 | - $fixes = count($this->changeset); |
|
| 406 | - @ob_end_clean(); |
|
| 407 | - echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
| 408 | - ob_start(); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - $this->changeset = []; |
|
| 412 | - |
|
| 413 | - }//end endChangeset() |
|
| 414 | - |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Stop recording actions for a changeset, and discard logged changes. |
|
| 418 | - * |
|
| 419 | - * @return void |
|
| 420 | - */ |
|
| 421 | - public function rollbackChangeset() |
|
| 422 | - { |
|
| 423 | - $this->inChangeset = false; |
|
| 424 | - $this->inConflict = false; |
|
| 425 | - |
|
| 426 | - if (empty($this->changeset) === false) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 428 | - $bt = debug_backtrace(); |
|
| 429 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 430 | - $sniff = $bt[2]['class']; |
|
| 431 | - $line = $bt[1]['line']; |
|
| 432 | - } else { |
|
| 433 | - $sniff = $bt[1]['class']; |
|
| 434 | - $line = $bt[0]['line']; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $numChanges = count($this->changeset); |
|
| 438 | - |
|
| 439 | - @ob_end_clean(); |
|
| 440 | - echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
| 441 | - echo "\t=> Changeset rolled back".PHP_EOL; |
|
| 442 | - ob_start(); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - $this->changeset = []; |
|
| 446 | - }//end if |
|
| 447 | - |
|
| 448 | - }//end rollbackChangeset() |
|
| 449 | - |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Replace the entire contents of a token. |
|
| 453 | - * |
|
| 454 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 455 | - * @param string $content The new content of the token. |
|
| 456 | - * |
|
| 457 | - * @return bool If the change was accepted. |
|
| 458 | - */ |
|
| 459 | - public function replaceToken($stackPtr, $content) |
|
| 460 | - { |
|
| 461 | - if ($this->inConflict === true) { |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - if ($this->inChangeset === false |
|
| 466 | - && isset($this->fixedTokens[$stackPtr]) === true |
|
| 467 | - ) { |
|
| 468 | - $indent = "\t"; |
|
| 469 | - if (empty($this->changeset) === false) { |
|
| 470 | - $indent .= "\t"; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 474 | - @ob_end_clean(); |
|
| 475 | - echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
| 476 | - ob_start(); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return false; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 483 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 484 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 485 | - $sniff = $bt[2]['class']; |
|
| 486 | - $line = $bt[1]['line']; |
|
| 487 | - } else { |
|
| 488 | - $sniff = $bt[1]['class']; |
|
| 489 | - $line = $bt[0]['line']; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - $tokens = $this->currentFile->getTokens(); |
|
| 493 | - $type = $tokens[$stackPtr]['type']; |
|
| 494 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 495 | - $newContent = Common::prepareForOutput($content); |
|
| 496 | - if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[($stackPtr + 1)]) === true) { |
|
| 497 | - // Add some context for whitespace only changes. |
|
| 498 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 499 | - $oldContent .= $append; |
|
| 500 | - $newContent .= $append; |
|
| 501 | - } |
|
| 502 | - }//end if |
|
| 503 | - |
|
| 504 | - if ($this->inChangeset === true) { |
|
| 505 | - $this->changeset[$stackPtr] = $content; |
|
| 506 | - |
|
| 507 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 508 | - @ob_end_clean(); |
|
| 509 | - echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 510 | - ob_start(); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - return true; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - if (isset($this->oldTokenValues[$stackPtr]) === false) { |
|
| 517 | - $this->oldTokenValues[$stackPtr] = [ |
|
| 518 | - 'curr' => $content, |
|
| 519 | - 'prev' => $this->tokens[$stackPtr], |
|
| 520 | - 'loop' => $this->loops, |
|
| 521 | - ]; |
|
| 522 | - } else { |
|
| 523 | - if ($this->oldTokenValues[$stackPtr]['prev'] === $content |
|
| 524 | - && $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
| 525 | - ) { |
|
| 526 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 527 | - $indent = "\t"; |
|
| 528 | - if (empty($this->changeset) === false) { |
|
| 529 | - $indent .= "\t"; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - $loop = $this->oldTokenValues[$stackPtr]['loop']; |
|
| 533 | - |
|
| 534 | - @ob_end_clean(); |
|
| 535 | - echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
| 536 | - echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
| 540 | - $this->inConflict = true; |
|
| 541 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 542 | - echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 547 | - ob_start(); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - return false; |
|
| 551 | - }//end if |
|
| 552 | - |
|
| 553 | - $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr']; |
|
| 554 | - $this->oldTokenValues[$stackPtr]['curr'] = $content; |
|
| 555 | - $this->oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
| 556 | - }//end if |
|
| 557 | - |
|
| 558 | - $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr]; |
|
| 559 | - $this->tokens[$stackPtr] = $content; |
|
| 560 | - $this->numFixes++; |
|
| 561 | - |
|
| 562 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 563 | - $indent = "\t"; |
|
| 564 | - if (empty($this->changeset) === false) { |
|
| 565 | - $indent .= "\tA: "; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - if (ob_get_level() > 0) { |
|
| 569 | - ob_end_clean(); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 573 | - ob_start(); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - return true; |
|
| 577 | - |
|
| 578 | - }//end replaceToken() |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Reverts the previous fix made to a token. |
|
| 583 | - * |
|
| 584 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 585 | - * |
|
| 586 | - * @return bool If a change was reverted. |
|
| 587 | - */ |
|
| 588 | - public function revertToken($stackPtr) |
|
| 589 | - { |
|
| 590 | - if (isset($this->fixedTokens[$stackPtr]) === false) { |
|
| 591 | - return false; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 595 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 596 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 597 | - $sniff = $bt[2]['class']; |
|
| 598 | - $line = $bt[1]['line']; |
|
| 599 | - } else { |
|
| 600 | - $sniff = $bt[1]['class']; |
|
| 601 | - $line = $bt[0]['line']; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - $tokens = $this->currentFile->getTokens(); |
|
| 605 | - $type = $tokens[$stackPtr]['type']; |
|
| 606 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 607 | - $newContent = Common::prepareForOutput($this->fixedTokens[$stackPtr]); |
|
| 608 | - if (trim($this->tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
| 609 | - // Add some context for whitespace only changes. |
|
| 610 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 611 | - $oldContent .= $append; |
|
| 612 | - $newContent .= $append; |
|
| 613 | - } |
|
| 614 | - }//end if |
|
| 615 | - |
|
| 616 | - $this->tokens[$stackPtr] = $this->fixedTokens[$stackPtr]; |
|
| 617 | - unset($this->fixedTokens[$stackPtr]); |
|
| 618 | - $this->numFixes--; |
|
| 619 | - |
|
| 620 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 621 | - $indent = "\t"; |
|
| 622 | - if (empty($this->changeset) === false) { |
|
| 623 | - $indent .= "\tR: "; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - @ob_end_clean(); |
|
| 627 | - echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 628 | - ob_start(); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - return true; |
|
| 632 | - |
|
| 633 | - }//end revertToken() |
|
| 634 | - |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Replace the content of a token with a part of its current content. |
|
| 638 | - * |
|
| 639 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 640 | - * @param int $start The first character to keep. |
|
| 641 | - * @param int $length The number of characters to keep. If NULL, the content of |
|
| 642 | - * the token from $start to the end of the content is kept. |
|
| 643 | - * |
|
| 644 | - * @return bool If the change was accepted. |
|
| 645 | - */ |
|
| 646 | - public function substrToken($stackPtr, $start, $length=null) |
|
| 647 | - { |
|
| 648 | - $current = $this->getTokenContent($stackPtr); |
|
| 649 | - |
|
| 650 | - if ($length === null) { |
|
| 651 | - $newContent = substr($current, $start); |
|
| 652 | - } else { |
|
| 653 | - $newContent = substr($current, $start, $length); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - return $this->replaceToken($stackPtr, $newContent); |
|
| 657 | - |
|
| 658 | - }//end substrToken() |
|
| 659 | - |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Adds a newline to end of a token's content. |
|
| 663 | - * |
|
| 664 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 665 | - * |
|
| 666 | - * @return bool If the change was accepted. |
|
| 667 | - */ |
|
| 668 | - public function addNewline($stackPtr) |
|
| 669 | - { |
|
| 670 | - $current = $this->getTokenContent($stackPtr); |
|
| 671 | - return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
|
| 672 | - |
|
| 673 | - }//end addNewline() |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Adds a newline to the start of a token's content. |
|
| 678 | - * |
|
| 679 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 680 | - * |
|
| 681 | - * @return bool If the change was accepted. |
|
| 682 | - */ |
|
| 683 | - public function addNewlineBefore($stackPtr) |
|
| 684 | - { |
|
| 685 | - $current = $this->getTokenContent($stackPtr); |
|
| 686 | - return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
|
| 687 | - |
|
| 688 | - }//end addNewlineBefore() |
|
| 689 | - |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * Adds content to the end of a token's current content. |
|
| 693 | - * |
|
| 694 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 695 | - * @param string $content The content to add. |
|
| 696 | - * |
|
| 697 | - * @return bool If the change was accepted. |
|
| 698 | - */ |
|
| 699 | - public function addContent($stackPtr, $content) |
|
| 700 | - { |
|
| 701 | - $current = $this->getTokenContent($stackPtr); |
|
| 702 | - return $this->replaceToken($stackPtr, $current.$content); |
|
| 703 | - |
|
| 704 | - }//end addContent() |
|
| 705 | - |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * Adds content to the start of a token's current content. |
|
| 709 | - * |
|
| 710 | - * @param int $stackPtr The position of the token in the token stack. |
|
| 711 | - * @param string $content The content to add. |
|
| 712 | - * |
|
| 713 | - * @return bool If the change was accepted. |
|
| 714 | - */ |
|
| 715 | - public function addContentBefore($stackPtr, $content) |
|
| 716 | - { |
|
| 717 | - $current = $this->getTokenContent($stackPtr); |
|
| 718 | - return $this->replaceToken($stackPtr, $content.$current); |
|
| 719 | - |
|
| 720 | - }//end addContentBefore() |
|
| 21 | + /** |
|
| 22 | + * Is the fixer enabled and fixing a file? |
|
| 23 | + * |
|
| 24 | + * Sniffs should check this value to ensure they are not |
|
| 25 | + * doing extra processing to prepare for a fix when fixing is |
|
| 26 | + * not required. |
|
| 27 | + * |
|
| 28 | + * @var boolean |
|
| 29 | + */ |
|
| 30 | + public $enabled = false; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The number of times we have looped over a file. |
|
| 34 | + * |
|
| 35 | + * @var integer |
|
| 36 | + */ |
|
| 37 | + public $loops = 0; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The file being fixed. |
|
| 41 | + * |
|
| 42 | + * @var \PHP_CodeSniffer\Files\File |
|
| 43 | + */ |
|
| 44 | + private $currentFile = null; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The list of tokens that make up the file contents. |
|
| 48 | + * |
|
| 49 | + * This is a simplified list which just contains the token content and nothing |
|
| 50 | + * else. This is the array that is updated as fixes are made, not the file's |
|
| 51 | + * token array. Imploding this array will give you the file content back. |
|
| 52 | + * |
|
| 53 | + * @var array<int, string> |
|
| 54 | + */ |
|
| 55 | + private $tokens = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * A list of tokens that have already been fixed. |
|
| 59 | + * |
|
| 60 | + * We don't allow the same token to be fixed more than once each time |
|
| 61 | + * through a file as this can easily cause conflicts between sniffs. |
|
| 62 | + * |
|
| 63 | + * @var int[] |
|
| 64 | + */ |
|
| 65 | + private $fixedTokens = []; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The last value of each fixed token. |
|
| 69 | + * |
|
| 70 | + * If a token is being "fixed" back to its last value, the fix is |
|
| 71 | + * probably conflicting with another. |
|
| 72 | + * |
|
| 73 | + * @var array<int, string> |
|
| 74 | + */ |
|
| 75 | + private $oldTokenValues = []; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * A list of tokens that have been fixed during a changeset. |
|
| 79 | + * |
|
| 80 | + * All changes in changeset must be able to be applied, or else |
|
| 81 | + * the entire changeset is rejected. |
|
| 82 | + * |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + private $changeset = []; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Is there an open changeset. |
|
| 89 | + * |
|
| 90 | + * @var boolean |
|
| 91 | + */ |
|
| 92 | + private $inChangeset = false; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Is the current fixing loop in conflict? |
|
| 96 | + * |
|
| 97 | + * @var boolean |
|
| 98 | + */ |
|
| 99 | + private $inConflict = false; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * The number of fixes that have been performed. |
|
| 103 | + * |
|
| 104 | + * @var integer |
|
| 105 | + */ |
|
| 106 | + private $numFixes = 0; |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Starts fixing a new file. |
|
| 111 | + * |
|
| 112 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being fixed. |
|
| 113 | + * |
|
| 114 | + * @return void |
|
| 115 | + */ |
|
| 116 | + public function startFile(File $phpcsFile) |
|
| 117 | + { |
|
| 118 | + $this->currentFile = $phpcsFile; |
|
| 119 | + $this->numFixes = 0; |
|
| 120 | + $this->fixedTokens = []; |
|
| 121 | + |
|
| 122 | + $tokens = $phpcsFile->getTokens(); |
|
| 123 | + $this->tokens = []; |
|
| 124 | + foreach ($tokens as $index => $token) { |
|
| 125 | + if (isset($token['orig_content']) === true) { |
|
| 126 | + $this->tokens[$index] = $token['orig_content']; |
|
| 127 | + } else { |
|
| 128 | + $this->tokens[$index] = $token['content']; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + }//end startFile() |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Attempt to fix the file by processing it until no fixes are made. |
|
| 137 | + * |
|
| 138 | + * @return boolean |
|
| 139 | + */ |
|
| 140 | + public function fixFile() |
|
| 141 | + { |
|
| 142 | + $fixable = $this->currentFile->getFixableCount(); |
|
| 143 | + if ($fixable === 0) { |
|
| 144 | + // Nothing to fix. |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $this->enabled = true; |
|
| 149 | + |
|
| 150 | + $this->loops = 0; |
|
| 151 | + while ($this->loops < 50) { |
|
| 152 | + ob_start(); |
|
| 153 | + |
|
| 154 | + // Only needed once file content has changed. |
|
| 155 | + $contents = $this->getContents(); |
|
| 156 | + |
|
| 157 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 158 | + @ob_end_clean(); |
|
| 159 | + echo '---START FILE CONTENT---'.PHP_EOL; |
|
| 160 | + $lines = explode($this->currentFile->eolChar, $contents); |
|
| 161 | + $max = strlen(count($lines)); |
|
| 162 | + foreach ($lines as $lineNum => $line) { |
|
| 163 | + $lineNum++; |
|
| 164 | + echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
| 168 | + ob_start(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->inConflict = false; |
|
| 172 | + $this->currentFile->ruleset->populateTokenListeners(); |
|
| 173 | + $this->currentFile->setContent($contents); |
|
| 174 | + $this->currentFile->process(); |
|
| 175 | + ob_end_clean(); |
|
| 176 | + |
|
| 177 | + $this->loops++; |
|
| 178 | + |
|
| 179 | + if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 180 | + echo "\r".str_repeat(' ', 80)."\r"; |
|
| 181 | + echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass"; |
|
| 182 | + if ($this->loops > 1) { |
|
| 183 | + echo 'es'; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + echo ']... '; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if ($this->numFixes === 0 && $this->inConflict === false) { |
|
| 190 | + // Nothing left to do. |
|
| 191 | + break; |
|
| 192 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 193 | + echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
| 194 | + } |
|
| 195 | + }//end while |
|
| 196 | + |
|
| 197 | + $this->enabled = false; |
|
| 198 | + |
|
| 199 | + if ($this->numFixes > 0) { |
|
| 200 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 201 | + if (ob_get_level() > 0) { |
|
| 202 | + ob_end_clean(); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL; |
|
| 206 | + ob_start(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return true; |
|
| 213 | + |
|
| 214 | + }//end fixFile() |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Generates a text diff of the original file and the new content. |
|
| 219 | + * |
|
| 220 | + * @param string $filePath Optional file path to diff the file against. |
|
| 221 | + * If not specified, the original version of the |
|
| 222 | + * file will be used. |
|
| 223 | + * @param boolean $colors Print colored output or not. |
|
| 224 | + * |
|
| 225 | + * @return string |
|
| 226 | + */ |
|
| 227 | + public function generateDiff($filePath=null, $colors=true) |
|
| 228 | + { |
|
| 229 | + if ($filePath === null) { |
|
| 230 | + $filePath = $this->currentFile->getFilename(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
| 234 | + if (strpos($filePath, $cwd) === 0) { |
|
| 235 | + $filename = substr($filePath, strlen($cwd)); |
|
| 236 | + } else { |
|
| 237 | + $filename = $filePath; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $contents = $this->getContents(); |
|
| 241 | + |
|
| 242 | + $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
| 243 | + $fixedFile = fopen($tempName, 'w'); |
|
| 244 | + fwrite($fixedFile, $contents); |
|
| 245 | + |
|
| 246 | + // We must use something like shell_exec() because whitespace at the end |
|
| 247 | + // of lines is critical to diff files. |
|
| 248 | + $filename = escapeshellarg($filename); |
|
| 249 | + $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; |
|
| 250 | + |
|
| 251 | + $diff = shell_exec($cmd); |
|
| 252 | + |
|
| 253 | + fclose($fixedFile); |
|
| 254 | + if (is_file($tempName) === true) { |
|
| 255 | + unlink($tempName); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ($colors === false) { |
|
| 259 | + return $diff; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + $diffLines = explode(PHP_EOL, $diff); |
|
| 263 | + if (count($diffLines) === 1) { |
|
| 264 | + // Seems to be required for cygwin. |
|
| 265 | + $diffLines = explode("\n", $diff); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $diff = []; |
|
| 269 | + foreach ($diffLines as $line) { |
|
| 270 | + if (isset($line[0]) === true) { |
|
| 271 | + switch ($line[0]) { |
|
| 272 | + case '-': |
|
| 273 | + $diff[] = "\033[31m$line\033[0m"; |
|
| 274 | + break; |
|
| 275 | + case '+': |
|
| 276 | + $diff[] = "\033[32m$line\033[0m"; |
|
| 277 | + break; |
|
| 278 | + default: |
|
| 279 | + $diff[] = $line; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $diff = implode(PHP_EOL, $diff); |
|
| 285 | + |
|
| 286 | + return $diff; |
|
| 287 | + |
|
| 288 | + }//end generateDiff() |
|
| 289 | + |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get a count of fixes that have been performed on the file. |
|
| 293 | + * |
|
| 294 | + * This value is reset every time a new file is started, or an existing |
|
| 295 | + * file is restarted. |
|
| 296 | + * |
|
| 297 | + * @return int |
|
| 298 | + */ |
|
| 299 | + public function getFixCount() |
|
| 300 | + { |
|
| 301 | + return $this->numFixes; |
|
| 302 | + |
|
| 303 | + }//end getFixCount() |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get the current content of the file, as a string. |
|
| 308 | + * |
|
| 309 | + * @return string |
|
| 310 | + */ |
|
| 311 | + public function getContents() |
|
| 312 | + { |
|
| 313 | + $contents = implode($this->tokens); |
|
| 314 | + return $contents; |
|
| 315 | + |
|
| 316 | + }//end getContents() |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the current fixed content of a token. |
|
| 321 | + * |
|
| 322 | + * This function takes changesets into account so should be used |
|
| 323 | + * instead of directly accessing the token array. |
|
| 324 | + * |
|
| 325 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 326 | + * |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + public function getTokenContent($stackPtr) |
|
| 330 | + { |
|
| 331 | + if ($this->inChangeset === true |
|
| 332 | + && isset($this->changeset[$stackPtr]) === true |
|
| 333 | + ) { |
|
| 334 | + return $this->changeset[$stackPtr]; |
|
| 335 | + } else { |
|
| 336 | + return $this->tokens[$stackPtr]; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + }//end getTokenContent() |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Start recording actions for a changeset. |
|
| 344 | + * |
|
| 345 | + * @return void |
|
| 346 | + */ |
|
| 347 | + public function beginChangeset() |
|
| 348 | + { |
|
| 349 | + if ($this->inConflict === true) { |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 354 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 355 | + $sniff = $bt[1]['class']; |
|
| 356 | + $line = $bt[0]['line']; |
|
| 357 | + |
|
| 358 | + @ob_end_clean(); |
|
| 359 | + echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
| 360 | + ob_start(); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $this->changeset = []; |
|
| 364 | + $this->inChangeset = true; |
|
| 365 | + |
|
| 366 | + }//end beginChangeset() |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Stop recording actions for a changeset, and apply logged changes. |
|
| 371 | + * |
|
| 372 | + * @return boolean |
|
| 373 | + */ |
|
| 374 | + public function endChangeset() |
|
| 375 | + { |
|
| 376 | + if ($this->inConflict === true) { |
|
| 377 | + return false; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + $this->inChangeset = false; |
|
| 381 | + |
|
| 382 | + $success = true; |
|
| 383 | + $applied = []; |
|
| 384 | + foreach ($this->changeset as $stackPtr => $content) { |
|
| 385 | + $success = $this->replaceToken($stackPtr, $content); |
|
| 386 | + if ($success === false) { |
|
| 387 | + break; |
|
| 388 | + } else { |
|
| 389 | + $applied[] = $stackPtr; |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + if ($success === false) { |
|
| 394 | + // Rolling back all changes. |
|
| 395 | + foreach ($applied as $stackPtr) { |
|
| 396 | + $this->revertToken($stackPtr); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 400 | + @ob_end_clean(); |
|
| 401 | + echo "\t=> Changeset failed to apply".PHP_EOL; |
|
| 402 | + ob_start(); |
|
| 403 | + } |
|
| 404 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 405 | + $fixes = count($this->changeset); |
|
| 406 | + @ob_end_clean(); |
|
| 407 | + echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
| 408 | + ob_start(); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + $this->changeset = []; |
|
| 412 | + |
|
| 413 | + }//end endChangeset() |
|
| 414 | + |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Stop recording actions for a changeset, and discard logged changes. |
|
| 418 | + * |
|
| 419 | + * @return void |
|
| 420 | + */ |
|
| 421 | + public function rollbackChangeset() |
|
| 422 | + { |
|
| 423 | + $this->inChangeset = false; |
|
| 424 | + $this->inConflict = false; |
|
| 425 | + |
|
| 426 | + if (empty($this->changeset) === false) { |
|
| 427 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 428 | + $bt = debug_backtrace(); |
|
| 429 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 430 | + $sniff = $bt[2]['class']; |
|
| 431 | + $line = $bt[1]['line']; |
|
| 432 | + } else { |
|
| 433 | + $sniff = $bt[1]['class']; |
|
| 434 | + $line = $bt[0]['line']; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $numChanges = count($this->changeset); |
|
| 438 | + |
|
| 439 | + @ob_end_clean(); |
|
| 440 | + echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
| 441 | + echo "\t=> Changeset rolled back".PHP_EOL; |
|
| 442 | + ob_start(); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + $this->changeset = []; |
|
| 446 | + }//end if |
|
| 447 | + |
|
| 448 | + }//end rollbackChangeset() |
|
| 449 | + |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Replace the entire contents of a token. |
|
| 453 | + * |
|
| 454 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 455 | + * @param string $content The new content of the token. |
|
| 456 | + * |
|
| 457 | + * @return bool If the change was accepted. |
|
| 458 | + */ |
|
| 459 | + public function replaceToken($stackPtr, $content) |
|
| 460 | + { |
|
| 461 | + if ($this->inConflict === true) { |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + if ($this->inChangeset === false |
|
| 466 | + && isset($this->fixedTokens[$stackPtr]) === true |
|
| 467 | + ) { |
|
| 468 | + $indent = "\t"; |
|
| 469 | + if (empty($this->changeset) === false) { |
|
| 470 | + $indent .= "\t"; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 474 | + @ob_end_clean(); |
|
| 475 | + echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
| 476 | + ob_start(); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return false; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 483 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 484 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 485 | + $sniff = $bt[2]['class']; |
|
| 486 | + $line = $bt[1]['line']; |
|
| 487 | + } else { |
|
| 488 | + $sniff = $bt[1]['class']; |
|
| 489 | + $line = $bt[0]['line']; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + $tokens = $this->currentFile->getTokens(); |
|
| 493 | + $type = $tokens[$stackPtr]['type']; |
|
| 494 | + $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 495 | + $newContent = Common::prepareForOutput($content); |
|
| 496 | + if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[($stackPtr + 1)]) === true) { |
|
| 497 | + // Add some context for whitespace only changes. |
|
| 498 | + $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 499 | + $oldContent .= $append; |
|
| 500 | + $newContent .= $append; |
|
| 501 | + } |
|
| 502 | + }//end if |
|
| 503 | + |
|
| 504 | + if ($this->inChangeset === true) { |
|
| 505 | + $this->changeset[$stackPtr] = $content; |
|
| 506 | + |
|
| 507 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 508 | + @ob_end_clean(); |
|
| 509 | + echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 510 | + ob_start(); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return true; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + if (isset($this->oldTokenValues[$stackPtr]) === false) { |
|
| 517 | + $this->oldTokenValues[$stackPtr] = [ |
|
| 518 | + 'curr' => $content, |
|
| 519 | + 'prev' => $this->tokens[$stackPtr], |
|
| 520 | + 'loop' => $this->loops, |
|
| 521 | + ]; |
|
| 522 | + } else { |
|
| 523 | + if ($this->oldTokenValues[$stackPtr]['prev'] === $content |
|
| 524 | + && $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
| 525 | + ) { |
|
| 526 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 527 | + $indent = "\t"; |
|
| 528 | + if (empty($this->changeset) === false) { |
|
| 529 | + $indent .= "\t"; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + $loop = $this->oldTokenValues[$stackPtr]['loop']; |
|
| 533 | + |
|
| 534 | + @ob_end_clean(); |
|
| 535 | + echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
| 536 | + echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
| 540 | + $this->inConflict = true; |
|
| 541 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 542 | + echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 547 | + ob_start(); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + return false; |
|
| 551 | + }//end if |
|
| 552 | + |
|
| 553 | + $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr']; |
|
| 554 | + $this->oldTokenValues[$stackPtr]['curr'] = $content; |
|
| 555 | + $this->oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
| 556 | + }//end if |
|
| 557 | + |
|
| 558 | + $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr]; |
|
| 559 | + $this->tokens[$stackPtr] = $content; |
|
| 560 | + $this->numFixes++; |
|
| 561 | + |
|
| 562 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 563 | + $indent = "\t"; |
|
| 564 | + if (empty($this->changeset) === false) { |
|
| 565 | + $indent .= "\tA: "; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + if (ob_get_level() > 0) { |
|
| 569 | + ob_end_clean(); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 573 | + ob_start(); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + return true; |
|
| 577 | + |
|
| 578 | + }//end replaceToken() |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Reverts the previous fix made to a token. |
|
| 583 | + * |
|
| 584 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 585 | + * |
|
| 586 | + * @return bool If a change was reverted. |
|
| 587 | + */ |
|
| 588 | + public function revertToken($stackPtr) |
|
| 589 | + { |
|
| 590 | + if (isset($this->fixedTokens[$stackPtr]) === false) { |
|
| 591 | + return false; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 595 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 596 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 597 | + $sniff = $bt[2]['class']; |
|
| 598 | + $line = $bt[1]['line']; |
|
| 599 | + } else { |
|
| 600 | + $sniff = $bt[1]['class']; |
|
| 601 | + $line = $bt[0]['line']; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + $tokens = $this->currentFile->getTokens(); |
|
| 605 | + $type = $tokens[$stackPtr]['type']; |
|
| 606 | + $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 607 | + $newContent = Common::prepareForOutput($this->fixedTokens[$stackPtr]); |
|
| 608 | + if (trim($this->tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
| 609 | + // Add some context for whitespace only changes. |
|
| 610 | + $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 611 | + $oldContent .= $append; |
|
| 612 | + $newContent .= $append; |
|
| 613 | + } |
|
| 614 | + }//end if |
|
| 615 | + |
|
| 616 | + $this->tokens[$stackPtr] = $this->fixedTokens[$stackPtr]; |
|
| 617 | + unset($this->fixedTokens[$stackPtr]); |
|
| 618 | + $this->numFixes--; |
|
| 619 | + |
|
| 620 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 621 | + $indent = "\t"; |
|
| 622 | + if (empty($this->changeset) === false) { |
|
| 623 | + $indent .= "\tR: "; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + @ob_end_clean(); |
|
| 627 | + echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 628 | + ob_start(); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + return true; |
|
| 632 | + |
|
| 633 | + }//end revertToken() |
|
| 634 | + |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Replace the content of a token with a part of its current content. |
|
| 638 | + * |
|
| 639 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 640 | + * @param int $start The first character to keep. |
|
| 641 | + * @param int $length The number of characters to keep. If NULL, the content of |
|
| 642 | + * the token from $start to the end of the content is kept. |
|
| 643 | + * |
|
| 644 | + * @return bool If the change was accepted. |
|
| 645 | + */ |
|
| 646 | + public function substrToken($stackPtr, $start, $length=null) |
|
| 647 | + { |
|
| 648 | + $current = $this->getTokenContent($stackPtr); |
|
| 649 | + |
|
| 650 | + if ($length === null) { |
|
| 651 | + $newContent = substr($current, $start); |
|
| 652 | + } else { |
|
| 653 | + $newContent = substr($current, $start, $length); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + return $this->replaceToken($stackPtr, $newContent); |
|
| 657 | + |
|
| 658 | + }//end substrToken() |
|
| 659 | + |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Adds a newline to end of a token's content. |
|
| 663 | + * |
|
| 664 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 665 | + * |
|
| 666 | + * @return bool If the change was accepted. |
|
| 667 | + */ |
|
| 668 | + public function addNewline($stackPtr) |
|
| 669 | + { |
|
| 670 | + $current = $this->getTokenContent($stackPtr); |
|
| 671 | + return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
|
| 672 | + |
|
| 673 | + }//end addNewline() |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Adds a newline to the start of a token's content. |
|
| 678 | + * |
|
| 679 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 680 | + * |
|
| 681 | + * @return bool If the change was accepted. |
|
| 682 | + */ |
|
| 683 | + public function addNewlineBefore($stackPtr) |
|
| 684 | + { |
|
| 685 | + $current = $this->getTokenContent($stackPtr); |
|
| 686 | + return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
|
| 687 | + |
|
| 688 | + }//end addNewlineBefore() |
|
| 689 | + |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * Adds content to the end of a token's current content. |
|
| 693 | + * |
|
| 694 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 695 | + * @param string $content The content to add. |
|
| 696 | + * |
|
| 697 | + * @return bool If the change was accepted. |
|
| 698 | + */ |
|
| 699 | + public function addContent($stackPtr, $content) |
|
| 700 | + { |
|
| 701 | + $current = $this->getTokenContent($stackPtr); |
|
| 702 | + return $this->replaceToken($stackPtr, $current.$content); |
|
| 703 | + |
|
| 704 | + }//end addContent() |
|
| 705 | + |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * Adds content to the start of a token's current content. |
|
| 709 | + * |
|
| 710 | + * @param int $stackPtr The position of the token in the token stack. |
|
| 711 | + * @param string $content The content to add. |
|
| 712 | + * |
|
| 713 | + * @return bool If the change was accepted. |
|
| 714 | + */ |
|
| 715 | + public function addContentBefore($stackPtr, $content) |
|
| 716 | + { |
|
| 717 | + $current = $this->getTokenContent($stackPtr); |
|
| 718 | + return $this->replaceToken($stackPtr, $content.$current); |
|
| 719 | + |
|
| 720 | + }//end addContentBefore() |
|
| 721 | 721 | |
| 722 | 722 | |
| 723 | 723 | }//end class |
@@ -269,14 +269,14 @@ |
||
| 269 | 269 | foreach ($diffLines as $line) { |
| 270 | 270 | if (isset($line[0]) === true) { |
| 271 | 271 | switch ($line[0]) { |
| 272 | - case '-': |
|
| 273 | - $diff[] = "\033[31m$line\033[0m"; |
|
| 274 | - break; |
|
| 275 | - case '+': |
|
| 276 | - $diff[] = "\033[32m$line\033[0m"; |
|
| 277 | - break; |
|
| 278 | - default: |
|
| 279 | - $diff[] = $line; |
|
| 272 | + case '-': |
|
| 273 | + $diff[] = "\033[31m$line\033[0m"; |
|
| 274 | + break; |
|
| 275 | + case '+': |
|
| 276 | + $diff[] = "\033[32m$line\033[0m"; |
|
| 277 | + break; |
|
| 278 | + default: |
|
| 279 | + $diff[] = $line; |
|
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @var array<int, string> |
| 54 | 54 | */ |
| 55 | - private $tokens = []; |
|
| 55 | + private $tokens = [ ]; |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * A list of tokens that have already been fixed. |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @var int[] |
| 64 | 64 | */ |
| 65 | - private $fixedTokens = []; |
|
| 65 | + private $fixedTokens = [ ]; |
|
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * The last value of each fixed token. |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @var array<int, string> |
| 74 | 74 | */ |
| 75 | - private $oldTokenValues = []; |
|
| 75 | + private $oldTokenValues = [ ]; |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * A list of tokens that have been fixed during a changeset. |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @var array |
| 84 | 84 | */ |
| 85 | - private $changeset = []; |
|
| 85 | + private $changeset = [ ]; |
|
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * Is there an open changeset. |
@@ -113,19 +113,19 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return void |
| 115 | 115 | */ |
| 116 | - public function startFile(File $phpcsFile) |
|
| 116 | + public function startFile( File $phpcsFile ) |
|
| 117 | 117 | { |
| 118 | 118 | $this->currentFile = $phpcsFile; |
| 119 | 119 | $this->numFixes = 0; |
| 120 | - $this->fixedTokens = []; |
|
| 120 | + $this->fixedTokens = [ ]; |
|
| 121 | 121 | |
| 122 | 122 | $tokens = $phpcsFile->getTokens(); |
| 123 | - $this->tokens = []; |
|
| 124 | - foreach ($tokens as $index => $token) { |
|
| 125 | - if (isset($token['orig_content']) === true) { |
|
| 126 | - $this->tokens[$index] = $token['orig_content']; |
|
| 123 | + $this->tokens = [ ]; |
|
| 124 | + foreach ( $tokens as $index => $token ) { |
|
| 125 | + if ( isset( $token[ 'orig_content' ] ) === true ) { |
|
| 126 | + $this->tokens[ $index ] = $token[ 'orig_content' ]; |
|
| 127 | 127 | } else { |
| 128 | - $this->tokens[$index] = $token['content']; |
|
| 128 | + $this->tokens[ $index ] = $token[ 'content' ]; |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | public function fixFile() |
| 141 | 141 | { |
| 142 | 142 | $fixable = $this->currentFile->getFixableCount(); |
| 143 | - if ($fixable === 0) { |
|
| 143 | + if ( $fixable === 0 ) { |
|
| 144 | 144 | // Nothing to fix. |
| 145 | 145 | return false; |
| 146 | 146 | } |
@@ -148,61 +148,61 @@ discard block |
||
| 148 | 148 | $this->enabled = true; |
| 149 | 149 | |
| 150 | 150 | $this->loops = 0; |
| 151 | - while ($this->loops < 50) { |
|
| 151 | + while ( $this->loops < 50 ) { |
|
| 152 | 152 | ob_start(); |
| 153 | 153 | |
| 154 | 154 | // Only needed once file content has changed. |
| 155 | 155 | $contents = $this->getContents(); |
| 156 | 156 | |
| 157 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
| 157 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
| 158 | 158 | @ob_end_clean(); |
| 159 | - echo '---START FILE CONTENT---'.PHP_EOL; |
|
| 160 | - $lines = explode($this->currentFile->eolChar, $contents); |
|
| 161 | - $max = strlen(count($lines)); |
|
| 162 | - foreach ($lines as $lineNum => $line) { |
|
| 159 | + echo '---START FILE CONTENT---' . PHP_EOL; |
|
| 160 | + $lines = explode( $this->currentFile->eolChar, $contents ); |
|
| 161 | + $max = strlen( count( $lines ) ); |
|
| 162 | + foreach ( $lines as $lineNum => $line ) { |
|
| 163 | 163 | $lineNum++; |
| 164 | - echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
| 164 | + echo str_pad( $lineNum, $max, ' ', STR_PAD_LEFT ) . '|' . $line . PHP_EOL; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
| 167 | + echo '--- END FILE CONTENT ---' . PHP_EOL; |
|
| 168 | 168 | ob_start(); |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | $this->inConflict = false; |
| 172 | 172 | $this->currentFile->ruleset->populateTokenListeners(); |
| 173 | - $this->currentFile->setContent($contents); |
|
| 173 | + $this->currentFile->setContent( $contents ); |
|
| 174 | 174 | $this->currentFile->process(); |
| 175 | 175 | ob_end_clean(); |
| 176 | 176 | |
| 177 | 177 | $this->loops++; |
| 178 | 178 | |
| 179 | - if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 180 | - echo "\r".str_repeat(' ', 80)."\r"; |
|
| 179 | + if ( PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 180 | + echo "\r" . str_repeat( ' ', 80 ) . "\r"; |
|
| 181 | 181 | echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass"; |
| 182 | - if ($this->loops > 1) { |
|
| 182 | + if ( $this->loops > 1 ) { |
|
| 183 | 183 | echo 'es'; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | echo ']... '; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - if ($this->numFixes === 0 && $this->inConflict === false) { |
|
| 189 | + if ( $this->numFixes === 0 && $this->inConflict === false ) { |
|
| 190 | 190 | // Nothing left to do. |
| 191 | 191 | break; |
| 192 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 193 | - echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
| 192 | + } else if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 193 | + echo "\t* fixed $this->numFixes violations, starting loop " . ( $this->loops + 1 ) . ' *' . PHP_EOL; |
|
| 194 | 194 | } |
| 195 | 195 | }//end while |
| 196 | 196 | |
| 197 | 197 | $this->enabled = false; |
| 198 | 198 | |
| 199 | - if ($this->numFixes > 0) { |
|
| 200 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 201 | - if (ob_get_level() > 0) { |
|
| 199 | + if ( $this->numFixes > 0 ) { |
|
| 200 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 201 | + if ( ob_get_level() > 0 ) { |
|
| 202 | 202 | ob_end_clean(); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL; |
|
| 205 | + echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***" . PHP_EOL; |
|
| 206 | 206 | ob_start(); |
| 207 | 207 | } |
| 208 | 208 | |
@@ -224,64 +224,64 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return string |
| 226 | 226 | */ |
| 227 | - public function generateDiff($filePath=null, $colors=true) |
|
| 227 | + public function generateDiff( $filePath = null, $colors = true ) |
|
| 228 | 228 | { |
| 229 | - if ($filePath === null) { |
|
| 229 | + if ( $filePath === null ) { |
|
| 230 | 230 | $filePath = $this->currentFile->getFilename(); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
| 234 | - if (strpos($filePath, $cwd) === 0) { |
|
| 235 | - $filename = substr($filePath, strlen($cwd)); |
|
| 233 | + $cwd = getcwd() . DIRECTORY_SEPARATOR; |
|
| 234 | + if ( strpos( $filePath, $cwd ) === 0 ) { |
|
| 235 | + $filename = substr( $filePath, strlen( $cwd ) ); |
|
| 236 | 236 | } else { |
| 237 | 237 | $filename = $filePath; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | $contents = $this->getContents(); |
| 241 | 241 | |
| 242 | - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
| 243 | - $fixedFile = fopen($tempName, 'w'); |
|
| 244 | - fwrite($fixedFile, $contents); |
|
| 242 | + $tempName = tempnam( sys_get_temp_dir(), 'phpcs-fixer' ); |
|
| 243 | + $fixedFile = fopen( $tempName, 'w' ); |
|
| 244 | + fwrite( $fixedFile, $contents ); |
|
| 245 | 245 | |
| 246 | 246 | // We must use something like shell_exec() because whitespace at the end |
| 247 | 247 | // of lines is critical to diff files. |
| 248 | - $filename = escapeshellarg($filename); |
|
| 248 | + $filename = escapeshellarg( $filename ); |
|
| 249 | 249 | $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; |
| 250 | 250 | |
| 251 | - $diff = shell_exec($cmd); |
|
| 251 | + $diff = shell_exec( $cmd ); |
|
| 252 | 252 | |
| 253 | - fclose($fixedFile); |
|
| 254 | - if (is_file($tempName) === true) { |
|
| 255 | - unlink($tempName); |
|
| 253 | + fclose( $fixedFile ); |
|
| 254 | + if ( is_file( $tempName ) === true ) { |
|
| 255 | + unlink( $tempName ); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if ($colors === false) { |
|
| 258 | + if ( $colors === false ) { |
|
| 259 | 259 | return $diff; |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | - $diffLines = explode(PHP_EOL, $diff); |
|
| 263 | - if (count($diffLines) === 1) { |
|
| 262 | + $diffLines = explode( PHP_EOL, $diff ); |
|
| 263 | + if ( count( $diffLines ) === 1 ) { |
|
| 264 | 264 | // Seems to be required for cygwin. |
| 265 | - $diffLines = explode("\n", $diff); |
|
| 265 | + $diffLines = explode( "\n", $diff ); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - $diff = []; |
|
| 269 | - foreach ($diffLines as $line) { |
|
| 270 | - if (isset($line[0]) === true) { |
|
| 271 | - switch ($line[0]) { |
|
| 268 | + $diff = [ ]; |
|
| 269 | + foreach ( $diffLines as $line ) { |
|
| 270 | + if ( isset( $line[ 0 ] ) === true ) { |
|
| 271 | + switch ( $line[ 0 ] ) { |
|
| 272 | 272 | case '-': |
| 273 | - $diff[] = "\033[31m$line\033[0m"; |
|
| 273 | + $diff[ ] = "\033[31m$line\033[0m"; |
|
| 274 | 274 | break; |
| 275 | 275 | case '+': |
| 276 | - $diff[] = "\033[32m$line\033[0m"; |
|
| 276 | + $diff[ ] = "\033[32m$line\033[0m"; |
|
| 277 | 277 | break; |
| 278 | 278 | default: |
| 279 | - $diff[] = $line; |
|
| 279 | + $diff[ ] = $line; |
|
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | - $diff = implode(PHP_EOL, $diff); |
|
| 284 | + $diff = implode( PHP_EOL, $diff ); |
|
| 285 | 285 | |
| 286 | 286 | return $diff; |
| 287 | 287 | |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | */ |
| 311 | 311 | public function getContents() |
| 312 | 312 | { |
| 313 | - $contents = implode($this->tokens); |
|
| 313 | + $contents = implode( $this->tokens ); |
|
| 314 | 314 | return $contents; |
| 315 | 315 | |
| 316 | 316 | }//end getContents() |
@@ -326,14 +326,14 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @return string |
| 328 | 328 | */ |
| 329 | - public function getTokenContent($stackPtr) |
|
| 329 | + public function getTokenContent( $stackPtr ) |
|
| 330 | 330 | { |
| 331 | - if ($this->inChangeset === true |
|
| 332 | - && isset($this->changeset[$stackPtr]) === true |
|
| 331 | + if ( $this->inChangeset === true |
|
| 332 | + && isset( $this->changeset[ $stackPtr ] ) === true |
|
| 333 | 333 | ) { |
| 334 | - return $this->changeset[$stackPtr]; |
|
| 334 | + return $this->changeset[ $stackPtr ]; |
|
| 335 | 335 | } else { |
| 336 | - return $this->tokens[$stackPtr]; |
|
| 336 | + return $this->tokens[ $stackPtr ]; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | }//end getTokenContent() |
@@ -346,21 +346,21 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | public function beginChangeset() |
| 348 | 348 | { |
| 349 | - if ($this->inConflict === true) { |
|
| 349 | + if ( $this->inConflict === true ) { |
|
| 350 | 350 | return false; |
| 351 | 351 | } |
| 352 | 352 | |
| 353 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 354 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 355 | - $sniff = $bt[1]['class']; |
|
| 356 | - $line = $bt[0]['line']; |
|
| 353 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 354 | + $bt = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
|
| 355 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
| 356 | + $line = $bt[ 0 ][ 'line' ]; |
|
| 357 | 357 | |
| 358 | 358 | @ob_end_clean(); |
| 359 | - echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
| 359 | + echo "\t=> Changeset started by $sniff (line $line)" . PHP_EOL; |
|
| 360 | 360 | ob_start(); |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - $this->changeset = []; |
|
| 363 | + $this->changeset = [ ]; |
|
| 364 | 364 | $this->inChangeset = true; |
| 365 | 365 | |
| 366 | 366 | }//end beginChangeset() |
@@ -373,42 +373,42 @@ discard block |
||
| 373 | 373 | */ |
| 374 | 374 | public function endChangeset() |
| 375 | 375 | { |
| 376 | - if ($this->inConflict === true) { |
|
| 376 | + if ( $this->inConflict === true ) { |
|
| 377 | 377 | return false; |
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | $this->inChangeset = false; |
| 381 | 381 | |
| 382 | 382 | $success = true; |
| 383 | - $applied = []; |
|
| 384 | - foreach ($this->changeset as $stackPtr => $content) { |
|
| 385 | - $success = $this->replaceToken($stackPtr, $content); |
|
| 386 | - if ($success === false) { |
|
| 383 | + $applied = [ ]; |
|
| 384 | + foreach ( $this->changeset as $stackPtr => $content ) { |
|
| 385 | + $success = $this->replaceToken( $stackPtr, $content ); |
|
| 386 | + if ( $success === false ) { |
|
| 387 | 387 | break; |
| 388 | 388 | } else { |
| 389 | - $applied[] = $stackPtr; |
|
| 389 | + $applied[ ] = $stackPtr; |
|
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | - if ($success === false) { |
|
| 393 | + if ( $success === false ) { |
|
| 394 | 394 | // Rolling back all changes. |
| 395 | - foreach ($applied as $stackPtr) { |
|
| 396 | - $this->revertToken($stackPtr); |
|
| 395 | + foreach ( $applied as $stackPtr ) { |
|
| 396 | + $this->revertToken( $stackPtr ); |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 399 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 400 | 400 | @ob_end_clean(); |
| 401 | - echo "\t=> Changeset failed to apply".PHP_EOL; |
|
| 401 | + echo "\t=> Changeset failed to apply" . PHP_EOL; |
|
| 402 | 402 | ob_start(); |
| 403 | 403 | } |
| 404 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 405 | - $fixes = count($this->changeset); |
|
| 404 | + } else if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 405 | + $fixes = count( $this->changeset ); |
|
| 406 | 406 | @ob_end_clean(); |
| 407 | - echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
| 407 | + echo "\t=> Changeset ended: $fixes changes applied" . PHP_EOL; |
|
| 408 | 408 | ob_start(); |
| 409 | 409 | } |
| 410 | 410 | |
| 411 | - $this->changeset = []; |
|
| 411 | + $this->changeset = [ ]; |
|
| 412 | 412 | |
| 413 | 413 | }//end endChangeset() |
| 414 | 414 | |
@@ -423,26 +423,26 @@ discard block |
||
| 423 | 423 | $this->inChangeset = false; |
| 424 | 424 | $this->inConflict = false; |
| 425 | 425 | |
| 426 | - if (empty($this->changeset) === false) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 426 | + if ( empty( $this->changeset ) === false ) { |
|
| 427 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 428 | 428 | $bt = debug_backtrace(); |
| 429 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 430 | - $sniff = $bt[2]['class']; |
|
| 431 | - $line = $bt[1]['line']; |
|
| 429 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer\Fixer' ) { |
|
| 430 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
| 431 | + $line = $bt[ 1 ][ 'line' ]; |
|
| 432 | 432 | } else { |
| 433 | - $sniff = $bt[1]['class']; |
|
| 434 | - $line = $bt[0]['line']; |
|
| 433 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
| 434 | + $line = $bt[ 0 ][ 'line' ]; |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - $numChanges = count($this->changeset); |
|
| 437 | + $numChanges = count( $this->changeset ); |
|
| 438 | 438 | |
| 439 | 439 | @ob_end_clean(); |
| 440 | - echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
| 441 | - echo "\t=> Changeset rolled back".PHP_EOL; |
|
| 440 | + echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)" . PHP_EOL; |
|
| 441 | + echo "\t=> Changeset rolled back" . PHP_EOL; |
|
| 442 | 442 | ob_start(); |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | - $this->changeset = []; |
|
| 445 | + $this->changeset = [ ]; |
|
| 446 | 446 | }//end if |
| 447 | 447 | |
| 448 | 448 | }//end rollbackChangeset() |
@@ -456,120 +456,120 @@ discard block |
||
| 456 | 456 | * |
| 457 | 457 | * @return bool If the change was accepted. |
| 458 | 458 | */ |
| 459 | - public function replaceToken($stackPtr, $content) |
|
| 459 | + public function replaceToken( $stackPtr, $content ) |
|
| 460 | 460 | { |
| 461 | - if ($this->inConflict === true) { |
|
| 461 | + if ( $this->inConflict === true ) { |
|
| 462 | 462 | return false; |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | - if ($this->inChangeset === false |
|
| 466 | - && isset($this->fixedTokens[$stackPtr]) === true |
|
| 465 | + if ( $this->inChangeset === false |
|
| 466 | + && isset( $this->fixedTokens[ $stackPtr ] ) === true |
|
| 467 | 467 | ) { |
| 468 | 468 | $indent = "\t"; |
| 469 | - if (empty($this->changeset) === false) { |
|
| 469 | + if ( empty( $this->changeset ) === false ) { |
|
| 470 | 470 | $indent .= "\t"; |
| 471 | 471 | } |
| 472 | 472 | |
| 473 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 473 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 474 | 474 | @ob_end_clean(); |
| 475 | - echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
| 475 | + echo "$indent* token $stackPtr has already been modified, skipping *" . PHP_EOL; |
|
| 476 | 476 | ob_start(); |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | 479 | return false; |
| 480 | 480 | } |
| 481 | 481 | |
| 482 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 483 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 484 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 485 | - $sniff = $bt[2]['class']; |
|
| 486 | - $line = $bt[1]['line']; |
|
| 482 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 483 | + $bt = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
|
| 484 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer\Fixer' ) { |
|
| 485 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
| 486 | + $line = $bt[ 1 ][ 'line' ]; |
|
| 487 | 487 | } else { |
| 488 | - $sniff = $bt[1]['class']; |
|
| 489 | - $line = $bt[0]['line']; |
|
| 488 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
| 489 | + $line = $bt[ 0 ][ 'line' ]; |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | $tokens = $this->currentFile->getTokens(); |
| 493 | - $type = $tokens[$stackPtr]['type']; |
|
| 494 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 495 | - $newContent = Common::prepareForOutput($content); |
|
| 496 | - if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[($stackPtr + 1)]) === true) { |
|
| 493 | + $type = $tokens[ $stackPtr ][ 'type' ]; |
|
| 494 | + $oldContent = Common::prepareForOutput( $this->tokens[ $stackPtr ] ); |
|
| 495 | + $newContent = Common::prepareForOutput( $content ); |
|
| 496 | + if ( trim( $this->tokens[ $stackPtr ] ) === '' && isset( $this->tokens[ ( $stackPtr + 1 ) ] ) === true ) { |
|
| 497 | 497 | // Add some context for whitespace only changes. |
| 498 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 498 | + $append = Common::prepareForOutput( $this->tokens[ ( $stackPtr + 1 ) ] ); |
|
| 499 | 499 | $oldContent .= $append; |
| 500 | 500 | $newContent .= $append; |
| 501 | 501 | } |
| 502 | 502 | }//end if |
| 503 | 503 | |
| 504 | - if ($this->inChangeset === true) { |
|
| 505 | - $this->changeset[$stackPtr] = $content; |
|
| 504 | + if ( $this->inChangeset === true ) { |
|
| 505 | + $this->changeset[ $stackPtr ] = $content; |
|
| 506 | 506 | |
| 507 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 507 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 508 | 508 | @ob_end_clean(); |
| 509 | - echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 509 | + echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
| 510 | 510 | ob_start(); |
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | return true; |
| 514 | 514 | } |
| 515 | 515 | |
| 516 | - if (isset($this->oldTokenValues[$stackPtr]) === false) { |
|
| 517 | - $this->oldTokenValues[$stackPtr] = [ |
|
| 516 | + if ( isset( $this->oldTokenValues[ $stackPtr ] ) === false ) { |
|
| 517 | + $this->oldTokenValues[ $stackPtr ] = [ |
|
| 518 | 518 | 'curr' => $content, |
| 519 | - 'prev' => $this->tokens[$stackPtr], |
|
| 519 | + 'prev' => $this->tokens[ $stackPtr ], |
|
| 520 | 520 | 'loop' => $this->loops, |
| 521 | 521 | ]; |
| 522 | 522 | } else { |
| 523 | - if ($this->oldTokenValues[$stackPtr]['prev'] === $content |
|
| 524 | - && $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
| 523 | + if ( $this->oldTokenValues[ $stackPtr ][ 'prev' ] === $content |
|
| 524 | + && $this->oldTokenValues[ $stackPtr ][ 'loop' ] === ( $this->loops - 1 ) |
|
| 525 | 525 | ) { |
| 526 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 526 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 527 | 527 | $indent = "\t"; |
| 528 | - if (empty($this->changeset) === false) { |
|
| 528 | + if ( empty( $this->changeset ) === false ) { |
|
| 529 | 529 | $indent .= "\t"; |
| 530 | 530 | } |
| 531 | 531 | |
| 532 | - $loop = $this->oldTokenValues[$stackPtr]['loop']; |
|
| 532 | + $loop = $this->oldTokenValues[ $stackPtr ][ 'loop' ]; |
|
| 533 | 533 | |
| 534 | 534 | @ob_end_clean(); |
| 535 | - echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
| 536 | - echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
| 535 | + echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****" . PHP_EOL; |
|
| 536 | + echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****" . PHP_EOL; |
|
| 537 | 537 | } |
| 538 | 538 | |
| 539 | - if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
| 539 | + if ( $this->oldTokenValues[ $stackPtr ][ 'loop' ] >= ( $this->loops - 1 ) ) { |
|
| 540 | 540 | $this->inConflict = true; |
| 541 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 542 | - echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
| 541 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 542 | + echo "$indent**** ignoring all changes until next loop ****" . PHP_EOL; |
|
| 543 | 543 | } |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 546 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 547 | 547 | ob_start(); |
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | return false; |
| 551 | 551 | }//end if |
| 552 | 552 | |
| 553 | - $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr']; |
|
| 554 | - $this->oldTokenValues[$stackPtr]['curr'] = $content; |
|
| 555 | - $this->oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
| 553 | + $this->oldTokenValues[ $stackPtr ][ 'prev' ] = $this->oldTokenValues[ $stackPtr ][ 'curr' ]; |
|
| 554 | + $this->oldTokenValues[ $stackPtr ][ 'curr' ] = $content; |
|
| 555 | + $this->oldTokenValues[ $stackPtr ][ 'loop' ] = $this->loops; |
|
| 556 | 556 | }//end if |
| 557 | 557 | |
| 558 | - $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr]; |
|
| 559 | - $this->tokens[$stackPtr] = $content; |
|
| 558 | + $this->fixedTokens[ $stackPtr ] = $this->tokens[ $stackPtr ]; |
|
| 559 | + $this->tokens[ $stackPtr ] = $content; |
|
| 560 | 560 | $this->numFixes++; |
| 561 | 561 | |
| 562 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 562 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 563 | 563 | $indent = "\t"; |
| 564 | - if (empty($this->changeset) === false) { |
|
| 564 | + if ( empty( $this->changeset ) === false ) { |
|
| 565 | 565 | $indent .= "\tA: "; |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | - if (ob_get_level() > 0) { |
|
| 568 | + if ( ob_get_level() > 0 ) { |
|
| 569 | 569 | ob_end_clean(); |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | - echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 572 | + echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
| 573 | 573 | ob_start(); |
| 574 | 574 | } |
| 575 | 575 | |
@@ -585,46 +585,46 @@ discard block |
||
| 585 | 585 | * |
| 586 | 586 | * @return bool If a change was reverted. |
| 587 | 587 | */ |
| 588 | - public function revertToken($stackPtr) |
|
| 588 | + public function revertToken( $stackPtr ) |
|
| 589 | 589 | { |
| 590 | - if (isset($this->fixedTokens[$stackPtr]) === false) { |
|
| 590 | + if ( isset( $this->fixedTokens[ $stackPtr ] ) === false ) { |
|
| 591 | 591 | return false; |
| 592 | 592 | } |
| 593 | 593 | |
| 594 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 595 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 596 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
| 597 | - $sniff = $bt[2]['class']; |
|
| 598 | - $line = $bt[1]['line']; |
|
| 594 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 595 | + $bt = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
|
| 596 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer\Fixer' ) { |
|
| 597 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
| 598 | + $line = $bt[ 1 ][ 'line' ]; |
|
| 599 | 599 | } else { |
| 600 | - $sniff = $bt[1]['class']; |
|
| 601 | - $line = $bt[0]['line']; |
|
| 600 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
| 601 | + $line = $bt[ 0 ][ 'line' ]; |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | $tokens = $this->currentFile->getTokens(); |
| 605 | - $type = $tokens[$stackPtr]['type']; |
|
| 606 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
| 607 | - $newContent = Common::prepareForOutput($this->fixedTokens[$stackPtr]); |
|
| 608 | - if (trim($this->tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
| 605 | + $type = $tokens[ $stackPtr ][ 'type' ]; |
|
| 606 | + $oldContent = Common::prepareForOutput( $this->tokens[ $stackPtr ] ); |
|
| 607 | + $newContent = Common::prepareForOutput( $this->fixedTokens[ $stackPtr ] ); |
|
| 608 | + if ( trim( $this->tokens[ $stackPtr ] ) === '' && isset( $tokens[ ( $stackPtr + 1 ) ] ) === true ) { |
|
| 609 | 609 | // Add some context for whitespace only changes. |
| 610 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
| 610 | + $append = Common::prepareForOutput( $this->tokens[ ( $stackPtr + 1 ) ] ); |
|
| 611 | 611 | $oldContent .= $append; |
| 612 | 612 | $newContent .= $append; |
| 613 | 613 | } |
| 614 | 614 | }//end if |
| 615 | 615 | |
| 616 | - $this->tokens[$stackPtr] = $this->fixedTokens[$stackPtr]; |
|
| 617 | - unset($this->fixedTokens[$stackPtr]); |
|
| 616 | + $this->tokens[ $stackPtr ] = $this->fixedTokens[ $stackPtr ]; |
|
| 617 | + unset( $this->fixedTokens[ $stackPtr ] ); |
|
| 618 | 618 | $this->numFixes--; |
| 619 | 619 | |
| 620 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 620 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 621 | 621 | $indent = "\t"; |
| 622 | - if (empty($this->changeset) === false) { |
|
| 622 | + if ( empty( $this->changeset ) === false ) { |
|
| 623 | 623 | $indent .= "\tR: "; |
| 624 | 624 | } |
| 625 | 625 | |
| 626 | 626 | @ob_end_clean(); |
| 627 | - echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
| 627 | + echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
| 628 | 628 | ob_start(); |
| 629 | 629 | } |
| 630 | 630 | |
@@ -643,17 +643,17 @@ discard block |
||
| 643 | 643 | * |
| 644 | 644 | * @return bool If the change was accepted. |
| 645 | 645 | */ |
| 646 | - public function substrToken($stackPtr, $start, $length=null) |
|
| 646 | + public function substrToken( $stackPtr, $start, $length = null ) |
|
| 647 | 647 | { |
| 648 | - $current = $this->getTokenContent($stackPtr); |
|
| 648 | + $current = $this->getTokenContent( $stackPtr ); |
|
| 649 | 649 | |
| 650 | - if ($length === null) { |
|
| 651 | - $newContent = substr($current, $start); |
|
| 650 | + if ( $length === null ) { |
|
| 651 | + $newContent = substr( $current, $start ); |
|
| 652 | 652 | } else { |
| 653 | - $newContent = substr($current, $start, $length); |
|
| 653 | + $newContent = substr( $current, $start, $length ); |
|
| 654 | 654 | } |
| 655 | 655 | |
| 656 | - return $this->replaceToken($stackPtr, $newContent); |
|
| 656 | + return $this->replaceToken( $stackPtr, $newContent ); |
|
| 657 | 657 | |
| 658 | 658 | }//end substrToken() |
| 659 | 659 | |
@@ -665,10 +665,10 @@ discard block |
||
| 665 | 665 | * |
| 666 | 666 | * @return bool If the change was accepted. |
| 667 | 667 | */ |
| 668 | - public function addNewline($stackPtr) |
|
| 668 | + public function addNewline( $stackPtr ) |
|
| 669 | 669 | { |
| 670 | - $current = $this->getTokenContent($stackPtr); |
|
| 671 | - return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
|
| 670 | + $current = $this->getTokenContent( $stackPtr ); |
|
| 671 | + return $this->replaceToken( $stackPtr, $current . $this->currentFile->eolChar ); |
|
| 672 | 672 | |
| 673 | 673 | }//end addNewline() |
| 674 | 674 | |
@@ -680,10 +680,10 @@ discard block |
||
| 680 | 680 | * |
| 681 | 681 | * @return bool If the change was accepted. |
| 682 | 682 | */ |
| 683 | - public function addNewlineBefore($stackPtr) |
|
| 683 | + public function addNewlineBefore( $stackPtr ) |
|
| 684 | 684 | { |
| 685 | - $current = $this->getTokenContent($stackPtr); |
|
| 686 | - return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
|
| 685 | + $current = $this->getTokenContent( $stackPtr ); |
|
| 686 | + return $this->replaceToken( $stackPtr, $this->currentFile->eolChar . $current ); |
|
| 687 | 687 | |
| 688 | 688 | }//end addNewlineBefore() |
| 689 | 689 | |
@@ -696,10 +696,10 @@ discard block |
||
| 696 | 696 | * |
| 697 | 697 | * @return bool If the change was accepted. |
| 698 | 698 | */ |
| 699 | - public function addContent($stackPtr, $content) |
|
| 699 | + public function addContent( $stackPtr, $content ) |
|
| 700 | 700 | { |
| 701 | - $current = $this->getTokenContent($stackPtr); |
|
| 702 | - return $this->replaceToken($stackPtr, $current.$content); |
|
| 701 | + $current = $this->getTokenContent( $stackPtr ); |
|
| 702 | + return $this->replaceToken( $stackPtr, $current . $content ); |
|
| 703 | 703 | |
| 704 | 704 | }//end addContent() |
| 705 | 705 | |
@@ -712,10 +712,10 @@ discard block |
||
| 712 | 712 | * |
| 713 | 713 | * @return bool If the change was accepted. |
| 714 | 714 | */ |
| 715 | - public function addContentBefore($stackPtr, $content) |
|
| 715 | + public function addContentBefore( $stackPtr, $content ) |
|
| 716 | 716 | { |
| 717 | - $current = $this->getTokenContent($stackPtr); |
|
| 718 | - return $this->replaceToken($stackPtr, $content.$current); |
|
| 717 | + $current = $this->getTokenContent( $stackPtr ); |
|
| 718 | + return $this->replaceToken( $stackPtr, $content . $current ); |
|
| 719 | 719 | |
| 720 | 720 | }//end addContentBefore() |
| 721 | 721 | |
@@ -15,8 +15,7 @@ discard block |
||
| 15 | 15 | use PHP_CodeSniffer\Files\File; |
| 16 | 16 | use PHP_CodeSniffer\Util\Common; |
| 17 | 17 | |
| 18 | -class Fixer |
|
| 19 | -{ |
|
| 18 | +class Fixer { |
|
| 20 | 19 | |
| 21 | 20 | /** |
| 22 | 21 | * Is the fixer enabled and fixing a file? |
@@ -113,8 +112,7 @@ discard block |
||
| 113 | 112 | * |
| 114 | 113 | * @return void |
| 115 | 114 | */ |
| 116 | - public function startFile(File $phpcsFile) |
|
| 117 | - { |
|
| 115 | + public function startFile(File $phpcsFile) { |
|
| 118 | 116 | $this->currentFile = $phpcsFile; |
| 119 | 117 | $this->numFixes = 0; |
| 120 | 118 | $this->fixedTokens = []; |
@@ -137,8 +135,7 @@ discard block |
||
| 137 | 135 | * |
| 138 | 136 | * @return boolean |
| 139 | 137 | */ |
| 140 | - public function fixFile() |
|
| 141 | - { |
|
| 138 | + public function fixFile() { |
|
| 142 | 139 | $fixable = $this->currentFile->getFixableCount(); |
| 143 | 140 | if ($fixable === 0) { |
| 144 | 141 | // Nothing to fix. |
@@ -224,8 +221,7 @@ discard block |
||
| 224 | 221 | * |
| 225 | 222 | * @return string |
| 226 | 223 | */ |
| 227 | - public function generateDiff($filePath=null, $colors=true) |
|
| 228 | - { |
|
| 224 | + public function generateDiff($filePath=null, $colors=true) { |
|
| 229 | 225 | if ($filePath === null) { |
| 230 | 226 | $filePath = $this->currentFile->getFilename(); |
| 231 | 227 | } |
@@ -296,8 +292,7 @@ discard block |
||
| 296 | 292 | * |
| 297 | 293 | * @return int |
| 298 | 294 | */ |
| 299 | - public function getFixCount() |
|
| 300 | - { |
|
| 295 | + public function getFixCount() { |
|
| 301 | 296 | return $this->numFixes; |
| 302 | 297 | |
| 303 | 298 | }//end getFixCount() |
@@ -308,8 +303,7 @@ discard block |
||
| 308 | 303 | * |
| 309 | 304 | * @return string |
| 310 | 305 | */ |
| 311 | - public function getContents() |
|
| 312 | - { |
|
| 306 | + public function getContents() { |
|
| 313 | 307 | $contents = implode($this->tokens); |
| 314 | 308 | return $contents; |
| 315 | 309 | |
@@ -326,8 +320,7 @@ discard block |
||
| 326 | 320 | * |
| 327 | 321 | * @return string |
| 328 | 322 | */ |
| 329 | - public function getTokenContent($stackPtr) |
|
| 330 | - { |
|
| 323 | + public function getTokenContent($stackPtr) { |
|
| 331 | 324 | if ($this->inChangeset === true |
| 332 | 325 | && isset($this->changeset[$stackPtr]) === true |
| 333 | 326 | ) { |
@@ -344,8 +337,7 @@ discard block |
||
| 344 | 337 | * |
| 345 | 338 | * @return void |
| 346 | 339 | */ |
| 347 | - public function beginChangeset() |
|
| 348 | - { |
|
| 340 | + public function beginChangeset() { |
|
| 349 | 341 | if ($this->inConflict === true) { |
| 350 | 342 | return false; |
| 351 | 343 | } |
@@ -371,8 +363,7 @@ discard block |
||
| 371 | 363 | * |
| 372 | 364 | * @return boolean |
| 373 | 365 | */ |
| 374 | - public function endChangeset() |
|
| 375 | - { |
|
| 366 | + public function endChangeset() { |
|
| 376 | 367 | if ($this->inConflict === true) { |
| 377 | 368 | return false; |
| 378 | 369 | } |
@@ -418,8 +409,7 @@ discard block |
||
| 418 | 409 | * |
| 419 | 410 | * @return void |
| 420 | 411 | */ |
| 421 | - public function rollbackChangeset() |
|
| 422 | - { |
|
| 412 | + public function rollbackChangeset() { |
|
| 423 | 413 | $this->inChangeset = false; |
| 424 | 414 | $this->inConflict = false; |
| 425 | 415 | |
@@ -456,8 +446,7 @@ discard block |
||
| 456 | 446 | * |
| 457 | 447 | * @return bool If the change was accepted. |
| 458 | 448 | */ |
| 459 | - public function replaceToken($stackPtr, $content) |
|
| 460 | - { |
|
| 449 | + public function replaceToken($stackPtr, $content) { |
|
| 461 | 450 | if ($this->inConflict === true) { |
| 462 | 451 | return false; |
| 463 | 452 | } |
@@ -585,8 +574,7 @@ discard block |
||
| 585 | 574 | * |
| 586 | 575 | * @return bool If a change was reverted. |
| 587 | 576 | */ |
| 588 | - public function revertToken($stackPtr) |
|
| 589 | - { |
|
| 577 | + public function revertToken($stackPtr) { |
|
| 590 | 578 | if (isset($this->fixedTokens[$stackPtr]) === false) { |
| 591 | 579 | return false; |
| 592 | 580 | } |
@@ -643,8 +631,7 @@ discard block |
||
| 643 | 631 | * |
| 644 | 632 | * @return bool If the change was accepted. |
| 645 | 633 | */ |
| 646 | - public function substrToken($stackPtr, $start, $length=null) |
|
| 647 | - { |
|
| 634 | + public function substrToken($stackPtr, $start, $length=null) { |
|
| 648 | 635 | $current = $this->getTokenContent($stackPtr); |
| 649 | 636 | |
| 650 | 637 | if ($length === null) { |
@@ -665,8 +652,7 @@ discard block |
||
| 665 | 652 | * |
| 666 | 653 | * @return bool If the change was accepted. |
| 667 | 654 | */ |
| 668 | - public function addNewline($stackPtr) |
|
| 669 | - { |
|
| 655 | + public function addNewline($stackPtr) { |
|
| 670 | 656 | $current = $this->getTokenContent($stackPtr); |
| 671 | 657 | return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
| 672 | 658 | |
@@ -680,8 +666,7 @@ discard block |
||
| 680 | 666 | * |
| 681 | 667 | * @return bool If the change was accepted. |
| 682 | 668 | */ |
| 683 | - public function addNewlineBefore($stackPtr) |
|
| 684 | - { |
|
| 669 | + public function addNewlineBefore($stackPtr) { |
|
| 685 | 670 | $current = $this->getTokenContent($stackPtr); |
| 686 | 671 | return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
| 687 | 672 | |
@@ -696,8 +681,7 @@ discard block |
||
| 696 | 681 | * |
| 697 | 682 | * @return bool If the change was accepted. |
| 698 | 683 | */ |
| 699 | - public function addContent($stackPtr, $content) |
|
| 700 | - { |
|
| 684 | + public function addContent($stackPtr, $content) { |
|
| 701 | 685 | $current = $this->getTokenContent($stackPtr); |
| 702 | 686 | return $this->replaceToken($stackPtr, $current.$content); |
| 703 | 687 | |
@@ -712,8 +696,7 @@ discard block |
||
| 712 | 696 | * |
| 713 | 697 | * @return bool If the change was accepted. |
| 714 | 698 | */ |
| 715 | - public function addContentBefore($stackPtr, $content) |
|
| 716 | - { |
|
| 699 | + public function addContentBefore($stackPtr, $content) { |
|
| 717 | 700 | $current = $this->getTokenContent($stackPtr); |
| 718 | 701 | return $this->replaceToken($stackPtr, $content.$current); |
| 719 | 702 | |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | * its data should be counted in the grand totals. |
| 30 | 30 | * |
| 31 | 31 | * @param array $report Prepared report data. |
| 32 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 32 | + * @param File $phpcsFile The file being reported on. |
|
| 33 | 33 | * @param bool $showSources Show sources? |
| 34 | 34 | * @param int $width Maximum allowed line width. |
| 35 | 35 | * |
@@ -21,229 +21,229 @@ |
||
| 21 | 21 | { |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Generate a partial report for a single processed file. |
|
| 26 | - * |
|
| 27 | - * Function should return TRUE if it printed or stored data about the file |
|
| 28 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 29 | - * its data should be counted in the grand totals. |
|
| 30 | - * |
|
| 31 | - * @param array $report Prepared report data. |
|
| 32 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 33 | - * @param bool $showSources Show sources? |
|
| 34 | - * @param int $width Maximum allowed line width. |
|
| 35 | - * |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 39 | - { |
|
| 40 | - $errors = $phpcsFile->getFixableCount(); |
|
| 41 | - if ($errors !== 0) { |
|
| 42 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 43 | - ob_end_clean(); |
|
| 44 | - $startTime = microtime(true); |
|
| 45 | - echo "\t=> Fixing file: $errors/$errors violations remaining"; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $fixed = $phpcsFile->fixer->fixFile(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if ($phpcsFile->config->stdin === true) { |
|
| 52 | - // Replacing STDIN, so output current file to STDOUT |
|
| 53 | - // even if nothing was fixed. Exit here because we |
|
| 54 | - // can't process any more than 1 file in this setup. |
|
| 55 | - $fixedContent = $phpcsFile->fixer->getContents(); |
|
| 56 | - throw new DeepExitException($fixedContent, 1); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - if ($errors === 0) { |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 64 | - if ($fixed === false) { |
|
| 65 | - echo 'ERROR'; |
|
| 66 | - } else { |
|
| 67 | - echo 'DONE'; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 71 | - if ($timeTaken < 1000) { |
|
| 72 | - $timeTaken = round($timeTaken); |
|
| 73 | - echo " in {$timeTaken}ms".PHP_EOL; |
|
| 74 | - } else { |
|
| 75 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 76 | - echo " in $timeTaken secs".PHP_EOL; |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if ($fixed === true) { |
|
| 81 | - // The filename in the report may be truncated due to a basepath setting |
|
| 82 | - // but we are using it for writing here and not display, |
|
| 83 | - // so find the correct path if basepath is in use. |
|
| 84 | - $newFilename = $report['filename'].$phpcsFile->config->suffix; |
|
| 85 | - if ($phpcsFile->config->basepath !== null) { |
|
| 86 | - $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $newContent = $phpcsFile->fixer->getContents(); |
|
| 90 | - file_put_contents($newFilename, $newContent); |
|
| 91 | - |
|
| 92 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 93 | - if ($newFilename === $report['filename']) { |
|
| 94 | - echo "\t=> File was overwritten".PHP_EOL; |
|
| 95 | - } else { |
|
| 96 | - echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 102 | - ob_start(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $errorCount = $phpcsFile->getErrorCount(); |
|
| 106 | - $warningCount = $phpcsFile->getWarningCount(); |
|
| 107 | - $fixableCount = $phpcsFile->getFixableCount(); |
|
| 108 | - $fixedCount = ($errors - $fixableCount); |
|
| 109 | - echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL; |
|
| 110 | - |
|
| 111 | - return $fixed; |
|
| 112 | - |
|
| 113 | - }//end generateFileReport() |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Prints a summary of fixed files. |
|
| 118 | - * |
|
| 119 | - * @param string $cachedData Any partial report data that was returned from |
|
| 120 | - * generateFileReport during the run. |
|
| 121 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 122 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 123 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 124 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 125 | - * @param bool $showSources Show sources? |
|
| 126 | - * @param int $width Maximum allowed line width. |
|
| 127 | - * @param bool $interactive Are we running in interactive mode? |
|
| 128 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 129 | - * |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public function generate( |
|
| 133 | - $cachedData, |
|
| 134 | - $totalFiles, |
|
| 135 | - $totalErrors, |
|
| 136 | - $totalWarnings, |
|
| 137 | - $totalFixable, |
|
| 138 | - $showSources=false, |
|
| 139 | - $width=80, |
|
| 140 | - $interactive=false, |
|
| 141 | - $toScreen=true |
|
| 142 | - ) { |
|
| 143 | - $lines = explode(PHP_EOL, $cachedData); |
|
| 144 | - array_pop($lines); |
|
| 145 | - |
|
| 146 | - if (empty($lines) === true) { |
|
| 147 | - echo PHP_EOL.'No fixable errors were found'.PHP_EOL; |
|
| 148 | - return; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $reportFiles = []; |
|
| 152 | - $maxLength = 0; |
|
| 153 | - $totalFixed = 0; |
|
| 154 | - $failures = 0; |
|
| 155 | - |
|
| 156 | - foreach ($lines as $line) { |
|
| 157 | - $parts = explode('>>', $line); |
|
| 158 | - $fileLen = strlen($parts[0]); |
|
| 159 | - $reportFiles[$parts[0]] = [ |
|
| 160 | - 'errors' => $parts[1], |
|
| 161 | - 'warnings' => $parts[2], |
|
| 162 | - 'fixable' => $parts[3], |
|
| 163 | - 'fixed' => $parts[4], |
|
| 164 | - 'strlen' => $fileLen, |
|
| 165 | - ]; |
|
| 166 | - |
|
| 167 | - $maxLength = max($maxLength, $fileLen); |
|
| 168 | - |
|
| 169 | - $totalFixed += $parts[4]; |
|
| 170 | - |
|
| 171 | - if ($parts[3] > 0) { |
|
| 172 | - $failures++; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $width = min($width, ($maxLength + 21)); |
|
| 177 | - $width = max($width, 70); |
|
| 178 | - |
|
| 179 | - echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL; |
|
| 180 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 181 | - echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED REMAINING'."\033[0m".PHP_EOL; |
|
| 182 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 183 | - |
|
| 184 | - foreach ($reportFiles as $file => $data) { |
|
| 185 | - $padding = ($width - 18 - $data['strlen']); |
|
| 186 | - if ($padding < 0) { |
|
| 187 | - $file = '...'.substr($file, (($padding * -1) + 3)); |
|
| 188 | - $padding = 0; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - echo $file.str_repeat(' ', $padding).' '; |
|
| 192 | - |
|
| 193 | - if ($data['fixable'] > 0) { |
|
| 194 | - echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL; |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $remaining = ($data['errors'] + $data['warnings']); |
|
| 199 | - |
|
| 200 | - if ($data['fixed'] !== 0) { |
|
| 201 | - echo $data['fixed']; |
|
| 202 | - echo str_repeat(' ', (7 - strlen((string) $data['fixed']))); |
|
| 203 | - } else { |
|
| 204 | - echo '0 '; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if ($remaining !== 0) { |
|
| 208 | - echo $remaining; |
|
| 209 | - } else { |
|
| 210 | - echo '0'; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - echo PHP_EOL; |
|
| 214 | - }//end foreach |
|
| 215 | - |
|
| 216 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 217 | - echo "\033[1mA TOTAL OF $totalFixed ERROR"; |
|
| 218 | - if ($totalFixed !== 1) { |
|
| 219 | - echo 'S'; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $numFiles = count($reportFiles); |
|
| 223 | - echo ' WERE FIXED IN '.$numFiles.' FILE'; |
|
| 224 | - if ($numFiles !== 1) { |
|
| 225 | - echo 'S'; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - echo "\033[0m"; |
|
| 229 | - |
|
| 230 | - if ($failures > 0) { |
|
| 231 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
| 232 | - echo "\033[1mPHPCBF FAILED TO FIX $failures FILE"; |
|
| 233 | - if ($failures !== 1) { |
|
| 234 | - echo 'S'; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - echo "\033[0m"; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
| 241 | - |
|
| 242 | - if ($toScreen === true && $interactive === false) { |
|
| 243 | - Util\Timing::printRunTime(); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - }//end generate() |
|
| 24 | + /** |
|
| 25 | + * Generate a partial report for a single processed file. |
|
| 26 | + * |
|
| 27 | + * Function should return TRUE if it printed or stored data about the file |
|
| 28 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 29 | + * its data should be counted in the grand totals. |
|
| 30 | + * |
|
| 31 | + * @param array $report Prepared report data. |
|
| 32 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 33 | + * @param bool $showSources Show sources? |
|
| 34 | + * @param int $width Maximum allowed line width. |
|
| 35 | + * |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 39 | + { |
|
| 40 | + $errors = $phpcsFile->getFixableCount(); |
|
| 41 | + if ($errors !== 0) { |
|
| 42 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 43 | + ob_end_clean(); |
|
| 44 | + $startTime = microtime(true); |
|
| 45 | + echo "\t=> Fixing file: $errors/$errors violations remaining"; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $fixed = $phpcsFile->fixer->fixFile(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if ($phpcsFile->config->stdin === true) { |
|
| 52 | + // Replacing STDIN, so output current file to STDOUT |
|
| 53 | + // even if nothing was fixed. Exit here because we |
|
| 54 | + // can't process any more than 1 file in this setup. |
|
| 55 | + $fixedContent = $phpcsFile->fixer->getContents(); |
|
| 56 | + throw new DeepExitException($fixedContent, 1); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + if ($errors === 0) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 64 | + if ($fixed === false) { |
|
| 65 | + echo 'ERROR'; |
|
| 66 | + } else { |
|
| 67 | + echo 'DONE'; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 71 | + if ($timeTaken < 1000) { |
|
| 72 | + $timeTaken = round($timeTaken); |
|
| 73 | + echo " in {$timeTaken}ms".PHP_EOL; |
|
| 74 | + } else { |
|
| 75 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
| 76 | + echo " in $timeTaken secs".PHP_EOL; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if ($fixed === true) { |
|
| 81 | + // The filename in the report may be truncated due to a basepath setting |
|
| 82 | + // but we are using it for writing here and not display, |
|
| 83 | + // so find the correct path if basepath is in use. |
|
| 84 | + $newFilename = $report['filename'].$phpcsFile->config->suffix; |
|
| 85 | + if ($phpcsFile->config->basepath !== null) { |
|
| 86 | + $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $newContent = $phpcsFile->fixer->getContents(); |
|
| 90 | + file_put_contents($newFilename, $newContent); |
|
| 91 | + |
|
| 92 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 93 | + if ($newFilename === $report['filename']) { |
|
| 94 | + echo "\t=> File was overwritten".PHP_EOL; |
|
| 95 | + } else { |
|
| 96 | + echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 102 | + ob_start(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $errorCount = $phpcsFile->getErrorCount(); |
|
| 106 | + $warningCount = $phpcsFile->getWarningCount(); |
|
| 107 | + $fixableCount = $phpcsFile->getFixableCount(); |
|
| 108 | + $fixedCount = ($errors - $fixableCount); |
|
| 109 | + echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL; |
|
| 110 | + |
|
| 111 | + return $fixed; |
|
| 112 | + |
|
| 113 | + }//end generateFileReport() |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Prints a summary of fixed files. |
|
| 118 | + * |
|
| 119 | + * @param string $cachedData Any partial report data that was returned from |
|
| 120 | + * generateFileReport during the run. |
|
| 121 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 122 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 123 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 124 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 125 | + * @param bool $showSources Show sources? |
|
| 126 | + * @param int $width Maximum allowed line width. |
|
| 127 | + * @param bool $interactive Are we running in interactive mode? |
|
| 128 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 129 | + * |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public function generate( |
|
| 133 | + $cachedData, |
|
| 134 | + $totalFiles, |
|
| 135 | + $totalErrors, |
|
| 136 | + $totalWarnings, |
|
| 137 | + $totalFixable, |
|
| 138 | + $showSources=false, |
|
| 139 | + $width=80, |
|
| 140 | + $interactive=false, |
|
| 141 | + $toScreen=true |
|
| 142 | + ) { |
|
| 143 | + $lines = explode(PHP_EOL, $cachedData); |
|
| 144 | + array_pop($lines); |
|
| 145 | + |
|
| 146 | + if (empty($lines) === true) { |
|
| 147 | + echo PHP_EOL.'No fixable errors were found'.PHP_EOL; |
|
| 148 | + return; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $reportFiles = []; |
|
| 152 | + $maxLength = 0; |
|
| 153 | + $totalFixed = 0; |
|
| 154 | + $failures = 0; |
|
| 155 | + |
|
| 156 | + foreach ($lines as $line) { |
|
| 157 | + $parts = explode('>>', $line); |
|
| 158 | + $fileLen = strlen($parts[0]); |
|
| 159 | + $reportFiles[$parts[0]] = [ |
|
| 160 | + 'errors' => $parts[1], |
|
| 161 | + 'warnings' => $parts[2], |
|
| 162 | + 'fixable' => $parts[3], |
|
| 163 | + 'fixed' => $parts[4], |
|
| 164 | + 'strlen' => $fileLen, |
|
| 165 | + ]; |
|
| 166 | + |
|
| 167 | + $maxLength = max($maxLength, $fileLen); |
|
| 168 | + |
|
| 169 | + $totalFixed += $parts[4]; |
|
| 170 | + |
|
| 171 | + if ($parts[3] > 0) { |
|
| 172 | + $failures++; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $width = min($width, ($maxLength + 21)); |
|
| 177 | + $width = max($width, 70); |
|
| 178 | + |
|
| 179 | + echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL; |
|
| 180 | + echo str_repeat('-', $width).PHP_EOL; |
|
| 181 | + echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED REMAINING'."\033[0m".PHP_EOL; |
|
| 182 | + echo str_repeat('-', $width).PHP_EOL; |
|
| 183 | + |
|
| 184 | + foreach ($reportFiles as $file => $data) { |
|
| 185 | + $padding = ($width - 18 - $data['strlen']); |
|
| 186 | + if ($padding < 0) { |
|
| 187 | + $file = '...'.substr($file, (($padding * -1) + 3)); |
|
| 188 | + $padding = 0; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + echo $file.str_repeat(' ', $padding).' '; |
|
| 192 | + |
|
| 193 | + if ($data['fixable'] > 0) { |
|
| 194 | + echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL; |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $remaining = ($data['errors'] + $data['warnings']); |
|
| 199 | + |
|
| 200 | + if ($data['fixed'] !== 0) { |
|
| 201 | + echo $data['fixed']; |
|
| 202 | + echo str_repeat(' ', (7 - strlen((string) $data['fixed']))); |
|
| 203 | + } else { |
|
| 204 | + echo '0 '; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if ($remaining !== 0) { |
|
| 208 | + echo $remaining; |
|
| 209 | + } else { |
|
| 210 | + echo '0'; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + echo PHP_EOL; |
|
| 214 | + }//end foreach |
|
| 215 | + |
|
| 216 | + echo str_repeat('-', $width).PHP_EOL; |
|
| 217 | + echo "\033[1mA TOTAL OF $totalFixed ERROR"; |
|
| 218 | + if ($totalFixed !== 1) { |
|
| 219 | + echo 'S'; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $numFiles = count($reportFiles); |
|
| 223 | + echo ' WERE FIXED IN '.$numFiles.' FILE'; |
|
| 224 | + if ($numFiles !== 1) { |
|
| 225 | + echo 'S'; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + echo "\033[0m"; |
|
| 229 | + |
|
| 230 | + if ($failures > 0) { |
|
| 231 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
| 232 | + echo "\033[1mPHPCBF FAILED TO FIX $failures FILE"; |
|
| 233 | + if ($failures !== 1) { |
|
| 234 | + echo 'S'; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + echo "\033[0m"; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
| 241 | + |
|
| 242 | + if ($toScreen === true && $interactive === false) { |
|
| 243 | + Util\Timing::printRunTime(); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + }//end generate() |
|
| 247 | 247 | |
| 248 | 248 | |
| 249 | 249 | }//end class |
@@ -35,78 +35,78 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @return bool |
| 37 | 37 | */ |
| 38 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 38 | + public function generateFileReport( $report, File $phpcsFile, $showSources = false, $width = 80 ) |
|
| 39 | 39 | { |
| 40 | 40 | $errors = $phpcsFile->getFixableCount(); |
| 41 | - if ($errors !== 0) { |
|
| 42 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 41 | + if ( $errors !== 0 ) { |
|
| 42 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 43 | 43 | ob_end_clean(); |
| 44 | - $startTime = microtime(true); |
|
| 44 | + $startTime = microtime( true ); |
|
| 45 | 45 | echo "\t=> Fixing file: $errors/$errors violations remaining"; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $fixed = $phpcsFile->fixer->fixFile(); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if ($phpcsFile->config->stdin === true) { |
|
| 51 | + if ( $phpcsFile->config->stdin === true ) { |
|
| 52 | 52 | // Replacing STDIN, so output current file to STDOUT |
| 53 | 53 | // even if nothing was fixed. Exit here because we |
| 54 | 54 | // can't process any more than 1 file in this setup. |
| 55 | 55 | $fixedContent = $phpcsFile->fixer->getContents(); |
| 56 | - throw new DeepExitException($fixedContent, 1); |
|
| 56 | + throw new DeepExitException( $fixedContent, 1 ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - if ($errors === 0) { |
|
| 59 | + if ( $errors === 0 ) { |
|
| 60 | 60 | return false; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 64 | - if ($fixed === false) { |
|
| 63 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 64 | + if ( $fixed === false ) { |
|
| 65 | 65 | echo 'ERROR'; |
| 66 | 66 | } else { |
| 67 | 67 | echo 'DONE'; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 71 | - if ($timeTaken < 1000) { |
|
| 72 | - $timeTaken = round($timeTaken); |
|
| 73 | - echo " in {$timeTaken}ms".PHP_EOL; |
|
| 70 | + $timeTaken = ( ( microtime( true ) - $startTime ) * 1000 ); |
|
| 71 | + if ( $timeTaken < 1000 ) { |
|
| 72 | + $timeTaken = round( $timeTaken ); |
|
| 73 | + echo " in {$timeTaken}ms" . PHP_EOL; |
|
| 74 | 74 | } else { |
| 75 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 76 | - echo " in $timeTaken secs".PHP_EOL; |
|
| 75 | + $timeTaken = round( ( $timeTaken / 1000 ), 2 ); |
|
| 76 | + echo " in $timeTaken secs" . PHP_EOL; |
|
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ($fixed === true) { |
|
| 80 | + if ( $fixed === true ) { |
|
| 81 | 81 | // The filename in the report may be truncated due to a basepath setting |
| 82 | 82 | // but we are using it for writing here and not display, |
| 83 | 83 | // so find the correct path if basepath is in use. |
| 84 | - $newFilename = $report['filename'].$phpcsFile->config->suffix; |
|
| 85 | - if ($phpcsFile->config->basepath !== null) { |
|
| 86 | - $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename; |
|
| 84 | + $newFilename = $report[ 'filename' ] . $phpcsFile->config->suffix; |
|
| 85 | + if ( $phpcsFile->config->basepath !== null ) { |
|
| 86 | + $newFilename = $phpcsFile->config->basepath . DIRECTORY_SEPARATOR . $newFilename; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | $newContent = $phpcsFile->fixer->getContents(); |
| 90 | - file_put_contents($newFilename, $newContent); |
|
| 90 | + file_put_contents( $newFilename, $newContent ); |
|
| 91 | 91 | |
| 92 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 93 | - if ($newFilename === $report['filename']) { |
|
| 94 | - echo "\t=> File was overwritten".PHP_EOL; |
|
| 92 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 93 | + if ( $newFilename === $report[ 'filename' ] ) { |
|
| 94 | + echo "\t=> File was overwritten" . PHP_EOL; |
|
| 95 | 95 | } else { |
| 96 | - echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
| 96 | + echo "\t=> Fixed file written to " . basename( $newFilename ) . PHP_EOL; |
|
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 101 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 102 | 102 | ob_start(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | $errorCount = $phpcsFile->getErrorCount(); |
| 106 | 106 | $warningCount = $phpcsFile->getWarningCount(); |
| 107 | 107 | $fixableCount = $phpcsFile->getFixableCount(); |
| 108 | - $fixedCount = ($errors - $fixableCount); |
|
| 109 | - echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL; |
|
| 108 | + $fixedCount = ( $errors - $fixableCount ); |
|
| 109 | + echo $report[ 'filename' ] . ">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount" . PHP_EOL; |
|
| 110 | 110 | |
| 111 | 111 | return $fixed; |
| 112 | 112 | |
@@ -135,76 +135,76 @@ discard block |
||
| 135 | 135 | $totalErrors, |
| 136 | 136 | $totalWarnings, |
| 137 | 137 | $totalFixable, |
| 138 | - $showSources=false, |
|
| 139 | - $width=80, |
|
| 140 | - $interactive=false, |
|
| 141 | - $toScreen=true |
|
| 138 | + $showSources = false, |
|
| 139 | + $width = 80, |
|
| 140 | + $interactive = false, |
|
| 141 | + $toScreen = true |
|
| 142 | 142 | ) { |
| 143 | - $lines = explode(PHP_EOL, $cachedData); |
|
| 144 | - array_pop($lines); |
|
| 143 | + $lines = explode( PHP_EOL, $cachedData ); |
|
| 144 | + array_pop( $lines ); |
|
| 145 | 145 | |
| 146 | - if (empty($lines) === true) { |
|
| 147 | - echo PHP_EOL.'No fixable errors were found'.PHP_EOL; |
|
| 146 | + if ( empty( $lines ) === true ) { |
|
| 147 | + echo PHP_EOL . 'No fixable errors were found' . PHP_EOL; |
|
| 148 | 148 | return; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $reportFiles = []; |
|
| 151 | + $reportFiles = [ ]; |
|
| 152 | 152 | $maxLength = 0; |
| 153 | 153 | $totalFixed = 0; |
| 154 | 154 | $failures = 0; |
| 155 | 155 | |
| 156 | - foreach ($lines as $line) { |
|
| 157 | - $parts = explode('>>', $line); |
|
| 158 | - $fileLen = strlen($parts[0]); |
|
| 159 | - $reportFiles[$parts[0]] = [ |
|
| 160 | - 'errors' => $parts[1], |
|
| 161 | - 'warnings' => $parts[2], |
|
| 162 | - 'fixable' => $parts[3], |
|
| 163 | - 'fixed' => $parts[4], |
|
| 156 | + foreach ( $lines as $line ) { |
|
| 157 | + $parts = explode( '>>', $line ); |
|
| 158 | + $fileLen = strlen( $parts[ 0 ] ); |
|
| 159 | + $reportFiles[ $parts[ 0 ] ] = [ |
|
| 160 | + 'errors' => $parts[ 1 ], |
|
| 161 | + 'warnings' => $parts[ 2 ], |
|
| 162 | + 'fixable' => $parts[ 3 ], |
|
| 163 | + 'fixed' => $parts[ 4 ], |
|
| 164 | 164 | 'strlen' => $fileLen, |
| 165 | 165 | ]; |
| 166 | 166 | |
| 167 | - $maxLength = max($maxLength, $fileLen); |
|
| 167 | + $maxLength = max( $maxLength, $fileLen ); |
|
| 168 | 168 | |
| 169 | - $totalFixed += $parts[4]; |
|
| 169 | + $totalFixed += $parts[ 4 ]; |
|
| 170 | 170 | |
| 171 | - if ($parts[3] > 0) { |
|
| 171 | + if ( $parts[ 3 ] > 0 ) { |
|
| 172 | 172 | $failures++; |
| 173 | 173 | } |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - $width = min($width, ($maxLength + 21)); |
|
| 177 | - $width = max($width, 70); |
|
| 176 | + $width = min( $width, ( $maxLength + 21 ) ); |
|
| 177 | + $width = max( $width, 70 ); |
|
| 178 | 178 | |
| 179 | - echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL; |
|
| 180 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 181 | - echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED REMAINING'."\033[0m".PHP_EOL; |
|
| 182 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 179 | + echo PHP_EOL . "\033[1m" . 'PHPCBF RESULT SUMMARY' . "\033[0m" . PHP_EOL; |
|
| 180 | + echo str_repeat( '-', $width ) . PHP_EOL; |
|
| 181 | + echo "\033[1m" . 'FILE' . str_repeat( ' ', ( $width - 20 ) ) . 'FIXED REMAINING' . "\033[0m" . PHP_EOL; |
|
| 182 | + echo str_repeat( '-', $width ) . PHP_EOL; |
|
| 183 | 183 | |
| 184 | - foreach ($reportFiles as $file => $data) { |
|
| 185 | - $padding = ($width - 18 - $data['strlen']); |
|
| 186 | - if ($padding < 0) { |
|
| 187 | - $file = '...'.substr($file, (($padding * -1) + 3)); |
|
| 184 | + foreach ( $reportFiles as $file => $data ) { |
|
| 185 | + $padding = ( $width - 18 - $data[ 'strlen' ] ); |
|
| 186 | + if ( $padding < 0 ) { |
|
| 187 | + $file = '...' . substr( $file, ( ( $padding * -1 ) + 3 ) ); |
|
| 188 | 188 | $padding = 0; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - echo $file.str_repeat(' ', $padding).' '; |
|
| 191 | + echo $file . str_repeat( ' ', $padding ) . ' '; |
|
| 192 | 192 | |
| 193 | - if ($data['fixable'] > 0) { |
|
| 194 | - echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL; |
|
| 193 | + if ( $data[ 'fixable' ] > 0 ) { |
|
| 194 | + echo "\033[31mFAILED TO FIX\033[0m" . PHP_EOL; |
|
| 195 | 195 | continue; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - $remaining = ($data['errors'] + $data['warnings']); |
|
| 198 | + $remaining = ( $data[ 'errors' ] + $data[ 'warnings' ] ); |
|
| 199 | 199 | |
| 200 | - if ($data['fixed'] !== 0) { |
|
| 201 | - echo $data['fixed']; |
|
| 202 | - echo str_repeat(' ', (7 - strlen((string) $data['fixed']))); |
|
| 200 | + if ( $data[ 'fixed' ] !== 0 ) { |
|
| 201 | + echo $data[ 'fixed' ]; |
|
| 202 | + echo str_repeat( ' ', ( 7 - strlen( (string)$data[ 'fixed' ] ) ) ); |
|
| 203 | 203 | } else { |
| 204 | 204 | echo '0 '; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - if ($remaining !== 0) { |
|
| 207 | + if ( $remaining !== 0 ) { |
|
| 208 | 208 | echo $remaining; |
| 209 | 209 | } else { |
| 210 | 210 | echo '0'; |
@@ -213,33 +213,33 @@ discard block |
||
| 213 | 213 | echo PHP_EOL; |
| 214 | 214 | }//end foreach |
| 215 | 215 | |
| 216 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 216 | + echo str_repeat( '-', $width ) . PHP_EOL; |
|
| 217 | 217 | echo "\033[1mA TOTAL OF $totalFixed ERROR"; |
| 218 | - if ($totalFixed !== 1) { |
|
| 218 | + if ( $totalFixed !== 1 ) { |
|
| 219 | 219 | echo 'S'; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - $numFiles = count($reportFiles); |
|
| 223 | - echo ' WERE FIXED IN '.$numFiles.' FILE'; |
|
| 224 | - if ($numFiles !== 1) { |
|
| 222 | + $numFiles = count( $reportFiles ); |
|
| 223 | + echo ' WERE FIXED IN ' . $numFiles . ' FILE'; |
|
| 224 | + if ( $numFiles !== 1 ) { |
|
| 225 | 225 | echo 'S'; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | echo "\033[0m"; |
| 229 | 229 | |
| 230 | - if ($failures > 0) { |
|
| 231 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
| 230 | + if ( $failures > 0 ) { |
|
| 231 | + echo PHP_EOL . str_repeat( '-', $width ) . PHP_EOL; |
|
| 232 | 232 | echo "\033[1mPHPCBF FAILED TO FIX $failures FILE"; |
| 233 | - if ($failures !== 1) { |
|
| 233 | + if ( $failures !== 1 ) { |
|
| 234 | 234 | echo 'S'; |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | echo "\033[0m"; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
| 240 | + echo PHP_EOL . str_repeat( '-', $width ) . PHP_EOL . PHP_EOL; |
|
| 241 | 241 | |
| 242 | - if ($toScreen === true && $interactive === false) { |
|
| 242 | + if ( $toScreen === true && $interactive === false ) { |
|
| 243 | 243 | Util\Timing::printRunTime(); |
| 244 | 244 | } |
| 245 | 245 | |
@@ -17,8 +17,7 @@ discard block |
||
| 17 | 17 | use PHP_CodeSniffer\Files\File; |
| 18 | 18 | use PHP_CodeSniffer\Util; |
| 19 | 19 | |
| 20 | -class Cbf implements Report |
|
| 21 | -{ |
|
| 20 | +class Cbf implements Report { |
|
| 22 | 21 | |
| 23 | 22 | |
| 24 | 23 | /** |
@@ -35,8 +34,7 @@ discard block |
||
| 35 | 34 | * |
| 36 | 35 | * @return bool |
| 37 | 36 | */ |
| 38 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 39 | - { |
|
| 37 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) { |
|
| 40 | 38 | $errors = $phpcsFile->getFixableCount(); |
| 41 | 39 | if ($errors !== 0) { |
| 42 | 40 | if (PHP_CODESNIFFER_VERBOSITY > 0) { |
@@ -593,7 +593,7 @@ discard block |
||
| 593 | 593 | * @param string $file The path of the file that raised the error. |
| 594 | 594 | * @param int $line The line number the error was raised at. |
| 595 | 595 | * |
| 596 | - * @return void |
|
| 596 | + * @return boolean |
|
| 597 | 597 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
| 598 | 598 | */ |
| 599 | 599 | public function handleErrors($code, $message, $file, $line) |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | * |
| 709 | 709 | * @param array $childProcs An array of child processes to wait for. |
| 710 | 710 | * |
| 711 | - * @return void |
|
| 711 | + * @return boolean |
|
| 712 | 712 | */ |
| 713 | 713 | private function processChildProcs($childProcs) |
| 714 | 714 | { |
@@ -24,863 +24,863 @@ |
||
| 24 | 24 | class Runner |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The config data for the run. |
|
| 29 | - * |
|
| 30 | - * @var \PHP_CodeSniffer\Config |
|
| 31 | - */ |
|
| 32 | - public $config = null; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The ruleset used for the run. |
|
| 36 | - * |
|
| 37 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | - */ |
|
| 39 | - public $ruleset = null; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The reporter used for generating reports after the run. |
|
| 43 | - * |
|
| 44 | - * @var \PHP_CodeSniffer\Reporter |
|
| 45 | - */ |
|
| 46 | - public $reporter = null; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Run the PHPCS script. |
|
| 51 | - * |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function runPHPCS() |
|
| 55 | - { |
|
| 56 | - try { |
|
| 57 | - Util\Timing::startTiming(); |
|
| 58 | - Runner::checkRequirements(); |
|
| 59 | - |
|
| 60 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Creating the Config object populates it with all required settings |
|
| 65 | - // based on the CLI arguments provided to the script and any config |
|
| 66 | - // values the user has set. |
|
| 67 | - $this->config = new Config(); |
|
| 68 | - |
|
| 69 | - // Init the run and load the rulesets to set additional config vars. |
|
| 70 | - $this->init(); |
|
| 71 | - |
|
| 72 | - // Print a list of sniffs in each of the supplied standards. |
|
| 73 | - // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | - if ($this->config->explain === true) { |
|
| 75 | - $standards = $this->config->standards; |
|
| 76 | - foreach ($standards as $standard) { |
|
| 77 | - $this->config->standards = [$standard]; |
|
| 78 | - $ruleset = new Ruleset($this->config); |
|
| 79 | - $ruleset->explain(); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return 0; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Generate documentation for each of the supplied standards. |
|
| 86 | - if ($this->config->generator !== null) { |
|
| 87 | - $standards = $this->config->standards; |
|
| 88 | - foreach ($standards as $standard) { |
|
| 89 | - $this->config->standards = [$standard]; |
|
| 90 | - $ruleset = new Ruleset($this->config); |
|
| 91 | - $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | - $generator = new $class($ruleset); |
|
| 93 | - $generator->generate(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Other report formats don't really make sense in interactive mode |
|
| 100 | - // so we hard-code the full report here and when outputting. |
|
| 101 | - // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | - if ($this->config->interactive === true) { |
|
| 103 | - $this->config->reports = ['full' => null]; |
|
| 104 | - $this->config->parallel = 1; |
|
| 105 | - $this->config->showProgress = false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | - // sure where the file came from or if it will change in the future. |
|
| 110 | - if ($this->config->stdin === true) { |
|
| 111 | - $this->config->cache = false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $numErrors = $this->run(); |
|
| 115 | - |
|
| 116 | - // Print all the reports for this run. |
|
| 117 | - $toScreen = $this->reporter->printReports(); |
|
| 118 | - |
|
| 119 | - // Only print timer output if no reports were |
|
| 120 | - // printed to the screen so we don't put additional output |
|
| 121 | - // in something like an XML report. If we are printing to screen, |
|
| 122 | - // the report types would have already worked out who should |
|
| 123 | - // print the timer info. |
|
| 124 | - if ($this->config->interactive === false |
|
| 125 | - && ($toScreen === false |
|
| 126 | - || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | - ) { |
|
| 128 | - Util\Timing::printRunTime(); |
|
| 129 | - } |
|
| 130 | - } catch (DeepExitException $e) { |
|
| 131 | - echo $e->getMessage(); |
|
| 132 | - return $e->getCode(); |
|
| 133 | - }//end try |
|
| 134 | - |
|
| 135 | - if ($numErrors === 0) { |
|
| 136 | - // No errors found. |
|
| 137 | - return 0; |
|
| 138 | - } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | - // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | - return 1; |
|
| 141 | - } else { |
|
| 142 | - // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | - return 2; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - }//end runPHPCS() |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Run the PHPCBF script. |
|
| 151 | - * |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 154 | - public function runPHPCBF() |
|
| 155 | - { |
|
| 156 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | - define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - try { |
|
| 161 | - Util\Timing::startTiming(); |
|
| 162 | - Runner::checkRequirements(); |
|
| 163 | - |
|
| 164 | - // Creating the Config object populates it with all required settings |
|
| 165 | - // based on the CLI arguments provided to the script and any config |
|
| 166 | - // values the user has set. |
|
| 167 | - $this->config = new Config(); |
|
| 168 | - |
|
| 169 | - // When processing STDIN, we can't output anything to the screen |
|
| 170 | - // or it will end up mixed in with the file output. |
|
| 171 | - if ($this->config->stdin === true) { |
|
| 172 | - $this->config->verbosity = 0; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - // Init the run and load the rulesets to set additional config vars. |
|
| 176 | - $this->init(); |
|
| 177 | - |
|
| 178 | - // When processing STDIN, we only process one file at a time and |
|
| 179 | - // we don't process all the way through, so we can't use the parallel |
|
| 180 | - // running system. |
|
| 181 | - if ($this->config->stdin === true) { |
|
| 182 | - $this->config->parallel = 1; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Override some of the command line settings that might break the fixes. |
|
| 186 | - $this->config->generator = null; |
|
| 187 | - $this->config->explain = false; |
|
| 188 | - $this->config->interactive = false; |
|
| 189 | - $this->config->cache = false; |
|
| 190 | - $this->config->showSources = false; |
|
| 191 | - $this->config->recordErrors = false; |
|
| 192 | - $this->config->reportFile = null; |
|
| 193 | - $this->config->reports = ['cbf' => null]; |
|
| 194 | - |
|
| 195 | - // If a standard tries to set command line arguments itself, some |
|
| 196 | - // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | - // dying if any are found. |
|
| 198 | - $this->config->dieOnUnknownArg = false; |
|
| 199 | - |
|
| 200 | - $this->run(); |
|
| 201 | - $this->reporter->printReports(); |
|
| 202 | - |
|
| 203 | - echo PHP_EOL; |
|
| 204 | - Util\Timing::printRunTime(); |
|
| 205 | - } catch (DeepExitException $e) { |
|
| 206 | - echo $e->getMessage(); |
|
| 207 | - return $e->getCode(); |
|
| 208 | - }//end try |
|
| 209 | - |
|
| 210 | - if ($this->reporter->totalFixed === 0) { |
|
| 211 | - // Nothing was fixed by PHPCBF. |
|
| 212 | - if ($this->reporter->totalFixable === 0) { |
|
| 213 | - // Nothing found that could be fixed. |
|
| 214 | - return 0; |
|
| 215 | - } else { |
|
| 216 | - // Something failed to fix. |
|
| 217 | - return 2; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if ($this->reporter->totalFixable === 0) { |
|
| 222 | - // PHPCBF fixed all fixable errors. |
|
| 223 | - return 1; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | - return 2; |
|
| 228 | - |
|
| 229 | - }//end runPHPCBF() |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | - * |
|
| 235 | - * @return array |
|
| 236 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 237 | - */ |
|
| 238 | - public function checkRequirements() |
|
| 239 | - { |
|
| 240 | - // Check the PHP version. |
|
| 241 | - if (PHP_VERSION_ID < 50400) { |
|
| 242 | - $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | - throw new DeepExitException($error, 3); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $requiredExtensions = [ |
|
| 247 | - 'tokenizer', |
|
| 248 | - 'xmlwriter', |
|
| 249 | - 'SimpleXML', |
|
| 250 | - ]; |
|
| 251 | - $missingExtensions = []; |
|
| 252 | - |
|
| 253 | - foreach ($requiredExtensions as $extension) { |
|
| 254 | - if (extension_loaded($extension) === false) { |
|
| 255 | - $missingExtensions[] = $extension; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (empty($missingExtensions) === false) { |
|
| 260 | - $last = array_pop($requiredExtensions); |
|
| 261 | - $required = implode(', ', $requiredExtensions); |
|
| 262 | - $required .= ' and '.$last; |
|
| 263 | - |
|
| 264 | - if (count($missingExtensions) === 1) { |
|
| 265 | - $missing = $missingExtensions[0]; |
|
| 266 | - } else { |
|
| 267 | - $last = array_pop($missingExtensions); |
|
| 268 | - $missing = implode(', ', $missingExtensions); |
|
| 269 | - $missing .= ' and '.$last; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL; |
|
| 273 | - $error = sprintf($error, $required, $missing); |
|
| 274 | - throw new DeepExitException($error, 3); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - }//end checkRequirements() |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Init the rulesets and other high-level settings. |
|
| 282 | - * |
|
| 283 | - * @return void |
|
| 284 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 285 | - */ |
|
| 286 | - public function init() |
|
| 287 | - { |
|
| 288 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // Ensure this option is enabled or else line endings will not always |
|
| 293 | - // be detected properly for files created on a Mac with the /r line ending. |
|
| 294 | - ini_set('auto_detect_line_endings', true); |
|
| 295 | - |
|
| 296 | - // Disable the PCRE JIT as this caused issues with parallel running. |
|
| 297 | - ini_set('pcre.jit', false); |
|
| 298 | - |
|
| 299 | - // Check that the standards are valid. |
|
| 300 | - foreach ($this->config->standards as $standard) { |
|
| 301 | - if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | - // They didn't select a valid coding standard, so help them |
|
| 303 | - // out by letting them know which standards are installed. |
|
| 304 | - $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | - ob_start(); |
|
| 306 | - Util\Standards::printInstalledStandards(); |
|
| 307 | - $error .= ob_get_contents(); |
|
| 308 | - ob_end_clean(); |
|
| 309 | - throw new DeepExitException($error, 3); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Saves passing the Config object into other objects that only need |
|
| 314 | - // the verbosity flag for debug output. |
|
| 315 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | - define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | - // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | - $tokens = new Util\Tokens(); |
|
| 322 | - |
|
| 323 | - // Allow autoloading of custom files inside installed standards. |
|
| 324 | - $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | - foreach ($installedStandards as $name => $details) { |
|
| 326 | - Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // The ruleset contains all the information about how the files |
|
| 330 | - // should be checked and/or fixed. |
|
| 331 | - try { |
|
| 332 | - $this->ruleset = new Ruleset($this->config); |
|
| 333 | - } catch (RuntimeException $e) { |
|
| 334 | - $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | - $error .= $this->config->printShortUsage(true); |
|
| 336 | - throw new DeepExitException($error, 3); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - }//end init() |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Performs the run. |
|
| 344 | - * |
|
| 345 | - * @return int The number of errors and warnings found. |
|
| 346 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | - */ |
|
| 349 | - private function run() |
|
| 350 | - { |
|
| 351 | - // The class that manages all reporters for the run. |
|
| 352 | - $this->reporter = new Reporter($this->config); |
|
| 353 | - |
|
| 354 | - // Include bootstrap files. |
|
| 355 | - foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | - include $bootstrap; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if ($this->config->stdin === true) { |
|
| 360 | - $fileContents = $this->config->stdinContent; |
|
| 361 | - if ($fileContents === null) { |
|
| 362 | - $handle = fopen('php://stdin', 'r'); |
|
| 363 | - stream_set_blocking($handle, true); |
|
| 364 | - $fileContents = stream_get_contents($handle); |
|
| 365 | - fclose($handle); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | - $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | - $todo->addFile($dummy->path, $dummy); |
|
| 371 | - } else { |
|
| 372 | - if (empty($this->config->files) === true) { |
|
| 373 | - $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | - $error .= $this->config->printShortUsage(true); |
|
| 375 | - throw new DeepExitException($error, 3); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | - echo 'Creating file list... '; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | - |
|
| 384 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | - $numFiles = count($todo); |
|
| 386 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ($this->config->cache === true) { |
|
| 390 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | - echo 'Loading cache... '; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - Cache::load($this->ruleset, $this->config); |
|
| 395 | - |
|
| 396 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | - $size = Cache::getSize(); |
|
| 398 | - echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - }//end if |
|
| 402 | - |
|
| 403 | - // Turn all sniff errors into exceptions. |
|
| 404 | - set_error_handler([$this, 'handleErrors']); |
|
| 405 | - |
|
| 406 | - // If verbosity is too high, turn off parallelism so the |
|
| 407 | - // debug output is clean. |
|
| 408 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | - $this->config->parallel = 1; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | - if (function_exists('pcntl_fork') === false) { |
|
| 414 | - $this->config->parallel = 1; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $lastDir = ''; |
|
| 418 | - $numFiles = count($todo); |
|
| 419 | - |
|
| 420 | - if ($this->config->parallel === 1) { |
|
| 421 | - // Running normally. |
|
| 422 | - $numProcessed = 0; |
|
| 423 | - foreach ($todo as $path => $file) { |
|
| 424 | - if ($file->ignored === false) { |
|
| 425 | - $currDir = dirname($path); |
|
| 426 | - if ($lastDir !== $currDir) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $lastDir = $currDir; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $this->processFile($file); |
|
| 435 | - } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | - echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - $numProcessed++; |
|
| 440 | - $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | - } |
|
| 442 | - } else { |
|
| 443 | - // Batching and forking. |
|
| 444 | - $childProcs = []; |
|
| 445 | - $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | - |
|
| 447 | - for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | - $startAt = ($batch * $numPerBatch); |
|
| 449 | - if ($startAt >= $numFiles) { |
|
| 450 | - break; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - $endAt = ($startAt + $numPerBatch); |
|
| 454 | - if ($endAt > $numFiles) { |
|
| 455 | - $endAt = $numFiles; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | - $pid = pcntl_fork(); |
|
| 460 | - if ($pid === -1) { |
|
| 461 | - throw new RuntimeException('Failed to create child process'); |
|
| 462 | - } else if ($pid !== 0) { |
|
| 463 | - $childProcs[] = [ |
|
| 464 | - 'pid' => $pid, |
|
| 465 | - 'out' => $childOutFilename, |
|
| 466 | - ]; |
|
| 467 | - } else { |
|
| 468 | - // Move forward to the start of the batch. |
|
| 469 | - $todo->rewind(); |
|
| 470 | - for ($i = 0; $i < $startAt; $i++) { |
|
| 471 | - $todo->next(); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - // Reset the reporter to make sure only figures from this |
|
| 475 | - // file batch are recorded. |
|
| 476 | - $this->reporter->totalFiles = 0; |
|
| 477 | - $this->reporter->totalErrors = 0; |
|
| 478 | - $this->reporter->totalWarnings = 0; |
|
| 479 | - $this->reporter->totalFixable = 0; |
|
| 480 | - $this->reporter->totalFixed = 0; |
|
| 481 | - |
|
| 482 | - // Process the files. |
|
| 483 | - $pathsProcessed = []; |
|
| 484 | - ob_start(); |
|
| 485 | - for ($i = $startAt; $i < $endAt; $i++) { |
|
| 486 | - $path = $todo->key(); |
|
| 487 | - $file = $todo->current(); |
|
| 488 | - |
|
| 489 | - if ($file->ignored === true) { |
|
| 490 | - continue; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - $currDir = dirname($path); |
|
| 494 | - if ($lastDir !== $currDir) { |
|
| 495 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 496 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - $lastDir = $currDir; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - $this->processFile($file); |
|
| 503 | - |
|
| 504 | - $pathsProcessed[] = $path; |
|
| 505 | - $todo->next(); |
|
| 506 | - }//end for |
|
| 507 | - |
|
| 508 | - $debugOutput = ob_get_contents(); |
|
| 509 | - ob_end_clean(); |
|
| 510 | - |
|
| 511 | - // Write information about the run to the filesystem |
|
| 512 | - // so it can be picked up by the main process. |
|
| 513 | - $childOutput = [ |
|
| 514 | - 'totalFiles' => $this->reporter->totalFiles, |
|
| 515 | - 'totalErrors' => $this->reporter->totalErrors, |
|
| 516 | - 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 517 | - 'totalFixable' => $this->reporter->totalFixable, |
|
| 518 | - 'totalFixed' => $this->reporter->totalFixed, |
|
| 519 | - ]; |
|
| 520 | - |
|
| 521 | - $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 522 | - $output .= var_export($childOutput, true); |
|
| 523 | - $output .= ";\n\$debugOutput = "; |
|
| 524 | - $output .= var_export($debugOutput, true); |
|
| 525 | - |
|
| 526 | - if ($this->config->cache === true) { |
|
| 527 | - $childCache = []; |
|
| 528 | - foreach ($pathsProcessed as $path) { |
|
| 529 | - $childCache[$path] = Cache::get($path); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - $output .= ";\n\$childCache = "; |
|
| 533 | - $output .= var_export($childCache, true); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - $output .= ";\n?".'>'; |
|
| 537 | - file_put_contents($childOutFilename, $output); |
|
| 538 | - exit($pid); |
|
| 539 | - }//end if |
|
| 540 | - }//end for |
|
| 541 | - |
|
| 542 | - $success = $this->processChildProcs($childProcs); |
|
| 543 | - if ($success === false) { |
|
| 544 | - throw new RuntimeException('One or more child processes failed to run'); |
|
| 545 | - } |
|
| 546 | - }//end if |
|
| 547 | - |
|
| 548 | - restore_error_handler(); |
|
| 549 | - |
|
| 550 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 551 | - && $this->config->interactive === false |
|
| 552 | - && $this->config->showProgress === true |
|
| 553 | - ) { |
|
| 554 | - echo PHP_EOL.PHP_EOL; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - if ($this->config->cache === true) { |
|
| 558 | - Cache::save(); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 562 | - $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 563 | - |
|
| 564 | - $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 565 | - if ($ignoreErrors !== null) { |
|
| 566 | - $ignoreErrors = (bool) $ignoreErrors; |
|
| 567 | - if ($ignoreErrors === true) { |
|
| 568 | - $return -= $this->reporter->totalErrors; |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - if ($ignoreWarnings !== null) { |
|
| 573 | - $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 574 | - if ($ignoreWarnings === true) { |
|
| 575 | - $return -= $this->reporter->totalWarnings; |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - return $return; |
|
| 580 | - |
|
| 581 | - }//end run() |
|
| 582 | - |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Converts all PHP errors into exceptions. |
|
| 586 | - * |
|
| 587 | - * This method forces a sniff to stop processing if it is not |
|
| 588 | - * able to handle a specific piece of code, instead of continuing |
|
| 589 | - * and potentially getting into a loop. |
|
| 590 | - * |
|
| 591 | - * @param int $code The level of error raised. |
|
| 592 | - * @param string $message The error message. |
|
| 593 | - * @param string $file The path of the file that raised the error. |
|
| 594 | - * @param int $line The line number the error was raised at. |
|
| 595 | - * |
|
| 596 | - * @return void |
|
| 597 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 598 | - */ |
|
| 599 | - public function handleErrors($code, $message, $file, $line) |
|
| 600 | - { |
|
| 601 | - if ((error_reporting() & $code) === 0) { |
|
| 602 | - // This type of error is being muted. |
|
| 603 | - return true; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - throw new RuntimeException("$message in $file on line $line"); |
|
| 607 | - |
|
| 608 | - }//end handleErrors() |
|
| 609 | - |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Processes a single file, including checking and fixing. |
|
| 613 | - * |
|
| 614 | - * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 615 | - * |
|
| 616 | - * @return void |
|
| 617 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 618 | - */ |
|
| 619 | - public function processFile($file) |
|
| 620 | - { |
|
| 621 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 622 | - $startTime = microtime(true); |
|
| 623 | - echo 'Processing '.basename($file->path).' '; |
|
| 624 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 625 | - echo PHP_EOL; |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - try { |
|
| 630 | - $file->process(); |
|
| 631 | - |
|
| 632 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 633 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 634 | - if ($timeTaken < 1000) { |
|
| 635 | - $timeTaken = round($timeTaken); |
|
| 636 | - echo "DONE in {$timeTaken}ms"; |
|
| 637 | - } else { |
|
| 638 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 639 | - echo "DONE in $timeTaken secs"; |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 643 | - $errors = $file->getFixableCount(); |
|
| 644 | - echo " ($errors fixable violations)".PHP_EOL; |
|
| 645 | - } else { |
|
| 646 | - $errors = $file->getErrorCount(); |
|
| 647 | - $warnings = $file->getWarningCount(); |
|
| 648 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 649 | - } |
|
| 650 | - } |
|
| 651 | - } catch (\Exception $e) { |
|
| 652 | - $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 653 | - $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 654 | - }//end try |
|
| 655 | - |
|
| 656 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 657 | - |
|
| 658 | - if ($this->config->interactive === true) { |
|
| 659 | - /* |
|
| 27 | + /** |
|
| 28 | + * The config data for the run. |
|
| 29 | + * |
|
| 30 | + * @var \PHP_CodeSniffer\Config |
|
| 31 | + */ |
|
| 32 | + public $config = null; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The ruleset used for the run. |
|
| 36 | + * |
|
| 37 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | + */ |
|
| 39 | + public $ruleset = null; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The reporter used for generating reports after the run. |
|
| 43 | + * |
|
| 44 | + * @var \PHP_CodeSniffer\Reporter |
|
| 45 | + */ |
|
| 46 | + public $reporter = null; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Run the PHPCS script. |
|
| 51 | + * |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function runPHPCS() |
|
| 55 | + { |
|
| 56 | + try { |
|
| 57 | + Util\Timing::startTiming(); |
|
| 58 | + Runner::checkRequirements(); |
|
| 59 | + |
|
| 60 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Creating the Config object populates it with all required settings |
|
| 65 | + // based on the CLI arguments provided to the script and any config |
|
| 66 | + // values the user has set. |
|
| 67 | + $this->config = new Config(); |
|
| 68 | + |
|
| 69 | + // Init the run and load the rulesets to set additional config vars. |
|
| 70 | + $this->init(); |
|
| 71 | + |
|
| 72 | + // Print a list of sniffs in each of the supplied standards. |
|
| 73 | + // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | + if ($this->config->explain === true) { |
|
| 75 | + $standards = $this->config->standards; |
|
| 76 | + foreach ($standards as $standard) { |
|
| 77 | + $this->config->standards = [$standard]; |
|
| 78 | + $ruleset = new Ruleset($this->config); |
|
| 79 | + $ruleset->explain(); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return 0; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Generate documentation for each of the supplied standards. |
|
| 86 | + if ($this->config->generator !== null) { |
|
| 87 | + $standards = $this->config->standards; |
|
| 88 | + foreach ($standards as $standard) { |
|
| 89 | + $this->config->standards = [$standard]; |
|
| 90 | + $ruleset = new Ruleset($this->config); |
|
| 91 | + $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | + $generator = new $class($ruleset); |
|
| 93 | + $generator->generate(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Other report formats don't really make sense in interactive mode |
|
| 100 | + // so we hard-code the full report here and when outputting. |
|
| 101 | + // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | + if ($this->config->interactive === true) { |
|
| 103 | + $this->config->reports = ['full' => null]; |
|
| 104 | + $this->config->parallel = 1; |
|
| 105 | + $this->config->showProgress = false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | + // sure where the file came from or if it will change in the future. |
|
| 110 | + if ($this->config->stdin === true) { |
|
| 111 | + $this->config->cache = false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $numErrors = $this->run(); |
|
| 115 | + |
|
| 116 | + // Print all the reports for this run. |
|
| 117 | + $toScreen = $this->reporter->printReports(); |
|
| 118 | + |
|
| 119 | + // Only print timer output if no reports were |
|
| 120 | + // printed to the screen so we don't put additional output |
|
| 121 | + // in something like an XML report. If we are printing to screen, |
|
| 122 | + // the report types would have already worked out who should |
|
| 123 | + // print the timer info. |
|
| 124 | + if ($this->config->interactive === false |
|
| 125 | + && ($toScreen === false |
|
| 126 | + || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | + ) { |
|
| 128 | + Util\Timing::printRunTime(); |
|
| 129 | + } |
|
| 130 | + } catch (DeepExitException $e) { |
|
| 131 | + echo $e->getMessage(); |
|
| 132 | + return $e->getCode(); |
|
| 133 | + }//end try |
|
| 134 | + |
|
| 135 | + if ($numErrors === 0) { |
|
| 136 | + // No errors found. |
|
| 137 | + return 0; |
|
| 138 | + } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | + // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | + return 1; |
|
| 141 | + } else { |
|
| 142 | + // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | + return 2; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + }//end runPHPCS() |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Run the PHPCBF script. |
|
| 151 | + * |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | + public function runPHPCBF() |
|
| 155 | + { |
|
| 156 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | + define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + try { |
|
| 161 | + Util\Timing::startTiming(); |
|
| 162 | + Runner::checkRequirements(); |
|
| 163 | + |
|
| 164 | + // Creating the Config object populates it with all required settings |
|
| 165 | + // based on the CLI arguments provided to the script and any config |
|
| 166 | + // values the user has set. |
|
| 167 | + $this->config = new Config(); |
|
| 168 | + |
|
| 169 | + // When processing STDIN, we can't output anything to the screen |
|
| 170 | + // or it will end up mixed in with the file output. |
|
| 171 | + if ($this->config->stdin === true) { |
|
| 172 | + $this->config->verbosity = 0; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + // Init the run and load the rulesets to set additional config vars. |
|
| 176 | + $this->init(); |
|
| 177 | + |
|
| 178 | + // When processing STDIN, we only process one file at a time and |
|
| 179 | + // we don't process all the way through, so we can't use the parallel |
|
| 180 | + // running system. |
|
| 181 | + if ($this->config->stdin === true) { |
|
| 182 | + $this->config->parallel = 1; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Override some of the command line settings that might break the fixes. |
|
| 186 | + $this->config->generator = null; |
|
| 187 | + $this->config->explain = false; |
|
| 188 | + $this->config->interactive = false; |
|
| 189 | + $this->config->cache = false; |
|
| 190 | + $this->config->showSources = false; |
|
| 191 | + $this->config->recordErrors = false; |
|
| 192 | + $this->config->reportFile = null; |
|
| 193 | + $this->config->reports = ['cbf' => null]; |
|
| 194 | + |
|
| 195 | + // If a standard tries to set command line arguments itself, some |
|
| 196 | + // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | + // dying if any are found. |
|
| 198 | + $this->config->dieOnUnknownArg = false; |
|
| 199 | + |
|
| 200 | + $this->run(); |
|
| 201 | + $this->reporter->printReports(); |
|
| 202 | + |
|
| 203 | + echo PHP_EOL; |
|
| 204 | + Util\Timing::printRunTime(); |
|
| 205 | + } catch (DeepExitException $e) { |
|
| 206 | + echo $e->getMessage(); |
|
| 207 | + return $e->getCode(); |
|
| 208 | + }//end try |
|
| 209 | + |
|
| 210 | + if ($this->reporter->totalFixed === 0) { |
|
| 211 | + // Nothing was fixed by PHPCBF. |
|
| 212 | + if ($this->reporter->totalFixable === 0) { |
|
| 213 | + // Nothing found that could be fixed. |
|
| 214 | + return 0; |
|
| 215 | + } else { |
|
| 216 | + // Something failed to fix. |
|
| 217 | + return 2; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if ($this->reporter->totalFixable === 0) { |
|
| 222 | + // PHPCBF fixed all fixable errors. |
|
| 223 | + return 1; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | + return 2; |
|
| 228 | + |
|
| 229 | + }//end runPHPCBF() |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | + * |
|
| 235 | + * @return array |
|
| 236 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 237 | + */ |
|
| 238 | + public function checkRequirements() |
|
| 239 | + { |
|
| 240 | + // Check the PHP version. |
|
| 241 | + if (PHP_VERSION_ID < 50400) { |
|
| 242 | + $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | + throw new DeepExitException($error, 3); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $requiredExtensions = [ |
|
| 247 | + 'tokenizer', |
|
| 248 | + 'xmlwriter', |
|
| 249 | + 'SimpleXML', |
|
| 250 | + ]; |
|
| 251 | + $missingExtensions = []; |
|
| 252 | + |
|
| 253 | + foreach ($requiredExtensions as $extension) { |
|
| 254 | + if (extension_loaded($extension) === false) { |
|
| 255 | + $missingExtensions[] = $extension; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (empty($missingExtensions) === false) { |
|
| 260 | + $last = array_pop($requiredExtensions); |
|
| 261 | + $required = implode(', ', $requiredExtensions); |
|
| 262 | + $required .= ' and '.$last; |
|
| 263 | + |
|
| 264 | + if (count($missingExtensions) === 1) { |
|
| 265 | + $missing = $missingExtensions[0]; |
|
| 266 | + } else { |
|
| 267 | + $last = array_pop($missingExtensions); |
|
| 268 | + $missing = implode(', ', $missingExtensions); |
|
| 269 | + $missing .= ' and '.$last; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL; |
|
| 273 | + $error = sprintf($error, $required, $missing); |
|
| 274 | + throw new DeepExitException($error, 3); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + }//end checkRequirements() |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Init the rulesets and other high-level settings. |
|
| 282 | + * |
|
| 283 | + * @return void |
|
| 284 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 285 | + */ |
|
| 286 | + public function init() |
|
| 287 | + { |
|
| 288 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // Ensure this option is enabled or else line endings will not always |
|
| 293 | + // be detected properly for files created on a Mac with the /r line ending. |
|
| 294 | + ini_set('auto_detect_line_endings', true); |
|
| 295 | + |
|
| 296 | + // Disable the PCRE JIT as this caused issues with parallel running. |
|
| 297 | + ini_set('pcre.jit', false); |
|
| 298 | + |
|
| 299 | + // Check that the standards are valid. |
|
| 300 | + foreach ($this->config->standards as $standard) { |
|
| 301 | + if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | + // They didn't select a valid coding standard, so help them |
|
| 303 | + // out by letting them know which standards are installed. |
|
| 304 | + $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | + ob_start(); |
|
| 306 | + Util\Standards::printInstalledStandards(); |
|
| 307 | + $error .= ob_get_contents(); |
|
| 308 | + ob_end_clean(); |
|
| 309 | + throw new DeepExitException($error, 3); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Saves passing the Config object into other objects that only need |
|
| 314 | + // the verbosity flag for debug output. |
|
| 315 | + if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | + define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | + // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | + $tokens = new Util\Tokens(); |
|
| 322 | + |
|
| 323 | + // Allow autoloading of custom files inside installed standards. |
|
| 324 | + $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | + foreach ($installedStandards as $name => $details) { |
|
| 326 | + Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // The ruleset contains all the information about how the files |
|
| 330 | + // should be checked and/or fixed. |
|
| 331 | + try { |
|
| 332 | + $this->ruleset = new Ruleset($this->config); |
|
| 333 | + } catch (RuntimeException $e) { |
|
| 334 | + $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | + $error .= $this->config->printShortUsage(true); |
|
| 336 | + throw new DeepExitException($error, 3); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + }//end init() |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Performs the run. |
|
| 344 | + * |
|
| 345 | + * @return int The number of errors and warnings found. |
|
| 346 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | + */ |
|
| 349 | + private function run() |
|
| 350 | + { |
|
| 351 | + // The class that manages all reporters for the run. |
|
| 352 | + $this->reporter = new Reporter($this->config); |
|
| 353 | + |
|
| 354 | + // Include bootstrap files. |
|
| 355 | + foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | + include $bootstrap; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if ($this->config->stdin === true) { |
|
| 360 | + $fileContents = $this->config->stdinContent; |
|
| 361 | + if ($fileContents === null) { |
|
| 362 | + $handle = fopen('php://stdin', 'r'); |
|
| 363 | + stream_set_blocking($handle, true); |
|
| 364 | + $fileContents = stream_get_contents($handle); |
|
| 365 | + fclose($handle); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | + $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | + $todo->addFile($dummy->path, $dummy); |
|
| 371 | + } else { |
|
| 372 | + if (empty($this->config->files) === true) { |
|
| 373 | + $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | + $error .= $this->config->printShortUsage(true); |
|
| 375 | + throw new DeepExitException($error, 3); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | + echo 'Creating file list... '; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | + |
|
| 384 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | + $numFiles = count($todo); |
|
| 386 | + echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ($this->config->cache === true) { |
|
| 390 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | + echo 'Loading cache... '; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + Cache::load($this->ruleset, $this->config); |
|
| 395 | + |
|
| 396 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | + $size = Cache::getSize(); |
|
| 398 | + echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + }//end if |
|
| 402 | + |
|
| 403 | + // Turn all sniff errors into exceptions. |
|
| 404 | + set_error_handler([$this, 'handleErrors']); |
|
| 405 | + |
|
| 406 | + // If verbosity is too high, turn off parallelism so the |
|
| 407 | + // debug output is clean. |
|
| 408 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | + $this->config->parallel = 1; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | + if (function_exists('pcntl_fork') === false) { |
|
| 414 | + $this->config->parallel = 1; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $lastDir = ''; |
|
| 418 | + $numFiles = count($todo); |
|
| 419 | + |
|
| 420 | + if ($this->config->parallel === 1) { |
|
| 421 | + // Running normally. |
|
| 422 | + $numProcessed = 0; |
|
| 423 | + foreach ($todo as $path => $file) { |
|
| 424 | + if ($file->ignored === false) { |
|
| 425 | + $currDir = dirname($path); |
|
| 426 | + if ($lastDir !== $currDir) { |
|
| 427 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $lastDir = $currDir; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $this->processFile($file); |
|
| 435 | + } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | + echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $numProcessed++; |
|
| 440 | + $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | + } |
|
| 442 | + } else { |
|
| 443 | + // Batching and forking. |
|
| 444 | + $childProcs = []; |
|
| 445 | + $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | + |
|
| 447 | + for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | + $startAt = ($batch * $numPerBatch); |
|
| 449 | + if ($startAt >= $numFiles) { |
|
| 450 | + break; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + $endAt = ($startAt + $numPerBatch); |
|
| 454 | + if ($endAt > $numFiles) { |
|
| 455 | + $endAt = $numFiles; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | + $pid = pcntl_fork(); |
|
| 460 | + if ($pid === -1) { |
|
| 461 | + throw new RuntimeException('Failed to create child process'); |
|
| 462 | + } else if ($pid !== 0) { |
|
| 463 | + $childProcs[] = [ |
|
| 464 | + 'pid' => $pid, |
|
| 465 | + 'out' => $childOutFilename, |
|
| 466 | + ]; |
|
| 467 | + } else { |
|
| 468 | + // Move forward to the start of the batch. |
|
| 469 | + $todo->rewind(); |
|
| 470 | + for ($i = 0; $i < $startAt; $i++) { |
|
| 471 | + $todo->next(); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + // Reset the reporter to make sure only figures from this |
|
| 475 | + // file batch are recorded. |
|
| 476 | + $this->reporter->totalFiles = 0; |
|
| 477 | + $this->reporter->totalErrors = 0; |
|
| 478 | + $this->reporter->totalWarnings = 0; |
|
| 479 | + $this->reporter->totalFixable = 0; |
|
| 480 | + $this->reporter->totalFixed = 0; |
|
| 481 | + |
|
| 482 | + // Process the files. |
|
| 483 | + $pathsProcessed = []; |
|
| 484 | + ob_start(); |
|
| 485 | + for ($i = $startAt; $i < $endAt; $i++) { |
|
| 486 | + $path = $todo->key(); |
|
| 487 | + $file = $todo->current(); |
|
| 488 | + |
|
| 489 | + if ($file->ignored === true) { |
|
| 490 | + continue; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + $currDir = dirname($path); |
|
| 494 | + if ($lastDir !== $currDir) { |
|
| 495 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 496 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + $lastDir = $currDir; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + $this->processFile($file); |
|
| 503 | + |
|
| 504 | + $pathsProcessed[] = $path; |
|
| 505 | + $todo->next(); |
|
| 506 | + }//end for |
|
| 507 | + |
|
| 508 | + $debugOutput = ob_get_contents(); |
|
| 509 | + ob_end_clean(); |
|
| 510 | + |
|
| 511 | + // Write information about the run to the filesystem |
|
| 512 | + // so it can be picked up by the main process. |
|
| 513 | + $childOutput = [ |
|
| 514 | + 'totalFiles' => $this->reporter->totalFiles, |
|
| 515 | + 'totalErrors' => $this->reporter->totalErrors, |
|
| 516 | + 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 517 | + 'totalFixable' => $this->reporter->totalFixable, |
|
| 518 | + 'totalFixed' => $this->reporter->totalFixed, |
|
| 519 | + ]; |
|
| 520 | + |
|
| 521 | + $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 522 | + $output .= var_export($childOutput, true); |
|
| 523 | + $output .= ";\n\$debugOutput = "; |
|
| 524 | + $output .= var_export($debugOutput, true); |
|
| 525 | + |
|
| 526 | + if ($this->config->cache === true) { |
|
| 527 | + $childCache = []; |
|
| 528 | + foreach ($pathsProcessed as $path) { |
|
| 529 | + $childCache[$path] = Cache::get($path); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + $output .= ";\n\$childCache = "; |
|
| 533 | + $output .= var_export($childCache, true); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + $output .= ";\n?".'>'; |
|
| 537 | + file_put_contents($childOutFilename, $output); |
|
| 538 | + exit($pid); |
|
| 539 | + }//end if |
|
| 540 | + }//end for |
|
| 541 | + |
|
| 542 | + $success = $this->processChildProcs($childProcs); |
|
| 543 | + if ($success === false) { |
|
| 544 | + throw new RuntimeException('One or more child processes failed to run'); |
|
| 545 | + } |
|
| 546 | + }//end if |
|
| 547 | + |
|
| 548 | + restore_error_handler(); |
|
| 549 | + |
|
| 550 | + if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 551 | + && $this->config->interactive === false |
|
| 552 | + && $this->config->showProgress === true |
|
| 553 | + ) { |
|
| 554 | + echo PHP_EOL.PHP_EOL; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + if ($this->config->cache === true) { |
|
| 558 | + Cache::save(); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 562 | + $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 563 | + |
|
| 564 | + $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 565 | + if ($ignoreErrors !== null) { |
|
| 566 | + $ignoreErrors = (bool) $ignoreErrors; |
|
| 567 | + if ($ignoreErrors === true) { |
|
| 568 | + $return -= $this->reporter->totalErrors; |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + if ($ignoreWarnings !== null) { |
|
| 573 | + $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 574 | + if ($ignoreWarnings === true) { |
|
| 575 | + $return -= $this->reporter->totalWarnings; |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + return $return; |
|
| 580 | + |
|
| 581 | + }//end run() |
|
| 582 | + |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Converts all PHP errors into exceptions. |
|
| 586 | + * |
|
| 587 | + * This method forces a sniff to stop processing if it is not |
|
| 588 | + * able to handle a specific piece of code, instead of continuing |
|
| 589 | + * and potentially getting into a loop. |
|
| 590 | + * |
|
| 591 | + * @param int $code The level of error raised. |
|
| 592 | + * @param string $message The error message. |
|
| 593 | + * @param string $file The path of the file that raised the error. |
|
| 594 | + * @param int $line The line number the error was raised at. |
|
| 595 | + * |
|
| 596 | + * @return void |
|
| 597 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 598 | + */ |
|
| 599 | + public function handleErrors($code, $message, $file, $line) |
|
| 600 | + { |
|
| 601 | + if ((error_reporting() & $code) === 0) { |
|
| 602 | + // This type of error is being muted. |
|
| 603 | + return true; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + throw new RuntimeException("$message in $file on line $line"); |
|
| 607 | + |
|
| 608 | + }//end handleErrors() |
|
| 609 | + |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Processes a single file, including checking and fixing. |
|
| 613 | + * |
|
| 614 | + * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 615 | + * |
|
| 616 | + * @return void |
|
| 617 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 618 | + */ |
|
| 619 | + public function processFile($file) |
|
| 620 | + { |
|
| 621 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 622 | + $startTime = microtime(true); |
|
| 623 | + echo 'Processing '.basename($file->path).' '; |
|
| 624 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 625 | + echo PHP_EOL; |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + try { |
|
| 630 | + $file->process(); |
|
| 631 | + |
|
| 632 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 633 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 634 | + if ($timeTaken < 1000) { |
|
| 635 | + $timeTaken = round($timeTaken); |
|
| 636 | + echo "DONE in {$timeTaken}ms"; |
|
| 637 | + } else { |
|
| 638 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
| 639 | + echo "DONE in $timeTaken secs"; |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 643 | + $errors = $file->getFixableCount(); |
|
| 644 | + echo " ($errors fixable violations)".PHP_EOL; |
|
| 645 | + } else { |
|
| 646 | + $errors = $file->getErrorCount(); |
|
| 647 | + $warnings = $file->getWarningCount(); |
|
| 648 | + echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 649 | + } |
|
| 650 | + } |
|
| 651 | + } catch (\Exception $e) { |
|
| 652 | + $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 653 | + $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 654 | + }//end try |
|
| 655 | + |
|
| 656 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 657 | + |
|
| 658 | + if ($this->config->interactive === true) { |
|
| 659 | + /* |
|
| 660 | 660 | Running interactively. |
| 661 | 661 | Print the error report for the current file and then wait for user input. |
| 662 | 662 | */ |
| 663 | 663 | |
| 664 | - // Get current violations and then clear the list to make sure |
|
| 665 | - // we only print violations for a single file each time. |
|
| 666 | - $numErrors = null; |
|
| 667 | - while ($numErrors !== 0) { |
|
| 668 | - $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 669 | - if ($numErrors === 0) { |
|
| 670 | - continue; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - $this->reporter->printReport('full'); |
|
| 674 | - |
|
| 675 | - echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 676 | - $input = fgets(STDIN); |
|
| 677 | - $input = trim($input); |
|
| 678 | - |
|
| 679 | - switch ($input) { |
|
| 680 | - case 's': |
|
| 681 | - break(2); |
|
| 682 | - case 'q': |
|
| 683 | - throw new DeepExitException('', 0); |
|
| 684 | - default: |
|
| 685 | - // Repopulate the sniffs because some of them save their state |
|
| 686 | - // and only clear it when the file changes, but we are rechecking |
|
| 687 | - // the same file. |
|
| 688 | - $file->ruleset->populateTokenListeners(); |
|
| 689 | - $file->reloadContent(); |
|
| 690 | - $file->process(); |
|
| 691 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 692 | - break; |
|
| 693 | - } |
|
| 694 | - }//end while |
|
| 695 | - }//end if |
|
| 696 | - |
|
| 697 | - // Clean up the file to save (a lot of) memory. |
|
| 698 | - $file->cleanUp(); |
|
| 699 | - |
|
| 700 | - }//end processFile() |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * Waits for child processes to complete and cleans up after them. |
|
| 705 | - * |
|
| 706 | - * The reporting information returned by each child process is merged |
|
| 707 | - * into the main reporter class. |
|
| 708 | - * |
|
| 709 | - * @param array $childProcs An array of child processes to wait for. |
|
| 710 | - * |
|
| 711 | - * @return void |
|
| 712 | - */ |
|
| 713 | - private function processChildProcs($childProcs) |
|
| 714 | - { |
|
| 715 | - $numProcessed = 0; |
|
| 716 | - $totalBatches = count($childProcs); |
|
| 717 | - |
|
| 718 | - $success = true; |
|
| 719 | - |
|
| 720 | - while (count($childProcs) > 0) { |
|
| 721 | - foreach ($childProcs as $key => $procData) { |
|
| 722 | - $res = pcntl_waitpid($procData['pid'], $status, WNOHANG); |
|
| 723 | - if ($res === $procData['pid']) { |
|
| 724 | - if (file_exists($procData['out']) === true) { |
|
| 725 | - include $procData['out']; |
|
| 726 | - |
|
| 727 | - unlink($procData['out']); |
|
| 728 | - unset($childProcs[$key]); |
|
| 729 | - |
|
| 730 | - $numProcessed++; |
|
| 731 | - |
|
| 732 | - if (isset($childOutput) === false) { |
|
| 733 | - // The child process died, so the run has failed. |
|
| 734 | - $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 735 | - $file->setErrorCounts(1, 0, 0, 0); |
|
| 736 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 737 | - $success = false; |
|
| 738 | - continue; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 742 | - $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 743 | - $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 744 | - $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 745 | - $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 746 | - |
|
| 747 | - if (isset($debugOutput) === true) { |
|
| 748 | - echo $debugOutput; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - if (isset($childCache) === true) { |
|
| 752 | - foreach ($childCache as $path => $cache) { |
|
| 753 | - Cache::set($path, $cache); |
|
| 754 | - } |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - // Fake a processed file so we can print progress output for the batch. |
|
| 758 | - $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 759 | - $file->setErrorCounts( |
|
| 760 | - $childOutput['totalErrors'], |
|
| 761 | - $childOutput['totalWarnings'], |
|
| 762 | - $childOutput['totalFixable'], |
|
| 763 | - $childOutput['totalFixed'] |
|
| 764 | - ); |
|
| 765 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 766 | - }//end if |
|
| 767 | - }//end if |
|
| 768 | - }//end foreach |
|
| 769 | - }//end while |
|
| 770 | - |
|
| 771 | - return $success; |
|
| 772 | - |
|
| 773 | - }//end processChildProcs() |
|
| 774 | - |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * Print progress information for a single processed file. |
|
| 778 | - * |
|
| 779 | - * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | - * @param int $numFiles The total number of files to process. |
|
| 781 | - * @param int $numProcessed The number of files that have been processed, |
|
| 782 | - * including this one. |
|
| 783 | - * |
|
| 784 | - * @return void |
|
| 785 | - */ |
|
| 786 | - public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | - { |
|
| 788 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | - || $this->config->showProgress === false |
|
| 790 | - ) { |
|
| 791 | - return; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - // Show progress information. |
|
| 795 | - if ($file->ignored === true) { |
|
| 796 | - echo 'S'; |
|
| 797 | - } else { |
|
| 798 | - $errors = $file->getErrorCount(); |
|
| 799 | - $warnings = $file->getWarningCount(); |
|
| 800 | - $fixable = $file->getFixableCount(); |
|
| 801 | - $fixed = $file->getFixedCount(); |
|
| 802 | - |
|
| 803 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | - // Files with fixed errors or warnings are F (green). |
|
| 805 | - // Files with unfixable errors or warnings are E (red). |
|
| 806 | - // Files with no errors or warnings are . (black). |
|
| 807 | - if ($fixable > 0) { |
|
| 808 | - if ($this->config->colors === true) { |
|
| 809 | - echo "\033[31m"; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - echo 'E'; |
|
| 813 | - |
|
| 814 | - if ($this->config->colors === true) { |
|
| 815 | - echo "\033[0m"; |
|
| 816 | - } |
|
| 817 | - } else if ($fixed > 0) { |
|
| 818 | - if ($this->config->colors === true) { |
|
| 819 | - echo "\033[32m"; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - echo 'F'; |
|
| 823 | - |
|
| 824 | - if ($this->config->colors === true) { |
|
| 825 | - echo "\033[0m"; |
|
| 826 | - } |
|
| 827 | - } else { |
|
| 828 | - echo '.'; |
|
| 829 | - }//end if |
|
| 830 | - } else { |
|
| 831 | - // Files with errors are E (red). |
|
| 832 | - // Files with fixable errors are E (green). |
|
| 833 | - // Files with warnings are W (yellow). |
|
| 834 | - // Files with fixable warnings are W (green). |
|
| 835 | - // Files with no errors or warnings are . (black). |
|
| 836 | - if ($errors > 0) { |
|
| 837 | - if ($this->config->colors === true) { |
|
| 838 | - if ($fixable > 0) { |
|
| 839 | - echo "\033[32m"; |
|
| 840 | - } else { |
|
| 841 | - echo "\033[31m"; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - echo 'E'; |
|
| 846 | - |
|
| 847 | - if ($this->config->colors === true) { |
|
| 848 | - echo "\033[0m"; |
|
| 849 | - } |
|
| 850 | - } else if ($warnings > 0) { |
|
| 851 | - if ($this->config->colors === true) { |
|
| 852 | - if ($fixable > 0) { |
|
| 853 | - echo "\033[32m"; |
|
| 854 | - } else { |
|
| 855 | - echo "\033[33m"; |
|
| 856 | - } |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - echo 'W'; |
|
| 860 | - |
|
| 861 | - if ($this->config->colors === true) { |
|
| 862 | - echo "\033[0m"; |
|
| 863 | - } |
|
| 864 | - } else { |
|
| 865 | - echo '.'; |
|
| 866 | - }//end if |
|
| 867 | - }//end if |
|
| 868 | - }//end if |
|
| 869 | - |
|
| 870 | - $numPerLine = 60; |
|
| 871 | - if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | - return; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | - $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | - if ($numProcessed === $numFiles && $numFiles > $numPerLine) { |
|
| 878 | - $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 882 | - |
|
| 883 | - }//end printProgress() |
|
| 664 | + // Get current violations and then clear the list to make sure |
|
| 665 | + // we only print violations for a single file each time. |
|
| 666 | + $numErrors = null; |
|
| 667 | + while ($numErrors !== 0) { |
|
| 668 | + $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 669 | + if ($numErrors === 0) { |
|
| 670 | + continue; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + $this->reporter->printReport('full'); |
|
| 674 | + |
|
| 675 | + echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 676 | + $input = fgets(STDIN); |
|
| 677 | + $input = trim($input); |
|
| 678 | + |
|
| 679 | + switch ($input) { |
|
| 680 | + case 's': |
|
| 681 | + break(2); |
|
| 682 | + case 'q': |
|
| 683 | + throw new DeepExitException('', 0); |
|
| 684 | + default: |
|
| 685 | + // Repopulate the sniffs because some of them save their state |
|
| 686 | + // and only clear it when the file changes, but we are rechecking |
|
| 687 | + // the same file. |
|
| 688 | + $file->ruleset->populateTokenListeners(); |
|
| 689 | + $file->reloadContent(); |
|
| 690 | + $file->process(); |
|
| 691 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 692 | + break; |
|
| 693 | + } |
|
| 694 | + }//end while |
|
| 695 | + }//end if |
|
| 696 | + |
|
| 697 | + // Clean up the file to save (a lot of) memory. |
|
| 698 | + $file->cleanUp(); |
|
| 699 | + |
|
| 700 | + }//end processFile() |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * Waits for child processes to complete and cleans up after them. |
|
| 705 | + * |
|
| 706 | + * The reporting information returned by each child process is merged |
|
| 707 | + * into the main reporter class. |
|
| 708 | + * |
|
| 709 | + * @param array $childProcs An array of child processes to wait for. |
|
| 710 | + * |
|
| 711 | + * @return void |
|
| 712 | + */ |
|
| 713 | + private function processChildProcs($childProcs) |
|
| 714 | + { |
|
| 715 | + $numProcessed = 0; |
|
| 716 | + $totalBatches = count($childProcs); |
|
| 717 | + |
|
| 718 | + $success = true; |
|
| 719 | + |
|
| 720 | + while (count($childProcs) > 0) { |
|
| 721 | + foreach ($childProcs as $key => $procData) { |
|
| 722 | + $res = pcntl_waitpid($procData['pid'], $status, WNOHANG); |
|
| 723 | + if ($res === $procData['pid']) { |
|
| 724 | + if (file_exists($procData['out']) === true) { |
|
| 725 | + include $procData['out']; |
|
| 726 | + |
|
| 727 | + unlink($procData['out']); |
|
| 728 | + unset($childProcs[$key]); |
|
| 729 | + |
|
| 730 | + $numProcessed++; |
|
| 731 | + |
|
| 732 | + if (isset($childOutput) === false) { |
|
| 733 | + // The child process died, so the run has failed. |
|
| 734 | + $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 735 | + $file->setErrorCounts(1, 0, 0, 0); |
|
| 736 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 737 | + $success = false; |
|
| 738 | + continue; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 742 | + $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 743 | + $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 744 | + $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 745 | + $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 746 | + |
|
| 747 | + if (isset($debugOutput) === true) { |
|
| 748 | + echo $debugOutput; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + if (isset($childCache) === true) { |
|
| 752 | + foreach ($childCache as $path => $cache) { |
|
| 753 | + Cache::set($path, $cache); |
|
| 754 | + } |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + // Fake a processed file so we can print progress output for the batch. |
|
| 758 | + $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 759 | + $file->setErrorCounts( |
|
| 760 | + $childOutput['totalErrors'], |
|
| 761 | + $childOutput['totalWarnings'], |
|
| 762 | + $childOutput['totalFixable'], |
|
| 763 | + $childOutput['totalFixed'] |
|
| 764 | + ); |
|
| 765 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 766 | + }//end if |
|
| 767 | + }//end if |
|
| 768 | + }//end foreach |
|
| 769 | + }//end while |
|
| 770 | + |
|
| 771 | + return $success; |
|
| 772 | + |
|
| 773 | + }//end processChildProcs() |
|
| 774 | + |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * Print progress information for a single processed file. |
|
| 778 | + * |
|
| 779 | + * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | + * @param int $numFiles The total number of files to process. |
|
| 781 | + * @param int $numProcessed The number of files that have been processed, |
|
| 782 | + * including this one. |
|
| 783 | + * |
|
| 784 | + * @return void |
|
| 785 | + */ |
|
| 786 | + public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | + { |
|
| 788 | + if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | + || $this->config->showProgress === false |
|
| 790 | + ) { |
|
| 791 | + return; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + // Show progress information. |
|
| 795 | + if ($file->ignored === true) { |
|
| 796 | + echo 'S'; |
|
| 797 | + } else { |
|
| 798 | + $errors = $file->getErrorCount(); |
|
| 799 | + $warnings = $file->getWarningCount(); |
|
| 800 | + $fixable = $file->getFixableCount(); |
|
| 801 | + $fixed = $file->getFixedCount(); |
|
| 802 | + |
|
| 803 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | + // Files with fixed errors or warnings are F (green). |
|
| 805 | + // Files with unfixable errors or warnings are E (red). |
|
| 806 | + // Files with no errors or warnings are . (black). |
|
| 807 | + if ($fixable > 0) { |
|
| 808 | + if ($this->config->colors === true) { |
|
| 809 | + echo "\033[31m"; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + echo 'E'; |
|
| 813 | + |
|
| 814 | + if ($this->config->colors === true) { |
|
| 815 | + echo "\033[0m"; |
|
| 816 | + } |
|
| 817 | + } else if ($fixed > 0) { |
|
| 818 | + if ($this->config->colors === true) { |
|
| 819 | + echo "\033[32m"; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + echo 'F'; |
|
| 823 | + |
|
| 824 | + if ($this->config->colors === true) { |
|
| 825 | + echo "\033[0m"; |
|
| 826 | + } |
|
| 827 | + } else { |
|
| 828 | + echo '.'; |
|
| 829 | + }//end if |
|
| 830 | + } else { |
|
| 831 | + // Files with errors are E (red). |
|
| 832 | + // Files with fixable errors are E (green). |
|
| 833 | + // Files with warnings are W (yellow). |
|
| 834 | + // Files with fixable warnings are W (green). |
|
| 835 | + // Files with no errors or warnings are . (black). |
|
| 836 | + if ($errors > 0) { |
|
| 837 | + if ($this->config->colors === true) { |
|
| 838 | + if ($fixable > 0) { |
|
| 839 | + echo "\033[32m"; |
|
| 840 | + } else { |
|
| 841 | + echo "\033[31m"; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + echo 'E'; |
|
| 846 | + |
|
| 847 | + if ($this->config->colors === true) { |
|
| 848 | + echo "\033[0m"; |
|
| 849 | + } |
|
| 850 | + } else if ($warnings > 0) { |
|
| 851 | + if ($this->config->colors === true) { |
|
| 852 | + if ($fixable > 0) { |
|
| 853 | + echo "\033[32m"; |
|
| 854 | + } else { |
|
| 855 | + echo "\033[33m"; |
|
| 856 | + } |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + echo 'W'; |
|
| 860 | + |
|
| 861 | + if ($this->config->colors === true) { |
|
| 862 | + echo "\033[0m"; |
|
| 863 | + } |
|
| 864 | + } else { |
|
| 865 | + echo '.'; |
|
| 866 | + }//end if |
|
| 867 | + }//end if |
|
| 868 | + }//end if |
|
| 869 | + |
|
| 870 | + $numPerLine = 60; |
|
| 871 | + if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | + return; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | + $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | + if ($numProcessed === $numFiles && $numFiles > $numPerLine) { |
|
| 878 | + $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 882 | + |
|
| 883 | + }//end printProgress() |
|
| 884 | 884 | |
| 885 | 885 | |
| 886 | 886 | }//end class |
@@ -677,19 +677,19 @@ |
||
| 677 | 677 | $input = trim($input); |
| 678 | 678 | |
| 679 | 679 | switch ($input) { |
| 680 | - case 's': |
|
| 681 | - break(2); |
|
| 682 | - case 'q': |
|
| 683 | - throw new DeepExitException('', 0); |
|
| 684 | - default: |
|
| 685 | - // Repopulate the sniffs because some of them save their state |
|
| 686 | - // and only clear it when the file changes, but we are rechecking |
|
| 687 | - // the same file. |
|
| 688 | - $file->ruleset->populateTokenListeners(); |
|
| 689 | - $file->reloadContent(); |
|
| 690 | - $file->process(); |
|
| 691 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 692 | - break; |
|
| 680 | + case 's': |
|
| 681 | + break(2); |
|
| 682 | + case 'q': |
|
| 683 | + throw new DeepExitException('', 0); |
|
| 684 | + default: |
|
| 685 | + // Repopulate the sniffs because some of them save their state |
|
| 686 | + // and only clear it when the file changes, but we are rechecking |
|
| 687 | + // the same file. |
|
| 688 | + $file->ruleset->populateTokenListeners(); |
|
| 689 | + $file->reloadContent(); |
|
| 690 | + $file->process(); |
|
| 691 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 692 | + break; |
|
| 693 | 693 | } |
| 694 | 694 | }//end while |
| 695 | 695 | }//end if |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | Util\Timing::startTiming(); |
| 58 | 58 | Runner::checkRequirements(); |
| 59 | 59 | |
| 60 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 60 | + if ( defined( 'PHP_CODESNIFFER_CBF' ) === false ) { |
|
| 61 | + define( 'PHP_CODESNIFFER_CBF', false ); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // Creating the Config object populates it with all required settings |
@@ -71,11 +71,11 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | // Print a list of sniffs in each of the supplied standards. |
| 73 | 73 | // We fudge the config here so that each standard is explained in isolation. |
| 74 | - if ($this->config->explain === true) { |
|
| 74 | + if ( $this->config->explain === true ) { |
|
| 75 | 75 | $standards = $this->config->standards; |
| 76 | - foreach ($standards as $standard) { |
|
| 77 | - $this->config->standards = [$standard]; |
|
| 78 | - $ruleset = new Ruleset($this->config); |
|
| 76 | + foreach ( $standards as $standard ) { |
|
| 77 | + $this->config->standards = [ $standard ]; |
|
| 78 | + $ruleset = new Ruleset( $this->config ); |
|
| 79 | 79 | $ruleset->explain(); |
| 80 | 80 | } |
| 81 | 81 | |
@@ -83,13 +83,13 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | // Generate documentation for each of the supplied standards. |
| 86 | - if ($this->config->generator !== null) { |
|
| 86 | + if ( $this->config->generator !== null ) { |
|
| 87 | 87 | $standards = $this->config->standards; |
| 88 | - foreach ($standards as $standard) { |
|
| 89 | - $this->config->standards = [$standard]; |
|
| 90 | - $ruleset = new Ruleset($this->config); |
|
| 91 | - $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | - $generator = new $class($ruleset); |
|
| 88 | + foreach ( $standards as $standard ) { |
|
| 89 | + $this->config->standards = [ $standard ]; |
|
| 90 | + $ruleset = new Ruleset( $this->config ); |
|
| 91 | + $class = 'PHP_CodeSniffer\Generators\\' . $this->config->generator; |
|
| 92 | + $generator = new $class( $ruleset ); |
|
| 93 | 93 | $generator->generate(); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -99,15 +99,15 @@ discard block |
||
| 99 | 99 | // Other report formats don't really make sense in interactive mode |
| 100 | 100 | // so we hard-code the full report here and when outputting. |
| 101 | 101 | // We also ensure parallel processing is off because we need to do one file at a time. |
| 102 | - if ($this->config->interactive === true) { |
|
| 103 | - $this->config->reports = ['full' => null]; |
|
| 102 | + if ( $this->config->interactive === true ) { |
|
| 103 | + $this->config->reports = [ 'full' => null ]; |
|
| 104 | 104 | $this->config->parallel = 1; |
| 105 | 105 | $this->config->showProgress = false; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // Disable caching if we are processing STDIN as we can't be 100% |
| 109 | 109 | // sure where the file came from or if it will change in the future. |
| 110 | - if ($this->config->stdin === true) { |
|
| 110 | + if ( $this->config->stdin === true ) { |
|
| 111 | 111 | $this->config->cache = false; |
| 112 | 112 | } |
| 113 | 113 | |
@@ -121,21 +121,21 @@ discard block |
||
| 121 | 121 | // in something like an XML report. If we are printing to screen, |
| 122 | 122 | // the report types would have already worked out who should |
| 123 | 123 | // print the timer info. |
| 124 | - if ($this->config->interactive === false |
|
| 125 | - && ($toScreen === false |
|
| 126 | - || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 124 | + if ( $this->config->interactive === false |
|
| 125 | + && ( $toScreen === false |
|
| 126 | + || ( ( $this->reporter->totalErrors + $this->reporter->totalWarnings ) === 0 && $this->config->showProgress === true ) ) |
|
| 127 | 127 | ) { |
| 128 | 128 | Util\Timing::printRunTime(); |
| 129 | 129 | } |
| 130 | - } catch (DeepExitException $e) { |
|
| 130 | + } catch ( DeepExitException $e ) { |
|
| 131 | 131 | echo $e->getMessage(); |
| 132 | 132 | return $e->getCode(); |
| 133 | 133 | }//end try |
| 134 | 134 | |
| 135 | - if ($numErrors === 0) { |
|
| 135 | + if ( $numErrors === 0 ) { |
|
| 136 | 136 | // No errors found. |
| 137 | 137 | return 0; |
| 138 | - } else if ($this->reporter->totalFixable === 0) { |
|
| 138 | + } else if ( $this->reporter->totalFixable === 0 ) { |
|
| 139 | 139 | // Errors found, but none of them can be fixed by PHPCBF. |
| 140 | 140 | return 1; |
| 141 | 141 | } else { |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function runPHPCBF() |
| 155 | 155 | { |
| 156 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | - define('PHP_CODESNIFFER_CBF', true); |
|
| 156 | + if ( defined( 'PHP_CODESNIFFER_CBF' ) === false ) { |
|
| 157 | + define( 'PHP_CODESNIFFER_CBF', true ); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | try { |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | // When processing STDIN, we can't output anything to the screen |
| 170 | 170 | // or it will end up mixed in with the file output. |
| 171 | - if ($this->config->stdin === true) { |
|
| 171 | + if ( $this->config->stdin === true ) { |
|
| 172 | 172 | $this->config->verbosity = 0; |
| 173 | 173 | } |
| 174 | 174 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | // When processing STDIN, we only process one file at a time and |
| 179 | 179 | // we don't process all the way through, so we can't use the parallel |
| 180 | 180 | // running system. |
| 181 | - if ($this->config->stdin === true) { |
|
| 181 | + if ( $this->config->stdin === true ) { |
|
| 182 | 182 | $this->config->parallel = 1; |
| 183 | 183 | } |
| 184 | 184 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | $this->config->showSources = false; |
| 191 | 191 | $this->config->recordErrors = false; |
| 192 | 192 | $this->config->reportFile = null; |
| 193 | - $this->config->reports = ['cbf' => null]; |
|
| 193 | + $this->config->reports = [ 'cbf' => null ]; |
|
| 194 | 194 | |
| 195 | 195 | // If a standard tries to set command line arguments itself, some |
| 196 | 196 | // may be blocked because PHPCBF is running, so stop the script |
@@ -202,14 +202,14 @@ discard block |
||
| 202 | 202 | |
| 203 | 203 | echo PHP_EOL; |
| 204 | 204 | Util\Timing::printRunTime(); |
| 205 | - } catch (DeepExitException $e) { |
|
| 205 | + } catch ( DeepExitException $e ) { |
|
| 206 | 206 | echo $e->getMessage(); |
| 207 | 207 | return $e->getCode(); |
| 208 | 208 | }//end try |
| 209 | 209 | |
| 210 | - if ($this->reporter->totalFixed === 0) { |
|
| 210 | + if ( $this->reporter->totalFixed === 0 ) { |
|
| 211 | 211 | // Nothing was fixed by PHPCBF. |
| 212 | - if ($this->reporter->totalFixable === 0) { |
|
| 212 | + if ( $this->reporter->totalFixable === 0 ) { |
|
| 213 | 213 | // Nothing found that could be fixed. |
| 214 | 214 | return 0; |
| 215 | 215 | } else { |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - if ($this->reporter->totalFixable === 0) { |
|
| 221 | + if ( $this->reporter->totalFixable === 0 ) { |
|
| 222 | 222 | // PHPCBF fixed all fixable errors. |
| 223 | 223 | return 1; |
| 224 | 224 | } |
@@ -238,9 +238,9 @@ discard block |
||
| 238 | 238 | public function checkRequirements() |
| 239 | 239 | { |
| 240 | 240 | // Check the PHP version. |
| 241 | - if (PHP_VERSION_ID < 50400) { |
|
| 242 | - $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | - throw new DeepExitException($error, 3); |
|
| 241 | + if ( PHP_VERSION_ID < 50400 ) { |
|
| 242 | + $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.' . PHP_EOL; |
|
| 243 | + throw new DeepExitException( $error, 3 ); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | $requiredExtensions = [ |
@@ -248,30 +248,30 @@ discard block |
||
| 248 | 248 | 'xmlwriter', |
| 249 | 249 | 'SimpleXML', |
| 250 | 250 | ]; |
| 251 | - $missingExtensions = []; |
|
| 251 | + $missingExtensions = [ ]; |
|
| 252 | 252 | |
| 253 | - foreach ($requiredExtensions as $extension) { |
|
| 254 | - if (extension_loaded($extension) === false) { |
|
| 255 | - $missingExtensions[] = $extension; |
|
| 253 | + foreach ( $requiredExtensions as $extension ) { |
|
| 254 | + if ( extension_loaded( $extension ) === false ) { |
|
| 255 | + $missingExtensions[ ] = $extension; |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - if (empty($missingExtensions) === false) { |
|
| 260 | - $last = array_pop($requiredExtensions); |
|
| 261 | - $required = implode(', ', $requiredExtensions); |
|
| 262 | - $required .= ' and '.$last; |
|
| 259 | + if ( empty( $missingExtensions ) === false ) { |
|
| 260 | + $last = array_pop( $requiredExtensions ); |
|
| 261 | + $required = implode( ', ', $requiredExtensions ); |
|
| 262 | + $required .= ' and ' . $last; |
|
| 263 | 263 | |
| 264 | - if (count($missingExtensions) === 1) { |
|
| 265 | - $missing = $missingExtensions[0]; |
|
| 264 | + if ( count( $missingExtensions ) === 1 ) { |
|
| 265 | + $missing = $missingExtensions[ 0 ]; |
|
| 266 | 266 | } else { |
| 267 | - $last = array_pop($missingExtensions); |
|
| 268 | - $missing = implode(', ', $missingExtensions); |
|
| 269 | - $missing .= ' and '.$last; |
|
| 267 | + $last = array_pop( $missingExtensions ); |
|
| 268 | + $missing = implode( ', ', $missingExtensions ); |
|
| 269 | + $missing .= ' and ' . $last; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL; |
|
| 273 | - $error = sprintf($error, $required, $missing); |
|
| 274 | - throw new DeepExitException($error, 3); |
|
| 272 | + $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.' . PHP_EOL; |
|
| 273 | + $error = sprintf( $error, $required, $missing ); |
|
| 274 | + throw new DeepExitException( $error, 3 ); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | }//end checkRequirements() |
@@ -285,35 +285,35 @@ discard block |
||
| 285 | 285 | */ |
| 286 | 286 | public function init() |
| 287 | 287 | { |
| 288 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 288 | + if ( defined( 'PHP_CODESNIFFER_CBF' ) === false ) { |
|
| 289 | + define( 'PHP_CODESNIFFER_CBF', false ); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | // Ensure this option is enabled or else line endings will not always |
| 293 | 293 | // be detected properly for files created on a Mac with the /r line ending. |
| 294 | - ini_set('auto_detect_line_endings', true); |
|
| 294 | + ini_set( 'auto_detect_line_endings', true ); |
|
| 295 | 295 | |
| 296 | 296 | // Disable the PCRE JIT as this caused issues with parallel running. |
| 297 | - ini_set('pcre.jit', false); |
|
| 297 | + ini_set( 'pcre.jit', false ); |
|
| 298 | 298 | |
| 299 | 299 | // Check that the standards are valid. |
| 300 | - foreach ($this->config->standards as $standard) { |
|
| 301 | - if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 300 | + foreach ( $this->config->standards as $standard ) { |
|
| 301 | + if ( Util\Standards::isInstalledStandard( $standard ) === false ) { |
|
| 302 | 302 | // They didn't select a valid coding standard, so help them |
| 303 | 303 | // out by letting them know which standards are installed. |
| 304 | - $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 304 | + $error = 'ERROR: the "' . $standard . '" coding standard is not installed. '; |
|
| 305 | 305 | ob_start(); |
| 306 | 306 | Util\Standards::printInstalledStandards(); |
| 307 | 307 | $error .= ob_get_contents(); |
| 308 | 308 | ob_end_clean(); |
| 309 | - throw new DeepExitException($error, 3); |
|
| 309 | + throw new DeepExitException( $error, 3 ); |
|
| 310 | 310 | } |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | // Saves passing the Config object into other objects that only need |
| 314 | 314 | // the verbosity flag for debug output. |
| 315 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | - define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 315 | + if ( defined( 'PHP_CODESNIFFER_VERBOSITY' ) === false ) { |
|
| 316 | + define( 'PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity ); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // Create this class so it is autoloaded and sets up a bunch |
@@ -322,18 +322,18 @@ discard block |
||
| 322 | 322 | |
| 323 | 323 | // Allow autoloading of custom files inside installed standards. |
| 324 | 324 | $installedStandards = Standards::getInstalledStandardDetails(); |
| 325 | - foreach ($installedStandards as $name => $details) { |
|
| 326 | - Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 325 | + foreach ( $installedStandards as $name => $details ) { |
|
| 326 | + Autoload::addSearchPath( $details[ 'path' ], $details[ 'namespace' ] ); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | // The ruleset contains all the information about how the files |
| 330 | 330 | // should be checked and/or fixed. |
| 331 | 331 | try { |
| 332 | - $this->ruleset = new Ruleset($this->config); |
|
| 333 | - } catch (RuntimeException $e) { |
|
| 334 | - $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | - $error .= $this->config->printShortUsage(true); |
|
| 336 | - throw new DeepExitException($error, 3); |
|
| 332 | + $this->ruleset = new Ruleset( $this->config ); |
|
| 333 | + } catch ( RuntimeException $e ) { |
|
| 334 | + $error = 'ERROR: ' . $e->getMessage() . PHP_EOL . PHP_EOL; |
|
| 335 | + $error .= $this->config->printShortUsage( true ); |
|
| 336 | + throw new DeepExitException( $error, 3 ); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | }//end init() |
@@ -349,125 +349,125 @@ discard block |
||
| 349 | 349 | private function run() |
| 350 | 350 | { |
| 351 | 351 | // The class that manages all reporters for the run. |
| 352 | - $this->reporter = new Reporter($this->config); |
|
| 352 | + $this->reporter = new Reporter( $this->config ); |
|
| 353 | 353 | |
| 354 | 354 | // Include bootstrap files. |
| 355 | - foreach ($this->config->bootstrap as $bootstrap) { |
|
| 355 | + foreach ( $this->config->bootstrap as $bootstrap ) { |
|
| 356 | 356 | include $bootstrap; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - if ($this->config->stdin === true) { |
|
| 359 | + if ( $this->config->stdin === true ) { |
|
| 360 | 360 | $fileContents = $this->config->stdinContent; |
| 361 | - if ($fileContents === null) { |
|
| 362 | - $handle = fopen('php://stdin', 'r'); |
|
| 363 | - stream_set_blocking($handle, true); |
|
| 364 | - $fileContents = stream_get_contents($handle); |
|
| 365 | - fclose($handle); |
|
| 361 | + if ( $fileContents === null ) { |
|
| 362 | + $handle = fopen( 'php://stdin', 'r' ); |
|
| 363 | + stream_set_blocking( $handle, true ); |
|
| 364 | + $fileContents = stream_get_contents( $handle ); |
|
| 365 | + fclose( $handle ); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | - $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | - $todo->addFile($dummy->path, $dummy); |
|
| 368 | + $todo = new FileList( $this->config, $this->ruleset ); |
|
| 369 | + $dummy = new DummyFile( $fileContents, $this->ruleset, $this->config ); |
|
| 370 | + $todo->addFile( $dummy->path, $dummy ); |
|
| 371 | 371 | } else { |
| 372 | - if (empty($this->config->files) === true) { |
|
| 373 | - $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | - $error .= $this->config->printShortUsage(true); |
|
| 375 | - throw new DeepExitException($error, 3); |
|
| 372 | + if ( empty( $this->config->files ) === true ) { |
|
| 373 | + $error = 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL; |
|
| 374 | + $error .= $this->config->printShortUsage( true ); |
|
| 375 | + throw new DeepExitException( $error, 3 ); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 378 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 379 | 379 | echo 'Creating file list... '; |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 382 | + $todo = new FileList( $this->config, $this->ruleset ); |
|
| 383 | 383 | |
| 384 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | - $numFiles = count($todo); |
|
| 386 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 384 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 385 | + $numFiles = count( $todo ); |
|
| 386 | + echo "DONE ($numFiles files in queue)" . PHP_EOL; |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | - if ($this->config->cache === true) { |
|
| 390 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 389 | + if ( $this->config->cache === true ) { |
|
| 390 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 391 | 391 | echo 'Loading cache... '; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - Cache::load($this->ruleset, $this->config); |
|
| 394 | + Cache::load( $this->ruleset, $this->config ); |
|
| 395 | 395 | |
| 396 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 396 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 397 | 397 | $size = Cache::getSize(); |
| 398 | - echo "DONE ($size files in cache)".PHP_EOL; |
|
| 398 | + echo "DONE ($size files in cache)" . PHP_EOL; |
|
| 399 | 399 | } |
| 400 | 400 | } |
| 401 | 401 | }//end if |
| 402 | 402 | |
| 403 | 403 | // Turn all sniff errors into exceptions. |
| 404 | - set_error_handler([$this, 'handleErrors']); |
|
| 404 | + set_error_handler( [ $this, 'handleErrors' ] ); |
|
| 405 | 405 | |
| 406 | 406 | // If verbosity is too high, turn off parallelism so the |
| 407 | 407 | // debug output is clean. |
| 408 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 408 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 409 | 409 | $this->config->parallel = 1; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | // If the PCNTL extension isn't installed, we can't fork. |
| 413 | - if (function_exists('pcntl_fork') === false) { |
|
| 413 | + if ( function_exists( 'pcntl_fork' ) === false ) { |
|
| 414 | 414 | $this->config->parallel = 1; |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | $lastDir = ''; |
| 418 | - $numFiles = count($todo); |
|
| 418 | + $numFiles = count( $todo ); |
|
| 419 | 419 | |
| 420 | - if ($this->config->parallel === 1) { |
|
| 420 | + if ( $this->config->parallel === 1 ) { |
|
| 421 | 421 | // Running normally. |
| 422 | 422 | $numProcessed = 0; |
| 423 | - foreach ($todo as $path => $file) { |
|
| 424 | - if ($file->ignored === false) { |
|
| 425 | - $currDir = dirname($path); |
|
| 426 | - if ($lastDir !== $currDir) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 423 | + foreach ( $todo as $path => $file ) { |
|
| 424 | + if ( $file->ignored === false ) { |
|
| 425 | + $currDir = dirname( $path ); |
|
| 426 | + if ( $lastDir !== $currDir ) { |
|
| 427 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 428 | + echo 'Changing into directory ' . Common::stripBasepath( $currDir, $this->config->basepath ) . PHP_EOL; |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | $lastDir = $currDir; |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | - $this->processFile($file); |
|
| 435 | - } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | - echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 434 | + $this->processFile( $file ); |
|
| 435 | + } else if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 436 | + echo 'Skipping ' . basename( $file->path ) . PHP_EOL; |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | $numProcessed++; |
| 440 | - $this->printProgress($file, $numFiles, $numProcessed); |
|
| 440 | + $this->printProgress( $file, $numFiles, $numProcessed ); |
|
| 441 | 441 | } |
| 442 | 442 | } else { |
| 443 | 443 | // Batching and forking. |
| 444 | - $childProcs = []; |
|
| 445 | - $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 444 | + $childProcs = [ ]; |
|
| 445 | + $numPerBatch = ceil( $numFiles / $this->config->parallel ); |
|
| 446 | 446 | |
| 447 | - for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | - $startAt = ($batch * $numPerBatch); |
|
| 449 | - if ($startAt >= $numFiles) { |
|
| 447 | + for ( $batch = 0; $batch < $this->config->parallel; $batch++ ) { |
|
| 448 | + $startAt = ( $batch * $numPerBatch ); |
|
| 449 | + if ( $startAt >= $numFiles ) { |
|
| 450 | 450 | break; |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | - $endAt = ($startAt + $numPerBatch); |
|
| 454 | - if ($endAt > $numFiles) { |
|
| 453 | + $endAt = ( $startAt + $numPerBatch ); |
|
| 454 | + if ( $endAt > $numFiles ) { |
|
| 455 | 455 | $endAt = $numFiles; |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 458 | + $childOutFilename = tempnam( sys_get_temp_dir(), 'phpcs-child' ); |
|
| 459 | 459 | $pid = pcntl_fork(); |
| 460 | - if ($pid === -1) { |
|
| 461 | - throw new RuntimeException('Failed to create child process'); |
|
| 462 | - } else if ($pid !== 0) { |
|
| 463 | - $childProcs[] = [ |
|
| 460 | + if ( $pid === -1 ) { |
|
| 461 | + throw new RuntimeException( 'Failed to create child process' ); |
|
| 462 | + } else if ( $pid !== 0 ) { |
|
| 463 | + $childProcs[ ] = [ |
|
| 464 | 464 | 'pid' => $pid, |
| 465 | 465 | 'out' => $childOutFilename, |
| 466 | 466 | ]; |
| 467 | 467 | } else { |
| 468 | 468 | // Move forward to the start of the batch. |
| 469 | 469 | $todo->rewind(); |
| 470 | - for ($i = 0; $i < $startAt; $i++) { |
|
| 470 | + for ( $i = 0; $i < $startAt; $i++ ) { |
|
| 471 | 471 | $todo->next(); |
| 472 | 472 | } |
| 473 | 473 | |
@@ -480,28 +480,28 @@ discard block |
||
| 480 | 480 | $this->reporter->totalFixed = 0; |
| 481 | 481 | |
| 482 | 482 | // Process the files. |
| 483 | - $pathsProcessed = []; |
|
| 483 | + $pathsProcessed = [ ]; |
|
| 484 | 484 | ob_start(); |
| 485 | - for ($i = $startAt; $i < $endAt; $i++) { |
|
| 485 | + for ( $i = $startAt; $i < $endAt; $i++ ) { |
|
| 486 | 486 | $path = $todo->key(); |
| 487 | 487 | $file = $todo->current(); |
| 488 | 488 | |
| 489 | - if ($file->ignored === true) { |
|
| 489 | + if ( $file->ignored === true ) { |
|
| 490 | 490 | continue; |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - $currDir = dirname($path); |
|
| 494 | - if ($lastDir !== $currDir) { |
|
| 495 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 496 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 493 | + $currDir = dirname( $path ); |
|
| 494 | + if ( $lastDir !== $currDir ) { |
|
| 495 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 496 | + echo 'Changing into directory ' . Common::stripBasepath( $currDir, $this->config->basepath ) . PHP_EOL; |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | $lastDir = $currDir; |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | - $this->processFile($file); |
|
| 502 | + $this->processFile( $file ); |
|
| 503 | 503 | |
| 504 | - $pathsProcessed[] = $path; |
|
| 504 | + $pathsProcessed[ ] = $path; |
|
| 505 | 505 | $todo->next(); |
| 506 | 506 | }//end for |
| 507 | 507 | |
@@ -518,60 +518,60 @@ discard block |
||
| 518 | 518 | 'totalFixed' => $this->reporter->totalFixed, |
| 519 | 519 | ]; |
| 520 | 520 | |
| 521 | - $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 522 | - $output .= var_export($childOutput, true); |
|
| 521 | + $output = '<' . '?php' . "\n" . ' $childOutput = '; |
|
| 522 | + $output .= var_export( $childOutput, true ); |
|
| 523 | 523 | $output .= ";\n\$debugOutput = "; |
| 524 | - $output .= var_export($debugOutput, true); |
|
| 524 | + $output .= var_export( $debugOutput, true ); |
|
| 525 | 525 | |
| 526 | - if ($this->config->cache === true) { |
|
| 527 | - $childCache = []; |
|
| 528 | - foreach ($pathsProcessed as $path) { |
|
| 529 | - $childCache[$path] = Cache::get($path); |
|
| 526 | + if ( $this->config->cache === true ) { |
|
| 527 | + $childCache = [ ]; |
|
| 528 | + foreach ( $pathsProcessed as $path ) { |
|
| 529 | + $childCache[ $path ] = Cache::get( $path ); |
|
| 530 | 530 | } |
| 531 | 531 | |
| 532 | 532 | $output .= ";\n\$childCache = "; |
| 533 | - $output .= var_export($childCache, true); |
|
| 533 | + $output .= var_export( $childCache, true ); |
|
| 534 | 534 | } |
| 535 | 535 | |
| 536 | - $output .= ";\n?".'>'; |
|
| 537 | - file_put_contents($childOutFilename, $output); |
|
| 538 | - exit($pid); |
|
| 536 | + $output .= ";\n?" . '>'; |
|
| 537 | + file_put_contents( $childOutFilename, $output ); |
|
| 538 | + exit( $pid ); |
|
| 539 | 539 | }//end if |
| 540 | 540 | }//end for |
| 541 | 541 | |
| 542 | - $success = $this->processChildProcs($childProcs); |
|
| 543 | - if ($success === false) { |
|
| 544 | - throw new RuntimeException('One or more child processes failed to run'); |
|
| 542 | + $success = $this->processChildProcs( $childProcs ); |
|
| 543 | + if ( $success === false ) { |
|
| 544 | + throw new RuntimeException( 'One or more child processes failed to run' ); |
|
| 545 | 545 | } |
| 546 | 546 | }//end if |
| 547 | 547 | |
| 548 | 548 | restore_error_handler(); |
| 549 | 549 | |
| 550 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 550 | + if ( PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 551 | 551 | && $this->config->interactive === false |
| 552 | 552 | && $this->config->showProgress === true |
| 553 | 553 | ) { |
| 554 | - echo PHP_EOL.PHP_EOL; |
|
| 554 | + echo PHP_EOL . PHP_EOL; |
|
| 555 | 555 | } |
| 556 | 556 | |
| 557 | - if ($this->config->cache === true) { |
|
| 557 | + if ( $this->config->cache === true ) { |
|
| 558 | 558 | Cache::save(); |
| 559 | 559 | } |
| 560 | 560 | |
| 561 | - $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 562 | - $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 561 | + $ignoreWarnings = Config::getConfigData( 'ignore_warnings_on_exit' ); |
|
| 562 | + $ignoreErrors = Config::getConfigData( 'ignore_errors_on_exit' ); |
|
| 563 | 563 | |
| 564 | - $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 565 | - if ($ignoreErrors !== null) { |
|
| 566 | - $ignoreErrors = (bool) $ignoreErrors; |
|
| 567 | - if ($ignoreErrors === true) { |
|
| 564 | + $return = ( $this->reporter->totalErrors + $this->reporter->totalWarnings ); |
|
| 565 | + if ( $ignoreErrors !== null ) { |
|
| 566 | + $ignoreErrors = (bool)$ignoreErrors; |
|
| 567 | + if ( $ignoreErrors === true ) { |
|
| 568 | 568 | $return -= $this->reporter->totalErrors; |
| 569 | 569 | } |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | - if ($ignoreWarnings !== null) { |
|
| 573 | - $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 574 | - if ($ignoreWarnings === true) { |
|
| 572 | + if ( $ignoreWarnings !== null ) { |
|
| 573 | + $ignoreWarnings = (bool)$ignoreWarnings; |
|
| 574 | + if ( $ignoreWarnings === true ) { |
|
| 575 | 575 | $return -= $this->reporter->totalWarnings; |
| 576 | 576 | } |
| 577 | 577 | } |
@@ -596,14 +596,14 @@ discard block |
||
| 596 | 596 | * @return void |
| 597 | 597 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
| 598 | 598 | */ |
| 599 | - public function handleErrors($code, $message, $file, $line) |
|
| 599 | + public function handleErrors( $code, $message, $file, $line ) |
|
| 600 | 600 | { |
| 601 | - if ((error_reporting() & $code) === 0) { |
|
| 601 | + if ( ( error_reporting() & $code ) === 0 ) { |
|
| 602 | 602 | // This type of error is being muted. |
| 603 | 603 | return true; |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | - throw new RuntimeException("$message in $file on line $line"); |
|
| 606 | + throw new RuntimeException( "$message in $file on line $line" ); |
|
| 607 | 607 | |
| 608 | 608 | }//end handleErrors() |
| 609 | 609 | |
@@ -616,12 +616,12 @@ discard block |
||
| 616 | 616 | * @return void |
| 617 | 617 | * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
| 618 | 618 | */ |
| 619 | - public function processFile($file) |
|
| 619 | + public function processFile( $file ) |
|
| 620 | 620 | { |
| 621 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 622 | - $startTime = microtime(true); |
|
| 623 | - echo 'Processing '.basename($file->path).' '; |
|
| 624 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 621 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 622 | + $startTime = microtime( true ); |
|
| 623 | + echo 'Processing ' . basename( $file->path ) . ' '; |
|
| 624 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
| 625 | 625 | echo PHP_EOL; |
| 626 | 626 | } |
| 627 | 627 | } |
@@ -629,33 +629,33 @@ discard block |
||
| 629 | 629 | try { |
| 630 | 630 | $file->process(); |
| 631 | 631 | |
| 632 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 633 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 634 | - if ($timeTaken < 1000) { |
|
| 635 | - $timeTaken = round($timeTaken); |
|
| 632 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
| 633 | + $timeTaken = ( ( microtime( true ) - $startTime ) * 1000 ); |
|
| 634 | + if ( $timeTaken < 1000 ) { |
|
| 635 | + $timeTaken = round( $timeTaken ); |
|
| 636 | 636 | echo "DONE in {$timeTaken}ms"; |
| 637 | 637 | } else { |
| 638 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 638 | + $timeTaken = round( ( $timeTaken / 1000 ), 2 ); |
|
| 639 | 639 | echo "DONE in $timeTaken secs"; |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 642 | + if ( PHP_CODESNIFFER_CBF === true ) { |
|
| 643 | 643 | $errors = $file->getFixableCount(); |
| 644 | - echo " ($errors fixable violations)".PHP_EOL; |
|
| 644 | + echo " ($errors fixable violations)" . PHP_EOL; |
|
| 645 | 645 | } else { |
| 646 | 646 | $errors = $file->getErrorCount(); |
| 647 | 647 | $warnings = $file->getWarningCount(); |
| 648 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 648 | + echo " ($errors errors, $warnings warnings)" . PHP_EOL; |
|
| 649 | 649 | } |
| 650 | 650 | } |
| 651 | - } catch (\Exception $e) { |
|
| 652 | - $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 653 | - $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 651 | + } catch ( \Exception $e ) { |
|
| 652 | + $error = 'An error occurred during processing; checking has been aborted. The error message was: ' . $e->getMessage(); |
|
| 653 | + $file->addErrorOnLine( $error, 1, 'Internal.Exception' ); |
|
| 654 | 654 | }//end try |
| 655 | 655 | |
| 656 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 656 | + $this->reporter->cacheFileReport( $file, $this->config ); |
|
| 657 | 657 | |
| 658 | - if ($this->config->interactive === true) { |
|
| 658 | + if ( $this->config->interactive === true ) { |
|
| 659 | 659 | /* |
| 660 | 660 | Running interactively. |
| 661 | 661 | Print the error report for the current file and then wait for user input. |
@@ -664,23 +664,23 @@ discard block |
||
| 664 | 664 | // Get current violations and then clear the list to make sure |
| 665 | 665 | // we only print violations for a single file each time. |
| 666 | 666 | $numErrors = null; |
| 667 | - while ($numErrors !== 0) { |
|
| 668 | - $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 669 | - if ($numErrors === 0) { |
|
| 667 | + while ( $numErrors !== 0 ) { |
|
| 668 | + $numErrors = ( $file->getErrorCount() + $file->getWarningCount() ); |
|
| 669 | + if ( $numErrors === 0 ) { |
|
| 670 | 670 | continue; |
| 671 | 671 | } |
| 672 | 672 | |
| 673 | - $this->reporter->printReport('full'); |
|
| 673 | + $this->reporter->printReport( 'full' ); |
|
| 674 | 674 | |
| 675 | 675 | echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
| 676 | - $input = fgets(STDIN); |
|
| 677 | - $input = trim($input); |
|
| 676 | + $input = fgets( STDIN ); |
|
| 677 | + $input = trim( $input ); |
|
| 678 | 678 | |
| 679 | - switch ($input) { |
|
| 679 | + switch ( $input ) { |
|
| 680 | 680 | case 's': |
| 681 | - break(2); |
|
| 681 | + break( 2 ); |
|
| 682 | 682 | case 'q': |
| 683 | - throw new DeepExitException('', 0); |
|
| 683 | + throw new DeepExitException( '', 0 ); |
|
| 684 | 684 | default: |
| 685 | 685 | // Repopulate the sniffs because some of them save their state |
| 686 | 686 | // and only clear it when the file changes, but we are rechecking |
@@ -688,7 +688,7 @@ discard block |
||
| 688 | 688 | $file->ruleset->populateTokenListeners(); |
| 689 | 689 | $file->reloadContent(); |
| 690 | 690 | $file->process(); |
| 691 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 691 | + $this->reporter->cacheFileReport( $file, $this->config ); |
|
| 692 | 692 | break; |
| 693 | 693 | } |
| 694 | 694 | }//end while |
@@ -710,59 +710,59 @@ discard block |
||
| 710 | 710 | * |
| 711 | 711 | * @return void |
| 712 | 712 | */ |
| 713 | - private function processChildProcs($childProcs) |
|
| 713 | + private function processChildProcs( $childProcs ) |
|
| 714 | 714 | { |
| 715 | 715 | $numProcessed = 0; |
| 716 | - $totalBatches = count($childProcs); |
|
| 716 | + $totalBatches = count( $childProcs ); |
|
| 717 | 717 | |
| 718 | 718 | $success = true; |
| 719 | 719 | |
| 720 | - while (count($childProcs) > 0) { |
|
| 721 | - foreach ($childProcs as $key => $procData) { |
|
| 722 | - $res = pcntl_waitpid($procData['pid'], $status, WNOHANG); |
|
| 723 | - if ($res === $procData['pid']) { |
|
| 724 | - if (file_exists($procData['out']) === true) { |
|
| 725 | - include $procData['out']; |
|
| 720 | + while ( count( $childProcs ) > 0 ) { |
|
| 721 | + foreach ( $childProcs as $key => $procData ) { |
|
| 722 | + $res = pcntl_waitpid( $procData[ 'pid' ], $status, WNOHANG ); |
|
| 723 | + if ( $res === $procData[ 'pid' ] ) { |
|
| 724 | + if ( file_exists( $procData[ 'out' ] ) === true ) { |
|
| 725 | + include $procData[ 'out' ]; |
|
| 726 | 726 | |
| 727 | - unlink($procData['out']); |
|
| 728 | - unset($childProcs[$key]); |
|
| 727 | + unlink( $procData[ 'out' ] ); |
|
| 728 | + unset( $childProcs[ $key ] ); |
|
| 729 | 729 | |
| 730 | 730 | $numProcessed++; |
| 731 | 731 | |
| 732 | - if (isset($childOutput) === false) { |
|
| 732 | + if ( isset( $childOutput ) === false ) { |
|
| 733 | 733 | // The child process died, so the run has failed. |
| 734 | - $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 735 | - $file->setErrorCounts(1, 0, 0, 0); |
|
| 736 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 734 | + $file = new DummyFile( null, $this->ruleset, $this->config ); |
|
| 735 | + $file->setErrorCounts( 1, 0, 0, 0 ); |
|
| 736 | + $this->printProgress( $file, $totalBatches, $numProcessed ); |
|
| 737 | 737 | $success = false; |
| 738 | 738 | continue; |
| 739 | 739 | } |
| 740 | 740 | |
| 741 | - $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 742 | - $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 743 | - $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 744 | - $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 745 | - $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 741 | + $this->reporter->totalFiles += $childOutput[ 'totalFiles' ]; |
|
| 742 | + $this->reporter->totalErrors += $childOutput[ 'totalErrors' ]; |
|
| 743 | + $this->reporter->totalWarnings += $childOutput[ 'totalWarnings' ]; |
|
| 744 | + $this->reporter->totalFixable += $childOutput[ 'totalFixable' ]; |
|
| 745 | + $this->reporter->totalFixed += $childOutput[ 'totalFixed' ]; |
|
| 746 | 746 | |
| 747 | - if (isset($debugOutput) === true) { |
|
| 747 | + if ( isset( $debugOutput ) === true ) { |
|
| 748 | 748 | echo $debugOutput; |
| 749 | 749 | } |
| 750 | 750 | |
| 751 | - if (isset($childCache) === true) { |
|
| 752 | - foreach ($childCache as $path => $cache) { |
|
| 753 | - Cache::set($path, $cache); |
|
| 751 | + if ( isset( $childCache ) === true ) { |
|
| 752 | + foreach ( $childCache as $path => $cache ) { |
|
| 753 | + Cache::set( $path, $cache ); |
|
| 754 | 754 | } |
| 755 | 755 | } |
| 756 | 756 | |
| 757 | 757 | // Fake a processed file so we can print progress output for the batch. |
| 758 | - $file = new DummyFile(null, $this->ruleset, $this->config); |
|
| 758 | + $file = new DummyFile( null, $this->ruleset, $this->config ); |
|
| 759 | 759 | $file->setErrorCounts( |
| 760 | - $childOutput['totalErrors'], |
|
| 761 | - $childOutput['totalWarnings'], |
|
| 762 | - $childOutput['totalFixable'], |
|
| 763 | - $childOutput['totalFixed'] |
|
| 760 | + $childOutput[ 'totalErrors' ], |
|
| 761 | + $childOutput[ 'totalWarnings' ], |
|
| 762 | + $childOutput[ 'totalFixable' ], |
|
| 763 | + $childOutput[ 'totalFixed' ] |
|
| 764 | 764 | ); |
| 765 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 765 | + $this->printProgress( $file, $totalBatches, $numProcessed ); |
|
| 766 | 766 | }//end if |
| 767 | 767 | }//end if |
| 768 | 768 | }//end foreach |
@@ -783,16 +783,16 @@ discard block |
||
| 783 | 783 | * |
| 784 | 784 | * @return void |
| 785 | 785 | */ |
| 786 | - public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 786 | + public function printProgress( File $file, $numFiles, $numProcessed ) |
|
| 787 | 787 | { |
| 788 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 788 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | 789 | || $this->config->showProgress === false |
| 790 | 790 | ) { |
| 791 | 791 | return; |
| 792 | 792 | } |
| 793 | 793 | |
| 794 | 794 | // Show progress information. |
| 795 | - if ($file->ignored === true) { |
|
| 795 | + if ( $file->ignored === true ) { |
|
| 796 | 796 | echo 'S'; |
| 797 | 797 | } else { |
| 798 | 798 | $errors = $file->getErrorCount(); |
@@ -800,28 +800,28 @@ discard block |
||
| 800 | 800 | $fixable = $file->getFixableCount(); |
| 801 | 801 | $fixed = $file->getFixedCount(); |
| 802 | 802 | |
| 803 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 803 | + if ( PHP_CODESNIFFER_CBF === true ) { |
|
| 804 | 804 | // Files with fixed errors or warnings are F (green). |
| 805 | 805 | // Files with unfixable errors or warnings are E (red). |
| 806 | 806 | // Files with no errors or warnings are . (black). |
| 807 | - if ($fixable > 0) { |
|
| 808 | - if ($this->config->colors === true) { |
|
| 807 | + if ( $fixable > 0 ) { |
|
| 808 | + if ( $this->config->colors === true ) { |
|
| 809 | 809 | echo "\033[31m"; |
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | echo 'E'; |
| 813 | 813 | |
| 814 | - if ($this->config->colors === true) { |
|
| 814 | + if ( $this->config->colors === true ) { |
|
| 815 | 815 | echo "\033[0m"; |
| 816 | 816 | } |
| 817 | - } else if ($fixed > 0) { |
|
| 818 | - if ($this->config->colors === true) { |
|
| 817 | + } else if ( $fixed > 0 ) { |
|
| 818 | + if ( $this->config->colors === true ) { |
|
| 819 | 819 | echo "\033[32m"; |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | 822 | echo 'F'; |
| 823 | 823 | |
| 824 | - if ($this->config->colors === true) { |
|
| 824 | + if ( $this->config->colors === true ) { |
|
| 825 | 825 | echo "\033[0m"; |
| 826 | 826 | } |
| 827 | 827 | } else { |
@@ -833,9 +833,9 @@ discard block |
||
| 833 | 833 | // Files with warnings are W (yellow). |
| 834 | 834 | // Files with fixable warnings are W (green). |
| 835 | 835 | // Files with no errors or warnings are . (black). |
| 836 | - if ($errors > 0) { |
|
| 837 | - if ($this->config->colors === true) { |
|
| 838 | - if ($fixable > 0) { |
|
| 836 | + if ( $errors > 0 ) { |
|
| 837 | + if ( $this->config->colors === true ) { |
|
| 838 | + if ( $fixable > 0 ) { |
|
| 839 | 839 | echo "\033[32m"; |
| 840 | 840 | } else { |
| 841 | 841 | echo "\033[31m"; |
@@ -844,12 +844,12 @@ discard block |
||
| 844 | 844 | |
| 845 | 845 | echo 'E'; |
| 846 | 846 | |
| 847 | - if ($this->config->colors === true) { |
|
| 847 | + if ( $this->config->colors === true ) { |
|
| 848 | 848 | echo "\033[0m"; |
| 849 | 849 | } |
| 850 | - } else if ($warnings > 0) { |
|
| 851 | - if ($this->config->colors === true) { |
|
| 852 | - if ($fixable > 0) { |
|
| 850 | + } else if ( $warnings > 0 ) { |
|
| 851 | + if ( $this->config->colors === true ) { |
|
| 852 | + if ( $fixable > 0 ) { |
|
| 853 | 853 | echo "\033[32m"; |
| 854 | 854 | } else { |
| 855 | 855 | echo "\033[33m"; |
@@ -858,7 +858,7 @@ discard block |
||
| 858 | 858 | |
| 859 | 859 | echo 'W'; |
| 860 | 860 | |
| 861 | - if ($this->config->colors === true) { |
|
| 861 | + if ( $this->config->colors === true ) { |
|
| 862 | 862 | echo "\033[0m"; |
| 863 | 863 | } |
| 864 | 864 | } else { |
@@ -868,17 +868,17 @@ discard block |
||
| 868 | 868 | }//end if |
| 869 | 869 | |
| 870 | 870 | $numPerLine = 60; |
| 871 | - if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 871 | + if ( $numProcessed !== $numFiles && ( $numProcessed % $numPerLine ) !== 0 ) { |
|
| 872 | 872 | return; |
| 873 | 873 | } |
| 874 | 874 | |
| 875 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | - $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | - if ($numProcessed === $numFiles && $numFiles > $numPerLine) { |
|
| 878 | - $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 875 | + $percent = round( ( $numProcessed / $numFiles ) * 100 ); |
|
| 876 | + $padding = ( strlen( $numFiles ) - strlen( $numProcessed ) ); |
|
| 877 | + if ( $numProcessed === $numFiles && $numFiles > $numPerLine ) { |
|
| 878 | + $padding += ( $numPerLine - ( $numFiles - ( floor( $numFiles / $numPerLine ) * $numPerLine ) ) ); |
|
| 879 | 879 | } |
| 880 | 880 | |
| 881 | - echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 881 | + echo str_repeat( ' ', $padding ) . " $numProcessed / $numFiles ($percent%)" . PHP_EOL; |
|
| 882 | 882 | |
| 883 | 883 | }//end printProgress() |
| 884 | 884 | |
@@ -21,8 +21,7 @@ discard block |
||
| 21 | 21 | use PHP_CodeSniffer\Exceptions\RuntimeException; |
| 22 | 22 | use PHP_CodeSniffer\Exceptions\DeepExitException; |
| 23 | 23 | |
| 24 | -class Runner |
|
| 25 | -{ |
|
| 24 | +class Runner { |
|
| 26 | 25 | |
| 27 | 26 | /** |
| 28 | 27 | * The config data for the run. |
@@ -51,8 +50,7 @@ discard block |
||
| 51 | 50 | * |
| 52 | 51 | * @return array |
| 53 | 52 | */ |
| 54 | - public function runPHPCS() |
|
| 55 | - { |
|
| 53 | + public function runPHPCS() { |
|
| 56 | 54 | try { |
| 57 | 55 | Util\Timing::startTiming(); |
| 58 | 56 | Runner::checkRequirements(); |
@@ -151,8 +149,7 @@ discard block |
||
| 151 | 149 | * |
| 152 | 150 | * @return array |
| 153 | 151 | */ |
| 154 | - public function runPHPCBF() |
|
| 155 | - { |
|
| 152 | + public function runPHPCBF() { |
|
| 156 | 153 | if (defined('PHP_CODESNIFFER_CBF') === false) { |
| 157 | 154 | define('PHP_CODESNIFFER_CBF', true); |
| 158 | 155 | } |
@@ -235,8 +232,7 @@ discard block |
||
| 235 | 232 | * @return array |
| 236 | 233 | * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
| 237 | 234 | */ |
| 238 | - public function checkRequirements() |
|
| 239 | - { |
|
| 235 | + public function checkRequirements() { |
|
| 240 | 236 | // Check the PHP version. |
| 241 | 237 | if (PHP_VERSION_ID < 50400) { |
| 242 | 238 | $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
@@ -283,8 +279,7 @@ discard block |
||
| 283 | 279 | * @return void |
| 284 | 280 | * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
| 285 | 281 | */ |
| 286 | - public function init() |
|
| 287 | - { |
|
| 282 | + public function init() { |
|
| 288 | 283 | if (defined('PHP_CODESNIFFER_CBF') === false) { |
| 289 | 284 | define('PHP_CODESNIFFER_CBF', false); |
| 290 | 285 | } |
@@ -346,8 +341,7 @@ discard block |
||
| 346 | 341 | * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
| 347 | 342 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
| 348 | 343 | */ |
| 349 | - private function run() |
|
| 350 | - { |
|
| 344 | + private function run() { |
|
| 351 | 345 | // The class that manages all reporters for the run. |
| 352 | 346 | $this->reporter = new Reporter($this->config); |
| 353 | 347 | |
@@ -596,8 +590,7 @@ discard block |
||
| 596 | 590 | * @return void |
| 597 | 591 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
| 598 | 592 | */ |
| 599 | - public function handleErrors($code, $message, $file, $line) |
|
| 600 | - { |
|
| 593 | + public function handleErrors($code, $message, $file, $line) { |
|
| 601 | 594 | if ((error_reporting() & $code) === 0) { |
| 602 | 595 | // This type of error is being muted. |
| 603 | 596 | return true; |
@@ -616,8 +609,7 @@ discard block |
||
| 616 | 609 | * @return void |
| 617 | 610 | * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
| 618 | 611 | */ |
| 619 | - public function processFile($file) |
|
| 620 | - { |
|
| 612 | + public function processFile($file) { |
|
| 621 | 613 | if (PHP_CODESNIFFER_VERBOSITY > 0) { |
| 622 | 614 | $startTime = microtime(true); |
| 623 | 615 | echo 'Processing '.basename($file->path).' '; |
@@ -710,8 +702,7 @@ discard block |
||
| 710 | 702 | * |
| 711 | 703 | * @return void |
| 712 | 704 | */ |
| 713 | - private function processChildProcs($childProcs) |
|
| 714 | - { |
|
| 705 | + private function processChildProcs($childProcs) { |
|
| 715 | 706 | $numProcessed = 0; |
| 716 | 707 | $totalBatches = count($childProcs); |
| 717 | 708 | |
@@ -783,8 +774,7 @@ discard block |
||
| 783 | 774 | * |
| 784 | 775 | * @return void |
| 785 | 776 | */ |
| 786 | - public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | - { |
|
| 777 | + public function printProgress(File $file, $numFiles, $numProcessed) { |
|
| 788 | 778 | if (PHP_CODESNIFFER_VERBOSITY > 0 |
| 789 | 779 | || $this->config->showProgress === false |
| 790 | 780 | ) { |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | /** |
| 40 | 40 | * Registers the tokens that this sniff wants to listen for. |
| 41 | 41 | * |
| 42 | - * @return int[] |
|
| 42 | + * @return integer[] |
|
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
@@ -36,99 +36,99 @@ |
||
| 36 | 36 | { |
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Registers the tokens that this sniff wants to listen for. |
|
| 41 | - * |
|
| 42 | - * @return int[] |
|
| 43 | - */ |
|
| 44 | - public function register() |
|
| 45 | - { |
|
| 46 | - return [T_FOR]; |
|
| 47 | - |
|
| 48 | - }//end register() |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Processes this test, when one of its tokens is encountered. |
|
| 53 | - * |
|
| 54 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 55 | - * @param int $stackPtr The position of the current token |
|
| 56 | - * in the stack passed in $tokens. |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function process(File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 62 | - $tokens = $phpcsFile->getTokens(); |
|
| 63 | - $token = $tokens[$stackPtr]; |
|
| 64 | - |
|
| 65 | - // Skip for-loop without body. |
|
| 66 | - if (isset($token['scope_opener']) === false) { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Find incrementors for outer loop. |
|
| 71 | - $outer = $this->findIncrementers($tokens, $token); |
|
| 72 | - |
|
| 73 | - // Skip if empty. |
|
| 74 | - if (count($outer) === 0) { |
|
| 75 | - return; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // Find nested for loops. |
|
| 79 | - $start = ++$token['scope_opener']; |
|
| 80 | - $end = --$token['scope_closer']; |
|
| 81 | - |
|
| 82 | - for (; $start <= $end; ++$start) { |
|
| 83 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
| 84 | - continue; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
| 88 | - $diff = array_intersect($outer, $inner); |
|
| 89 | - |
|
| 90 | - if (count($diff) !== 0) { |
|
| 91 | - $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
| 92 | - $data = [join(', ', $diff)]; |
|
| 93 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - }//end process() |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get all used variables in the incrementer part of a for statement. |
|
| 102 | - * |
|
| 103 | - * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
| 104 | - * @param array(string=>mixed) $token Current for loop token |
|
| 105 | - * |
|
| 106 | - * @return string[] List of all found incrementer variables. |
|
| 107 | - */ |
|
| 108 | - protected function findIncrementers(array $tokens, array $token) |
|
| 109 | - { |
|
| 110 | - // Skip invalid statement. |
|
| 111 | - if (isset($token['parenthesis_opener']) === false) { |
|
| 112 | - return []; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $start = ++$token['parenthesis_opener']; |
|
| 116 | - $end = --$token['parenthesis_closer']; |
|
| 117 | - |
|
| 118 | - $incrementers = []; |
|
| 119 | - $semicolons = 0; |
|
| 120 | - for ($next = $start; $next <= $end; ++$next) { |
|
| 121 | - $code = $tokens[$next]['code']; |
|
| 122 | - if ($code === T_SEMICOLON) { |
|
| 123 | - ++$semicolons; |
|
| 124 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
| 125 | - $incrementers[] = $tokens[$next]['content']; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return $incrementers; |
|
| 130 | - |
|
| 131 | - }//end findIncrementers() |
|
| 39 | + /** |
|
| 40 | + * Registers the tokens that this sniff wants to listen for. |
|
| 41 | + * |
|
| 42 | + * @return int[] |
|
| 43 | + */ |
|
| 44 | + public function register() |
|
| 45 | + { |
|
| 46 | + return [T_FOR]; |
|
| 47 | + |
|
| 48 | + }//end register() |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Processes this test, when one of its tokens is encountered. |
|
| 53 | + * |
|
| 54 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 55 | + * @param int $stackPtr The position of the current token |
|
| 56 | + * in the stack passed in $tokens. |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function process(File $phpcsFile, $stackPtr) |
|
| 61 | + { |
|
| 62 | + $tokens = $phpcsFile->getTokens(); |
|
| 63 | + $token = $tokens[$stackPtr]; |
|
| 64 | + |
|
| 65 | + // Skip for-loop without body. |
|
| 66 | + if (isset($token['scope_opener']) === false) { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Find incrementors for outer loop. |
|
| 71 | + $outer = $this->findIncrementers($tokens, $token); |
|
| 72 | + |
|
| 73 | + // Skip if empty. |
|
| 74 | + if (count($outer) === 0) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // Find nested for loops. |
|
| 79 | + $start = ++$token['scope_opener']; |
|
| 80 | + $end = --$token['scope_closer']; |
|
| 81 | + |
|
| 82 | + for (; $start <= $end; ++$start) { |
|
| 83 | + if ($tokens[$start]['code'] !== T_FOR) { |
|
| 84 | + continue; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
| 88 | + $diff = array_intersect($outer, $inner); |
|
| 89 | + |
|
| 90 | + if (count($diff) !== 0) { |
|
| 91 | + $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
| 92 | + $data = [join(', ', $diff)]; |
|
| 93 | + $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + }//end process() |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get all used variables in the incrementer part of a for statement. |
|
| 102 | + * |
|
| 103 | + * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
| 104 | + * @param array(string=>mixed) $token Current for loop token |
|
| 105 | + * |
|
| 106 | + * @return string[] List of all found incrementer variables. |
|
| 107 | + */ |
|
| 108 | + protected function findIncrementers(array $tokens, array $token) |
|
| 109 | + { |
|
| 110 | + // Skip invalid statement. |
|
| 111 | + if (isset($token['parenthesis_opener']) === false) { |
|
| 112 | + return []; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $start = ++$token['parenthesis_opener']; |
|
| 116 | + $end = --$token['parenthesis_closer']; |
|
| 117 | + |
|
| 118 | + $incrementers = []; |
|
| 119 | + $semicolons = 0; |
|
| 120 | + for ($next = $start; $next <= $end; ++$next) { |
|
| 121 | + $code = $tokens[$next]['code']; |
|
| 122 | + if ($code === T_SEMICOLON) { |
|
| 123 | + ++$semicolons; |
|
| 124 | + } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
| 125 | + $incrementers[] = $tokens[$next]['content']; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return $incrementers; |
|
| 130 | + |
|
| 131 | + }//end findIncrementers() |
|
| 132 | 132 | |
| 133 | 133 | |
| 134 | 134 | }//end class |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
| 46 | - return [T_FOR]; |
|
| 46 | + return [ T_FOR ]; |
|
| 47 | 47 | |
| 48 | 48 | }//end register() |
| 49 | 49 | |
@@ -57,40 +57,40 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return void |
| 59 | 59 | */ |
| 60 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 61 | 61 | { |
| 62 | 62 | $tokens = $phpcsFile->getTokens(); |
| 63 | - $token = $tokens[$stackPtr]; |
|
| 63 | + $token = $tokens[ $stackPtr ]; |
|
| 64 | 64 | |
| 65 | 65 | // Skip for-loop without body. |
| 66 | - if (isset($token['scope_opener']) === false) { |
|
| 66 | + if ( isset( $token[ 'scope_opener' ] ) === false ) { |
|
| 67 | 67 | return; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Find incrementors for outer loop. |
| 71 | - $outer = $this->findIncrementers($tokens, $token); |
|
| 71 | + $outer = $this->findIncrementers( $tokens, $token ); |
|
| 72 | 72 | |
| 73 | 73 | // Skip if empty. |
| 74 | - if (count($outer) === 0) { |
|
| 74 | + if ( count( $outer ) === 0 ) { |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // Find nested for loops. |
| 79 | - $start = ++$token['scope_opener']; |
|
| 80 | - $end = --$token['scope_closer']; |
|
| 79 | + $start = ++$token[ 'scope_opener' ]; |
|
| 80 | + $end = --$token[ 'scope_closer' ]; |
|
| 81 | 81 | |
| 82 | - for (; $start <= $end; ++$start) { |
|
| 83 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
| 82 | + for ( ; $start <= $end; ++$start ) { |
|
| 83 | + if ( $tokens[ $start ][ 'code' ] !== T_FOR ) { |
|
| 84 | 84 | continue; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
| 88 | - $diff = array_intersect($outer, $inner); |
|
| 87 | + $inner = $this->findIncrementers( $tokens, $tokens[ $start ] ); |
|
| 88 | + $diff = array_intersect( $outer, $inner ); |
|
| 89 | 89 | |
| 90 | - if (count($diff) !== 0) { |
|
| 90 | + if ( count( $diff ) !== 0 ) { |
|
| 91 | 91 | $error = 'Loop incrementor (%s) jumbling with inner loop'; |
| 92 | - $data = [join(', ', $diff)]; |
|
| 93 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
| 92 | + $data = [ join( ', ', $diff ) ]; |
|
| 93 | + $phpcsFile->addWarning( $error, $stackPtr, 'Found', $data ); |
|
| 94 | 94 | } |
| 95 | 95 | } |
| 96 | 96 | |
@@ -105,24 +105,24 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @return string[] List of all found incrementer variables. |
| 107 | 107 | */ |
| 108 | - protected function findIncrementers(array $tokens, array $token) |
|
| 108 | + protected function findIncrementers( array $tokens, array $token ) |
|
| 109 | 109 | { |
| 110 | 110 | // Skip invalid statement. |
| 111 | - if (isset($token['parenthesis_opener']) === false) { |
|
| 112 | - return []; |
|
| 111 | + if ( isset( $token[ 'parenthesis_opener' ] ) === false ) { |
|
| 112 | + return [ ]; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - $start = ++$token['parenthesis_opener']; |
|
| 116 | - $end = --$token['parenthesis_closer']; |
|
| 115 | + $start = ++$token[ 'parenthesis_opener' ]; |
|
| 116 | + $end = --$token[ 'parenthesis_closer' ]; |
|
| 117 | 117 | |
| 118 | - $incrementers = []; |
|
| 118 | + $incrementers = [ ]; |
|
| 119 | 119 | $semicolons = 0; |
| 120 | - for ($next = $start; $next <= $end; ++$next) { |
|
| 121 | - $code = $tokens[$next]['code']; |
|
| 122 | - if ($code === T_SEMICOLON) { |
|
| 120 | + for ( $next = $start; $next <= $end; ++$next ) { |
|
| 121 | + $code = $tokens[ $next ][ 'code' ]; |
|
| 122 | + if ( $code === T_SEMICOLON ) { |
|
| 123 | 123 | ++$semicolons; |
| 124 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
| 125 | - $incrementers[] = $tokens[$next]['content']; |
|
| 124 | + } else if ( $semicolons === 2 && $code === T_VARIABLE ) { |
|
| 125 | + $incrementers[ ] = $tokens[ $next ][ 'content' ]; |
|
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | 128 | |
@@ -32,8 +32,7 @@ discard block |
||
| 32 | 32 | use PHP_CodeSniffer\Sniffs\Sniff; |
| 33 | 33 | use PHP_CodeSniffer\Files\File; |
| 34 | 34 | |
| 35 | -class JumbledIncrementerSniff implements Sniff |
|
| 36 | -{ |
|
| 35 | +class JumbledIncrementerSniff implements Sniff { |
|
| 37 | 36 | |
| 38 | 37 | |
| 39 | 38 | /** |
@@ -41,8 +40,7 @@ discard block |
||
| 41 | 40 | * |
| 42 | 41 | * @return int[] |
| 43 | 42 | */ |
| 44 | - public function register() |
|
| 45 | - { |
|
| 43 | + public function register() { |
|
| 46 | 44 | return [T_FOR]; |
| 47 | 45 | |
| 48 | 46 | }//end register() |
@@ -57,8 +55,7 @@ discard block |
||
| 57 | 55 | * |
| 58 | 56 | * @return void |
| 59 | 57 | */ |
| 60 | - public function process(File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 62 | 59 | $tokens = $phpcsFile->getTokens(); |
| 63 | 60 | $token = $tokens[$stackPtr]; |
| 64 | 61 | |
@@ -105,8 +102,7 @@ discard block |
||
| 105 | 102 | * |
| 106 | 103 | * @return string[] List of all found incrementer variables. |
| 107 | 104 | */ |
| 108 | - protected function findIncrementers(array $tokens, array $token) |
|
| 109 | - { |
|
| 105 | + protected function findIncrementers(array $tokens, array $token) { |
|
| 110 | 106 | // Skip invalid statement. |
| 111 | 107 | if (isset($token['parenthesis_opener']) === false) { |
| 112 | 108 | return []; |