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 |
||
| 29 | class absences_OptionsEditor extends Widget_Form |
||
| 30 | { |
||
| 31 | private $I; |
||
| 32 | |||
| 33 | public function __construct() |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | protected function addFields() |
||
| 58 | { |
||
| 59 | $W = bab_Widgets(); |
||
| 60 | |||
| 61 | $this->addItem($W->Acl()->setTitle(absences_translate('Vacations mangers'))->setName('managers')); |
||
| 62 | |||
| 63 | if (null !== bab_OCgetPrimaryOcId()) |
||
| 64 | { |
||
| 65 | $this->addItem($this->id_chart()); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->addItem($this->boolfield('chart_superiors_create_request', absences_translate("Allow delegated managers to create vacation requests for users in chart"))); |
||
| 69 | $this->addItem($this->boolfield('chart_superiors_set_rights', absences_translate("Allow delegated managers to update vacations rights for users in chart"))); |
||
| 70 | $this->addItem($this->boolfield('chart_superiors_user_edit', absences_translate("Allow delegated managers to update users settings for users in chart"))); |
||
| 71 | $this->addItem($this->boolfield('allow_mismatch', absences_translate("Allow users to create vacation requests with mismatch in total number of days"))); |
||
| 72 | $this->addItem($this->boolfield('workperiod_recover_request', absences_translate("Allow users to create workperiods request recovery"))); |
||
| 73 | $this->addItem($this->boolfield('display_personal_history', absences_translate("Users can access to the personal history"))); |
||
| 74 | $this->addItem($this->boolfield('modify_confirmed', absences_translate("Users can edit or delete confirmed requests"))); |
||
| 75 | $this->addItem($this->boolfield('modify_waiting', absences_translate("Users can edit or delete waiting requests"))); |
||
| 76 | $this->addItem($this->boolfield('email_manager_ondelete', absences_translate('Notify managers when a vacation request is deleted'))); |
||
| 77 | $this->addItem($this->boolfield('approb_email_defer', absences_translate('Use defered approbation email (daily notification)'), |
||
| 78 | absences_translate('The notification is sent every day between 12:00 and 13:00 to all approvers with waiting items'))); |
||
| 79 | $this->addItem($this->boolfield('user_add_email', absences_translate('Allow users to notify emails about the accepted vacations requests'))); |
||
| 80 | |||
| 81 | |||
| 82 | $this->addItem($W->Acl()->setTitle(absences_translate('Who can view the public calendar?'))->setName('public_planning')); |
||
| 83 | |||
| 84 | $this->addItem($this->boolfield('appliquant_email', absences_translate('Send an email to appliquant when a request is confirmed'))); |
||
| 85 | |||
| 86 | $this->addItem($W->LabelledWidget( |
||
| 87 | absences_translate('Alert for waiting request not modified in more than'), |
||
| 88 | $W->LineEdit()->setSize(3)->setMaxSize(3), |
||
| 89 | 'approb_alert', |
||
| 90 | absences_translate('Requests exceeding this period will be displayed in red in the email sent to approvers and displayed as bold in the manager waiting requests list, no additional mail are sent with this parameter'), |
||
| 91 | absences_translate('days') |
||
| 92 | )); |
||
| 93 | |||
| 94 | $this->addItem($this->boolfield('auto_approval', absences_translate('Auto approve if current user is responsible of the first approval step'))); |
||
| 95 | |||
| 96 | $this->addItem($W->LabelledWidget( |
||
| 97 | absences_translate('Auto confirm waiting request not modified in more than'), |
||
| 98 | $W->LineEdit()->setSize(3)->setMaxSize(3), |
||
| 99 | 'auto_confirm', |
||
| 100 | null, |
||
| 101 | absences_translate('days') |
||
| 102 | )); |
||
| 103 | |||
| 104 | $this->addItem($W->LabelledWidget( |
||
| 105 | absences_translate('End of avaibility for the recuperation in days after the working periode'), |
||
| 106 | $W->LineEdit()->setSize(3)->setMaxSize(3), |
||
| 107 | 'end_recup', |
||
| 108 | null, |
||
| 109 | absences_translate('days') |
||
| 110 | )); |
||
| 111 | |||
| 112 | $this->addItem($W->LabelledWidget( |
||
| 113 | absences_translate("Anticipation delay for the recovery period deposit"), |
||
| 114 | $W->LineEdit()->setSize(3)->setMaxSize(3), |
||
| 115 | 'delay_recovery', |
||
| 116 | null, |
||
| 117 | absences_translate('days') |
||
| 118 | )); |
||
| 119 | |||
| 120 | $months = array( |
||
| 121 | 1 => absences_translate('January'), |
||
| 122 | 2 => absences_translate('February'), |
||
| 123 | 3 => absences_translate('March'), |
||
| 124 | 4 => absences_translate('April'), |
||
| 125 | 5 => absences_translate('May'), |
||
| 126 | 6 => absences_translate('June'), |
||
| 127 | 7 => absences_translate('July'), |
||
| 128 | 8 => absences_translate('August'), |
||
| 129 | 9 => absences_translate('September'), |
||
| 130 | 10 => absences_translate('October'), |
||
| 131 | 11 => absences_translate('November'), |
||
| 132 | 12 => absences_translate('December') |
||
| 133 | ); |
||
| 134 | |||
| 135 | $this->addItem( |
||
| 136 | $W->VBoxItems( |
||
| 137 | $W->Label(absences_translate('The day when the periode is changing') . ' :')->addClass('widget-description'), |
||
| 138 | $W->FlowItems( |
||
| 139 | $W->LineEdit()->setSize(2)->setMaxSize(2)->setName('archivage_day'), |
||
| 140 | $W->Select()->setOptions($months)->setName('archivage_month') |
||
| 141 | ) |
||
| 142 | ) |
||
| 143 | ); |
||
| 144 | |||
| 145 | $this->addItem($this->boolfield('entity_planning', absences_translate("Allow chart members to access the entity planning of their main entity"))); |
||
| 146 | $this->addItem($this->boolfield('entity_planning_display_types', absences_translate("Display types and legend for all users in the entity planning, instead of entity responsible and managers only"))); |
||
| 147 | $this->addItem($this->boolfield('maintenance', absences_translate("Use mainteance mode, only managers can create or modify requests"))); |
||
| 148 | $this->addItem($this->organization_sync()); |
||
| 149 | $this->addItem($this->boolfield('sync_server', absences_translate("This server act as a synchronization server"))); |
||
| 150 | |||
| 151 | $client = $W->Frame(null, $W->VBoxLayout()->setVerticalSpacing(1,'em')); |
||
| 152 | |||
| 153 | $client->addItem($W->LabelledWidget( |
||
| 154 | absences_translate('Shared rights server url'), |
||
| 155 | $W->LineEdit()->setSize(40)->setMaxSize(255), |
||
| 156 | 'sync_url' |
||
| 157 | )); |
||
| 158 | |||
| 159 | $client->addItem($W->LabelledWidget( |
||
| 160 | absences_translate('Nickname'), |
||
| 161 | $W->LineEdit()->setSize(20)->setMaxSize(255), |
||
| 162 | 'sync_nickname' |
||
| 163 | )); |
||
| 164 | |||
| 165 | $client->addItem($W->LabelledWidget( |
||
| 166 | absences_translate('Password'), |
||
| 167 | $W->LineEdit()->setSize(20)->setMaxSize(255), |
||
| 168 | 'sync_password' |
||
| 169 | )); |
||
| 170 | |||
| 171 | $this->addItem($W->Section(absences_translate('This server act as a synchronization client'), $client)->setFoldable(true, true)); |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | protected function organization_sync() |
||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | View Code Duplication | protected function id_chart() |
|
| 207 | |||
| 208 | |||
| 209 | protected function boolfield($name, $title, $description = null) |
||
| 214 | |||
| 215 | |||
| 216 | protected function loadFormValues() |
||
| 229 | |||
| 230 | |||
| 231 | View Code Duplication | protected function addButtons() |
|
| 241 | } |
||
| 242 |
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.