| Conditions | 16 |
| Paths | 25 |
| Total Lines | 76 |
| Lines | 5 |
| Ratio | 6.58 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 112 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 113 | { |
||
| 114 | $tokens = $phpcsFile->getTokens(); |
||
| 115 | $tokenCode = $tokens[$stackPtr]['code']; |
||
| 116 | $tokenContentLc = strtolower($tokens[$stackPtr]['content']); |
||
| 117 | $isString = false; |
||
| 118 | |||
| 119 | /* |
||
| 120 | * For string tokens we only care if the string is a reserved word used |
||
| 121 | * as a function. This only happens in older versions of PHP where the |
||
| 122 | * token doesn't exist yet for that keyword or in later versions when the |
||
| 123 | * token is used in a method invocation. |
||
| 124 | */ |
||
| 125 | if ($tokenCode === T_STRING |
||
| 126 | && (isset($this->targetedStringTokens[$tokenContentLc]) === false) |
||
| 127 | ) { |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($tokenCode === T_STRING) { |
||
| 132 | $isString = true; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Make sure this is a function call. |
||
| 136 | $next = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 137 | if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
||
| 138 | // Not a function call. |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | // This sniff isn't concerned about function declarations. |
||
| 143 | $prev = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 144 | if ($prev !== false && $tokens[$prev]['code'] === T_FUNCTION) { |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | /* |
||
| 149 | * Deal with PHP 7 relaxing the rules. |
||
| 150 | * "As of PHP 7.0.0 these keywords are allowed as property, constant, and method names |
||
| 151 | * of classes, interfaces and traits...", i.e. they can be invoked as a method call. |
||
| 152 | * |
||
| 153 | * Only needed for those keywords which we sniff out via T_STRING. |
||
| 154 | */ |
||
| 155 | View Code Duplication | if (($tokens[$prev]['code'] === T_OBJECT_OPERATOR || $tokens[$prev]['code'] === T_DOUBLE_COLON) |
|
| 156 | && $this->supportsBelow('5.6') === false |
||
| 157 | ) { |
||
| 158 | return; |
||
| 159 | } |
||
| 160 | |||
| 161 | // For the word catch, it is valid to have an open parenthesis |
||
| 162 | // after it, but only if it is preceded by a right curly brace. |
||
| 163 | if ($tokenCode === T_CATCH) { |
||
| 164 | if ($prev !== false && $tokens[$prev]['code'] === T_CLOSE_CURLY_BRACKET) { |
||
| 165 | // Ok, it's fine. |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($isString === true) { |
||
| 171 | $version = $this->targetedStringTokens[$tokenContentLc]; |
||
| 172 | } else { |
||
| 173 | $version = $this->targetedTokens[$tokenCode]; |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($this->supportsAbove($version)) { |
||
| 177 | $error = "'%s' is a reserved keyword introduced in PHP version %s and cannot be invoked as a function (%s)"; |
||
| 178 | $errorCode = $this->stringToErrorCode($tokenContentLc).'Found'; |
||
| 179 | $data = array( |
||
| 180 | $tokenContentLc, |
||
| 181 | $version, |
||
| 182 | $tokens[$stackPtr]['type'], |
||
| 183 | ); |
||
| 184 | |||
| 185 | $phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
||
| 186 | } |
||
| 187 | }//end process() |
||
| 188 | |||
| 190 |