| Conditions | 11 |
| Paths | 2 |
| Total Lines | 46 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 29 | public function create(object|string|array $transformer): callable |
||
| 30 | { |
||
| 31 | $transformerId = $this->getCallableKey($transformer); |
||
| 32 | |||
| 33 | if (!isset($this->transformerCache[$transformerId])) { |
||
| 34 | $this->transformerCache[$transformerId] = function (...$params) use ($transformer) { |
||
| 35 | if ($transformer instanceof \Closure) { |
||
|
|
|||
| 36 | return $transformer(...$params); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (\is_array($transformer)) { |
||
| 40 | [$class, $method] = $transformer; |
||
| 41 | $object = \is_object($class) ? $class : $this->container->get($class); |
||
| 42 | if (null === $object) { |
||
| 43 | $object = new $class(); |
||
| 44 | } |
||
| 45 | /** @var object $object */ |
||
| 46 | $reflection = new \ReflectionMethod($object, $method); |
||
| 47 | $dependencies = []; |
||
| 48 | $paramsQueue = $params; |
||
| 49 | |||
| 50 | foreach ($reflection->getParameters() as $parameter) { |
||
| 51 | $type = $parameter->getType(); |
||
| 52 | if (!$type instanceof \ReflectionNamedType) { |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!$type->isBuiltin()) { |
||
| 57 | $dependencies[] = $this->container->get($type->getName()); |
||
| 58 | } elseif (!empty($paramsQueue)) { |
||
| 59 | $dependencies[] = array_shift($paramsQueue); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return $reflection->invoke($object, ...$dependencies); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!\is_callable($transformer)) { |
||
| 67 | throw new NotCallableException('Provided transformer is not callable.'); |
||
| 68 | } |
||
| 69 | |||
| 70 | return $transformer(...$params); |
||
| 71 | }; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $this->transformerCache[$transformerId]; |
||
| 75 | } |
||
| 108 |