| Conditions | 11 |
| Paths | 13 |
| Total Lines | 48 |
| Code Lines | 24 |
| 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 |
||
| 30 | public function loadClass(string $class, string $type = null, ...$params){ |
||
| 31 | $type = $type ?? $class; |
||
| 32 | |||
| 33 | try{ |
||
| 34 | $reflectionClass = new ReflectionClass($class); |
||
| 35 | $reflectionType = new ReflectionClass($type); |
||
| 36 | } |
||
| 37 | catch(Exception $e){ |
||
| 38 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | if($reflectionType->isTrait()){ |
||
| 43 | throw new TraitException($class.' cannot be an instance of trait '.$type); |
||
| 44 | } |
||
| 45 | |||
| 46 | if($reflectionClass->isAbstract()){ |
||
| 47 | throw new TraitException('cannot instance abstract class '.$class); |
||
| 48 | } |
||
| 49 | |||
| 50 | if($reflectionClass->isTrait()){ |
||
| 51 | throw new TraitException('cannot instance trait '.$class); |
||
| 52 | } |
||
| 53 | |||
| 54 | if($class !== $type){ |
||
| 55 | |||
| 56 | if($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)){ |
||
| 57 | throw new TraitException($class.' does not implement '.$type); |
||
| 58 | } |
||
| 59 | elseif(!$reflectionClass->isSubclassOf($type)) { |
||
| 60 | throw new TraitException($class.' does not inherit '.$type); |
||
| 61 | } |
||
| 62 | |||
| 63 | } |
||
| 64 | |||
| 65 | try{ |
||
| 66 | $object = $reflectionClass->newInstanceArgs($params); |
||
| 67 | |||
| 68 | if(!$object instanceof $type){ |
||
| 69 | throw new TraitException('how did u even get here?'); // @codeCoverageIgnore |
||
| 70 | } |
||
| 71 | |||
| 72 | return $object; |
||
| 73 | } |
||
| 74 | // @codeCoverageIgnoreStart |
||
| 75 | // here be dragons |
||
| 76 | catch(Exception $e){ |
||
| 77 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
||
| 78 | } |
||
| 84 |