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 | abstract class AbstractSegment implements SegmentInterface |
||
11 | { |
||
12 | const SEGMENT_SEPARATOR = "'"; |
||
13 | const DEFAULT_COUNTRY_CODE = 280; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $type; |
||
17 | /** @var int */ |
||
18 | protected $segmentNumber; |
||
19 | /** @var int */ |
||
20 | protected $version; |
||
21 | /** @var array */ |
||
22 | protected $dataElements; |
||
23 | |||
24 | /** |
||
25 | * AbstractSegment constructor. |
||
26 | * |
||
27 | * @param $type |
||
28 | * @param $segmentNumber |
||
29 | * @param $version |
||
30 | * @param array $dataElements |
||
31 | */ |
||
32 | 4 | public function __construct($type, $segmentNumber, $version, array $dataElements = array()) |
|
39 | |||
40 | /** |
||
41 | * @param array $dataElements |
||
42 | */ |
||
43 | 4 | public function setDataElements(array $dataElements = array()) |
|
47 | |||
48 | /** |
||
49 | * @return array |
||
50 | */ |
||
51 | 4 | public function getDataElements() |
|
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | 4 | public function toString() |
|
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | 4 | public function __toString() |
|
77 | |||
78 | /** |
||
79 | * @param bool $translateCodes |
||
80 | * @return string |
||
81 | */ |
||
82 | View Code Duplication | public function humanReadable($translateCodes = false) |
|
92 | |||
93 | /** |
||
94 | * @return int |
||
95 | */ |
||
96 | public function getSegmentNumber() |
||
100 | |||
101 | /** |
||
102 | * @return int |
||
103 | */ |
||
104 | public function getVersion() |
||
108 | } |
||
109 |
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.