Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class ContainerInteropLoggerFactory |
||
15 | { |
||
16 | const CONFIG_KEY = 'logger'; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $name; |
||
22 | |||
23 | /** |
||
24 | * @var ContainerInterface |
||
25 | */ |
||
26 | protected $container; |
||
27 | |||
28 | /** |
||
29 | * @var LoggerFactory |
||
30 | */ |
||
31 | private $loggerFactory; |
||
32 | |||
33 | 10 | public function __construct(string $name = 'default') |
|
37 | |||
38 | 10 | public function __invoke(ContainerInterface $container) : Logger |
|
46 | |||
47 | 2 | public static function __callStatic(string $name, array $arguments) : Logger |
|
59 | |||
60 | 10 | protected function getLoggerConfig(string $loggerName) : array |
|
81 | |||
82 | 10 | protected function createLogger(array $config) : Logger |
|
97 | |||
98 | 10 | protected function prepareHandlers(array $handlers) : array |
|
112 | |||
113 | protected function prepareProcessors(array $processors) : array |
||
123 | |||
124 | 4 | View Code Duplication | protected function resolveHandler(string $handlerName) : HandlerInterface |
138 | |||
139 | 4 | View Code Duplication | protected function resolveFormatter(string $formatterName) : FormatterInterface |
140 | { |
||
141 | 4 | $formatter = $this->resolveFromContainer($formatterName); |
|
142 | |||
143 | 4 | if (null === $formatter) { |
|
144 | 1 | throw InvalidContainerServiceException::forUnresolved('formatter', $formatterName); |
|
145 | } |
||
146 | |||
147 | 3 | if (! $formatter instanceof FormatterInterface) { |
|
148 | throw InvalidContainerServiceException::forInvalid('formatter', $formatterName, FormatterInterface::class); |
||
149 | } |
||
150 | |||
151 | 3 | return $formatter; |
|
152 | } |
||
153 | |||
154 | 4 | View Code Duplication | protected function resolveProcessor(string $processorName) : callable |
155 | { |
||
156 | 4 | $processor = $this->resolveFromContainer($processorName); |
|
157 | |||
158 | 4 | if (null === $processor) { |
|
159 | 1 | throw InvalidContainerServiceException::forUnresolved('processor', $processorName); |
|
160 | } |
||
161 | |||
162 | 3 | if (! is_callable($processor)) { |
|
163 | throw InvalidContainerServiceException::forInvalid('processor', $processorName, 'callable'); |
||
164 | } |
||
165 | |||
166 | 3 | return $processor; |
|
167 | } |
||
168 | |||
169 | 6 | final protected function resolveFromContainer(string $serviceOrFactory) |
|
182 | |||
183 | 7 | final protected function getLoggerFactory() : LoggerFactory |
|
191 | } |
||
192 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.