| Conditions | 23 |
| Paths | 6 |
| Total Lines | 178 |
| Code Lines | 90 |
| 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 |
||
| 242 | protected function buildFilterQueryPart($filters, $excludeFilters = []) { |
||
| 243 | |||
| 244 | $queryFilter = []; |
||
| 245 | |||
| 246 | // Build the column filter part. |
||
| 247 | if ($filters && is_array($filters)) { |
||
| 248 | |||
| 249 | $validKeys = [ |
||
| 250 | 'aliasState', 'persons', 'doctype', 'hasFiles', 'year', |
||
| 251 | 'universityCollection', 'creatorRole', 'creationDate' |
||
| 252 | ]; |
||
| 253 | |||
| 254 | foreach ($filters as $key => $filterValues) { |
||
| 255 | $queryFilterPart = []; |
||
| 256 | if (in_array($key, $validKeys, true)) { |
||
| 257 | if ($key == 'universityCollection') { |
||
| 258 | if ($filterValues && is_array($filterValues)) { |
||
| 259 | if (in_array("true", $filterValues)) { |
||
| 260 | $filterValue = $this->getSettings()['universityCollection']; |
||
| 261 | $queryFilterPart['bool']['should'][] = [ |
||
| 262 | 'term' => [ |
||
| 263 | 'collections' => $filterValue |
||
| 264 | ] |
||
| 265 | ]; |
||
| 266 | } else { |
||
| 267 | $filterValue = $this->getSettings()['universityCollection']; |
||
| 268 | $queryFilterPart['bool']['should'][] = [ |
||
| 269 | 'bool' => [ |
||
| 270 | 'must_not' => [ |
||
| 271 | 'term' => [ |
||
| 272 | 'collections' => $filterValue |
||
| 273 | ] |
||
| 274 | ] |
||
| 275 | ] |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 279 | } |
||
| 280 | } elseif ($key == 'creatorRole') { |
||
| 281 | $queryFilterPart = []; |
||
| 282 | if ($filterValues && is_array($filterValues)) { |
||
| 283 | if (in_array("librarian", $filterValues)) { |
||
| 284 | $creatorRolePart['bool']['must'] = [ |
||
| 285 | [ |
||
| 286 | 'term' => [ |
||
| 287 | 'creatorRole' => Security::ROLE_LIBRARIAN |
||
| 288 | ] |
||
| 289 | ], |
||
| 290 | [ |
||
| 291 | 'bool' => [ |
||
| 292 | 'must_not' => [ |
||
| 293 | 'term' => [ |
||
| 294 | 'creator' => $this->security->getUser()->getUid() |
||
| 295 | ] |
||
| 296 | ] |
||
| 297 | ] |
||
| 298 | ] |
||
| 299 | ]; |
||
| 300 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 301 | } elseif (in_array("user", $filterValues)) { |
||
| 302 | $creatorRolePart['bool']['must'] = [ |
||
| 303 | [ |
||
| 304 | 'term' => [ |
||
| 305 | 'creatorRole' => Security::ROLE_RESEARCHER |
||
| 306 | ] |
||
| 307 | ], |
||
| 308 | [ |
||
| 309 | 'bool' => [ |
||
| 310 | 'must_not' => [ |
||
| 311 | 'term' => [ |
||
| 312 | 'creator' => $this->security->getUser()->getUid() |
||
| 313 | ] |
||
| 314 | ] |
||
| 315 | ] |
||
| 316 | ] |
||
| 317 | ]; |
||
| 318 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 319 | } elseif (in_array("self", $filterValues)) { |
||
| 320 | $creatorRolePart['bool']['must'] = [ |
||
| 321 | [ |
||
| 322 | 'term' => [ |
||
| 323 | 'creator' => $this->security->getUser()->getUid() |
||
| 324 | ] |
||
| 325 | ] |
||
| 326 | ]; |
||
| 327 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 328 | } else { |
||
| 329 | $creatorRolePart['bool']['must'] = [ |
||
| 330 | [ |
||
| 331 | 'bool' => [ |
||
| 332 | 'must_not' => [ |
||
| 333 | 'term' => [ |
||
| 334 | 'creator' => $this->security->getUser()->getUid() |
||
| 335 | ] |
||
| 336 | ] |
||
| 337 | ] |
||
| 338 | ], |
||
| 339 | [ |
||
| 340 | 'bool' => [ |
||
| 341 | 'must_not' => [ |
||
| 342 | 'term' => [ |
||
| 343 | 'creatorRole' => Security::ROLE_LIBRARIAN |
||
| 344 | ] |
||
| 345 | ] |
||
| 346 | ] |
||
| 347 | ], |
||
| 348 | [ |
||
| 349 | 'bool' => [ |
||
| 350 | 'must_not' => [ |
||
| 351 | 'term' => [ |
||
| 352 | 'creatorRole' => Security::ROLE_RESEARCHER |
||
| 353 | ] |
||
| 354 | ] |
||
| 355 | ] |
||
| 356 | ] |
||
| 357 | ]; |
||
| 358 | $queryFilterPart['bool']['should'][] = $creatorRolePart; |
||
| 359 | } |
||
| 360 | |||
| 361 | if ($queryFilterPart) { |
||
| 362 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } else { |
||
| 366 | if ($filterValues && is_array($filterValues)) { |
||
| 367 | foreach ($filterValues as $filterValue) { |
||
| 368 | $queryFilterPart['bool']['should'][] = [ |
||
| 369 | 'term' => [ |
||
| 370 | $key => $filterValue |
||
| 371 | ] |
||
| 372 | ]; |
||
| 373 | } |
||
| 374 | $queryFilter['bool']['must'][] = $queryFilterPart; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | if ($excludeFilters && array_key_exists('aliasState', $excludeFilters)) { |
||
| 382 | if ($excludeFilters['aliasState']) { |
||
| 383 | foreach ($excludeFilters['aliasState'] as $aliasStateExclude) { |
||
| 384 | $queryFilter['bool']['must'][] = [ |
||
| 385 | 'bool' => [ |
||
| 386 | 'must_not' => [ |
||
| 387 | 'bool' => [ |
||
| 388 | 'must' => [ |
||
| 389 | [ |
||
| 390 | 'term' => [ |
||
| 391 | 'aliasState' => $aliasStateExclude |
||
| 392 | ] |
||
| 393 | ], |
||
| 394 | [ |
||
| 395 | 'bool' => [ |
||
| 396 | 'should' => [ |
||
| 397 | [ |
||
| 398 | 'term' => [ |
||
| 399 | 'creator' => $this->security->getUser()->getUid() |
||
| 400 | ] |
||
| 401 | ], |
||
| 402 | [ |
||
| 403 | 'term' => [ |
||
| 404 | 'fobIdentifiers' => $this->security->getUser()->getFisPersId() |
||
| 405 | ] |
||
| 406 | ] |
||
| 407 | ] |
||
| 408 | ] |
||
| 409 | ] |
||
| 410 | ] |
||
| 411 | ] |
||
| 412 | ] |
||
| 413 | ] |
||
| 414 | ]; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | return $queryFilter; |
||
| 420 | } |
||
| 554 |
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.