| Total Lines | 93 |
| Code Lines | 53 |
| 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 |
||
| 55 | public static function getHydrator($class) |
||
| 56 | { |
||
| 57 | $baseHydrator = self::$hydrators['stdClass'] ??= static function ($properties, $objects) { |
||
| 58 | foreach ($properties as $name => $values) { |
||
| 59 | foreach ($values as $i => $v) { |
||
| 60 | $objects[$i]->$name = $v; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | }; |
||
| 64 | |||
| 65 | switch ($class) { |
||
| 66 | case 'stdClass': |
||
| 67 | return $baseHydrator; |
||
| 68 | |||
| 69 | case 'ErrorException': |
||
| 70 | return $baseHydrator->bindTo(null, new class extends \ErrorException { |
||
| 71 | }); |
||
| 72 | |||
| 73 | case 'TypeError': |
||
| 74 | return $baseHydrator->bindTo(null, new class extends \Error { |
||
| 75 | }); |
||
| 76 | |||
| 77 | case 'SplObjectStorage': |
||
| 78 | return static function ($properties, $objects) { |
||
| 79 | foreach ($properties as $name => $values) { |
||
| 80 | if ("\0" === $name) { |
||
| 81 | foreach ($values as $i => $v) { |
||
| 82 | for ($j = 0; $j < \count($v); ++$j) { |
||
| 83 | $objects[$i]->attach($v[$j], $v[++$j]); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | foreach ($values as $i => $v) { |
||
| 89 | $objects[$i]->$name = $v; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | }; |
||
| 93 | } |
||
| 94 | |||
| 95 | if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) { |
||
| 96 | throw new ClassNotFoundException($class); |
||
| 97 | } |
||
| 98 | $classReflector = new \ReflectionClass($class); |
||
| 99 | |||
| 100 | switch ($class) { |
||
| 101 | case 'ArrayIterator': |
||
| 102 | case 'ArrayObject': |
||
| 103 | $constructor = $classReflector->getConstructor()->invokeArgs(...); |
||
| 104 | |||
| 105 | return static function ($properties, $objects) use ($constructor) { |
||
| 106 | foreach ($properties as $name => $values) { |
||
| 107 | if ("\0" !== $name) { |
||
| 108 | foreach ($values as $i => $v) { |
||
| 109 | $objects[$i]->$name = $v; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | foreach ($properties["\0"] ?? [] as $i => $v) { |
||
| 114 | $constructor($objects[$i], $v); |
||
| 115 | } |
||
| 116 | }; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!$classReflector->isInternal()) { |
||
| 120 | return $baseHydrator->bindTo(null, $class); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($classReflector->name !== $class) { |
||
| 124 | return self::$hydrators[$classReflector->name] ??= self::getHydrator($classReflector->name); |
||
| 125 | } |
||
| 126 | |||
| 127 | $propertySetters = []; |
||
| 128 | foreach ($classReflector->getProperties() as $propertyReflector) { |
||
| 129 | if (!$propertyReflector->isStatic()) { |
||
| 130 | $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!$propertySetters) { |
||
| 135 | return $baseHydrator; |
||
| 136 | } |
||
| 137 | |||
| 138 | return static function ($properties, $objects) use ($propertySetters) { |
||
| 139 | foreach ($properties as $name => $values) { |
||
| 140 | if ($setValue = $propertySetters[$name] ?? null) { |
||
| 141 | foreach ($values as $i => $v) { |
||
| 142 | $setValue($objects[$i], $v); |
||
| 143 | } |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | foreach ($values as $i => $v) { |
||
| 147 | $objects[$i]->$name = $v; |
||
| 148 | } |
||
| 321 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.