Conditions | 14 |
Paths | 100 |
Total Lines | 59 |
Code Lines | 30 |
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 |
||
115 | protected function resolveParameter( |
||
116 | ContainerInterface $container, |
||
117 | ReflectionParameter $parameter, |
||
118 | ?ParameterCollection $parameters = null |
||
119 | ): mixed { |
||
120 | $class = null; |
||
121 | $className = null; |
||
122 | $types = $this->getTypes($parameter); |
||
123 | |||
124 | // TODO: handle for union types |
||
125 | if (count($types) > 1) { |
||
126 | foreach ($types as /** @var ReflectionNamedType $type */ $type) { |
||
127 | $name = $type->getName(); |
||
128 | if ($type->isBuiltin() === false && $container->has($name)) { |
||
129 | $className = $name; |
||
130 | break; |
||
131 | } |
||
132 | } |
||
133 | } else { |
||
134 | if ($types[0]->isBuiltin() === false) { |
||
135 | $className = $types[0]->getName(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if ($className !== null) { |
||
140 | $class = new ReflectionClass($className); |
||
141 | } |
||
142 | |||
143 | //If the parameter is not a class |
||
144 | if ($class === null) { |
||
145 | if ($parameters !== null) { |
||
146 | if ( |
||
147 | $parameters->has($parameter->name) && |
||
148 | $parameters->get($parameter->name) !== null |
||
149 | ) { |
||
150 | return $parameters->get($parameter->name) |
||
151 | ->getValue($container); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if ($parameter->isDefaultValueAvailable()) { |
||
156 | try { |
||
157 | return $parameter->getDefaultValue(); |
||
158 | } catch (ReflectionException $e) { |
||
159 | throw new ContainerException($e->getMessage()); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | if ($parameter->isOptional()) { |
||
164 | // This branch is required to work around PHP bugs where a parameter is optional |
||
165 | // but has no default value available through reflection. Specifically, PDO exhibits |
||
166 | // this behavior. |
||
167 | return null; |
||
168 | } |
||
169 | |||
170 | throw new ContainerException(sprintf('Parameter [%s] is not bound!', $parameter->name)); |
||
171 | } |
||
172 | |||
173 | return $container->get($class->name); |
||
174 | } |
||
202 |