| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 141 |
| Code Lines | 91 |
| 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 |
||
| 90 | private function addService(Definition $definition, ?string $id, \DOMElement $parent): void |
||
| 91 | { |
||
| 92 | $service = $this->document->createElement('service'); |
||
| 93 | if (null !== $id) { |
||
| 94 | $service->setAttribute('id', $id); |
||
| 95 | } |
||
| 96 | if ($class = $definition->getClass()) { |
||
| 97 | if (str_starts_with($class, '\\')) { |
||
| 98 | $class = substr($class, 1); |
||
| 99 | } |
||
| 100 | |||
| 101 | $service->setAttribute('class', $class); |
||
| 102 | } |
||
| 103 | if (!$definition->isShared()) { |
||
| 104 | $service->setAttribute('shared', 'false'); |
||
| 105 | } |
||
| 106 | if ($definition->isPublic()) { |
||
| 107 | $service->setAttribute('public', 'true'); |
||
| 108 | } |
||
| 109 | if ($definition->isSynthetic()) { |
||
| 110 | $service->setAttribute('synthetic', 'true'); |
||
| 111 | } |
||
| 112 | if ($definition->isLazy()) { |
||
| 113 | $service->setAttribute('lazy', 'true'); |
||
| 114 | } |
||
| 115 | if (null !== $decoratedService = $definition->getDecoratedService()) { |
||
| 116 | [$decorated, $renamedId, $priority] = $decoratedService; |
||
| 117 | $service->setAttribute('decorates', $decorated); |
||
| 118 | |||
| 119 | $decorationOnInvalid = $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
||
| 120 | if (\in_array($decorationOnInvalid, [ContainerInterface::IGNORE_ON_INVALID_REFERENCE, ContainerInterface::NULL_ON_INVALID_REFERENCE], true)) { |
||
| 121 | $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE === $decorationOnInvalid ? 'null' : 'ignore'; |
||
| 122 | $service->setAttribute('decoration-on-invalid', $invalidBehavior); |
||
| 123 | } |
||
| 124 | if (null !== $renamedId) { |
||
| 125 | $service->setAttribute('decoration-inner-name', $renamedId); |
||
| 126 | } |
||
| 127 | if (0 !== $priority) { |
||
| 128 | $service->setAttribute('decoration-priority', $priority); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $tags = $definition->getTags(); |
||
| 133 | $tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors()); |
||
| 134 | foreach ($tags as $name => $tags) { |
||
| 135 | foreach ($tags as $attributes) { |
||
| 136 | $tag = $this->document->createElement('tag'); |
||
| 137 | |||
| 138 | // Check if we have recursive attributes |
||
| 139 | if (array_filter($attributes, \is_array(...))) { |
||
| 140 | $tag->setAttribute('name', $name); |
||
| 141 | $this->addTagRecursiveAttributes($tag, $attributes); |
||
| 142 | } else { |
||
| 143 | if (!\array_key_exists('name', $attributes)) { |
||
| 144 | $tag->setAttribute('name', $name); |
||
| 145 | } else { |
||
| 146 | $tag->appendChild($this->document->createTextNode($name)); |
||
| 147 | } |
||
| 148 | foreach ($attributes as $key => $value) { |
||
| 149 | $tag->setAttribute($key, $value ?? ''); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | $service->appendChild($tag); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($definition->getFile()) { |
||
| 157 | $file = $this->document->createElement('file'); |
||
| 158 | $file->appendChild($this->document->createTextNode($definition->getFile())); |
||
| 159 | $service->appendChild($file); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($parameters = $definition->getArguments()) { |
||
| 163 | $this->convertParameters($parameters, 'argument', $service); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($parameters = $definition->getProperties()) { |
||
| 167 | $this->convertParameters($parameters, 'property', $service, 'name'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->addMethodCalls($definition->getMethodCalls(), $service); |
||
| 171 | |||
| 172 | if ($callable = $definition->getFactory()) { |
||
| 173 | if (\is_array($callable) && ['Closure', 'fromCallable'] !== $callable && $definition->getClass() === $callable[0]) { |
||
| 174 | $service->setAttribute('constructor', $callable[1]); |
||
| 175 | } else { |
||
| 176 | $factory = $this->document->createElement('factory'); |
||
| 177 | |||
| 178 | if (\is_array($callable) && $callable[0] instanceof Definition) { |
||
| 179 | $this->addService($callable[0], null, $factory); |
||
| 180 | $factory->setAttribute('method', $callable[1]); |
||
| 181 | } elseif (\is_array($callable)) { |
||
| 182 | if (null !== $callable[0]) { |
||
| 183 | $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]); |
||
| 184 | } |
||
| 185 | $factory->setAttribute('method', $callable[1]); |
||
| 186 | } else { |
||
| 187 | $factory->setAttribute('function', $callable); |
||
| 188 | } |
||
| 189 | $service->appendChild($factory); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($definition->isDeprecated()) { |
||
| 194 | $deprecation = $definition->getDeprecation('%service_id%'); |
||
| 195 | $deprecated = $this->document->createElement('deprecated'); |
||
| 196 | $deprecated->appendChild($this->document->createTextNode($definition->getDeprecation('%service_id%')['message'])); |
||
| 197 | $deprecated->setAttribute('package', $deprecation['package']); |
||
| 198 | $deprecated->setAttribute('version', $deprecation['version']); |
||
| 199 | |||
| 200 | $service->appendChild($deprecated); |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($definition->isAutowired()) { |
||
| 204 | $service->setAttribute('autowire', 'true'); |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($definition->isAutoconfigured()) { |
||
| 208 | $service->setAttribute('autoconfigure', 'true'); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($definition->isAbstract()) { |
||
| 212 | $service->setAttribute('abstract', 'true'); |
||
| 213 | } |
||
| 214 | |||
| 215 | if ($callable = $definition->getConfigurator()) { |
||
| 216 | $configurator = $this->document->createElement('configurator'); |
||
| 217 | |||
| 218 | if (\is_array($callable) && $callable[0] instanceof Definition) { |
||
| 219 | $this->addService($callable[0], null, $configurator); |
||
| 220 | $configurator->setAttribute('method', $callable[1]); |
||
| 221 | } elseif (\is_array($callable)) { |
||
| 222 | $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]); |
||
| 223 | $configurator->setAttribute('method', $callable[1]); |
||
| 224 | } else { |
||
| 225 | $configurator->setAttribute('function', $callable); |
||
| 226 | } |
||
| 227 | $service->appendChild($configurator); |
||
| 228 | } |
||
| 229 | |||
| 230 | $parent->appendChild($service); |
||
| 231 | } |
||
| 433 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.