Total Complexity | 41 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ZombieReport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ZombieReport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class ZombieReport implements Countable |
||
11 | { |
||
12 | protected $additional_parameters = []; |
||
13 | |||
14 | protected $parameters_form = null; |
||
15 | |||
16 | public function __construct($additional_parameters = []) |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @return ZombieReport |
||
23 | */ |
||
24 | public static function create($additional_parameters = []) |
||
25 | { |
||
26 | return new self($additional_parameters); |
||
27 | } |
||
28 | |||
29 | public function get_additional_parameters() |
||
32 | } |
||
33 | |||
34 | public function get_parameters() |
||
35 | { |
||
36 | $result = [ |
||
37 | 'items' => [ |
||
38 | [ |
||
39 | 'name' => 'ceiling', |
||
40 | 'label' => get_lang('LastAccess'), |
||
41 | 'type' => 'date_picker', |
||
42 | 'default' => $this->get_ceiling('Y-m-d'), |
||
43 | 'rules' => [ |
||
44 | [ |
||
45 | 'type' => 'date', |
||
46 | 'message' => get_lang('Date'), |
||
47 | ], |
||
48 | ], |
||
49 | ], |
||
50 | [ |
||
51 | 'name' => 'active_only', |
||
52 | 'label' => get_lang('ActiveOnly'), |
||
53 | 'type' => 'checkbox', |
||
54 | 'default' => $this->get_active_only(), |
||
55 | ], |
||
56 | [ |
||
57 | 'name' => 'submit_button', |
||
58 | 'type' => 'button', |
||
59 | 'value' => get_lang('Search'), |
||
60 | 'attributes' => ['class' => 'search'], |
||
61 | ], |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | return $result; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return FormValidator |
||
70 | */ |
||
71 | public function get_parameters_form() |
||
72 | { |
||
73 | $form = new FormValidator( |
||
74 | 'zombie_report_parameters', |
||
75 | 'get', |
||
76 | null, |
||
77 | null, |
||
78 | ['class' => 'well form-horizontal form-search'] |
||
79 | ); |
||
80 | |||
81 | $form->addDatePicker('ceiling', get_lang('LastAccess')); |
||
82 | $form->addCheckBox('active_only', get_lang('ActiveOnly')); |
||
83 | $form->addButtonSearch(get_lang('Search')); |
||
84 | |||
85 | $params = [ |
||
86 | 'active_only' => $this->get_active_only(), |
||
87 | 'ceiling' => $this->get_ceiling('Y-m-d'), |
||
88 | ]; |
||
89 | $form->setDefaults($params); |
||
90 | $additional = $this->get_additional_parameters(); |
||
91 | foreach ($additional as $key => $value) { |
||
92 | $value = Security::remove_XSS($value); |
||
93 | $form->addHidden($key, $value); |
||
94 | } |
||
95 | |||
96 | return $form; |
||
97 | } |
||
98 | |||
99 | public function display_parameters($return = false) |
||
100 | { |
||
101 | $form = $this->get_parameters_form(); |
||
102 | $result = $form->returnForm(); |
||
103 | |||
104 | if ($return) { |
||
105 | return $result; |
||
106 | } else { |
||
107 | echo $result; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public function is_valid() |
||
112 | { |
||
113 | $form = $this->get_parameters_form(); |
||
114 | |||
115 | return $form->isSubmitted() == false || $form->validate(); |
||
116 | } |
||
117 | |||
118 | public function get_ceiling($format = null) |
||
132 | } |
||
133 | |||
134 | public function get_active_only() |
||
135 | { |
||
136 | $result = Request::get('active_only', false); |
||
137 | $result = $result === 'true' ? true : $result; |
||
138 | $result = $result === 'false' ? false : $result; |
||
139 | $result = (bool) $result; |
||
140 | |||
141 | return $result; |
||
142 | } |
||
143 | |||
144 | public function get_action() |
||
156 | } |
||
157 | |||
158 | public function perform_action() |
||
159 | { |
||
160 | $ids = Request::post('id'); |
||
161 | if (empty($ids)) { |
||
162 | return $ids; |
||
163 | } |
||
164 | |||
165 | $action = $this->get_action(); |
||
166 | switch ($action) { |
||
167 | case 'activate': |
||
168 | return UserManager::activate_users($ids); |
||
169 | break; |
||
|
|||
170 | case 'deactivate': |
||
171 | return UserManager::deactivate_users($ids); |
||
172 | break; |
||
173 | case 'delete': |
||
174 | return UserManager::delete_users($ids); |
||
175 | } |
||
176 | |||
177 | return false; |
||
178 | } |
||
179 | |||
180 | public function count() |
||
181 | { |
||
182 | $ceiling = $this->get_ceiling(); |
||
183 | $active_only = $this->get_active_only(); |
||
184 | $items = ZombieManager::listZombies($ceiling, $active_only, null, null); |
||
185 | |||
186 | return count($items); |
||
187 | } |
||
188 | |||
189 | public function get_data($from, $count, $column, $direction) |
||
190 | { |
||
191 | $ceiling = $this->get_ceiling(); |
||
192 | $active_only = $this->get_active_only(); |
||
193 | $items = ZombieManager::listZombies($ceiling, $active_only, $from, $count, $column, $direction); |
||
194 | $result = []; |
||
195 | foreach ($items as $item) { |
||
196 | $row = []; |
||
197 | $row[] = $item['user_id']; |
||
198 | $row[] = $item['official_code']; |
||
199 | $row[] = $item['firstname']; |
||
200 | $row[] = $item['lastname']; |
||
201 | $row[] = $item['username']; |
||
202 | $row[] = $item['email']; |
||
203 | $row[] = $item['status']; |
||
204 | $row[] = $item['auth_source']; |
||
205 | $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT); |
||
206 | $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT); |
||
207 | $row[] = $item['active']; |
||
208 | $result[] = $row; |
||
209 | } |
||
210 | |||
211 | return $result; |
||
212 | } |
||
213 | |||
214 | public function display_data($return = false) |
||
215 | { |
||
216 | $count = [$this, 'count']; |
||
217 | $data = [$this, 'get_data']; |
||
218 | |||
219 | $parameters = []; |
||
220 | $parameters['sec_token'] = Security::get_token(); |
||
221 | $parameters['ceiling'] = $this->get_ceiling(); |
||
222 | $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false'; |
||
223 | $additional_parameters = $this->get_additional_parameters(); |
||
224 | $parameters = array_merge($additional_parameters, $parameters); |
||
225 | |||
226 | $table = new SortableTable('zombie_users', $count, $data, 1, 50); |
||
227 | $table->set_additional_parameters($parameters); |
||
228 | |||
229 | $col = 0; |
||
230 | $table->set_header($col++, '', false); |
||
231 | $table->set_header($col++, get_lang('OfficialCode')); |
||
232 | $table->set_header($col++, get_lang('FirstName')); |
||
233 | $table->set_header($col++, get_lang('LastName')); |
||
234 | $table->set_header($col++, get_lang('LoginName')); |
||
235 | $table->set_header($col++, get_lang('Email')); |
||
236 | $table->set_header($col++, get_lang('Profile')); |
||
237 | $table->set_header($col++, get_lang('AuthenticationSource')); |
||
238 | $table->set_header($col++, get_lang('RegisteredDate')); |
||
239 | $table->set_header($col++, get_lang('LastAccess'), false); |
||
240 | $table->set_header($col, get_lang('Active'), false); |
||
241 | |||
242 | $table->set_column_filter(5, [$this, 'format_email']); |
||
243 | $table->set_column_filter(6, [$this, 'format_status']); |
||
244 | $table->set_column_filter(10, [$this, 'format_active']); |
||
245 | |||
246 | $table->set_form_actions([ |
||
247 | 'activate' => get_lang('Activate'), |
||
248 | 'deactivate' => get_lang('Deactivate'), |
||
249 | 'delete' => get_lang('Delete'), |
||
250 | ]); |
||
251 | |||
252 | if ($return) { |
||
253 | return $table->return_table(); |
||
254 | } else { |
||
255 | echo $table->return_table(); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Table formatter for the active column. |
||
261 | * |
||
262 | * @param string $active |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function format_active($active) |
||
267 | { |
||
268 | $active = $active == '1'; |
||
269 | if ($active) { |
||
270 | $image = 'accept'; |
||
271 | $text = get_lang('Yes'); |
||
272 | } else { |
||
273 | $image = 'error'; |
||
274 | $text = get_lang('No'); |
||
275 | } |
||
276 | |||
277 | $result = Display::return_icon($image.'.png', $text); |
||
278 | |||
279 | return $result; |
||
280 | } |
||
281 | |||
282 | public function format_status($status) |
||
283 | { |
||
284 | $statusname = api_get_status_langvars(); |
||
285 | |||
286 | return $statusname[$status]; |
||
287 | } |
||
288 | |||
289 | public function format_email($email) |
||
292 | } |
||
293 | |||
294 | public function display($return = false) |
||
295 | { |
||
296 | $result = $this->display_parameters($return); |
||
297 | $valid = $this->perform_action(); |
||
298 | |||
299 | if ($valid) { |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.