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 | View Code Duplication | class Utterance implements ApiResponseInterface |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var int |
||
11 | */ |
||
12 | private $utteranceId; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $text; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $tones; |
||
23 | |||
24 | /** |
||
25 | * Utterance constructor. |
||
26 | * |
||
27 | * @param int $utteranceId |
||
28 | * @param string $text |
||
29 | * @param array $tones |
||
30 | */ |
||
31 | 3 | public function __construct($utteranceId, $text, array $tones) |
|
37 | |||
38 | /** |
||
39 | * Create individual utterance |
||
40 | * |
||
41 | * @param array $data |
||
42 | * |
||
43 | * @return \IBM\Watson\ToneAnalyzer\Model\Utterance |
||
44 | */ |
||
45 | 3 | public static function create(array $data) |
|
55 | |||
56 | /** |
||
57 | * Get utterance id |
||
58 | * |
||
59 | * @return int |
||
60 | */ |
||
61 | 3 | public function getId() |
|
65 | |||
66 | /** |
||
67 | * Get utterance text |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 3 | public function getText() |
|
75 | |||
76 | /** |
||
77 | * Get utterance tones |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 3 | public function getTones() |
|
85 | } |
||
86 |
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.