Conditions | 14 |
Paths | 40 |
Total Lines | 47 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
49 | public function selectLimited(Support\Options $options) : array |
||
50 | { |
||
51 | $sql = 'SELECT'; |
||
52 | $sql .= ' parent.' . $this->settings->idColumnName . ' AS p_pid'; |
||
53 | $sql .= ', parent.' . $this->settings->parentIdColumnName . ' AS p_cid'; |
||
54 | $sql .= ', parent.' . $this->settings->leftColumnName . ' AS p_plt'; |
||
55 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
||
56 | $sql .= ', child.' . $this->settings->idColumnName . ' AS `' . $this->settings->idColumnName . '`'; |
||
57 | $sql .= ', child.' . $this->settings->parentIdColumnName . ' AS `' . $this->settings->parentIdColumnName . '`'; |
||
58 | $sql .= ', child.' . $this->settings->leftColumnName . ' AS `' . $this->settings->leftColumnName . '`'; |
||
59 | $sql .= ', child.' . $this->settings->rightColumnName . ' AS `' . $this->settings->rightColumnName . '`'; |
||
60 | $sql .= ', child.' . $this->settings->levelColumnName . ' AS `' . $this->settings->levelColumnName . '`'; |
||
61 | $sql .= ', child.' . $this->settings->positionColumnName . ' AS `' . $this->settings->positionColumnName . '`'; |
||
62 | } |
||
63 | $sql .= $this->addAdditionalColumns($options); |
||
64 | $sql .= ' FROM ' . $this->settings->tableName . ' AS parent'; |
||
65 | |||
66 | if (!is_null($options->currentId) || !is_null($options->parentId) || !empty($options->search) || $options->joinChild) { |
||
67 | // if there is filter or search, there must be inner join to select all of filtered children. |
||
68 | $sql .= ' INNER JOIN ' . $this->settings->tableName . ' AS child'; |
||
69 | $sql .= ' ON child.' . $this->settings->leftColumnName . ' BETWEEN parent.' . $this->settings->leftColumnName . ' AND parent.' . $this->settings->rightColumnName; |
||
70 | } |
||
71 | |||
72 | $sql .= ' WHERE 1'; |
||
73 | $params = []; |
||
74 | $sql .= $this->addFilterBySql($params, $options); |
||
75 | $sql .= $this->addCurrentIdSql($params, $options, 'parent.'); |
||
76 | $sql .= $this->addParentIdSql($params, $options, 'parent.'); |
||
77 | $sql .= $this->addSearchSql($params, $options, 'parent.'); |
||
78 | $sql .= $this->addCustomQuerySql($params, $options->where); |
||
79 | $sql .= $this->addSortingSql($params, $options); |
||
80 | |||
81 | // re-create query and prepare. second step is for set limit and fetch all items. |
||
82 | if (!$options->unlimited) { |
||
83 | if (empty($options->offset)) { |
||
84 | $options->offset = 0; |
||
85 | } |
||
86 | if (empty($options->limit) || (10000 < $options->limit)) { |
||
87 | $options->limit = 20; |
||
88 | } |
||
89 | |||
90 | $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit; |
||
91 | } |
||
92 | |||
93 | $result = $this->database->fetchAll($sql, ...$params); |
||
94 | |||
95 | return $result ? $this->fromDbRows($result) : []; |
||
96 | } |
||
134 |