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