Conditions | 29 |
Paths | 52 |
Total Lines | 103 |
Code Lines | 71 |
Lines | 16 |
Ratio | 15.53 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
93 | public function processTokenStream(TokenStream $uTokenStream) |
||
94 | { |
||
95 | foreach ($this->scanners as $tScanner) { |
||
96 | $tScanner->processTokenStream($uTokenStream); |
||
97 | } |
||
98 | |||
99 | $tBuffer = ""; |
||
100 | |||
101 | $tUses = []; |
||
102 | $tLastNamespace = null; |
||
103 | $tLastClass = null; |
||
104 | $tLastClassDerivedFrom = null; |
||
105 | $tExpectation = 0; // 1=namespace, 2=class |
||
106 | |||
107 | foreach ($uTokenStream as $tToken) { |
||
108 | if ($tToken[0] === T_WHITESPACE) { |
||
109 | continue; |
||
110 | } |
||
111 | |||
112 | if ($tExpectation === 0) { |
||
113 | if ($tToken[0] === T_NAMESPACE) { |
||
114 | $tBuffer = ""; |
||
115 | $tExpectation = 1; |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | if ($tToken[0] === T_CLASS) { |
||
120 | $tExpectation = 2; |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | if ($tToken[0] === T_USE) { |
||
125 | $tBuffer = ""; |
||
126 | $tExpectation = 5; |
||
127 | continue; |
||
128 | } |
||
129 | View Code Duplication | } elseif ($tExpectation === 1) { |
|
130 | if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
||
131 | $tBuffer .= $tToken[1]; |
||
132 | } else { |
||
133 | $tLastNamespace = $tBuffer; |
||
134 | $tExpectation = 0; |
||
135 | } |
||
136 | } elseif ($tExpectation === 2) { |
||
137 | $tLastClass = "{$tLastNamespace}\\{$tToken[1]}"; |
||
138 | $tExpectation = 3; |
||
139 | } elseif ($tExpectation === 3) { |
||
140 | if ($tToken[0] === T_EXTENDS) { |
||
141 | $tBuffer = ""; |
||
142 | $tExpectation = 4; |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | $tSkip = false; |
||
147 | if ($tLastClassDerivedFrom !== null && !class_exists($tLastClassDerivedFrom)) { |
||
148 | $tSkip = true; |
||
149 | throw new LogicException(sprintf( |
||
150 | "\"%s\" derived from \"%s\", but it could not be found.\n", |
||
151 | $tLastClass, |
||
152 | $tLastClassDerivedFrom |
||
153 | )); |
||
154 | } |
||
155 | |||
156 | if (!$tSkip && !isset($this->result[$tLastClass])) { |
||
157 | $this->processClass($tLastClass); |
||
158 | } |
||
159 | |||
160 | $tExpectation = 0; |
||
161 | } elseif ($tExpectation === 4) { |
||
162 | if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
||
163 | $tBuffer .= $tToken[1]; |
||
164 | } else { |
||
165 | $tFound = false; |
||
166 | |||
167 | foreach ($tUses as $tUse) { |
||
168 | $tLength = strlen($tBuffer); |
||
169 | if (strlen($tUse) >= $tLength && substr($tUse, -$tLength) === $tBuffer) { |
||
170 | $tLastClassDerivedFrom = $tUse; |
||
171 | $tFound = true; |
||
172 | break; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if (!$tFound) { |
||
177 | if (strpos($tBuffer, "\\") !== false) { |
||
178 | $tLastClassDerivedFrom = $tBuffer; |
||
179 | } else { |
||
180 | $tLastClassDerivedFrom = "{$tLastNamespace}\\{$tBuffer}"; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $tExpectation = 3; |
||
185 | } |
||
186 | View Code Duplication | } elseif ($tExpectation === 5) { |
|
187 | if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
||
188 | $tBuffer .= $tToken[1]; |
||
189 | } else { |
||
190 | $tUses[] = $tBuffer; |
||
191 | $tExpectation = 0; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
213 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.