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 |
||
52 | class Charge extends Model |
||
53 | { |
||
54 | const STATUS_FAILED = 'failed'; |
||
55 | const STATUS_EXPIRED = 'expired'; |
||
56 | const STATUS_PENDING = 'pending'; |
||
57 | const STATUS_REVERSED = 'reversed'; |
||
58 | const STATUS_SUCCESSFUL = 'successful'; |
||
59 | |||
60 | const EVENT_CREATE = 'charge.create'; |
||
61 | const EVENT_UPDATE = 'charge.update'; |
||
62 | const EVENT_CAPTURE = 'charge.capture'; |
||
63 | const EVENT_REVERSE = 'charge.reverse'; |
||
64 | const EVENT_COMPLETE = 'charge.complete'; |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | 6 | */ |
|
69 | public function __set($name, $value) |
||
77 | |||
78 | /** |
||
79 | * @param string $countryCode |
||
80 | * |
||
81 | * @return array |
||
82 | 4 | */ |
|
83 | public function getCreateData($countryCode = Country::TH) |
||
102 | |||
103 | /** |
||
104 | * @param string $countryCode |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | View Code Duplication | public function getCreateUsingTokenData($countryCode = Country::TH) |
|
117 | |||
118 | /** |
||
119 | * @param string $countryCode |
||
120 | 1 | * |
|
121 | * @return array |
||
122 | */ |
||
123 | 1 | View Code Duplication | public function getCreateUsingSourceData($countryCode = Country::TH) |
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getUpdateData() |
||
143 | |||
144 | /** |
||
145 | * @return array|Refund[] |
||
146 | */ |
||
147 | public function getRefunds() |
||
155 | |||
156 | /** |
||
157 | * @param $id |
||
158 | * |
||
159 | * @return Refund|null |
||
160 | */ |
||
161 | public function findRefunds($id) |
||
176 | } |
||
177 |
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.