| Conditions | 9 |
| Paths | 65 |
| Total Lines | 74 |
| Code Lines | 38 |
| 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 |
||
| 84 | public function searchInCaches(ISearchQuery $searchQuery, array $caches): array { |
||
| 85 | // search in multiple caches at once by creating one query in the following format |
||
| 86 | // SELECT ... FROM oc_filecache WHERE |
||
| 87 | // <filter expressions from the search query> |
||
| 88 | // AND ( |
||
| 89 | // <filter expression for storage1> OR |
||
| 90 | // <filter expression for storage2> OR |
||
| 91 | // ... |
||
| 92 | // ); |
||
| 93 | // |
||
| 94 | // This gives us all the files matching the search query from all caches |
||
| 95 | // |
||
| 96 | // while the resulting rows don't have a way to tell what storage they came from (multiple storages/caches can share storage_id) |
||
| 97 | // we can just ask every cache if the row belongs to them and give them the cache to do any post processing on the result. |
||
| 98 | |||
| 99 | $builder = $this->getQueryBuilder(); |
||
| 100 | |||
| 101 | $query = $builder->selectFileCache('file'); |
||
| 102 | |||
| 103 | if ($this->searchBuilder->shouldJoinTags($searchQuery->getSearchOperation())) { |
||
| 104 | $user = $searchQuery->getUser(); |
||
| 105 | if ($user === null) { |
||
| 106 | throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query"); |
||
| 107 | } |
||
| 108 | $query |
||
| 109 | ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
||
| 110 | ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
||
| 111 | $builder->expr()->eq('tagmap.type', 'tag.type'), |
||
| 112 | $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
||
| 113 | )) |
||
| 114 | ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
||
| 115 | ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID()))); |
||
| 116 | } |
||
| 117 | |||
| 118 | $searchExpr = $this->searchBuilder->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()); |
||
| 119 | if ($searchExpr) { |
||
| 120 | $query->andWhere($searchExpr); |
||
| 121 | } |
||
| 122 | |||
| 123 | $storageFilters = array_values(array_map(function (ICache $cache) { |
||
| 124 | return $cache->getQueryFilterForStorage(); |
||
| 125 | }, $caches)); |
||
| 126 | $query->andWhere($this->searchBuilder->searchOperatorToDBExpr($builder, new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $storageFilters))); |
||
| 127 | |||
| 128 | $this->searchBuilder->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
||
| 129 | |||
| 130 | if ($searchQuery->getLimit()) { |
||
| 131 | $query->setMaxResults($searchQuery->getLimit()); |
||
| 132 | } |
||
| 133 | if ($searchQuery->getOffset()) { |
||
| 134 | $query->setFirstResult($searchQuery->getOffset()); |
||
| 135 | } |
||
| 136 | |||
| 137 | $result = $query->execute(); |
||
| 138 | $files = $result->fetchAll(); |
||
| 139 | |||
| 140 | $rawEntries = array_map(function (array $data) { |
||
| 141 | return Cache::cacheEntryFromData($data, $this->mimetypeLoader); |
||
| 142 | }, $files); |
||
| 143 | |||
| 144 | $result->closeCursor(); |
||
| 145 | |||
| 146 | // loop trough all caches for each result to see if the result matches that storage |
||
| 147 | // results are grouped by the same array keys as the caches argument to allow the caller to distringuish the source of the results |
||
| 148 | $results = array_fill_keys(array_keys($caches), []); |
||
| 149 | foreach ($rawEntries as $rawEntry) { |
||
| 150 | foreach ($caches as $cacheKey => $cache) { |
||
| 151 | $entry = $cache->getCacheEntryFromSearchResult($rawEntry); |
||
| 152 | if ($entry) { |
||
| 153 | $results[$cacheKey][] = $entry; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | return $results; |
||
| 158 | } |
||
| 160 |