Conditions | 14 |
Paths | 40 |
Total Lines | 53 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
57 | public function selectLimited(Support\Options $options) : array |
||
58 | { |
||
59 | $sql = 'SELECT'; |
||
60 | $sql .= ' `parent`.`' . $this->settings->idColumnName . '` AS p_cid'; |
||
61 | $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS p_pid'; |
||
62 | $sql .= ', `parent`.`' . $this->settings->leftColumnName . '` AS p_lf'; |
||
63 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
||
64 | $sql .= ', `child`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`'; |
||
65 | $sql .= ', `child`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`'; |
||
66 | $sql .= ', `child`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`'; |
||
67 | $sql .= ', `child`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`'; |
||
68 | $sql .= ', `child`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`'; |
||
69 | $sql .= ', `child`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`'; |
||
70 | } |
||
71 | $sql .= $this->addAdditionalColumns($options); |
||
72 | $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`'; |
||
73 | |||
74 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
||
75 | // if there is filter or search, there must be inner join to select all of filtered children. |
||
76 | $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`'; |
||
77 | $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`'; |
||
78 | } |
||
79 | |||
80 | $sql .= ' WHERE 1'; |
||
81 | $sql .= $this->addFilterBy($options); |
||
82 | $sql .= $this->addCurrentId($options, '`parent`.'); |
||
83 | $sql .= $this->addParentId($options, '`parent`.'); |
||
84 | $sql .= $this->addSearch($options, '`parent`.'); |
||
85 | $sql .= $this->addCustomQuery($options->where); |
||
86 | $sql .= $this->addSorting($options); |
||
87 | |||
88 | // re-create query and prepare. second step is for set limit and fetch all items. |
||
89 | if (!$options->unlimited) { |
||
90 | if (empty($options->offset)) { |
||
91 | $options->offset = 0; |
||
92 | } |
||
93 | if (empty($options->limit) || (10000 < $options->limit)) { |
||
94 | $options->limit = 20; |
||
95 | } |
||
96 | |||
97 | $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit; |
||
98 | } |
||
99 | |||
100 | $Sth = $this->pdo->prepare($sql); |
||
101 | $this->bindCurrentId($options->currentId, $Sth); |
||
102 | $this->bindParentId($options->parentId, $Sth, true); |
||
103 | $this->bindSearch($options, $Sth); |
||
104 | $this->bindCustomQuery($options->where, $Sth); |
||
105 | |||
106 | $Sth->execute(); |
||
107 | $result = $Sth->fetchAll(); |
||
108 | |||
109 | return $result ? $this->fromDbRows($result) : []; |
||
110 | } |
||
154 |