| Conditions | 76 |
| Paths | 14784 |
| Total Lines | 215 |
| 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 |
||
| 163 | private function checkClass($class, $file = null) |
||
| 164 | { |
||
| 165 | $exists = null === $file || \class_exists($class, false) || \interface_exists($class, false) || \trait_exists($class, false); |
||
| 166 | |||
| 167 | if (null !== $file && $class && '\\' === $class[0]) { |
||
| 168 | $class = substr($class, 1); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($exists) { |
||
| 172 | if (isset(self::$checkedClasses[$class])) { |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | self::$checkedClasses[$class] = true; |
||
| 176 | |||
| 177 | $refl = new \ReflectionClass($class); |
||
| 178 | if (null === $file && $refl->isInternal()) { |
||
| 179 | return; |
||
| 180 | } |
||
| 181 | $name = $refl->getName(); |
||
| 182 | |||
| 183 | if ($name !== $class && 0 === \strcasecmp($name, $class)) { |
||
| 184 | throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".', $class, $name)); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Don't trigger deprecations for classes in the same vendor |
||
| 188 | if (2 > $len = 1 + (\strpos($name, '\\') ?: \strpos($name, '_'))) { |
||
| 189 | $len = 0; |
||
| 190 | $ns = ''; |
||
| 191 | } else { |
||
| 192 | $ns = \substr($name, 0, $len); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Detect annotations on the class |
||
| 196 | if (false !== $doc = $refl->getDocComment()) { |
||
| 197 | foreach (array('final', 'deprecated', 'internal') as $annotation) { |
||
| 198 | if (false !== \strpos($doc, $annotation) && preg_match('#\n \* @'.$annotation.'(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s', $doc, $notice)) { |
||
| 199 | self::${$annotation}[$name] = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : ''; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | $parentAndTraits = \class_uses($name, false); |
||
| 205 | if ($parent = \get_parent_class($class)) { |
||
| 206 | $parentAndTraits[] = $parent; |
||
| 207 | |||
| 208 | if (!isset(self::$checkedClasses[$parent])) { |
||
| 209 | $this->checkClass($parent); |
||
| 210 | } |
||
| 211 | |||
| 212 | if (isset(self::$final[$parent])) { |
||
| 213 | @trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $name), E_USER_DEPRECATED); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // Detect if the parent is annotated |
||
| 218 | foreach ($parentAndTraits + $this->getOwnInterfaces($name, $parent) as $use) { |
||
| 219 | if (!isset(self::$checkedClasses[$use])) { |
||
| 220 | $this->checkClass($use); |
||
| 221 | } |
||
| 222 | if (isset(self::$deprecated[$use]) && \strncmp($ns, $use, $len)) { |
||
| 223 | $type = class_exists($name, false) ? 'class' : (interface_exists($name, false) ? 'interface' : 'trait'); |
||
| 224 | $verb = class_exists($use, false) || interface_exists($name, false) ? 'extends' : (interface_exists($use, false) ? 'implements' : 'uses'); |
||
| 225 | |||
| 226 | @trigger_error(sprintf('The "%s" %s %s "%s" that is deprecated%s.', $name, $type, $verb, $use, self::$deprecated[$use]), E_USER_DEPRECATED); |
||
| 227 | } |
||
| 228 | if (isset(self::$internal[$use]) && \strncmp($ns, $use, $len)) { |
||
| 229 | @trigger_error(sprintf('The "%s" %s is considered internal%s. It may change without further notice. You should not use it from "%s".', $use, class_exists($use, false) ? 'class' : (interface_exists($use, false) ? 'interface' : 'trait'), self::$internal[$use], $name), E_USER_DEPRECATED); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // Inherit @final and @internal annotations for methods |
||
| 234 | self::$finalMethods[$name] = array(); |
||
| 235 | self::$internalMethods[$name] = array(); |
||
| 236 | foreach ($parentAndTraits as $use) { |
||
| 237 | foreach (array('finalMethods', 'internalMethods') as $property) { |
||
| 238 | if (isset(self::${$property}[$use])) { |
||
| 239 | self::${$property}[$name] = self::${$property}[$name] ? self::${$property}[$use] + self::${$property}[$name] : self::${$property}[$use]; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | $isClass = \class_exists($name, false); |
||
| 245 | foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) { |
||
| 246 | if ($method->class !== $name) { |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | |||
| 250 | // Method from a trait |
||
| 251 | if ($method->getFilename() !== $refl->getFileName()) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($isClass && $parent && isset(self::$finalMethods[$parent][$method->name])) { |
||
| 256 | list($declaringClass, $message) = self::$finalMethods[$parent][$method->name]; |
||
| 257 | @trigger_error(sprintf('The "%s::%s()" method is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $declaringClass, $method->name, $message, $name), E_USER_DEPRECATED); |
||
| 258 | } |
||
| 259 | |||
| 260 | foreach ($parentAndTraits as $use) { |
||
| 261 | if (isset(self::$internalMethods[$use][$method->name])) { |
||
| 262 | list($declaringClass, $message) = self::$internalMethods[$use][$method->name]; |
||
| 263 | if (\strncmp($ns, $declaringClass, $len)) { |
||
| 264 | @trigger_error(sprintf('The "%s::%s()" method is considered internal%s. It may change without further notice. You should not extend it from "%s".', $declaringClass, $method->name, $message, $name), E_USER_DEPRECATED); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // Detect method annotations |
||
| 270 | if (false === $doc = $method->getDocComment()) { |
||
| 271 | continue; |
||
| 272 | } |
||
| 273 | |||
| 274 | foreach (array('final', 'internal') as $annotation) { |
||
| 275 | if (false !== \strpos($doc, $annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) { |
||
| 276 | $message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : ''; |
||
| 277 | self::${$annotation.'Methods'}[$name][$method->name] = array($name, $message); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | if (isset(self::$php7Reserved[\strtolower($refl->getShortName())])) { |
||
| 283 | @trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | if ($file) { |
||
| 288 | if (!$exists) { |
||
| 289 | if (false !== strpos($class, '/')) { |
||
| 290 | 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)); |
||
| 291 | } |
||
| 292 | |||
| 293 | 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)); |
||
| 294 | } |
||
| 295 | if (self::$caseCheck) { |
||
| 296 | $real = explode('\\', $class.strrchr($file, '.')); |
||
| 297 | $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file)); |
||
| 298 | |||
| 299 | $i = count($tail) - 1; |
||
| 300 | $j = count($real) - 1; |
||
| 301 | |||
| 302 | while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) { |
||
| 303 | --$i; |
||
| 304 | --$j; |
||
| 305 | } |
||
| 306 | |||
| 307 | array_splice($tail, 0, $i + 1); |
||
| 308 | } |
||
| 309 | if (self::$caseCheck && $tail) { |
||
| 310 | $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail); |
||
| 311 | $tailLen = strlen($tail); |
||
| 312 | $real = $refl->getFileName(); |
||
| 313 | |||
| 314 | if (2 === self::$caseCheck) { |
||
| 315 | // realpath() on MacOSX doesn't normalize the case of characters |
||
| 316 | |||
| 317 | $i = 1 + strrpos($real, '/'); |
||
| 318 | $file = substr($real, $i); |
||
| 319 | $real = substr($real, 0, $i); |
||
| 320 | |||
| 321 | if (isset(self::$darwinCache[$real])) { |
||
| 322 | $kDir = $real; |
||
| 323 | } else { |
||
| 324 | $kDir = strtolower($real); |
||
| 325 | |||
| 326 | if (isset(self::$darwinCache[$kDir])) { |
||
| 327 | $real = self::$darwinCache[$kDir][0]; |
||
| 328 | } else { |
||
| 329 | $dir = getcwd(); |
||
| 330 | chdir($real); |
||
| 331 | $real = getcwd().'/'; |
||
| 332 | chdir($dir); |
||
| 333 | |||
| 334 | $dir = $real; |
||
| 335 | $k = $kDir; |
||
| 336 | $i = strlen($dir) - 1; |
||
| 337 | while (!isset(self::$darwinCache[$k])) { |
||
| 338 | self::$darwinCache[$k] = array($dir, array()); |
||
| 339 | self::$darwinCache[$dir] = &self::$darwinCache[$k]; |
||
| 340 | |||
| 341 | while ('/' !== $dir[--$i]) { |
||
| 342 | } |
||
| 343 | $k = substr($k, 0, ++$i); |
||
| 344 | $dir = substr($dir, 0, $i--); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | $dirFiles = self::$darwinCache[$kDir][1]; |
||
| 350 | |||
| 351 | if (isset($dirFiles[$file])) { |
||
| 352 | $kFile = $file; |
||
| 353 | } else { |
||
| 354 | $kFile = strtolower($file); |
||
| 355 | |||
| 356 | if (!isset($dirFiles[$kFile])) { |
||
| 357 | foreach (scandir($real, 2) as $f) { |
||
| 358 | if ('.' !== $f[0]) { |
||
| 359 | $dirFiles[$f] = $f; |
||
| 360 | if ($f === $file) { |
||
| 361 | $kFile = $k = $file; |
||
| 362 | } elseif ($f !== $k = strtolower($f)) { |
||
| 363 | $dirFiles[$k] = $f; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | self::$darwinCache[$kDir][1] = $dirFiles; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | $real .= $dirFiles[$kFile]; |
||
| 372 | } |
||
| 373 | |||
| 374 | if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true) |
||
| 375 | && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false) |
||
| 376 | ) { |
||
| 377 | 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))); |
||
| 378 | } |
||
| 410 |