| Conditions | 33 | 
| Paths | > 20000 | 
| Total Lines | 112 | 
| Code Lines | 63 | 
| 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 | ||
| 56 | private function addService(string $id, Definition $definition): string | ||
| 57 |     { | ||
| 58 | $code = " $id:\n"; | ||
| 59 |         if ($class = $definition->getClass()) { | ||
| 60 |             if (str_starts_with($class, '\\')) { | ||
| 61 | $class = substr($class, 1); | ||
| 62 | } | ||
| 63 | |||
| 64 |             $code .= \sprintf("        class: %s\n", $this->dumper->dump($class)); | ||
| 65 | } | ||
| 66 | |||
| 67 |         if (!$definition->isPrivate()) { | ||
| 68 |             $code .= \sprintf("        public: %s\n", $definition->isPublic() ? 'true' : 'false'); | ||
| 69 | } | ||
| 70 | |||
| 71 | $tagsCode = ''; | ||
| 72 | $tags = $definition->getTags(); | ||
| 73 | $tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors()); | ||
| 74 |         foreach ($tags as $name => $tags) { | ||
| 75 |             foreach ($tags as $attributes) { | ||
| 76 | $att = []; | ||
| 77 |                 foreach ($attributes as $key => $value) { | ||
| 78 |                     $att[] = \sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value)); | ||
| 79 | } | ||
| 80 |                 $att = $att ? ': { '.implode(', ', $att).' }' : ''; | ||
| 81 | |||
| 82 |                 $tagsCode .= \sprintf("            - %s%s\n", $this->dumper->dump($name), $att); | ||
| 83 | } | ||
| 84 | } | ||
| 85 |         if ($tagsCode) { | ||
| 86 | $code .= " tags:\n".$tagsCode; | ||
| 87 | } | ||
| 88 | |||
| 89 |         if ($definition->getFile()) { | ||
| 90 |             $code .= \sprintf("        file: %s\n", $this->dumper->dump($definition->getFile())); | ||
| 91 | } | ||
| 92 | |||
| 93 |         if ($definition->isSynthetic()) { | ||
| 94 | $code .= " synthetic: true\n"; | ||
| 95 | } | ||
| 96 | |||
| 97 |         if ($definition->isDeprecated()) { | ||
| 98 | $code .= " deprecated:\n"; | ||
| 99 |             foreach ($definition->getDeprecation('%service_id%') as $key => $value) { | ||
| 100 |                 if ('' !== $value) { | ||
| 101 |                     $code .= \sprintf("            %s: %s\n", $key, $this->dumper->dump($value)); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 |         if ($definition->isAutowired()) { | ||
| 107 | $code .= " autowire: true\n"; | ||
| 108 | } | ||
| 109 | |||
| 110 |         if ($definition->isAutoconfigured()) { | ||
| 111 | $code .= " autoconfigure: true\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 |         if ($definition->isAbstract()) { | ||
| 115 | $code .= " abstract: true\n"; | ||
| 116 | } | ||
| 117 | |||
| 118 |         if ($definition->isLazy()) { | ||
| 119 | $code .= " lazy: true\n"; | ||
| 120 | } | ||
| 121 | |||
| 122 |         if ($definition->getArguments()) { | ||
| 123 |             $code .= \sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0)); | ||
| 124 | } | ||
| 125 | |||
| 126 |         if ($definition->getProperties()) { | ||
| 127 |             $code .= \sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0)); | ||
| 128 | } | ||
| 129 | |||
| 130 |         if ($definition->getMethodCalls()) { | ||
| 131 |             $code .= \sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12)); | ||
| 132 | } | ||
| 133 | |||
| 134 |         if (!$definition->isShared()) { | ||
| 135 | $code .= " shared: false\n"; | ||
| 136 | } | ||
| 137 | |||
| 138 |         if (null !== $decoratedService = $definition->getDecoratedService()) { | ||
| 139 | [$decorated, $renamedId, $priority] = $decoratedService; | ||
| 140 |             $code .= \sprintf("        decorates: %s\n", $decorated); | ||
| 141 |             if (null !== $renamedId) { | ||
| 142 |                 $code .= \sprintf("        decoration_inner_name: %s\n", $renamedId); | ||
| 143 | } | ||
| 144 |             if (0 !== $priority) { | ||
| 145 |                 $code .= \sprintf("        decoration_priority: %s\n", $priority); | ||
| 146 | } | ||
| 147 | |||
| 148 | $decorationOnInvalid = $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; | ||
| 149 |             if (\in_array($decorationOnInvalid, [ContainerInterface::IGNORE_ON_INVALID_REFERENCE, ContainerInterface::NULL_ON_INVALID_REFERENCE])) { | ||
| 150 | $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE === $decorationOnInvalid ? 'null' : 'ignore'; | ||
| 151 |                 $code .= \sprintf("        decoration_on_invalid: %s\n", $invalidBehavior); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 |         if ($callable = $definition->getFactory()) { | ||
| 156 |             if (\is_array($callable) && ['Closure', 'fromCallable'] !== $callable && $definition->getClass() === $callable[0]) { | ||
| 157 |                 $code .= \sprintf("        constructor: %s\n", $callable[1]); | ||
| 158 |             } else { | ||
| 159 |                 $code .= \sprintf("        factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 |         if ($callable = $definition->getConfigurator()) { | ||
| 164 |             $code .= \sprintf("        configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0)); | ||
| 165 | } | ||
| 166 | |||
| 167 | return $code; | ||
| 168 | } | ||
| 381 |