Conditions | 13 |
Paths | 19 |
Total Lines | 88 |
Code Lines | 34 |
Lines | 15 |
Ratio | 17.05 % |
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 |
||
41 | public function resolve($class, $share = false) |
||
42 | { |
||
43 | if ($this->isDependencyLoaded($class)) { |
||
44 | return $this->dependencies[$class]; |
||
45 | } elseif (!empty($this->callbacks[$class])) { |
||
46 | $share = $this->callbacks[$class]['share'] === true || $share === true ? true : false; |
||
47 | //pass an instance of $this to the factory for dependency injection on cached classes |
||
48 | $dependency = $this->callbacks[$class]['factory']($this); |
||
49 | if ($share === true) { |
||
50 | $this->dependencies[$class] = $dependency; |
||
51 | } |
||
52 | |||
53 | return $dependency; |
||
54 | } |
||
55 | |||
56 | // Reflect on the $class |
||
57 | $reflectionClass = new \ReflectionClass($class); |
||
58 | |||
59 | // Fetch the constructor (instance of ReflectionMethod) |
||
60 | $constructor = $reflectionClass->getConstructor(); |
||
61 | |||
62 | // If there is no constructor, there is no |
||
63 | // dependencies, which means that our job is done. |
||
64 | View Code Duplication | if (! $constructor) { |
|
|
|||
65 | $dependency = new $class; |
||
66 | if ($share === true) { |
||
67 | $this->dependencies[$class] = $dependency; |
||
68 | } |
||
69 | |||
70 | return $dependency; |
||
71 | } |
||
72 | |||
73 | // Fetch the arguments from the constructor |
||
74 | // (collection of ReflectionParameter instances) |
||
75 | $params = $constructor->getParameters(); |
||
76 | |||
77 | // If there is a constructor, but no dependencies, |
||
78 | // our job is done. |
||
79 | View Code Duplication | if (count($params) === 0) { |
|
80 | $dependency = new $class; |
||
81 | if ($share === true) { |
||
82 | $this->dependencies[$class] = $dependency; |
||
83 | } |
||
84 | return $dependency; |
||
85 | } |
||
86 | |||
87 | // This is were we store the dependencies |
||
88 | $newInstanceParams = []; |
||
89 | |||
90 | // Loop over the constructor arguments |
||
91 | foreach ($params as $param) { |
||
92 | // Here we should perform a bunch of checks, such as: |
||
93 | // isArray(), isCallable(), isDefaultValueAvailable() |
||
94 | // isOptional() etc. |
||
95 | |||
96 | // For now, we just check to see if the argument is |
||
97 | // a class, so we can instantiate it, |
||
98 | // otherwise we just pass null. |
||
99 | if (is_null($param->getClass())) { |
||
100 | $newInstanceParams[] = null; |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | |||
105 | // This is where 'the magic happens'. We resolve each |
||
106 | // of the dependencies, by recursively calling the |
||
107 | // resolve() method. |
||
108 | // At one point, we will reach the bottom of the |
||
109 | // nested dependencies we need in order to instantiate |
||
110 | // the class. |
||
111 | $newInstanceParams[] = $this->resolve( |
||
112 | $param->getClass()->getName() |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | // Return the reflected class, instantiated with all its |
||
117 | // dependencies (this happens once for all the |
||
118 | // nested dependencies). |
||
119 | $dependency = $reflectionClass->newInstanceArgs( |
||
120 | $newInstanceParams |
||
121 | ); |
||
122 | |||
123 | if ($share === true) { |
||
124 | $this->dependencies[$class] = $dependency; |
||
125 | } |
||
126 | |||
127 | return $dependency; |
||
128 | } |
||
129 | } |
||
130 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.