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