getDistrictsDropdown()   C
last analyzed

Complexity

Conditions 14
Paths 16

Size

Total Lines 100
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 68
nc 16
nop 1
dl 0
loc 100
rs 5.7115
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Template dropdowns helpers file
4
 *
5
 * @package EBloodBank
6
 * @since   1.0.1
7
 */
8
namespace EBloodBank;
9
10
use EBloodBank\Models\User;
11
use EBloodBank\Models\City;
12
use EBloodBank\Models\District;
13
14
/**
15
 * @return string
16
 * @since 1.0.1
17
 */
18
function getTokenField(array $args)
19
{
20
    $output = '';
21
    $args = array_merge([
22
        'id'    => '',
23
        'name'  => '',
24
    ], $args);
25
26
    $session = Main::getInstance()->getSession();
27
    $sessionToken = $session->getCsrfToken();
28
    $sessionTokenValue = $sessionToken->getValue();
29
30
    if (is_null($sessionTokenValue)) {
0 ignored issues
show
introduced by
The condition is_null($sessionTokenValue) is always false.
Loading history...
31
        return $output;
32
    }
33
34
    $atts = [
35
        'type'  => 'hidden',
36
        'id'    => $args['id'],
37
        'name'  => $args['name'],
38
        'value' => $sessionTokenValue,
39
    ];
40
41
    $output = sprintf('<input%s/>', toAttributes($atts));
42
    return $output;
43
}
44
45
/**
46
 * @return string
47
 * @since 1.0
48
 */
49
function getUsersDropdown(array $args)
50
{
51
    $output = '';
52
    $args = array_merge([
53
        'id'                => '',
54
        'name'              => '',
55
        'atts'              => [],
56
        'users'             => 'all',
57
        'selected'          => [],
58
        'show_placeholder'  => false,
59
        'placeholder_value' => '',
60
        'placeholder_text'  => '',
61
    ], $args);
62
63
    $args['selected'] = (array) $args['selected'];
64
65
    array_walk($args['selected'], function ($value) {
66
        if ($value instanceof User) {
67
            return $value->get('id');
68
        }
69
    });
70
71
    $args['atts'] = array_merge((array) $args['atts'], [
72
        'name' => $args['name'],
73
        'id'   => $args['id'],
74
    ]);
75
76
    if (is_string($args['users']) && 'all' === $args['users']) {
77
        $em = Main::getInstance()->getEntityManager();
78
        $userRepository = $em->getRepository('Entities:User');
79
        $args['users'] = (array) $userRepository->findAll();
80
    }
81
82
    $output .= sprintf('<select%s>', toAttributes($args['atts']));
83
84
    if ($args['show_placeholder']) {
85
        $output .= sprintf(
86
            '<option%s>%s</option>',
87
            toAttributes(['value' => $args['placeholder_value']]),
88
            escHTML($args['placeholder_text'])
89
        );
90
    }
91
92
    foreach ($args['users'] as $user) {
93
        $userID = (int) $user->get('id');
94
95
        $output .= sprintf(
96
            '<option%s>%s</option>',
97
            toAttributes([
98
                'value'    => $userID,
99
                'selected' => in_array($userID, $args['selected']),
100
            ]),
101
            escHTML($user->get('name'))
102
        );
103
    }
104
105
    $output .= '</select>';
106
107
    return $output;
108
}
109
110
/**
111
 * @return string
112
 * @since 1.0
113
 */
