Completed
Push — feature/update_flight ( 5eb300...a7cb77 )
by Laurent
01:44
created

UserSelect::buildOptions()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 80
nop 0
dl 0
loc 52
rs 7.4917
c 0
b 0
f 0

How to fix   Long Method   

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
 *
4
 */
5
6
namespace flightlog\form;
7
8
use User;
9
10
/**
11
 * @author Laurent De Coninck <[email protected]>
12
 */
13
class UserSelect extends Select
14
{
15
16
    /**
17
     * @var \DoliDB
18
     */
19
    private $db;
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function __construct($name, array $options = [], \DoliDB $db)
25
    {
26
        parent::__construct($name, $options);
27
        $this->db = $db;
28
        $this->buildOptions();
29
30
    }
31
32
    /**
33
     * Build the options of the select
34
     */
35
    private function buildOptions()
36
    {
37
38
        if ((boolean) $this->getOption('show_empty')) {
39
            $this->addValueOption(-1, ' ');
40
        }
41
42
        if ((boolean) $this->getOption('show_every')) {
43
            $this->addValueOption(-2, 'Everybody');
44
        }
45
46
        // Forge request to select users
47
        $sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity";
48
        $sql .= " FROM " . MAIN_DB_PREFIX . "user as u";
49
        $sql .= " WHERE u.entity IN (0,1)";
50
        if (!empty($this->getOption('USER_HIDE_INACTIVE_IN_COMBOBOX'))) {
51
            $sql .= " AND u.statut <> 0";
52
        }
53
54
        if (empty($this->getOption('MAIN_FIRSTNAME_NAME_POSITION'))) {
55
            $sql .= " ORDER BY u.firstname ASC";
56
        } else {
57
            $sql .= " ORDER BY u.lastname ASC";
58
        }
59
60
        $resql = $this->db->query($sql);
61
        if ($resql) {
62
            $num = $this->db->num_rows($resql);
63
            $i = 0;
64
            if ($num) {
65
                $userstatic = new User($this->db);
66
67
                while ($i < $num) {
68
                    $obj = $this->db->fetch_object($resql);
69
70
                    $userstatic->id = $obj->rowid;
71
                    $userstatic->lastname = $obj->lastname;
72
                    $userstatic->firstname = $obj->firstname;
73
74
                    $fullNameMode = 0; //Lastname + firstname
75
                    if (empty($this->getOption('MAIN_FIRSTNAME_NAME_POSITION'))) {
76
                        $fullNameMode = 1; //firstname + lastname
77
                    }
78
79
                    $this->addValueOption($obj->rowid, $userstatic->getFullName(null, $fullNameMode));
80
                    $i++;
81
                }
82
            }
83
84
85
        }
86
    }
87
}