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