114
function getCitiesDropdown(array $args)
115
{
116
    $output = '';
117
    $args = array_merge([
118
        'id'                => '',
119
        'name'              => '',
120
        'atts'              => [],
121
        'cities'            => 'all',
122
        'selected'          => [],
123
        'show_placeholder'  => false,
124
        'placeholder_value' => '',
125
        'placeholder_text'  => '',
126
    ], $args);
127
128
    $args['selected'] = (array) $args['selected'];
129
130
    array_walk($args['selected'], function ($value) {
131
        if ($value instanceof City) {
132
            return $value->get('id');
133
        }
134
    });
135
136
    $args['atts'] = array_merge((array) $args['atts'], [
137
        'name' => $args['name'],
138
        'id'   => $args['id'],
139
    ]);
140
141
    if (is_string($args['cities']) && 'all' === $args['cities']) {
142
        $em = Main::getInstance()->getEntityManager();
143
        $cityRepository = $em->getRepository('Entities:City');
144
        $args['cities'] = (array) $cityRepository->findAll();
145
    }
146
147
    $output .= sprintf('<select%s>', toAttributes($args['atts']));
148
149
    if ($args['show_placeholder']) {
150
        $output .= sprintf(
151
            '<option%s>%s</option>',
152
            toAttributes(['value' => $args['placeholder_value']]),
153
            escHTML($args['placeholder_text'])
154
        );
155
    }
156
157
    foreach ($args['cities'] as $city) {
158
        $cityID = (int) $city->get('id');
159
160
        $output .= sprintf(
161
            '<option%s>%s</option>',
162
            toAttributes([
163
                'value'    => $cityID,
164
                'selected' => in_array($cityID, $args['selected']),
165
            ]),
166
            escHTML($city->get('name'))
167
        );
168
    }
169
170
    $output .= '</select>';
171
172
    return $output;
173
}
174
175
/**
176
 * @return void
177
 * @since 1.2
178
 */
179
function getDistrictsDropdown(array $args)
180
{
181
    $output = '';
182
    $args = array_merge([
183
        'id'                => '',
184
        'name'              => '',
185
        'atts'              => [],
186
        'selected'          => [],
187
        'districts'         => 'all',
188
        'group_by_cities'   => true,
189
        'show_placeholder'  => false,
190
        'placeholder_value' => '',
191
        'placeholder_text'  => '',
192
    ], $args);
193
194
    $args['selected'] = (array) $args['selected'];
195
196
    array_walk($args['selected'], function ($value) {
197
        if ($value instanceof District) {
198
            return $value->get('id');
199
        }
200
    });
201
202
    $args['atts'] = array_merge((array) $args['atts'], [
203
        'name' => $args['name'],
204
        'id'   => $args['id'],
205
    ]);
206
207
    if (is_string($args['districts']) && 'all' === $args['districts']) {
208
        $em = Main::getInstance()->getEntityManager();
209
        $districtRepository = $em->getRepository('Entities:District');
210
        $args['districts'] = (array) $districtRepository->findAll();
211
    }
212
213
    $output .= sprintf('<select%s>', toAttributes($args['atts']));
214
215
    if ($args['show_placeholder']) {
216
        $output .= sprintf(
217
            '<option%s>%s</option>',
218
            toAttributes(['value' => $args['placeholder_value']]),
219
            escHTML($args['placeholder_text'])
220
        );
221
    }
222
223
    if (! $args['group_by_cities']) {
224
        foreach ($args['districts'] as $district) {
225
            $districtID = (int) $district->get('id');
226
            $output .= sprintf(
227
                '<option%s>%s</option>',
228
                toAttributes([
229
                    'value'        => $districtID,
230
                    'data-city-id' => $district->get('city')->get('id'),
231
                    'selected'     => in_array($districtID, $args['selected']),
232
                ]),
233
                escHTML($district->get('name'))
234
            );
235
        }
236
    } else {
237
        $groups = [];
238
        foreach ($args['districts'] as $district) {
239
            $city = $district->get('city');
240
            $cityID = (int) $city->get('id');
241
            if (! isset($groups[$cityID])) {
242
                $groups[$cityID] = [
243
                    'atts'    => [
244
                        'label' => $city->get('name')
245
                    ],
246
                    'options' => []
247
                ];
248
            }
249
            $districtID = (int) $district->get('id');
250
            $groups[$cityID]['options'][$districtID] = [
251
                'atts'  => [
252
                    'value'        => $districtID,
253
                    'data-city-id' => $district->get('city')->get('id'),
254
                    'selected'     => in_array($districtID, $args['selected']),
255
                ],
256
                'text'  => $district->get('name'),
257
            ];
258
        }
259
        if (! empty($groups)) {
260
            foreach ($groups as $group) {
261
                $output .= sprintf('<optgroup%s>', toAttributes($group['atts']));
262
                if (! empty($group['options']) && is_array($group['options'])) {
263
                    foreach ($group['options'] as $option) {
264
                        $output .= sprintf(
265
                            '<option%s>%s</option>',
266
                            toAttributes($option['atts']),
267
                            escHTML($option['text'])
268
                        );
269
                    }
270
                }
271
                $output .= '</optgroup>';
272
            }
273
        }
274
    }
275
276
    $output .= '</select>';
277
278
    return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output returns the type string which is incompatible with the documented return type void.
Loading history...
279
}
280
281
/**
282
 * @return array
283
 * @since 1.2.2
284
 */
