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 namespace Nord\Lumen\Search; |
||
5 | class StringParser |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var string |
||
10 | */ |
||
11 | private $separator = ';'; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $delimiter = ':'; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Configuration constructor. |
||
21 | * |
||
22 | * @param array $config |
||
23 | */ |
||
24 | public function __construct(array $config = []) |
||
28 | |||
29 | |||
30 | /** |
||
31 | * @param $string |
||
32 | * |
||
33 | * @return array |
||
34 | * @throws InvalidArgument |
||
35 | */ |
||
36 | public function parse($string) |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param array $config |
||
54 | */ |
||
55 | View Code Duplication | protected function configure(array $config) |
|
65 | |||
66 | |||
67 | /** |
||
68 | * @param string $string |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | protected function splitItems($string) |
||
76 | |||
77 | |||
78 | /** |
||
79 | * @param string $string |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function splitItem($string) |
||
87 | } |
||
88 |
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.