Conditions | 31 |
Total Lines | 104 |
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 |
||
153 | public static function getSimpleHydrator($class) |
||
154 | { |
||
155 | $baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) { |
||
156 | $notByRef = (array) $this; |
||
157 | |||
158 | foreach ($properties as $name => &$value) { |
||
159 | if (!$noRef = $notByRef[$name] ?? false) { |
||
160 | $object->$name = $value; |
||
161 | $object->$name = &$value; |
||
162 | } elseif (true !== $noRef) { |
||
163 | $noRef($object, $value); |
||
164 | } else { |
||
165 | $object->$name = $value; |
||
166 | } |
||
167 | } |
||
168 | })->bindTo(new \stdClass()); |
||
169 | |||
170 | switch ($class) { |
||
171 | case 'stdClass': |
||
172 | return $baseHydrator; |
||
173 | |||
174 | case 'ErrorException': |
||
175 | return $baseHydrator->bindTo(new \stdClass(), new class extends \ErrorException { |
||
176 | }); |
||
177 | |||
178 | case 'TypeError': |
||
179 | return $baseHydrator->bindTo(new \stdClass(), new class extends \Error { |
||
180 | }); |
||
181 | |||
182 | case 'SplObjectStorage': |
||
183 | return static function ($properties, $object) { |
||
184 | foreach ($properties as $name => &$value) { |
||
185 | if ("\0" !== $name) { |
||
186 | $object->$name = $value; |
||
187 | $object->$name = &$value; |
||
188 | continue; |
||
189 | } |
||
190 | for ($i = 0; $i < \count($value); ++$i) { |
||
191 | $object->attach($value[$i], $value[++$i]); |
||
192 | } |
||
193 | } |
||
194 | }; |
||
195 | } |
||
196 | |||
197 | if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) { |
||
198 | throw new ClassNotFoundException($class); |
||
199 | } |
||
200 | $classReflector = new \ReflectionClass($class); |
||
201 | |||
202 | switch ($class) { |
||
203 | case 'ArrayIterator': |
||
204 | case 'ArrayObject': |
||
205 | $constructor = $classReflector->getConstructor()->invokeArgs(...); |
||
206 | |||
207 | return static function ($properties, $object) use ($constructor) { |
||
208 | foreach ($properties as $name => &$value) { |
||
209 | if ("\0" === $name) { |
||
210 | $constructor($object, $value); |
||
211 | } else { |
||
212 | $object->$name = $value; |
||
213 | $object->$name = &$value; |
||
214 | } |
||
215 | } |
||
216 | }; |
||
217 | } |
||
218 | |||
219 | if (!$classReflector->isInternal()) { |
||
220 | $notByRef = new \stdClass(); |
||
221 | foreach ($classReflector->getProperties() as $propertyReflector) { |
||
222 | if ($propertyReflector->isStatic()) { |
||
223 | continue; |
||
224 | } |
||
225 | if (\PHP_VERSION_ID >= 80400 && !$propertyReflector->isAbstract() && $propertyReflector->getHooks()) { |
||
226 | $notByRef->{$propertyReflector->name} = $propertyReflector->setRawValue(...); |
||
227 | } elseif ($propertyReflector->isReadOnly()) { |
||
228 | $notByRef->{$propertyReflector->name} = true; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return $baseHydrator->bindTo($notByRef, $class); |
||
233 | } |
||
234 | |||
235 | if ($classReflector->name !== $class) { |
||
236 | return self::$simpleHydrators[$classReflector->name] ??= self::getSimpleHydrator($classReflector->name); |
||
237 | } |
||
238 | |||
239 | $propertySetters = []; |
||
240 | foreach ($classReflector->getProperties() as $propertyReflector) { |
||
241 | if (!$propertyReflector->isStatic()) { |
||
242 | $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | if (!$propertySetters) { |
||
247 | return $baseHydrator; |
||
248 | } |
||
249 | |||
250 | return static function ($properties, $object) use ($propertySetters) { |
||
251 | foreach ($properties as $name => &$value) { |
||
252 | if ($setValue = $propertySetters[$name] ?? null) { |
||
253 | $setValue($object, $value); |
||
254 | } else { |
||
255 | $object->$name = $value; |
||
256 | $object->$name = &$value; |
||
257 | } |
||
321 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.