| Conditions | 23 |
| Paths | 246 |
| Total Lines | 125 |
| Code Lines | 55 |
| Lines | 7 |
| Ratio | 5.6 % |
| 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 |
||
| 62 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 63 | { |
||
| 64 | if ($this->supportsBelow('5.2') === false) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | $tokens = $phpcsFile->getTokens(); |
||
| 69 | $isNowdoc = false; |
||
| 70 | $isHeredoc = false; |
||
| 71 | |||
| 72 | switch ($tokens[$stackPtr]['type']) { |
||
| 73 | case 'T_START_NOWDOC': |
||
| 74 | case 'T_END_NOWDOC': |
||
| 75 | $isNowdoc = true; |
||
| 76 | break; |
||
| 77 | |||
| 78 | case 'T_START_HEREDOC': |
||
| 79 | /* |
||
| 80 | * If we have a heredoc opener, make sure we only report on double quoted identifiers. |
||
| 81 | * A double quoted identifier will have the opening quote on position 3 in the string: |
||
| 82 | * `<<<"ID"` |
||
| 83 | */ |
||
| 84 | if ($tokens[$stackPtr]['content'][3] !== '"') { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | $isHeredoc = true; |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | |||
| 92 | /* |
||
| 93 | * In PHP 5.2 the T_NOWDOC and the quoted T_HEREDOC tokens aren't recognized yet |
||
| 94 | * and PHPCS does not backfill for it, so we have to sniff for a specific |
||
| 95 | * combination of tokens. |
||
| 96 | */ |
||
| 97 | if ($tokens[$stackPtr]['code'] === T_SL) { |
||
| 98 | /* |
||
| 99 | * Check for the right token combination. |
||
| 100 | */ |
||
| 101 | View Code Duplication | if (isset($tokens[($stackPtr + 1)]) === false || $tokens[($stackPtr + 1)]['code'] !== T_LESS_THAN) { |
|
|
1 ignored issue
–
show
|
|||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | View Code Duplication | if (isset($tokens[($stackPtr + 2)]) === false |
|
|
1 ignored issue
–
show
|
|||
| 106 | || $tokens[($stackPtr + 2)]['code'] !== T_CONSTANT_ENCAPSED_STRING) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | /* |
||
| 111 | * Heredoc and nowdoc naming rules: |
||
| 112 | * "it must contain only alphanumeric characters and underscores and |
||
| 113 | * must start with a non-digit character or underscore" |
||
| 114 | * @link http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc |
||
| 115 | */ |
||
| 116 | if (preg_match('`^(["\'])([a-z][a-z0-9_]*)\1$`iD', $tokens[($stackPtr + 2)]['content'], $matches) < 1) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | // Remember whether we found a nowdoc or a heredoc. |
||
| 121 | switch ($matches[1]) { |
||
| 122 | case "'": |
||
| 123 | $isNowdoc = true; |
||
| 124 | break; |
||
| 125 | |||
| 126 | case '"': |
||
| 127 | $isHeredoc = true; |
||
| 128 | break; |
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | /* |
||
| 134 | * See if we can find the nowdoc/heredoc closer. |
||
| 135 | */ |
||
| 136 | $closer = null; |
||
| 137 | |||
| 138 | for ($i = ($stackPtr + 3); $i < $phpcsFile->numTokens; $i++) { |
||
| 139 | $maybeCloser = $phpcsFile->findNext(T_STRING, $i, null, false, $matches[2]); |
||
| 140 | if ($maybeCloser === false) { |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | // The closing identifier must begin in the first column of the line. |
||
| 145 | if ($tokens[$maybeCloser]['column'] !== 1) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | // The closing identifier must be the only content on that line, except for maybe a semi-colon. |
||
| 150 | $next = $phpcsFile->findNext(array(T_WHITESPACE, T_SEMICOLON), ($maybeCloser + 1), null, true); |
||
| 151 | if ($tokens[$maybeCloser]['line'] === $tokens[$next]['line']) { |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | |||
| 155 | $closer = $maybeCloser; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (isset($closer) === false) { |
||
| 160 | // No valid closer found. |
||
| 161 | return; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($isNowdoc === true) { |
||
| 166 | $error = 'Nowdocs are not present in PHP version 5.2 or earlier.'; |
||
| 167 | $phpcsFile->addError($error, $stackPtr, 'Found'); |
||
| 168 | |||
| 169 | if (isset($closer) !== false) { |
||
| 170 | // Also throw an error for the closer on older PHP versions and skip forward past it. |
||
| 171 | // Not needed for newer PHP versions as those will trigger this sniff for the closer token itself. |
||
| 172 | $phpcsFile->addError($error, $closer, 'Found'); |
||
| 173 | return ($closer + 1); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($isHeredoc === true) { |
||
| 178 | // Only throw an error for the opener. |
||
| 179 | $phpcsFile->addError('The Heredoc identifier may not be enclosed in (double) quotes in PHP version 5.2 or earlier.', $stackPtr, 'Found'); |
||
| 180 | |||
| 181 | if (isset($closer) !== false) { |
||
| 182 | return ($closer + 1); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | }//end process() |
||
| 187 | |||
| 189 |
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.