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 |
||
15 | View Code Duplication | class ChosenInlineResult extends BaseType |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | static protected $requiredParams = ['result_id', 'from', 'query']; |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | static protected $map = [ |
||
30 | 'result_id' => true, |
||
31 | 'from' => User::class, |
||
32 | 'location' => Location::class, |
||
33 | 'inline_message_id' => true, |
||
34 | 'query' => true, |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * The unique identifier for the result that was chosen. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $resultId; |
||
43 | |||
44 | /** |
||
45 | * The user that chose the result. |
||
46 | * |
||
47 | * @var User |
||
48 | */ |
||
49 | protected $from; |
||
50 | |||
51 | /** |
||
52 | * Optional. Sender location, only for bots that require user location |
||
53 | * |
||
54 | * @var Location |
||
55 | */ |
||
56 | protected $location; |
||
57 | |||
58 | /** |
||
59 | * Optional. Identifier of the sent inline message. |
||
60 | * Available only if there is an inline keyboard attached to the message. |
||
61 | * Will be also received in callback queries and can be used to edit the message. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $inlineMessageId; |
||
66 | |||
67 | /** |
||
68 | * The query that was used to obtain the result. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $query; |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | 2 | public function getResultId() |
|
81 | |||
82 | /** |
||
83 | * @param string $resultId |
||
84 | */ |
||
85 | 3 | public function setResultId($resultId) |
|
89 | |||
90 | /** |
||
91 | * @return User |
||
92 | */ |
||
93 | 2 | public function getFrom() |
|
97 | |||
98 | /** |
||
99 | * @param User $from |
||
100 | */ |
||
101 | 3 | public function setFrom(User $from) |
|
105 | |||
106 | /** |
||
107 | * @return Location |
||
108 | */ |
||
109 | public function getLocation() |
||
113 | |||
114 | /** |
||
115 | * @param Location $location |
||
116 | */ |
||
117 | public function setLocation($location) |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getInlineMessageId() |
||
129 | |||
130 | /** |
||
131 | * @param string $inlineMessageId |
||
132 | */ |
||
133 | public function setInlineMessageId($inlineMessageId) |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | 2 | public function getQuery() |
|
145 | |||
146 | /** |
||
147 | * @param string $query |
||
148 | */ |
||
149 | 3 | public function setQuery($query) |
|
153 | } |
||
154 |
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.