| Conditions | 15 |
| Paths | 72 |
| Total Lines | 48 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| 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 |
||
| 116 | public function set(string $id, object $definition = null): object |
||
| 117 | { |
||
| 118 | unset($this->aliases[$id]); // Incase new service definition exists in aliases. |
||
| 119 | |||
| 120 | if (null !== ($this->services[$id] ?? $this->privates[$id] ?? null)) { |
||
| 121 | throw new FrozenServiceException(\sprintf('The "%s" service is already initialized, and cannot be replaced.', $id)); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (null === $definition) { |
||
| 125 | ($definition = new Definition($id))->bindWith($id, $this); |
||
| 126 | } elseif ($definition instanceof Definitions\Statement) { |
||
| 127 | if ($definition->isClosureWrappable()) { |
||
| 128 | if ($this->resolver->getBuilder()) { |
||
| 129 | $entity = new ArrowFunction(['expr' => $this->resolver->resolve($definition->getValue(), $definition->getArguments())]); |
||
| 130 | } |
||
| 131 | $definition = $entity ?? fn () => $this->resolver->resolve($definition->getValue(), $definition->getArguments()); |
||
| 132 | } else { |
||
| 133 | $definition = new Definition($definition->getValue(), $definition->getArguments()); |
||
| 134 | } |
||
| 135 | } elseif ($definition instanceof Definitions\Reference) { |
||
| 136 | if (null === $previousDef = $this->definitions[(string) $definition] ?? null) { |
||
| 137 | throw $this->createNotFound((string) $definition); |
||
| 138 | } |
||
| 139 | $definition = clone $previousDef; |
||
| 140 | |||
| 141 | if ($definition instanceof Definitions\ShareableDefinitionInterface) { |
||
| 142 | $definition->abstract(false); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($definition instanceof Definitions\DefinitionInterface) { |
||
| 147 | if ($definition instanceof Definitions\TypedDefinitionInterface) { |
||
| 148 | $definition->isTyped() && $this->type($id, $definition->getTypes()); |
||
|
|
|||
| 149 | } |
||
| 150 | |||
| 151 | if ($definition instanceof Definitions\DefinitionAwareInterface) { |
||
| 152 | /** @var \Rade\DI\Definitions\Traits\DefinitionAwareTrait $definition */ |
||
| 153 | if ($definition->hasTags()) { |
||
| 154 | foreach ($definition->getTags() as $tag => $value) { |
||
| 155 | $this->tag($id, $tag, $value); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $definition->bindWith($id, $this); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $this->definitions[$id] = $definition; |
||
| 164 | } |
||
| 217 |