Conditions | 55 |
Paths | > 20000 |
Total Lines | 187 |
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 |
||
162 | public function loadClass($class) |
||
163 | { |
||
164 | ErrorHandler::stackErrors(); |
||
165 | |||
166 | try { |
||
167 | if ($this->isFinder) { |
||
168 | if ($file = $this->classLoader[0]->findFile($class)) { |
||
169 | require_once $file; |
||
170 | } |
||
171 | } else { |
||
172 | call_user_func($this->classLoader, $class); |
||
173 | $file = false; |
||
174 | } |
||
175 | } catch (\Exception $e) { |
||
176 | ErrorHandler::unstackErrors(); |
||
177 | |||
178 | throw $e; |
||
179 | } catch (\Throwable $e) { |
||
180 | ErrorHandler::unstackErrors(); |
||
181 | |||
182 | throw $e; |
||
183 | } |
||
184 | |||
185 | ErrorHandler::unstackErrors(); |
||
186 | |||
187 | $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false)); |
||
188 | |||
189 | if ($class && '\\' === $class[0]) { |
||
190 | $class = substr($class, 1); |
||
191 | } |
||
192 | |||
193 | if ($exists) { |
||
194 | $refl = new \ReflectionClass($class); |
||
195 | $name = $refl->getName(); |
||
196 | |||
197 | if ($name !== $class && 0 === strcasecmp($name, $class)) { |
||
198 | throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name)); |
||
199 | } |
||
200 | |||
201 | if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) { |
||
202 | @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED); |
||
203 | } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) { |
||
204 | self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]); |
||
205 | } else { |
||
206 | if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) { |
||
207 | $len = 0; |
||
208 | $ns = ''; |
||
209 | } else { |
||
210 | switch ($ns = substr($name, 0, $len)) { |
||
211 | case 'Symfony\Bridge\\': |
||
212 | case 'Symfony\Bundle\\': |
||
213 | case 'Symfony\Component\\': |
||
214 | $ns = 'Symfony\\'; |
||
215 | $len = strlen($ns); |
||
216 | break; |
||
217 | } |
||
218 | } |
||
219 | $parent = get_parent_class($class); |
||
220 | |||
221 | if (!$parent || strncmp($ns, $parent, $len)) { |
||
222 | if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) { |
||
223 | @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED); |
||
224 | } |
||
225 | |||
226 | $parentInterfaces = array(); |
||
227 | $deprecatedInterfaces = array(); |
||
228 | if ($parent) { |
||
229 | foreach (class_implements($parent) as $interface) { |
||
230 | $parentInterfaces[$interface] = 1; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | foreach ($refl->getInterfaceNames() as $interface) { |
||
235 | if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len)) { |
||
236 | $deprecatedInterfaces[] = $interface; |
||
237 | } |
||
238 | foreach (class_implements($interface) as $interface) { |
||
239 | $parentInterfaces[$interface] = 1; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | foreach ($deprecatedInterfaces as $interface) { |
||
244 | if (!isset($parentInterfaces[$interface])) { |
||
245 | @trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED); |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | |||
252 | if ($file) { |
||
253 | if (!$exists) { |
||
254 | if (false !== strpos($class, '/')) { |
||
255 | throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class)); |
||
256 | } |
||
257 | |||
258 | throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file)); |
||
259 | } |
||
260 | if (self::$caseCheck) { |
||
261 | $real = explode('\\', $class.strrchr($file, '.')); |
||
262 | $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file)); |
||
263 | |||
264 | $i = count($tail) - 1; |
||
265 | $j = count($real) - 1; |
||
266 | |||
267 | while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) { |
||
268 | --$i; |
||
269 | --$j; |
||
270 | } |
||
271 | |||
272 | array_splice($tail, 0, $i + 1); |
||
273 | } |
||
274 | if (self::$caseCheck && $tail) { |
||
275 | $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail); |
||
276 | $tailLen = strlen($tail); |
||
277 | $real = $refl->getFileName(); |
||
278 | |||
279 | if (2 === self::$caseCheck) { |
||
280 | // realpath() on MacOSX doesn't normalize the case of characters |
||
281 | |||
282 | $i = 1 + strrpos($real, '/'); |
||
283 | $file = substr($real, $i); |
||
284 | $real = substr($real, 0, $i); |
||
285 | |||
286 | if (isset(self::$darwinCache[$real])) { |
||
287 | $kDir = $real; |
||
288 | } else { |
||
289 | $kDir = strtolower($real); |
||
290 | |||
291 | if (isset(self::$darwinCache[$kDir])) { |
||
292 | $real = self::$darwinCache[$kDir][0]; |
||
293 | } else { |
||
294 | $dir = getcwd(); |
||
295 | chdir($real); |
||
296 | $real = getcwd().'/'; |
||
297 | chdir($dir); |
||
298 | |||
299 | $dir = $real; |
||
300 | $k = $kDir; |
||
301 | $i = strlen($dir) - 1; |
||
302 | while (!isset(self::$darwinCache[$k])) { |
||
303 | self::$darwinCache[$k] = array($dir, array()); |
||
304 | self::$darwinCache[$dir] = &self::$darwinCache[$k]; |
||
305 | |||
306 | while ('/' !== $dir[--$i]) { |
||
307 | } |
||
308 | $k = substr($k, 0, ++$i); |
||
309 | $dir = substr($dir, 0, $i--); |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | |||
314 | $dirFiles = self::$darwinCache[$kDir][1]; |
||
315 | |||
316 | if (isset($dirFiles[$file])) { |
||
317 | $kFile = $file; |
||
318 | } else { |
||
319 | $kFile = strtolower($file); |
||
320 | |||
321 | if (!isset($dirFiles[$kFile])) { |
||
322 | foreach (scandir($real, 2) as $f) { |
||
323 | if ('.' !== $f[0]) { |
||
324 | $dirFiles[$f] = $f; |
||
325 | if ($f === $file) { |
||
326 | $kFile = $k = $file; |
||
327 | } elseif ($f !== $k = strtolower($f)) { |
||
328 | $dirFiles[$k] = $f; |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | self::$darwinCache[$kDir][1] = $dirFiles; |
||
333 | } |
||
334 | } |
||
335 | |||
336 | $real .= $dirFiles[$kFile]; |
||
337 | } |
||
338 | |||
339 | if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true) |
||
340 | && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false) |
||
341 | ) { |
||
342 | throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1))); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | return true; |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 |
If you suppress an error, we recommend checking for the error condition explicitly: