| Conditions | 19 |
| Paths | 800 |
| Total Lines | 107 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 142 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 143 | { |
||
| 144 | $pageNumber = $request->query->get('page', 1); |
||
| 145 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
| 146 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
| 147 | |||
| 148 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
| 149 | $queryBuilder = $this->repository |
||
| 150 | ->createQueryBuilder(); |
||
| 151 | |||
| 152 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
| 153 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | $searchableFields = $this->getSearchableFields(); |
||
| 158 | if (!is_null($schema)) { |
||
| 159 | $searchableFields = $schema->getSearchable(); |
||
| 160 | } |
||
| 161 | |||
| 162 | // *** do we have an RQL expression, do we need to filter data? |
||
| 163 | if ($request->attributes->get('hasRql', false)) { |
||
| 164 | $queryBuilder = $this->doRqlQuery( |
||
| 165 | $queryBuilder, |
||
| 166 | $this->translator->translateSearchQuery( |
||
| 167 | $request->attributes->get('rqlQuery'), |
||
| 168 | $searchableFields |
||
| 169 | ) |
||
| 170 | ); |
||
| 171 | } else { |
||
| 172 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 173 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 174 | $queryBuilder->find($this->repository->getDocumentName()); |
||
| 175 | } |
||
| 176 | |||
| 177 | // define offset and limit |
||
| 178 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
||
| 179 | $queryBuilder->skip($startAt); |
||
| 180 | } else { |
||
| 181 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
| 185 | $queryBuilder->limit($numberPerPage); |
||
| 186 | } else { |
||
| 187 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
| 188 | } |
||
| 189 | |||
| 190 | // Limit can not be negative nor null. |
||
| 191 | if ($numberPerPage < 1) { |
||
| 192 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * add a default sort on id if none was specified earlier |
||
| 197 | * |
||
| 198 | * not specifying something to sort on leads to very weird cases when fetching references |
||
| 199 | */ |
||
| 200 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 201 | $queryBuilder->sort('_id'); |
||
| 202 | } |
||
| 203 | |||
| 204 | // on search: check if there is a fulltextsearch Index defined, apply it and search in there |
||
| 205 | if (strstr($request->attributes->get('rawRql'), 'search')) { |
||
| 206 | preg_match('/search\((.*?)\)/', $request->attributes->get('rawRql'), $match); |
||
| 207 | if (!empty($match) && strlen($match[1])) { |
||
| 208 | // this is performing a fulltextsearch in the text-Index of the collection (mongodb Version>=2.6) |
||
| 209 | if ((float) $this->getMongoDBVersion()>=2.6) { |
||
| 210 | // check if there is an index definition for fulltext-search in schema index and apply it. |
||
| 211 | if ($this->getSchema()->textSearchIndex && is_array($this->getSchema()->textSearchIndex) |
||
| 212 | && count($this->getSchema()->textSearchIndex)==2 ) { |
||
| 213 | if ($this->repository->getDocumentManager()->getDocumentCollection( |
||
| 214 | $this->repository->getClassName() |
||
| 215 | )->ensureIndex( |
||
| 216 | (array) $this->getSchema()->textSearchIndex[0], |
||
| 217 | (array) $this->getSchema()->textSearchIndex[1] |
||
| 218 | )) { |
||
| 219 | $queryBuilder->text($match[1]); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } else { |
||
| 223 | $this->logger->addNotice( |
||
| 224 | "Couldn't create text Index for Collection ".$this->repository->getClassName() |
||
| 225 | .". MongoDB Version < 2.6 (".$this->getMongoDBVersion().")" |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | // run query |
||
| 232 | $query = $queryBuilder->getQuery(); |
||
| 233 | $records = array_values($query->execute()->toArray()); |
||
| 234 | |||
| 235 | $totalCount = $query->count(); |
||
| 236 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
| 237 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
| 238 | if ($numPages > 1) { |
||
| 239 | $request->attributes->set('paging', true); |
||
| 240 | $request->attributes->set('page', $page); |
||
| 241 | $request->attributes->set('numPages', $numPages); |
||
| 242 | $request->attributes->set('startAt', $startAt); |
||
| 243 | $request->attributes->set('perPage', $numberPerPage); |
||
| 244 | $request->attributes->set('totalCount', $totalCount); |
||
| 245 | } |
||
| 246 | |||
| 247 | return $records; |
||
| 248 | } |
||
| 249 | |||
| 428 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.