285
function getGenders()
286
{
287
    return [
288
        'male'   => __('Male'),
289
        'female' => __('Female'),
290
    ];
291
}
292
293
/**
294
 * @return array
295
 * @since 1.2.2
296
 */
297
function getVisibilities()
298
{
299
    return [
300
        'everyone' => __('Everyone'),
301
        'members'  => __('Members'),
302
        'staff'    => __('Staff'),
303
    ];
304
}
305
306
/**
307
 * @return void
308
 * @since 1.2.2
309
 */
310
function getVisibilitiesDropdown(array $args)
311
{
312
    $output = '';
313
    $args = array_merge([
314
        'id'                => '',
315
        'name'              => '',
316
        'atts'              => [],
317
        'selected'          => [],
318
        'show_placeholder'  => false,
319
        'placeholder_value' => '',
320
        'placeholder_text'  => '',
321
    ], $args);
322
323
    $visibilities = getVisibilities();
324
325
    $args['selected'] = (array) $args['selected'];
326
327
    $args['atts'] = array_merge((array) $args['atts'], [
328
        'name' => $args['name'],
329
        'id'   => $args['id'],
330
    ]);
331
332
    $output .= sprintf('<select%s>', toAttributes($args['atts']));
333
334
    if ($args['show_placeholder']) {
335
        $output .= sprintf(
336
            '<option%s>%s</option>',
337
            toAttributes(['value' => $args['placeholder_value']]),
338
            escHTML($args['placeholder_text'])
339
        );
340
    }
341
342
    foreach ($visibilities as $key => $label) {
343
        $output .= sprintf(
344
            '<option%s>%s</option>',
345
            toAttributes([
346
                'selected' => in_array($key, $args['selected']),
347
            ]),
348
            escHTML($label)
349
        );
350
    }
351
352
    $output .= '</select>';
353
354
    return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output returns the type string which is incompatible with the documented return type void.
Loading history...
355
}
356
357
/**
358
 * @return array
359
 * @since 1.2.2
360
 */
361
function getBloodGroups()
362
{
363
    return [
364
        'A+',
365
        'A-',
366
        'B+',
367
        'B-',
368
        'O+',
369
        'O-',
370
        'AB+',
371
        'AB-',
372
    ];
373
}
374
375
/**
376
 * @return void
377
 * @since 1.2
378
 */
379
function getBloodGroupsDropdown(array $args)
380
{
381
    $output = '';
382
    $args = array_merge([
383
        'id'                => '',
384
        'name'              => '',
385
        'atts'              => [],
386
        'selected'          => [],
387
        'show_placeholder'  => false,
388
        'placeholder_value' => '',
389
        'placeholder_text'  => '',
390
    ], $args);
391
392
    $bloodGroups = getBloodGroups();
393
394
    $args['selected'] = (array) $args['selected'];
395
396
    $args['atts'] = array_merge((array) $args['atts'], [
397
        'name' => $args['name'],
398
        'id'   => $args['id'],
399
    ]);
400
401
    $output .= sprintf('<select%s>', toAttributes($args['atts']));
402
403
    if ($args['show_placeholder']) {
404
        $output .= sprintf(
405
            '<option%s>%s</option>',
406
            toAttributes(['value' => $args['placeholder_value']]),
407
            escHTML($args['placeholder_text'])
408
        );
409
    }
410
411
    foreach ($bloodGroups as $bloodGroup) {
412
        $output .= sprintf(
413
            '<option%s>%s</option>',
414
            toAttributes([
415
                'selected' => in_array($bloodGroup, $args['selected']),
416
            ]),
417
            escHTML($bloodGroup)
418
        );
419
    }
420
421
    $output .= '</select>';
422
423
    return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output returns the type string which is incompatible with the documented return type void.
Loading history...
424
}
425