Conditions | 34 |
Paths | 18007 |
Total Lines | 132 |
Code Lines | 80 |
Lines | 110 |
Ratio | 83.33 % |
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 |
||
53 | protected function createService(array $definition, $id) { |
||
54 | // This method is a verbatim copy of |
||
55 | // \Drupal\Component\DependencyInjection\Container::createService |
||
56 | // except for the following difference: |
||
57 | // - There are no instanceof checks on \stdClass, which are used in the |
||
58 | // parent class to avoid resolving services and parameters when it is |
||
59 | // known from dumping that there is nothing to resolve. |
||
60 | View Code Duplication | if (isset($definition['synthetic']) && $definition['synthetic'] === TRUE) { |
|
|
|||
61 | throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The service container does not know how to construct this service. The service will need to be set before it is first used.', $id)); |
||
62 | } |
||
63 | |||
64 | $arguments = array(); |
||
65 | if (isset($definition['arguments'])) { |
||
66 | $arguments = $this->resolveServicesAndParameters($definition['arguments']); |
||
67 | } |
||
68 | |||
69 | View Code Duplication | if (isset($definition['file'])) { |
|
70 | $file = $this->frozen ? $definition['file'] : current($this->resolveServicesAndParameters(array($definition['file']))); |
||
71 | require_once $file; |
||
72 | } |
||
73 | |||
74 | View Code Duplication | if (isset($definition['factory'])) { |
|
75 | $factory = $definition['factory']; |
||
76 | if (is_array($factory)) { |
||
77 | $factory = $this->resolveServicesAndParameters(array($factory[0], $factory[1])); |
||
78 | } |
||
79 | elseif (!is_string($factory)) { |
||
80 | throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id)); |
||
81 | } |
||
82 | |||
83 | $service = call_user_func_array($factory, $arguments); |
||
84 | } |
||
85 | else { |
||
86 | $class = $this->frozen ? $definition['class'] : current($this->resolveServicesAndParameters(array($definition['class']))); |
||
87 | $length = isset($definition['arguments_count']) ? $definition['arguments_count'] : count($arguments); |
||
88 | |||
89 | // Optimize class instantiation for services with up to 10 parameters as |
||
90 | // reflection is noticeably slow. |
||
91 | switch ($length) { |
||
92 | case 0: |
||
93 | $service = new $class(); |
||
94 | break; |
||
95 | |||
96 | case 1: |
||
97 | $service = new $class($arguments[0]); |
||
98 | break; |
||
99 | |||
100 | case 2: |
||
101 | $service = new $class($arguments[0], $arguments[1]); |
||
102 | break; |
||
103 | |||
104 | case 3: |
||
105 | $service = new $class($arguments[0], $arguments[1], $arguments[2]); |
||
106 | break; |
||
107 | |||
108 | case 4: |
||
109 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]); |
||
110 | break; |
||
111 | |||
112 | case 5: |
||
113 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]); |
||
114 | break; |
||
115 | |||
116 | case 6: |
||
117 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]); |
||
118 | break; |
||
119 | |||
120 | case 7: |
||
121 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]); |
||
122 | break; |
||
123 | |||
124 | case 8: |
||
125 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]); |
||
126 | break; |
||
127 | |||
128 | case 9: |
||
129 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8]); |
||
130 | break; |
||
131 | |||
132 | case 10: |
||
133 | $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8], $arguments[9]); |
||
134 | break; |
||
135 | |||
136 | default: |
||
137 | $r = new \ReflectionClass($class); |
||
138 | $service = $r->newInstanceArgs($arguments); |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // Share the service if it is public. |
||
144 | View Code Duplication | if (!isset($definition['public']) || $definition['public'] !== FALSE) { |
|
145 | // Forward compatibility fix for Symfony 2.8 update. |
||
146 | if (!isset($definition['shared']) || $definition['shared'] !== FALSE) { |
||
147 | $this->services[$id] = $service; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | View Code Duplication | if (isset($definition['calls'])) { |
|
152 | foreach ($definition['calls'] as $call) { |
||
153 | $method = $call[0]; |
||
154 | $arguments = array(); |
||
155 | if (!empty($call[1])) { |
||
156 | $arguments = $call[1]; |
||
157 | $arguments = $this->resolveServicesAndParameters($arguments); |
||
158 | } |
||
159 | call_user_func_array(array($service, $method), $arguments); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | View Code Duplication | if (isset($definition['properties'])) { |
|
164 | $definition['properties'] = $this->resolveServicesAndParameters($definition['properties']); |
||
165 | foreach ($definition['properties'] as $key => $value) { |
||
166 | $service->{$key} = $value; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | View Code Duplication | if (isset($definition['configurator'])) { |
|
171 | $callable = $definition['configurator']; |
||
172 | if (is_array($callable)) { |
||
173 | $callable = $this->resolveServicesAndParameters($callable); |
||
174 | } |
||
175 | |||
176 | if (!is_callable($callable)) { |
||
177 | throw new InvalidArgumentException(sprintf('The configurator for class "%s" is not a callable.', get_class($service))); |
||
178 | } |
||
179 | |||
180 | call_user_func($callable, $service); |
||
181 | } |
||
182 | |||
183 | return $service; |
||
184 | } |
||
185 | |||
277 |
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.