| Conditions | 5 |
| Paths | 3 |
| Total Lines | 56 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 51 | protected function prepareModels(): array |
||
| 52 | { |
||
| 53 | $pagination = $this->getPagination(); |
||
| 54 | |||
| 55 | if($pagination === false) { |
||
| 56 | $models = $this->queryRelationManager->all($this->db); |
||
| 57 | } else { |
||
| 58 | $limit = $pagination->getLimit(); |
||
| 59 | $offset = $pagination->getOffset(); |
||
| 60 | |||
| 61 | $pagination->totalCount = $this->getTotalCount(); |
||
| 62 | |||
| 63 | $mainTable = $this->queryRelationManager->getTableCollection()->getMainTable(); |
||
| 64 | $pkFields = $mainTable->getPrimaryKeyForSelect(); |
||
| 65 | |||
| 66 | if(count($pkFields) === 1) { |
||
| 67 | $ids = $this->queryRelationManager |
||
| 68 | ->prepare() |
||
| 69 | ->getQuery() |
||
| 70 | ->select($pkFields) |
||
| 71 | ->distinct() |
||
| 72 | ->limit($limit) |
||
| 73 | ->offset($offset) |
||
| 74 | ->column(); |
||
| 75 | |||
| 76 | $models = $this->queryRelationManager->filter(function(Query $q) use ($pkFields, $ids) { |
||
| 77 | $q->andWhere([$pkFields[0] => $ids]); |
||
| 78 | })->all(); |
||
| 79 | } else { |
||
| 80 | $pkValues = $this->queryRelationManager |
||
| 81 | ->prepare() |
||
| 82 | ->getQuery() |
||
| 83 | ->select($pkFields) |
||
| 84 | ->distinct() |
||
| 85 | ->limit($limit) |
||
| 86 | ->offset($offset) |
||
| 87 | ->all(); |
||
| 88 | |||
| 89 | $pkValuesPrefixed = []; |
||
| 90 | foreach($pkValues as $row) { |
||
| 91 | $rowPrefixed = []; |
||
| 92 | foreach($row as $field => $value) { |
||
| 93 | $rowPrefixed["{$mainTable->alias}.{$field}"] = $value; |
||
| 94 | } |
||
| 95 | $pkValuesPrefixed[] = $rowPrefixed; |
||
| 96 | } |
||
| 97 | |||
| 98 | $models = $this->queryRelationManager->filter( |
||
| 99 | function(Query $q) use ($pkFields, $pkValuesPrefixed) { |
||
| 100 | $q->andWhere(['in', $pkFields, $pkValuesPrefixed]); |
||
| 101 | } |
||
| 102 | )->all(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return $models; |
||
| 107 | } |
||
| 152 |