| Conditions | 20 |
| Paths | 372 |
| Total Lines | 114 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 77 | protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null, bool $canAddAdditionalTablesFetch = false): array |
||
| 78 | { |
||
| 79 | // From the table name and the additional tables we want to fetch, let's build a list of all tables |
||
| 80 | // that must be part of the select columns. |
||
| 81 | |||
| 82 | $connection = $this->tdbmService->getConnection(); |
||
| 83 | |||
| 84 | $tableGroups = []; |
||
| 85 | $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
||
| 86 | $tableGroupName = $this->getTableGroupName($allFetchedTables); |
||
| 87 | foreach ($allFetchedTables as $table) { |
||
| 88 | $tableGroups[$table] = $tableGroupName; |
||
| 89 | } |
||
| 90 | |||
| 91 | $columnsList = []; |
||
| 92 | $columnDescList = []; |
||
| 93 | $sortColumn = 0; |
||
| 94 | $reconstructedOrderBy = null; |
||
| 95 | |||
| 96 | if (is_string($orderBy)) { |
||
| 97 | $orderBy = trim($orderBy); |
||
| 98 | if ($orderBy === '') { |
||
| 99 | $orderBy = null; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | // Now, let's deal with "order by columns" |
||
| 104 | if ($orderBy !== null) { |
||
| 105 | $securedOrderBy = true; |
||
| 106 | $reconstructedOrderBys = []; |
||
| 107 | if ($orderBy instanceof UncheckedOrderBy) { |
||
| 108 | $securedOrderBy = false; |
||
| 109 | $orderBy = $orderBy->getOrderBy(); |
||
| 110 | $reconstructedOrderBy = $orderBy; |
||
| 111 | } |
||
| 112 | $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
||
| 113 | |||
| 114 | // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
||
| 115 | // Hence, we should add the table to the $additionalTablesFetch |
||
| 116 | foreach ($orderByColumns as $orderByColumn) { |
||
| 117 | if ($orderByColumn['type'] === 'colref') { |
||
| 118 | if ($orderByColumn['table'] !== null) { |
||
| 119 | if ($canAddAdditionalTablesFetch) { |
||
| 120 | $additionalTablesFetch[] = $orderByColumn['table']; |
||
| 121 | } else { |
||
| 122 | $sortColumnName = 'sort_column_'.$sortColumn; |
||
| 123 | $mysqlPlatform = new MySqlPlatform(); |
||
| 124 | $columnsList[] = $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.'.$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' as '.$sortColumnName; |
||
| 125 | $columnDescList[$sortColumnName] = [ |
||
| 126 | 'tableGroup' => null, |
||
| 127 | ]; |
||
| 128 | ++$sortColumn; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | if ($securedOrderBy) { |
||
| 132 | // Let's protect via MySQL since we go through MagicJoin |
||
| 133 | $mysqlPlatform = new MySqlPlatform(); |
||
| 134 | $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.' : '').$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction']; |
||
| 135 | } |
||
| 136 | } elseif ($orderByColumn['type'] === 'expr') { |
||
| 137 | $sortColumnName = 'sort_column_'.$sortColumn; |
||
| 138 | $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
||
| 139 | $columnDescList[$sortColumnName] = [ |
||
| 140 | 'tableGroup' => null, |
||
| 141 | ]; |
||
| 142 | ++$sortColumn; |
||
| 143 | |||
| 144 | if ($securedOrderBy) { |
||
| 145 | throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($reconstructedOrderBy === null) { |
||
| 151 | $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | foreach ($additionalTablesFetch as $additionalTable) { |
||
| 156 | if (in_array($additionalTable, $allFetchedTables, true)) { |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | |||
| 160 | $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
||
| 161 | $tableGroupName = $this->getTableGroupName($relatedTables); |
||
| 162 | foreach ($relatedTables as $table) { |
||
| 163 | $tableGroups[$table] = $tableGroupName; |
||
| 164 | } |
||
| 165 | $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
||
| 166 | } |
||
| 167 | |||
| 168 | // Let's remove any duplicate |
||
| 169 | $allFetchedTables = array_unique($allFetchedTables); |
||
| 170 | |||
| 171 | // We quote in MySQL because MagicJoin requires MySQL style quotes |
||
| 172 | $mysqlPlatform = new MySqlPlatform(); |
||
| 173 | |||
| 174 | // Now, let's build the column list |
||
| 175 | foreach ($allFetchedTables as $table) { |
||
| 176 | foreach ($this->schema->getTable($table)->getColumns() as $column) { |
||
| 177 | $columnName = $column->getName(); |
||
| 178 | $columnDescList[$table.'____'.$columnName] = [ |
||
| 179 | 'as' => $table.'____'.$columnName, |
||
| 180 | 'table' => $table, |
||
| 181 | 'column' => $columnName, |
||
| 182 | 'type' => $column->getType(), |
||
| 183 | 'tableGroup' => $tableGroups[$table], |
||
| 184 | ]; |
||
| 185 | $columnsList[] = $mysqlPlatform->quoteIdentifier($table).'.'.$mysqlPlatform->quoteIdentifier($columnName).' as '. |
||
| 186 | $connection->quoteIdentifier($table.'____'.$columnName); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
||
| 191 | } |
||
| 267 |