| Conditions | 8 |
| Paths | 40 |
| Total Lines | 56 |
| Code Lines | 40 |
| 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 |
||
| 64 | public function selectLimited(Support\Options $options) : array |
||
| 65 | { |
||
| 66 | $joinChild = $this->canJoinChild($options); |
||
| 67 | $sql = 'SELECT'; |
||
| 68 | $sql .= ' `parent`.`' . $this->settings->idColumnName . '` AS p_cid'; |
||
| 69 | $sql .= ', `parent`.`' . $this->settings->parentIdColumnName . '` AS p_pid'; |
||
| 70 | $sql .= ', `parent`.`' . $this->settings->leftColumnName . '` AS p_lf'; |
||
| 71 | if ($joinChild) { |
||
| 72 | $sql .= ', `child`.`' . $this->settings->idColumnName . '` AS `' . $this->settings->idColumnName . '`'; |
||
| 73 | $sql .= ', `child`.`' . $this->settings->parentIdColumnName . '` AS `' . $this->settings->parentIdColumnName . '`'; |
||
| 74 | $sql .= ', `child`.`' . $this->settings->leftColumnName . '` AS `' . $this->settings->leftColumnName . '`'; |
||
| 75 | $sql .= ', `child`.`' . $this->settings->rightColumnName . '` AS `' . $this->settings->rightColumnName . '`'; |
||
| 76 | $sql .= ', `child`.`' . $this->settings->levelColumnName . '` AS `' . $this->settings->levelColumnName . '`'; |
||
| 77 | $sql .= ', `child`.`' . $this->settings->positionColumnName . '` AS `' . $this->settings->positionColumnName . '`'; |
||
| 78 | } |
||
| 79 | $sql .= $this->addAdditionalColumns($options); |
||
| 80 | $sql .= ' FROM `' . $this->settings->tableName . '` AS `parent`'; |
||
| 81 | |||
| 82 | if ($joinChild) { |
||
| 83 | // if there is filter or search, there must be inner join to select all of filtered children. |
||
| 84 | $sql .= ' INNER JOIN `' . $this->settings->tableName . '` AS `child`'; |
||
| 85 | $sql .= ' ON `child`.`' . $this->settings->leftColumnName . '` BETWEEN `parent`.`' . $this->settings->leftColumnName . '` AND `parent`.`' . $this->settings->rightColumnName . '`'; |
||
| 86 | } |
||
| 87 | |||
| 88 | $sql .= ' WHERE TRUE'; |
||
| 89 | $sql .= $this->addFilterBy($options); |
||
| 90 | $sql .= $this->addCurrentId($options, '`parent`.'); |
||
| 91 | $sql .= $this->addParentId($options, '`parent`.'); |
||
| 92 | $sql .= $this->addSearch($options, '`parent`.'); |
||
| 93 | $sql .= $this->addCustomQuery($options->where); |
||
| 94 | $sql .= $this->addSoftDelete('`parent`.'); |
||
| 95 | $sql .= $this->addSorting($options); |
||
| 96 | |||
| 97 | // re-create query and prepare. second step is for set limit and fetch all items. |
||
| 98 | if (!$options->unlimited) { |
||
| 99 | if (empty($options->offset)) { |
||
| 100 | $options->offset = 0; |
||
| 101 | } |
||
| 102 | if (empty($options->limit) || (10000 < $options->limit)) { |
||
| 103 | $options->limit = 20; |
||
| 104 | } |
||
| 105 | |||
| 106 | $sql .= ' LIMIT ' . $options->offset . ', ' . $options->limit; |
||
| 107 | } |
||
| 108 | |||
| 109 | $Sth = $this->pdo->prepare($sql); |
||
| 110 | $this->bindCurrentId($options->currentId, $Sth); |
||
| 111 | $this->bindParentId($options->parentId, $Sth, true); |
||
| 112 | $this->bindSearch($options, $Sth); |
||
| 113 | $this->bindCustomQuery($options->where, $Sth); |
||
| 114 | $this->bindSoftDelete($Sth); |
||
| 115 | |||
| 116 | $Sth->execute(); |
||
| 117 | $result = $Sth->fetchAll(); |
||
| 118 | |||
| 119 | return $result ? $this->fromDbRows($result, $joinChild) : []; |
||
| 120 | } |
||
| 166 |