| Conditions | 26 |
| Paths | 1613 |
| Total Lines | 120 |
| Code Lines | 51 |
| Lines | 11 |
| Ratio | 9.17 % |
| 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 |
||
| 56 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 57 | { |
||
| 58 | $tokens = $phpcsFile->getTokens(); |
||
| 59 | |||
| 60 | // Ignore default value assignments in function definitions. |
||
| 61 | $function = $phpcsFile->findPrevious(array(T_FUNCTION, T_CLOSURE), ($stackPtr - 1), null, false, null, true); |
||
| 62 | View Code Duplication | if ($function !== false) { |
|
| 63 | $opener = $tokens[$function]['parenthesis_opener']; |
||
| 64 | $closer = $tokens[$function]['parenthesis_closer']; |
||
| 65 | if ($opener < $stackPtr && $closer > $stackPtr) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /* |
||
| 71 | The general rule is: |
||
| 72 | Find an equal sign and go backwards along the line. If you hit an |
||
| 73 | end bracket, skip to the opening bracket. When you find a variable, |
||
| 74 | stop. That variable must be the first non-empty token on the line |
||
| 75 | or in the statement. If not, throw an error. |
||
| 76 | */ |
||
| 77 | |||
| 78 | for ($varToken = ($stackPtr - 1); $varToken >= 0; $varToken--) { |
||
| 79 | // Skip brackets. |
||
| 80 | if (isset($tokens[$varToken]['parenthesis_opener']) === true && $tokens[$varToken]['parenthesis_opener'] < $varToken) { |
||
| 81 | $varToken = $tokens[$varToken]['parenthesis_opener']; |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | if (isset($tokens[$varToken]['bracket_opener']) === true) { |
||
| 86 | $varToken = $tokens[$varToken]['bracket_opener']; |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($tokens[$varToken]['code'] === T_SEMICOLON) { |
||
| 91 | // We've reached the next statement, so we |
||
| 92 | // didn't find a variable. |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($tokens[$varToken]['code'] === T_VARIABLE) { |
||
| 97 | // We found our variable. |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | }//end for |
||
| 101 | |||
| 102 | if ($varToken <= 0) { |
||
| 103 | // Didn't find a variable. |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Deal with this type of variable: self::$var by setting the var |
||
| 108 | // token to be "self" rather than "$var". |
||
| 109 | if ($tokens[($varToken - 1)]['code'] === T_DOUBLE_COLON) { |
||
| 110 | $varToken = ($varToken - 2); |
||
| 111 | } |
||
| 112 | |||
| 113 | // Deal with this type of variable: $obj->$var by setting the var |
||
| 114 | // token to be "$obj" rather than "$var". |
||
| 115 | if ($tokens[($varToken - 1)]['code'] === T_OBJECT_OPERATOR) { |
||
| 116 | $varToken = ($varToken - 2); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Deal with this type of variable: $$var by setting the var |
||
| 120 | // token to be "$" rather than "$var". |
||
| 121 | if ($tokens[($varToken - 1)]['content'] === '$') { |
||
| 122 | $varToken--; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Ignore member var definitions. |
||
| 126 | $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($varToken - 1), null, true); |
||
| 127 | if (isset(PHP_CodeSniffer_Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) { |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($tokens[$prev]['code'] === T_STATIC) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Make sure this variable is the first thing in the statement. |
||
| 136 | $varLine = $tokens[$varToken]['line']; |
||
| 137 | $prevLine = 0; |
||
| 138 | for ($i = ($varToken - 1); $i >= 0; $i--) { |
||
| 139 | if ($tokens[$i]['code'] === T_SEMICOLON) { |
||
| 140 | // We reached the end of the statement. |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($tokens[$i]['code'] === T_INLINE_THEN) { |
||
| 145 | // We reached the end of the inline THEN statement. |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($tokens[$i]['code'] === T_INLINE_ELSE) { |
||
| 150 | // We reached the end of the inline ELSE statement. |
||
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | View Code Duplication | if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$i]['code']]) === false) { |
|
| 155 | $prevLine = $tokens[$i]['line']; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | }//end for |
||
| 159 | |||
| 160 | // Ignore the first part of FOR loops as we are allowed to |
||
| 161 | // assign variables there even though the variable is not the |
||
| 162 | // first thing on the line. Also ignore WHILE loops. |
||
| 163 | if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS && isset($tokens[$i]['parenthesis_owner']) === true) { |
||
| 164 | $owner = $tokens[$i]['parenthesis_owner']; |
||
| 165 | if ($tokens[$owner]['code'] === T_FOR || $tokens[$owner]['code'] === T_WHILE) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($prevLine === $varLine) { |
||
| 171 | $error = 'Assignments must be the first block of code on a line'; |
||
| 172 | $phpcsFile->addError($error, $stackPtr, 'Found'); |
||
| 173 | } |
||
| 174 | |||
| 175 | }//end process() |
||
| 176 | |||
| 179 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.