Conditions | 16 |
Paths | 80 |
Total Lines | 85 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
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 |
||
68 | public function process(File $phpcsFile, $stackPtr) |
||
69 | { |
||
70 | $tokens = $phpcsFile->getTokens(); |
||
71 | $current = $tokens[$stackPtr]; |
||
72 | |||
73 | if ($current['code'] === T_ARRAY) { |
||
74 | $start = $current['parenthesis_opener']; |
||
75 | $end = $current['parenthesis_closer']; |
||
76 | } else { |
||
77 | $start = $current['bracket_opener']; |
||
78 | $end = $current['bracket_closer']; |
||
79 | } |
||
80 | |||
81 | if ($tokens[$start]['line'] === $tokens[$end]['line']) { |
||
82 | return; |
||
83 | } |
||
84 | |||
85 | $assignments = array(); |
||
86 | $keyEndColumn = -1; |
||
87 | $lastLine = -1; |
||
88 | |||
89 | for ($i = ($start + 1); $i < $end; $i++) { |
||
90 | $current = $tokens[$i]; |
||
91 | $previous = $tokens[($i - 1)]; |
||
92 | |||
93 | // Skip nested arrays. |
||
94 | if ((in_array($current['code'], $this->arrayTokens)) === true) { |
||
95 | if ($current['code'] === T_ARRAY) { |
||
96 | $i = ($current['parenthesis_closer'] + 1); |
||
97 | } else { |
||
98 | $i = ($current['bracket_closer'] + 1); |
||
99 | } |
||
100 | |||
101 | continue; |
||
102 | } |
||
103 | |||
104 | // Skip closures in array. |
||
105 | if ($current['code'] === T_CLOSURE) { |
||
106 | $i = ($current['scope_closer'] + 1); |
||
107 | continue; |
||
108 | } |
||
109 | |||
110 | if ($current['code'] === T_DOUBLE_ARROW) { |
||
111 | $assignments[] = $i; |
||
112 | $column = $previous['column']; |
||
113 | $line = $current['line']; |
||
114 | |||
115 | if ($lastLine === $line) { |
||
116 | $msg = 'only one "=>" assignments per line is allowed in a multi line array'; |
||
117 | $phpcsFile->addError($msg, $i, 'OneAssignmentPerLine'); |
||
118 | } |
||
119 | |||
120 | $hasKeyInLine = false; |
||
121 | |||
122 | $j = ($i - 1); |
||
123 | while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) { |
||
124 | if ((in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens)) === false) { |
||
125 | $hasKeyInLine = true; |
||
126 | } |
||
127 | |||
128 | $j--; |
||
129 | } |
||
130 | |||
131 | if ($hasKeyInLine === false) { |
||
132 | $phpcsFile->addError( |
||
133 | 'in arrays, keys and "=>" must be on the same line', |
||
134 | $i, |
||
135 | 'KeyAndValueNotOnSameLine' |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | if ($column > $keyEndColumn) { |
||
140 | $keyEndColumn = $column; |
||
141 | } |
||
142 | |||
143 | $lastLine = $line; |
||
144 | }//end if |
||
145 | }//end for |
||
146 | |||
147 | foreach ($assignments as $ptr) { |
||
148 | $current = $tokens[$ptr]; |
||
149 | $column = $current['column']; |
||
150 | |||
151 | if ($column !== ($keyEndColumn + 1)) { |
||
152 | $phpcsFile->addError('each "=>" assignments must be aligned', $ptr, 'AssignmentsNotAligned'); |
||
153 | } |
||
160 |