| Conditions | 23 |
| Paths | 6 |
| Total Lines | 166 |
| Code Lines | 85 |
| 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 |
||
| 239 | protected function buildFilterQueryPart($filters, $excludeFilters = []) { |
||
| 240 | |||
| 241 | $queryFilter = []; |
||
| 242 | |||
| 243 | // Build the column filter part. |
||
| 244 | if ($filters && is_array($filters)) { |
||
| 245 | |||
| 246 | $validKeys = [ |
||
| 247 | 'aliasState', 'authorAndPublisher', 'doctype', 'hasFiles', 'year', 'universityCollection', 'creatorRole' |
||
| 248 | ]; |
||
| 249 | |||
| 250 | foreach ($filters as $key => $filterValues) { |
||
| 251 | $queryFilterPart = []; |
||
| 252 | if (in_array($key, $validKeys, true)) { |
||
| 253 | if ($key == 'universityCollection') { |
||
| 254 | if ($filterValues && is_array($filterValues)) { |
||
| 255 | if (in_array("true", $filterValues)) { |
||
| 256 | $filterValue = $this->getSettings()['universityCollection']; |
||
| 257 | $queryFilterPart['bool']['should'][] = [ |
||
| 258 | 'term' => [ |
||
| 259 | 'collections' => $filterValue |
||
| 260 | ] |
||
| 261 | ]; |
||
| 262 | } else { |
||
| 263 | $filterValue = $this->getSettings()['universityCollection']; |
||
| 264 | $queryFilterPart['bool']['should'][] = [ |
||
| 265 | 'bool' => [ |
||
| 266 | 'must_not' => [ |
||
| 267 | 'term' => [ |
||
| 268 | 'collections' => $filterValue |
||
| 269 | ] |
||
| 270 | ] |
||
| 271 | ] |
||
| 272 | ]; |
||
| 273 | } |
||
| 274 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 275 | } |
||
| 276 | } elseif ($key == 'creatorRole') { |
||
| 277 | $queryFilterPart = []; |
||
| 278 | if ($filterValues && is_array($filterValues)) { |
||
| 279 | if (in_array("librarian", $filterValues)) { |
||
| 280 | $creatorRolePart['bool']['must'] = [ |
||
| 281 | [ |
||
| 282 | 'term' => [ |
||
| 283 | 'creatorRole' => Security::ROLE_LIBRARIAN |
||
| 284 | ] |
||
| 285 | ], |
||
| 286 | [ |
||
| 287 | 'bool' => [ |
||
| 288 | 'must_not' => [ |
||
| 289 | 'term' => [ |
||
| 290 | 'creator' => $this->security->getUser()->getUid() |
||
| 291 | ] |
||
| 292 | ] |
||
| 293 | ] |
||
| 294 | ] |
||
| 295 | ]; |
||
| 296 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 297 | } elseif (in_array("user", $filterValues)) { |
||
| 298 | $creatorRolePart['bool']['must'] = [ |
||
| 299 | [ |
||
| 300 | 'term' => [ |
||
| 301 | 'creatorRole' => Security::ROLE_RESEARCHER |
||
| 302 | ] |
||
| 303 | ], |
||
| 304 | [ |
||
| 305 | 'bool' => [ |
||
| 306 | 'must_not' => [ |
||
| 307 | 'term' => [ |
||
| 308 | 'creator' => $this->security->getUser()->getUid() |
||
| 309 | ] |
||
| 310 | ] |
||
| 311 | ] |
||
| 312 | ] |
||
| 313 | ]; |
||
| 314 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 315 | } elseif (in_array("self", $filterValues)) { |
||
| 316 | $creatorRolePart['bool']['must'] = [ |
||
| 317 | [ |
||
| 318 | 'term' => [ |
||
| 319 | 'creator' => $this->security->getUser()->getUid() |
||
| 320 | ] |
||
| 321 | ] |
||
| 322 | ]; |
||
| 323 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 324 | } else { |
||
| 325 | $creatorRolePart['bool']['must'] = [ |
||
| 326 | [ |
||
| 327 | 'bool' => [ |
||
| 328 | 'must_not' => [ |
||
| 329 | 'term' => [ |
||
| 330 | 'creator' => $this->security->getUser()->getUid() |
||
| 331 | ] |
||
| 332 | ] |
||
| 333 | ] |
||
| 334 | ], |
||
| 335 | [ |
||
| 336 | 'bool' => [ |
||
| 337 | 'must_not' => [ |
||
| 338 | 'term' => [ |
||
| 339 | 'creatorRole' => Security::ROLE_LIBRARIAN |
||
| 340 | ] |
||
| 341 | ] |
||
| 342 | ] |
||
| 343 | ], |
||
| 344 | [ |
||
| 345 | 'bool' => [ |
||
| 346 | 'must_not' => [ |
||
| 347 | 'term' => [ |
||
| 348 | 'creatorRole' => Security::ROLE_RESEARCHER |
||
| 349 | ] |
||
| 350 | ] |
||
| 351 | ] |
||
| 352 | ] |
||
| 353 | ]; |
||
| 354 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 355 | } |
||
| 356 | |||
| 357 | if ($queryFilterPart) { |
||
| 358 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } else { |
||
| 362 | if ($filterValues && is_array($filterValues)) { |
||
| 363 | foreach ($filterValues as $filterValue) { |
||
| 364 | $queryFilterPart['bool']['should'][] = [ |
||
| 365 | 'term' => [ |
||
| 366 | $key => $filterValue |
||
| 367 | ] |
||
| 368 | ]; |
||
| 369 | } |
||
| 370 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | if ($excludeFilters && array_key_exists('aliasState', $excludeFilters)) { |
||
| 378 | if ($excludeFilters['aliasState']) { |
||
| 379 | foreach ($excludeFilters['aliasState'] as $aliasStateExclude) { |
||
| 380 | $queryFilter['bool']['must'][] = [ |
||
| 381 | 'bool' => [ |
||
| 382 | 'must_not' => [ |
||
| 383 | 'bool' => [ |
||
| 384 | 'must' => [ |
||
| 385 | [ |
||
| 386 | 'term' => [ |
||
| 387 | 'aliasState' => $aliasStateExclude |
||
| 388 | ] |
||
| 389 | ], |
||
| 390 | [ |
||
| 391 | 'term' => [ |
||
| 392 | 'creator' => $this->security->getUser()->getUid() |
||
| 393 | ] |
||
| 394 | ] |
||
| 395 | ] |
||
| 396 | ] |
||
| 397 | ] |
||
| 398 | ] |
||
| 399 | ]; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | return $queryFilter; |
||
| 405 | } |
||
| 535 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.