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 |
||
7 | class FlashService |
||
8 | { |
||
9 | const ERROR = 'error'; |
||
10 | const SUCCESS = 'success'; |
||
11 | |||
12 | /** @var ISession */ |
||
13 | protected $session; |
||
14 | |||
15 | /** |
||
16 | * Helper constructor. |
||
17 | * |
||
18 | * @param ISession $session |
||
19 | */ |
||
20 | 7 | public function __construct(ISession $session) |
|
24 | |||
25 | /** |
||
26 | * @param string[] $value |
||
27 | */ |
||
28 | 1 | View Code Duplication | public function mergeSuccessMessages(array $value) |
36 | |||
37 | /** |
||
38 | * @param string[] $value |
||
39 | */ |
||
40 | 1 | View Code Duplication | public function mergeErrorMessages(array $value) |
48 | |||
49 | /** |
||
50 | * @param string $key |
||
51 | * @param string[] $value |
||
52 | */ |
||
53 | 1 | public function mergeFlashMessages(string $key, array $value) |
|
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | 1 | public function retrieveSuccessMessages() |
|
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | 1 | public function retrieveErrorMessages() |
|
77 | |||
78 | /** |
||
79 | * @param string $key |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | 1 | public function retrieveFlashMessages(string $key) |
|
87 | } |
||
88 | |||
89 |
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.