| Conditions | 31 |
| Paths | 16401 |
| Total Lines | 111 |
| Code Lines | 67 |
| Lines | 20 |
| Ratio | 18.02 % |
| 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 |
||
| 26 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
||
| 27 | $wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON]; |
||
| 28 | |||
| 29 | $tokens = $phpcsFile->getTokens(); |
||
| 30 | |||
| 31 | $tokenContent = $tokens[$stackPtr]['content']; |
||
| 32 | if (strtolower($tokenContent) !== 'is_null') { |
||
| 33 | return; |
||
| 34 | } |
||
| 35 | |||
| 36 | $previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 37 | if (!$previous || in_array($tokens[$previous]['code'], $wrongTokens)) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | $openingBraceIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 42 | if (!$openingBraceIndex || $tokens[$openingBraceIndex]['type'] !== 'T_OPEN_PARENTHESIS') { |
||
|
|
|||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | $closingBraceIndex = $tokens[$openingBraceIndex]['parenthesis_closer']; |
||
| 47 | |||
| 48 | $error = $tokenContent . '() found, should be strict === null check.'; |
||
| 49 | |||
| 50 | $possibleCastIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 51 | $negated = false; |
||
| 52 | if ($possibleCastIndex && $tokens[$possibleCastIndex]['code'] === T_BOOLEAN_NOT) { |
||
| 53 | $negated = true; |
||
| 54 | } |
||
| 55 | // We dont want to fix double !! |
||
| 56 | if ($negated) { |
||
| 57 | $anotherPossibleCastIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($possibleCastIndex - 1), null, true); |
||
| 58 | if ($tokens[$anotherPossibleCastIndex]['code'] === T_BOOLEAN_NOT) { |
||
| 59 | $phpcsFile->addError($error, $stackPtr); |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | // We don't want to fix stuff with bad inline assignment |
||
| 65 | if ($this->contains($phpcsFile, 'T_EQUAL', $openingBraceIndex + 1, $closingBraceIndex - 1)) { |
||
| 66 | $phpcsFile->addError($error, $stackPtr); |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | $beginningIndex = $negated ? $possibleCastIndex : $stackPtr; |
||
| 71 | $endIndex = $closingBraceIndex; |
||
| 72 | |||
| 73 | $fix = $phpcsFile->addFixableError($error, $stackPtr); |
||
| 74 | if ($fix) { |
||
| 75 | $needsBrackets = $this->needsBrackets($phpcsFile, $openingBraceIndex, $closingBraceIndex); |
||
| 76 | $leadingComparison = $this->hasLeadingComparison($phpcsFile, $beginningIndex); |
||
| 77 | $trailingComparison = $this->hasTrailingComparison($phpcsFile, $closingBraceIndex); |
||
| 78 | |||
| 79 | View Code Duplication | if ($leadingComparison) { |
|
| 80 | $possibleBeginningIndex = $this->findUnnecessaryLeadingComparisonStart($phpcsFile, $beginningIndex); |
||
| 81 | if ($possibleBeginningIndex !== null) { |
||
| 82 | $beginningIndex = $possibleBeginningIndex; |
||
| 83 | $leadingComparison = false; |
||
| 84 | if ($tokens[$beginningIndex]['code'] === T_FALSE) { |
||
| 85 | $negated = !$negated; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | View Code Duplication | if ($trailingComparison) { |
|
| 91 | $possibleEndIndex = $this->findUnnecessaryLeadingComparisonStart($phpcsFile, $endIndex); |
||
| 92 | if ($possibleEndIndex !== null) { |
||
| 93 | $endIndex = $possibleEndIndex; |
||
| 94 | $trailingComparison = false; |
||
| 95 | if ($tokens[$endIndex]['code'] === T_FALSE) { |
||
| 96 | $negated = !$negated; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!$needsBrackets && ($leadingComparison || $this->leadRequiresBrackets($phpcsFile, $beginningIndex))) { |
||
| 102 | $needsBrackets = true; |
||
| 103 | } |
||
| 104 | if (!$needsBrackets && $trailingComparison) { |
||
| 105 | $needsBrackets = true; |
||
| 106 | } |
||
| 107 | |||
| 108 | $comparisonString = ' ' . ($negated ? '!' : '=') . '== null'; |
||
| 109 | |||
| 110 | $phpcsFile->fixer->beginChangeset(); |
||
| 111 | |||
| 112 | if ($negated) { |
||
| 113 | //$phpcsFile->fixer->replaceToken($possibleCastIndex, ''); |
||
| 114 | } |
||
| 115 | if ($beginningIndex !== $stackPtr) { |
||
| 116 | for ($i = $beginningIndex; $i < $stackPtr; $i++) { |
||
| 117 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | if ($endIndex !== $closingBraceIndex) { |
||
| 121 | for ($i = $endIndex; $i > $closingBraceIndex; $i--) { |
||
| 122 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $phpcsFile->fixer->replaceToken($stackPtr, ''); |
||
| 127 | if (!$needsBrackets) { |
||
| 128 | $phpcsFile->fixer->replaceToken($openingBraceIndex, ''); |
||
| 129 | $phpcsFile->fixer->replaceToken($closingBraceIndex, $comparisonString); |
||
| 130 | } else { |
||
| 131 | $phpcsFile->fixer->replaceToken($closingBraceIndex, $comparisonString . ')'); |
||
| 132 | } |
||
| 133 | |||
| 134 | $phpcsFile->fixer->endChangeset(); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 257 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: