| Conditions | 6 |
| Paths | 9 |
| Total Lines | 65 |
| Code Lines | 33 |
| 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 |
||
| 131 | public function searchInCaches(ISearchQuery $searchQuery, array $caches): array { |
||
| 132 | // search in multiple caches at once by creating one query in the following format |
||
| 133 | // SELECT ... FROM oc_filecache WHERE |
||
| 134 | // <filter expressions from the search query> |
||
| 135 | // AND ( |
||
| 136 | // <filter expression for storage1> OR |
||
| 137 | // <filter expression for storage2> OR |
||
| 138 | // ... |
||
| 139 | // ); |
||
| 140 | // |
||
| 141 | // This gives us all the files matching the search query from all caches |
||
| 142 | // |
||
| 143 | // while the resulting rows don't have a way to tell what storage they came from (multiple storages/caches can share storage_id) |
||
| 144 | // 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. |
||
| 145 | |||
| 146 | $builder = $this->getQueryBuilder(); |
||
| 147 | |||
| 148 | $query = $builder->selectFileCache('file', false); |
||
| 149 | |||
| 150 | if ($this->searchBuilder->shouldJoinTags($searchQuery->getSearchOperation())) { |
||
| 151 | $user = $searchQuery->getUser(); |
||
| 152 | if ($user === null) { |
||
| 153 | throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query"); |
||
| 154 | } |
||
| 155 | $query |
||
| 156 | ->leftJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
||
| 157 | ->leftJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
||
| 158 | $builder->expr()->eq('tagmap.type', 'tag.type'), |
||
| 159 | $builder->expr()->eq('tagmap.categoryid', 'tag.id'), |
||
| 160 | $builder->expr()->eq('tag.type', $builder->createNamedParameter('files')), |
||
| 161 | $builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID())) |
||
| 162 | )) |
||
| 163 | ->leftJoin('file', 'systemtag_object_mapping', 'systemtagmap', $builder->expr()->andX( |
||
| 164 | $builder->expr()->eq('file.fileid', $builder->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)), |
||
| 165 | $builder->expr()->eq('systemtagmap.objecttype', $builder->createNamedParameter('files')) |
||
| 166 | )) |
||
| 167 | ->leftJoin('systemtagmap', 'systemtag', 'systemtag', $builder->expr()->andX( |
||
| 168 | $builder->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'), |
||
| 169 | $builder->expr()->eq('systemtag.visibility', $builder->createNamedParameter(true)) |
||
| 170 | )); |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->applySearchConstraints($query, $searchQuery, $caches); |
||
| 174 | |||
| 175 | $result = $query->execute(); |
||
| 176 | $files = $result->fetchAll(); |
||
| 177 | |||
| 178 | $rawEntries = array_map(function (array $data) { |
||
| 179 | return Cache::cacheEntryFromData($data, $this->mimetypeLoader); |
||
| 180 | }, $files); |
||
| 181 | |||
| 182 | $result->closeCursor(); |
||
| 183 | |||
| 184 | // loop through all caches for each result to see if the result matches that storage |
||
| 185 | // results are grouped by the same array keys as the caches argument to allow the caller to distinguish the source of the results |
||
| 186 | $results = array_fill_keys(array_keys($caches), []); |
||
| 187 | foreach ($rawEntries as $rawEntry) { |
||
| 188 | foreach ($caches as $cacheKey => $cache) { |
||
| 189 | $entry = $cache->getCacheEntryFromSearchResult($rawEntry); |
||
| 190 | if ($entry) { |
||
| 191 | $results[$cacheKey][] = $entry; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | return $results; |
||
| 196 | } |
||
| 231 |