| Conditions | 31 |
| Paths | 139 |
| Total Lines | 126 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 133 | public function process(File $phpcsFile, $stackPtr) |
||
| 134 | { |
||
| 135 | if ($this->supportsAbove('7.0') === false) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | $tokens = $phpcsFile->getTokens(); |
||
| 140 | $tokenCode = $tokens[$stackPtr]['code']; |
||
| 141 | $tokenType = $tokens[$stackPtr]['type']; |
||
| 142 | $tokenContentLc = strtolower($tokens[$stackPtr]['content']); |
||
| 143 | |||
| 144 | // For string tokens we only care about 'trait' as that is the only one |
||
| 145 | // which may not be correctly recognized as it's own token. |
||
| 146 | // This only happens in older versions of PHP where the token doesn't exist yet as a keyword. |
||
| 147 | if ($tokenCode === \T_STRING && $tokenContentLc !== 'trait') { |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | |||
| 151 | if (\in_array($tokenType, array('T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) { |
||
| 152 | // Check for the declared name being a name which is not tokenized as T_STRING. |
||
| 153 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 154 | if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
||
| 155 | $name = $tokens[$nextNonEmpty]['content']; |
||
| 156 | } else { |
||
| 157 | // Get the declared name if it's a T_STRING. |
||
| 158 | $name = $phpcsFile->getDeclarationName($stackPtr); |
||
| 159 | } |
||
| 160 | unset($nextNonEmpty); |
||
| 161 | |||
| 162 | if (isset($name) === false || \is_string($name) === false || $name === '') { |
||
| 163 | return; |
||
| 164 | } |
||
| 165 | |||
| 166 | $nameLc = strtolower($name); |
||
| 167 | if (isset($this->allForbiddenNames[$nameLc]) === false) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | } elseif ($tokenCode === \T_NAMESPACE) { |
||
| 172 | $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr); |
||
| 173 | |||
| 174 | if ($namespaceName === false || $namespaceName === '') { |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | $namespaceParts = explode('\\', $namespaceName); |
||
| 179 | foreach ($namespaceParts as $namespacePart) { |
||
| 180 | $partLc = strtolower($namespacePart); |
||
| 181 | if (isset($this->allForbiddenNames[$partLc]) === true) { |
||
| 182 | $name = $namespacePart; |
||
| 183 | $nameLc = $partLc; |
||
| 184 | break; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } elseif ($tokenCode === \T_STRING) { |
||
| 188 | // Traits which are not yet tokenized as T_TRAIT. |
||
| 189 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 190 | if ($nextNonEmpty === false) { |
||
| 191 | return; |
||
| 192 | } |
||
| 193 | |||
| 194 | $nextNonEmptyCode = $tokens[$nextNonEmpty]['code']; |
||
| 195 | |||
| 196 | if ($nextNonEmptyCode !== \T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) { |
||
| 197 | $name = $tokens[$nextNonEmpty]['content']; |
||
| 198 | $nameLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
| 199 | } elseif ($nextNonEmptyCode === \T_STRING) { |
||
| 200 | $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_OPEN_CURLY_BRACKET), ($stackPtr + 1)); |
||
| 201 | if ($endOfStatement === false) { |
||
| 202 | return; |
||
| 203 | } |
||
| 204 | |||
| 205 | do { |
||
| 206 | $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
| 207 | |||
| 208 | if (isset($this->allForbiddenNames[$nextNonEmptyLc]) === true) { |
||
| 209 | $name = $tokens[$nextNonEmpty]['content']; |
||
| 210 | $nameLc = $nextNonEmptyLc; |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | |||
| 214 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true); |
||
| 215 | } while ($nextNonEmpty !== false); |
||
| 216 | } |
||
| 217 | unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement); |
||
| 218 | } |
||
| 219 | |||
| 220 | if (isset($name, $nameLc) === false) { |
||
| 221 | return; |
||
| 222 | } |
||
| 223 | |||
| 224 | // Still here, so this is one of the reserved words. |
||
| 225 | // Build up the error message. |
||
| 226 | $error = "'%s' is a"; |
||
| 227 | $isError = null; |
||
| 228 | $errorCode = $this->stringToErrorCode($nameLc) . 'Found'; |
||
| 229 | $data = array( |
||
| 230 | $nameLc, |
||
| 231 | ); |
||
| 232 | |||
| 233 | if (isset($this->softReservedNames[$nameLc]) === true |
||
| 234 | && $this->supportsAbove($this->softReservedNames[$nameLc]) === true |
||
| 235 | ) { |
||
| 236 | $error .= ' soft reserved keyword as of PHP version %s'; |
||
| 237 | $isError = false; |
||
| 238 | $data[] = $this->softReservedNames[$nameLc]; |
||
| 239 | } |
||
| 240 | |||
| 241 | if (isset($this->forbiddenNames[$nameLc]) === true |
||
| 242 | && $this->supportsAbove($this->forbiddenNames[$nameLc]) === true |
||
| 243 | ) { |
||
| 244 | if (isset($isError) === true) { |
||
| 245 | $error .= ' and a'; |
||
| 246 | } |
||
| 247 | $error .= ' reserved keyword as of PHP version %s'; |
||
| 248 | $isError = true; |
||
| 249 | $data[] = $this->forbiddenNames[$nameLc]; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (isset($isError) === true) { |
||
| 253 | $error .= ' and should not be used to name a class, interface or trait or as part of a namespace (%s)'; |
||
| 254 | $data[] = $tokens[$stackPtr]['type']; |
||
| 255 | |||
| 256 | $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 |