Conditions | 10 |
Paths | 21 |
Total Lines | 75 |
Code Lines | 51 |
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 |
||
111 | private function formatSelect($baseSelect) |
||
112 | { |
||
113 | $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
||
114 | $tableGroup = $this->getTableGroupName($relatedTables); |
||
115 | |||
116 | $connection = $this->tdbmService->getConnection(); |
||
117 | $formattedSelect = []; |
||
118 | $columnDescritors = []; |
||
119 | $fetchedTables = []; |
||
120 | |||
121 | foreach ($baseSelect as $entry) { |
||
122 | if ($entry['expr_type'] !== 'colref') { |
||
123 | $formattedSelect[] = $entry; |
||
124 | continue; |
||
125 | } |
||
126 | |||
127 | $noQuotes = $entry['no_quotes']; |
||
128 | if ($noQuotes['delim'] != '.' || count($noQuotes['parts']) !== 2) { |
||
129 | $formattedSelect[] = $entry; |
||
130 | continue; |
||
131 | } |
||
132 | |||
133 | $tableName = $noQuotes['parts'][0]; |
||
134 | if (!in_array($tableName, $relatedTables)) { |
||
135 | $formattedSelect[] = $entry; |
||
136 | continue; |
||
137 | } |
||
138 | |||
139 | $columnName = $noQuotes['parts'][1]; |
||
140 | if ($columnName !== '*') { |
||
141 | $formattedSelect[] = $entry; |
||
142 | continue; |
||
143 | } |
||
144 | |||
145 | $table = $this->schema->getTable($tableName); |
||
146 | foreach ($table->getColumns() as $column) { |
||
147 | $columnName = $column->getName(); |
||
148 | $alias = "{$tableName}____{$columnName}"; |
||
149 | $formattedSelect[] = [ |
||
150 | 'expr_type' => 'colref', |
||
151 | 'base_expr' => $connection->quoteIdentifier($tableName).'.'.$connection->quoteIdentifier($columnName), |
||
152 | 'no_quotes' => [ |
||
153 | 'delim' => '.', |
||
154 | 'parts' => [ |
||
155 | $tableName, |
||
156 | $columnName |
||
157 | ] |
||
158 | ], |
||
159 | 'alias' => [ |
||
160 | 'as' => true, |
||
161 | 'name' => $alias, |
||
162 | ] |
||
163 | ]; |
||
164 | |||
165 | $columnDescritors[$alias] = [ |
||
166 | 'as' => $alias, |
||
167 | 'table' => $tableName, |
||
168 | 'column' => $columnName, |
||
169 | 'type' => $column->getType(), |
||
170 | 'tableGroup' => $tableGroup, |
||
171 | ]; |
||
172 | } |
||
173 | $fetchedTables[] = $tableName; |
||
174 | } |
||
175 | |||
176 | $missingTables = array_diff($relatedTables, $fetchedTables); |
||
177 | if (!empty($missingTables)) { |
||
178 | throw new TDBMException('Missing tables '.implode(', ', $missingTables).' in SELECT statement'); |
||
179 | } |
||
180 | |||
181 | for ($i = 0; $i < count($formattedSelect) - 1; $i++) { |
||
182 | $formattedSelect[$i]['delim'] = ','; |
||
183 | } |
||
184 | return [$formattedSelect, $columnDescritors]; |
||
185 | } |
||
186 | |||
267 |