Conditions | 23 |
Paths | 492 |
Total Lines | 119 |
Code Lines | 72 |
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 |
||
92 | protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null, bool $canAddAdditionalTablesFetch = false): array |
||
93 | { |
||
94 | // From the table name and the additional tables we want to fetch, let's build a list of all tables |
||
95 | // that must be part of the select columns. |
||
96 | |||
97 | $connection = $this->tdbmService->getConnection(); |
||
98 | |||
99 | $tableGroups = []; |
||
100 | $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
||
101 | $tableGroupName = $this->getTableGroupName($allFetchedTables); |
||
102 | foreach ($allFetchedTables as $table) { |
||
103 | $tableGroups[$table] = $tableGroupName; |
||
104 | } |
||
105 | |||
106 | $columnsList = []; |
||
107 | $columnDescList = []; |
||
108 | $sortColumn = 0; |
||
109 | $reconstructedOrderBy = null; |
||
110 | |||
111 | if (is_string($orderBy)) { |
||
112 | $orderBy = trim($orderBy); |
||
113 | if ($orderBy === '') { |
||
114 | $orderBy = null; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Now, let's deal with "order by columns" |
||
119 | if ($orderBy !== null) { |
||
120 | $securedOrderBy = true; |
||
121 | $reconstructedOrderBys = []; |
||
122 | if ($orderBy instanceof UncheckedOrderBy) { |
||
123 | $securedOrderBy = false; |
||
124 | $orderBy = $orderBy->getOrderBy(); |
||
125 | $reconstructedOrderBy = $orderBy; |
||
126 | } |
||
127 | $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
||
128 | |||
129 | // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
||
130 | // Hence, we should add the table to the $additionalTablesFetch |
||
131 | foreach ($orderByColumns as $orderByColumn) { |
||
132 | if ($orderByColumn['type'] === ExpressionType::COLREF) { |
||
133 | if ($orderByColumn['table'] !== null) { |
||
134 | if ($canAddAdditionalTablesFetch) { |
||
135 | $additionalTablesFetch[] = $orderByColumn['table']; |
||
136 | } else { |
||
137 | $sortColumnName = 'sort_column_'.$sortColumn; |
||
138 | $mysqlPlatform = new MySqlPlatform(); |
||
139 | $columnsList[] = $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.'.$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' as '.$sortColumnName; |
||
140 | $columnDescList[$sortColumnName] = [ |
||
141 | 'tableGroup' => null, |
||
142 | ]; |
||
143 | ++$sortColumn; |
||
144 | } |
||
145 | } |
||
146 | if ($securedOrderBy) { |
||
147 | // Let's protect via MySQL since we go through MagicJoin |
||
148 | $mysqlPlatform = new MySqlPlatform(); |
||
149 | $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.' : '').$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction']; |
||
150 | } |
||
151 | } elseif ($orderByColumn['type'] === 'expr') { |
||
152 | if ($securedOrderBy) { |
||
153 | 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")'); |
||
154 | } |
||
155 | |||
156 | $sortColumnName = 'sort_column_'.$sortColumn; |
||
157 | $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
||
158 | $columnDescList[$sortColumnName] = [ |
||
159 | 'tableGroup' => null, |
||
160 | ]; |
||
161 | ++$sortColumn; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | if ($reconstructedOrderBy === null) { |
||
166 | $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | foreach ($additionalTablesFetch as $additionalTable) { |
||
171 | if (in_array($additionalTable, $allFetchedTables, true)) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
||
176 | $tableGroupName = $this->getTableGroupName($relatedTables); |
||
177 | foreach ($relatedTables as $table) { |
||
178 | $tableGroups[$table] = $tableGroupName; |
||
179 | } |
||
180 | $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
||
181 | } |
||
182 | |||
183 | // Let's remove any duplicate |
||
184 | $allFetchedTables = array_unique($allFetchedTables); |
||
185 | |||
186 | // We quote in MySQL because MagicJoin requires MySQL style quotes |
||
187 | $mysqlPlatform = new MySqlPlatform(); |
||
188 | |||
189 | // Now, let's build the column list |
||
190 | foreach ($allFetchedTables as $table) { |
||
191 | foreach ($this->schema->getTable($table)->getColumns() as $column) { |
||
192 | $columnName = $column->getName(); |
||
193 | if ($this->resultIterator === null // @TODO (gua) don't take care of whitelist in case of LIMIT below 2 |
||
194 | || $table !== $mainTable |
||
195 | || $this->resultIterator->isInWhitelist($columnName, $table) |
||
196 | ) { |
||
197 | $columnDescList[$table . '____' . $columnName] = [ |
||
198 | 'as' => $table . '____' . $columnName, |
||
199 | 'table' => $table, |
||
200 | 'column' => $columnName, |
||
201 | 'type' => $column->getType(), |
||
202 | 'tableGroup' => $tableGroups[$table], |
||
203 | ]; |
||
204 | $columnsList[] = $mysqlPlatform->quoteIdentifier($table) . '.' . $mysqlPlatform->quoteIdentifier($columnName) . ' as ' . |
||
205 | $connection->quoteIdentifier($table . '____' . $columnName); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
||
211 | } |
||
308 |