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 |
||
10 | class HandlerFactory |
||
11 | { |
||
12 | /** |
||
13 | * @var GenericStrategyBasedFactory |
||
14 | */ |
||
15 | private $genericFactory; |
||
16 | |||
17 | /** |
||
18 | * Initializes a new HandlerFactory. |
||
19 | * |
||
20 | * @param GenericStrategyBasedFactory $genericFactory |
||
21 | */ |
||
22 | 4 | public function __construct(GenericStrategyBasedFactory $genericFactory) |
|
26 | |||
27 | /** |
||
28 | * Returns an instance of the given handler class with the given configuration. |
||
29 | * |
||
30 | * @param string $handlerClass |
||
31 | * @param array $config |
||
32 | * @param FormatterInterface|null $formatter |
||
33 | * @param array|callable[] $processors |
||
34 | * |
||
35 | * @return HandlerInterface |
||
36 | * |
||
37 | * @throws \yii\base\InvalidConfigException |
||
38 | * @throws \InvalidArgumentException |
||
39 | */ |
||
40 | 4 | public function make( |
|
61 | |||
62 | /** |
||
63 | * Validates the given handler class. |
||
64 | * |
||
65 | * @param string $handlerClass |
||
66 | * |
||
67 | * @throws \InvalidArgumentException |
||
68 | */ |
||
69 | 4 | View Code Duplication | private function validateHandler(string $handlerClass): void |
76 | } |
||
77 |
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.