Total Complexity | 41 |
Total Lines | 290 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TActiveRecordHasManyAssociation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TActiveRecordHasManyAssociation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
87 | class TActiveRecordHasManyAssociation extends TActiveRecordRelation |
||
88 | { |
||
89 | private $_association; |
||
90 | private $_sourceTable; |
||
91 | private $_foreignTable; |
||
92 | private $_association_columns = []; |
||
93 | |||
94 | /** |
||
95 | * Get the foreign key index values from the results and make calls to the |
||
96 | * database to find the corresponding foreign objects using association table. |
||
97 | * @param array original results. |
||
|
|||
98 | */ |
||
99 | protected function collectForeignObjects(&$results) |
||
100 | { |
||
101 | list($sourceKeys, $foreignKeys) = $this->getRelationForeignKeys(); |
||
102 | $properties = array_values($sourceKeys); |
||
103 | $indexValues = $this->getIndexValues($properties, $results); |
||
104 | $this->fetchForeignObjects($results, $foreignKeys, $indexValues, $sourceKeys); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return array 2 arrays of source keys and foreign keys from the association table. |
||
109 | */ |
||
110 | public function getRelationForeignKeys() |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return TDbTableInfo association table information. |
||
121 | */ |
||
122 | protected function getAssociationTable() |
||
123 | { |
||
124 | if($this->_association === null) |
||
125 | { |
||
126 | $gateway = $this->getSourceRecord()->getRecordGateway(); |
||
127 | $conn = $this->getSourceRecord()->getDbConnection(); |
||
128 | //table name may include the fk column name separated with a dot. |
||
129 | $table = explode('.', $this->getContext()->getAssociationTable()); |
||
130 | if(count($table) > 1) |
||
131 | { |
||
132 | $columns = preg_replace('/^\((.*)\)/', '\1', $table[1]); |
||
133 | $this->_association_columns = preg_split('/\s*[, ]\*/', $columns); |
||
134 | } |
||
135 | $this->_association = $gateway->getTableInfo($conn, $table[0]); |
||
136 | } |
||
137 | return $this->_association; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return TDbTableInfo source table information. |
||
142 | */ |
||
143 | protected function getSourceTable() |
||
144 | { |
||
145 | if($this->_sourceTable === null) |
||
146 | { |
||
147 | $gateway = $this->getSourceRecord()->getRecordGateway(); |
||
148 | $this->_sourceTable = $gateway->getRecordTableInfo($this->getSourceRecord()); |
||
149 | } |
||
150 | return $this->_sourceTable; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return TDbTableInfo foreign table information. |
||
155 | */ |
||
156 | protected function getForeignTable() |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return TDataGatewayCommand |
||
169 | */ |
||
170 | protected function getCommandBuilder() |
||
171 | { |
||
172 | return $this->getSourceRecord()->getRecordGateway()->getCommand($this->getSourceRecord()); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @return TDataGatewayCommand |
||
177 | */ |
||
178 | protected function getForeignCommandBuilder() |
||
182 | } |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Fetches the foreign objects using TActiveRecord::findAllByIndex() |
||
187 | * @param array field names |
||
188 | * @param array foreign key index values. |
||
189 | */ |
||
190 | protected function fetchForeignObjects(&$results, $foreignKeys, $indexValues, $sourceKeys) |
||
191 | { |
||
192 | $criteria = $this->getCriteria(); |
||
193 | $finder = $this->getContext()->getForeignRecordFinder(); |
||
194 | $type = get_class($finder); |
||
195 | $command = $this->createCommand($criteria, $foreignKeys, $indexValues, $sourceKeys); |
||
196 | $srcProps = array_keys($sourceKeys); |
||
197 | $collections = []; |
||
198 | foreach($this->getCommandBuilder()->onExecuteCommand($command, $command->query()) as $row) |
||
199 | { |
||
200 | $hash = $this->getObjectHash($row, $srcProps); |
||
201 | foreach($srcProps as $column) |
||
202 | unset($row[$column]); |
||
203 | $obj = $this->createFkObject($type, $row, $foreignKeys); |
||
204 | $collections[$hash][] = $obj; |
||
205 | } |
||
206 | $this->setResultCollection($results, $collections, array_values($sourceKeys)); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param string active record class name. |
||
211 | * @param array row data |
||
212 | * @param array foreign key column names |
||
213 | * @return TActiveRecord |
||
214 | */ |
||
215 | protected function createFkObject($type, $row, $foreignKeys) |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param TSqlCriteria |
||
229 | * @param TTableInfo association table info |
||
230 | * @param array field names |
||
231 | * @param array field values |
||
232 | */ |
||
233 | public function createCommand($criteria, $foreignKeys, $indexValues, $sourceKeys) |
||
234 | { |
||
235 | $innerJoin = $this->getAssociationJoin($foreignKeys, $indexValues, $sourceKeys); |
||
236 | $fkTable = $this->getForeignTable()->getTableFullName(); |
||
237 | $srcColumns = $this->getSourceColumns($sourceKeys); |
||
238 | if(($where = $criteria->getCondition()) === null) |
||
239 | $where = '1=1'; |
||
240 | $sql = "SELECT {$fkTable}.*, {$srcColumns} FROM {$fkTable} {$innerJoin} WHERE {$where}"; |
||
241 | |||
242 | $parameters = $criteria->getParameters()->toArray(); |
||
243 | $ordering = $criteria->getOrdersBy(); |
||
244 | $limit = $criteria->getLimit(); |
||
245 | $offset = $criteria->getOffset(); |
||
246 | |||
247 | $builder = $this->getForeignCommandBuilder()->getBuilder(); |
||
248 | $command = $builder->applyCriterias($sql, $parameters, $ordering, $limit, $offset); |
||
249 | $this->getCommandBuilder()->onCreateCommand($command, $criteria); |
||
250 | return $command; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param array source table column names. |
||
255 | * @return string comma separated source column names. |
||
256 | */ |
||
257 | protected function getSourceColumns($sourceKeys) |
||
258 | { |
||
259 | $columns = []; |
||
260 | $table = $this->getAssociationTable(); |
||
261 | $tableName = $table->getTableFullName(); |
||
262 | $columnNames = array_merge(array_keys($sourceKeys), $this->_association_columns); |
||
263 | foreach($columnNames as $name) |
||
264 | $columns[] = $tableName . '.' . $table->getColumn($name)->getColumnName(); |
||
265 | return implode(', ', $columns); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * SQL inner join for M-N relationship via association table. |
||
270 | * @param array foreign table column key names. |
||
271 | * @param array source table index values. |
||
272 | * @param array source table column names. |
||
273 | * @return string inner join condition for M-N relationship via association table. |
||
274 | */ |
||
275 | protected function getAssociationJoin($foreignKeys, $indexValues, $sourceKeys) |
||
276 | { |
||
277 | $refInfo = $this->getAssociationTable(); |
||
278 | $fkInfo = $this->getForeignTable(); |
||
279 | |||
280 | $refTable = $refInfo->getTableFullName(); |
||
281 | $fkTable = $fkInfo->getTableFullName(); |
||
282 | |||
283 | $joins = []; |
||
284 | $hasAssociationColumns = count($this->_association_columns) > 0; |
||
285 | $i = 0; |
||
286 | foreach($foreignKeys as $ref => $fk) |
||
287 | { |
||
288 | if($hasAssociationColumns) |
||
289 | $refField = $refInfo->getColumn($this->_association_columns[$i++])->getColumnName(); |
||
290 | else |
||
291 | $refField = $refInfo->getColumn($ref)->getColumnName(); |
||
292 | $fkField = $fkInfo->getColumn($fk)->getColumnName(); |
||
293 | $joins[] = "{$fkTable}.{$fkField} = {$refTable}.{$refField}"; |
||
294 | } |
||
295 | $joinCondition = implode(' AND ', $joins); |
||
296 | $index = $this->getCommandBuilder()->getIndexKeyCondition($refInfo, array_keys($sourceKeys), $indexValues); |
||
297 | return "INNER JOIN {$refTable} ON ({$joinCondition}) AND {$index}"; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Updates the associated foreign objects. |
||
302 | * @return boolean true if all update are success (including if no update was required), false otherwise . |
||
303 | */ |
||
304 | public function updateAssociatedRecords() |
||
305 | { |
||
306 | $obj = $this->getContext()->getSourceRecord(); |
||
307 | $fkObjects = &$obj->{$this->getContext()->getProperty()}; |
||
308 | $success = true; |
||
309 | if(($total = count($fkObjects)) > 0) |
||
310 | { |
||
311 | $source = $this->getSourceRecord(); |
||
312 | $builder = $this->getAssociationTableCommandBuilder(); |
||
313 | for($i = 0;$i < $total;$i++) |
||
314 | $success = $fkObjects[$i]->save() && $success; |
||
315 | return $this->updateAssociationTable($obj, $fkObjects, $builder) && $success; |
||
316 | } |
||
317 | return $success; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @return TDbCommandBuilder |
||
322 | */ |
||
323 | protected function getAssociationTableCommandBuilder() |
||
324 | { |
||
325 | $conn = $this->getContext()->getSourceRecord()->getDbConnection(); |
||
326 | return $this->getAssociationTable()->createCommandBuilder($conn); |
||
327 | } |
||
328 | |||
329 | private function hasAssociationData($builder, $data) |
||
330 | { |
||
331 | $condition = []; |
||
332 | $table = $this->getAssociationTable(); |
||
333 | foreach($data as $name => $value) |
||
334 | $condition[] = $table->getColumn($name)->getColumnName() . ' = ?'; |
||
335 | $command = $builder->createCountCommand(implode(' AND ', $condition), array_values($data)); |
||
336 | $result = $this->getCommandBuilder()->onExecuteCommand($command, intval($command->queryScalar())); |
||
337 | return intval($result) > 0; |
||
338 | } |
||
339 | |||
340 | private function addAssociationData($builder, $data) |
||
344 | } |
||
345 | |||
346 | private function updateAssociationTable($obj, $fkObjects, $builder) |
||
347 | { |
||
348 | $source = $this->getSourceRecordValues($obj); |
||
349 | $foreignKeys = $this->findForeignKeys($this->getAssociationTable(), $fkObjects[0]); |
||
350 | $success = true; |
||
351 | foreach($fkObjects as $fkObject) |
||
352 | { |
||
353 | $data = array_merge($source, $this->getForeignObjectValues($foreignKeys, $fkObject)); |
||
354 | if(!$this->hasAssociationData($builder, $data)) |
||
355 | $success = $this->addAssociationData($builder, $data) && $success; |
||
356 | } |
||
357 | return $success; |
||
358 | } |
||
359 | |||
360 | private function getSourceRecordValues($obj) |
||
361 | { |
||
362 | $sourceKeys = $this->findForeignKeys($this->getAssociationTable(), $obj); |
||
363 | $indexValues = $this->getIndexValues(array_values($sourceKeys), $obj); |
||
364 | $data = []; |
||
365 | $i = 0; |
||
366 | foreach($sourceKeys as $name => $srcKey) |
||
367 | $data[$name] = $indexValues[0][$i++]; |
||
368 | return $data; |
||
369 | } |
||
370 | |||
371 | private function getForeignObjectValues($foreignKeys, $fkObject) |
||
377 | } |
||
378 | } |
||
379 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths