| Conditions | 21 |
| Paths | 225 |
| Total Lines | 126 |
| Code Lines | 60 |
| Lines | 7 |
| Ratio | 5.56 % |
| 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 |
||
| 57 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 58 | { |
||
| 59 | if ($this->supportsAbove('7.0') === false) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $tokens = $phpcsFile->getTokens(); |
||
| 64 | |||
| 65 | if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY |
||
| 66 | && $this->isShortList($phpcsFile, $stackPtr) === false |
||
| 67 | ) { |
||
| 68 | // Short array, not short list. |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($tokens[$stackPtr]['code'] === T_LIST) { |
||
| 73 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 74 | View Code Duplication | if ($nextNonEmpty === false |
|
| 75 | || $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS |
||
| 76 | || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
||
| 77 | ) { |
||
| 78 | // Parse error or live coding. |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | $opener = $nextNonEmpty; |
||
| 83 | $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
||
| 84 | } else { |
||
| 85 | // Short list syntax. |
||
| 86 | $opener = $stackPtr; |
||
| 87 | |||
| 88 | if (isset($tokens[$stackPtr]['bracket_closer'])) { |
||
| 89 | $closer = $tokens[$stackPtr]['bracket_closer']; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | if (isset($opener, $closer) === false) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | /* |
||
| 98 | * OK, so we have the opener & closer, now we need to check all the variables in the |
||
| 99 | * list() to see if there are duplicates as that's the problem. |
||
| 100 | */ |
||
| 101 | $hasVars = $phpcsFile->findNext(array(T_VARIABLE, T_DOLLAR), ($opener + 1), $closer); |
||
|
|
|||
| 102 | if ($hasVars === false) { |
||
| 103 | // Empty list, not our concern. |
||
| 104 | return ($closer + 1); |
||
| 105 | } |
||
| 106 | |||
| 107 | // Set the variable delimiters based on the list type being examined. |
||
| 108 | $stopPoints = array(T_COMMA); |
||
| 109 | if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) { |
||
| 110 | $stopPoints[] = T_CLOSE_SHORT_ARRAY; |
||
| 111 | } else { |
||
| 112 | $stopPoints[] = T_CLOSE_PARENTHESIS; |
||
| 113 | } |
||
| 114 | |||
| 115 | $listVars = array(); |
||
| 116 | $emptyTokens = array_flip(\PHP_CodeSniffer_Tokens::$emptyTokens); |
||
| 117 | $lastStopPoint = $opener; |
||
| 118 | |||
| 119 | /* |
||
| 120 | * Create a list of all variables used within the `list()` construct. |
||
| 121 | * We're not concerned with whether these are nested or not, as any duplicate |
||
| 122 | * variable name used will be problematic, independent of nesting. |
||
| 123 | */ |
||
| 124 | do { |
||
| 125 | $nextStopPoint = $phpcsFile->findNext($stopPoints, ($lastStopPoint + 1), $closer); |
||
| 126 | if ($nextStopPoint === false) { |
||
| 127 | $nextStopPoint = $closer; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Also detect this in PHP 7.1 keyed lists. |
||
| 131 | $hasDoubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($lastStopPoint + 1), $nextStopPoint); |
||
| 132 | if ($hasDoubleArrow !== false) { |
||
| 133 | $lastStopPoint = $hasDoubleArrow; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Find the start of the variable, allowing for variable variables. |
||
| 137 | $nextStartPoint = $phpcsFile->findNext(array(T_VARIABLE, T_DOLLAR), ($lastStopPoint + 1), $nextStopPoint); |
||
| 138 | if ($nextStartPoint === false) { |
||
| 139 | // Skip past empty bits in the list, i.e. `list( $a, , ,)`. |
||
| 140 | $lastStopPoint = $nextStopPoint; |
||
| 141 | continue; |
||
| 142 | } |
||
| 143 | |||
| 144 | /* |
||
| 145 | * Gather the content of all non-empty tokens to determine the "variable name". |
||
| 146 | * Variable name in this context includes array or object property syntaxes, such |
||
| 147 | * as `$a['name']` and `$b->property`. |
||
| 148 | */ |
||
| 149 | $varContent = ''; |
||
| 150 | |||
| 151 | for ($i = $nextStartPoint; $i < $nextStopPoint; $i++) { |
||
| 152 | if (isset($emptyTokens[$tokens[$i]['code']])) { |
||
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | $varContent .= $tokens[$i]['content']; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($varContent !== '') { |
||
| 160 | $listVars[] = $varContent; |
||
| 161 | } |
||
| 162 | |||
| 163 | $lastStopPoint = $nextStopPoint; |
||
| 164 | |||
| 165 | } while ($lastStopPoint < $closer); |
||
| 166 | |||
| 167 | if (empty($listVars)) { |
||
| 168 | // Shouldn't be possible, but just in case. |
||
| 169 | return ($closer + 1); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Verify that all variables used in the list() construct are unique. |
||
| 173 | if (count($listVars) !== count(array_unique($listVars))) { |
||
| 174 | $phpcsFile->addError( |
||
| 175 | 'list() will assign variable from left-to-right since PHP 7.0. Ensure all variables in list() are unique to prevent unexpected results.', |
||
| 176 | $stackPtr, |
||
| 177 | 'Affected' |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | return ($closer + 1); |
||
| 182 | } |
||
| 183 | } |
||
| 184 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: