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