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