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 |
||
13 | class CheckInValidator extends SS_Object |
||
14 | { |
||
15 | const MESSAGE_ERROR = 'MESSAGE_ERROR'; |
||
16 | const MESSAGE_NO_CODE = 'MESSAGE_NO_CODE'; |
||
17 | const MESSAGE_CODE_NOT_FOUND = 'MESSAGE_CODE_NOT_FOUND'; |
||
18 | const MESSAGE_TICKET_CANCELLED = 'MESSAGE_TICKET_CANCELLED'; |
||
19 | const MESSAGE_ALREADY_CHECKED_IN = 'MESSAGE_ALREADY_CHECKED_IN'; |
||
20 | const MESSAGE_CHECK_OUT_SUCCESS = 'MESSAGE_CHECK_OUT_SUCCESS'; |
||
21 | const MESSAGE_CHECK_IN_SUCCESS = 'MESSAGE_CHECK_IN_SUCCESS'; |
||
22 | |||
23 | const MESSAGE_TYPE_GOOD = 'Good'; |
||
24 | const MESSAGE_TYPE_WARNING = 'Warning'; |
||
25 | const MESSAGE_TYPE_BAD = 'Bad'; |
||
26 | |||
27 | /** |
||
28 | * Allow people to check in and out |
||
29 | * |
||
30 | * @config |
||
31 | * @var bool |
||
32 | */ |
||
33 | private static $allow_checkout = false; |
||
34 | |||
35 | /** |
||
36 | * @var Attendee |
||
37 | */ |
||
38 | protected $attendee = null; |
||
39 | |||
40 | /** |
||
41 | * Validate the given ticket code |
||
42 | * |
||
43 | * @param null|string $ticketCode |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function validate($ticketCode = null) |
||
122 | |||
123 | /** |
||
124 | * Get the attendee instance |
||
125 | * |
||
126 | * @return Attendee |
||
127 | */ |
||
128 | public function getAttendee() |
||
132 | |||
133 | /** |
||
134 | * Translate the given type to a readable message |
||
135 | * |
||
136 | * @param $message string |
||
137 | * @param $ticket string |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | private static function message($message, $ticket = null) { |
||
144 | } |
||
145 |
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.