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 |
||
14 | class modules_helper |
||
15 | { |
||
16 | /** |
||
17 | * Auth object |
||
18 | * @var \phpbb\auth\auth |
||
19 | */ |
||
20 | protected $auth; |
||
21 | |||
22 | /** |
||
23 | * phpBB config |
||
24 | * @var \phpbb\config\config |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | /** @var \phpbb\controller\helper Controller helper */ |
||
29 | protected $controller_helper; |
||
30 | |||
31 | /** |
||
32 | * phpBB request |
||
33 | * @var \phpbb\request\request |
||
34 | */ |
||
35 | protected $request; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * NOTE: The parameters of this method must match in order and type with |
||
40 | * the dependencies defined in the services.yml file for this service. |
||
41 | * @param \phpbb\auth\auth $auth Auth object |
||
42 | * @param \phpbb\config\config $config phpBB config |
||
43 | * @param \phpbb\controller\helper $controller_helper Controller helper |
||
44 | * @param \phpbb\request\request $request phpBB request |
||
45 | */ |
||
46 | 100 | public function __construct($auth, $config, $controller_helper, $request) |
|
53 | |||
54 | /** |
||
55 | * Get an array of disallowed forums |
||
56 | * |
||
57 | * @param bool $disallow_access Whether the array for disallowing access |
||
58 | * should be filled |
||
59 | * @return array Array of forums the user is not allowed to access |
||
60 | */ |
||
61 | 32 | public function get_disallowed_forums($disallow_access) |
|
74 | |||
75 | /** |
||
76 | * Generate select box |
||
77 | * |
||
78 | * @param string $key Key of select box |
||
79 | * @param array $select_ary Array of select box options |
||
80 | * @param array $selected_options Array of selected options |
||
81 | * @param bool $multiple Whether multiple options should be selectable |
||
82 | * |
||
83 | * @return string HTML code of select box |
||
84 | * @access public |
||
85 | */ |
||
86 | 4 | public function generate_select_box($key, $select_ary, $selected_options, $multiple = false) |
|
87 | { |
||
88 | // Build options |
||
89 | 4 | $options = '<select id="' . $key . '" name="' . $key; |
|
90 | 4 | $options .= ($multiple) ? '[]" multiple="multiple">' : '">'; |
|
91 | 4 | foreach ($select_ary as $id => $option) |
|
92 | { |
||
93 | 4 | $options .= '<option value="' . $option['value'] . '"' . ((in_array($option['value'], $selected_options)) ? ' selected="selected"' : '') . (!empty($option['disabled']) ? ' disabled="disabled" class="disabled-option"' : '') . '>' . $option['title'] . '</option>'; |
|
94 | 4 | } |
|
95 | 4 | $options .= '</select>'; |
|
96 | |||
97 | 4 | return $options; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Generate forum select box |
||
102 | * |
||
103 | * @param string $value Value of select box |
||
104 | * @param string $key Key of select box |
||
105 | * |
||
106 | * @return string HTML code of select box |
||
107 | * @access public |
||
108 | */ |
||
109 | 1 | public function generate_forum_select($value, $key) |
|
110 | { |
||
111 | 1 | $forum_list = make_forum_select(false, false, true, true, true, false, true); |
|
112 | |||
113 | 1 | $selected_options = $select_ary = array(); |
|
114 | 1 | if (isset($this->config[$key]) && strlen($this->config[$key]) > 0) |
|
115 | 1 | { |
|
116 | 1 | $selected_options = explode(',', $this->config[$key]); |
|
117 | 1 | } |
|
118 | |||
119 | // Build forum options |
||
120 | 1 | foreach ($forum_list as $f_id => $f_row) |
|
121 | { |
||
122 | 1 | $select_ary[] = array( |
|
123 | 1 | 'value' => $f_id, |
|
124 | 1 | 'title' => $f_row['padding'] . $f_row['forum_name'], |
|
125 | 1 | 'disabled' => $f_row['disabled'], |
|
126 | ); |
||
127 | 1 | } |
|
128 | |||
129 | 1 | return $this->generate_select_box($key, $select_ary, $selected_options, true); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Store selected forums |
||
134 | * |
||
135 | * @param string $key Key name |
||
136 | * |
||
137 | * @return null |
||
138 | * @access public |
||
139 | */ |
||
140 | 1 | View Code Duplication | public function store_selected_forums($key) |
147 | |||
148 | /** |
||
149 | * Wrapper method for controller_helper::route() |
||
150 | * |
||
151 | * @param string $route Route name |
||
152 | * @param array $params Route parameters |
||
153 | * @param bool $is_amp |
||
154 | * @param bool $session_id |
||
155 | * @param bool $reference_type |
||
156 | * |
||
157 | * @return string URL for route |
||
158 | */ |
||
159 | 1 | public function route($route, $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH) |
|
163 | |||
164 | /** |
||
165 | * Display radio buttons for left/right choice |
||
166 | * |
||
167 | * @param int $value Selected value |
||
168 | * @param string $key Key of config variable |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 2 | public function display_left_right($value, $key) |
|
178 | |||
179 | /** |
||
180 | * Store left right choice |
||
181 | * |
||
182 | * @param string $key Config key |
||
183 | */ |
||
184 | 1 | public function store_left_right($key) |
|
191 | |||
192 | /** |
||
193 | * Show available styles |
||
194 | * |
||
195 | * @return string Select with available styles |
||
196 | */ |
||
197 | 2 | public function display_fa_styles() |
|
223 | |||
224 | /** |
||
225 | * Save styles that have been set as Font Awesome styles |
||
226 | * |
||
227 | * @param string $key Key of the parameter |
||
228 | */ |
||
229 | public function store_fa_styles($key) |
||
240 | } |
||
241 |