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