| Total Complexity | 96 |
| Total Lines | 635 |
| Duplicated Lines | 20 % |
| Coverage | 0.72% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilderFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class QueryBuilderFactory extends AbstractQuery |
||
| 8 | { |
||
| 9 | const DIRECTION_AZ = 'asc'; |
||
| 10 | |||
| 11 | const DIRECTION_ZA = 'desc'; |
||
| 12 | |||
| 13 | const DEFAULT_OPERATOR = 'eq'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var QueryBuilder |
||
| 17 | */ |
||
| 18 | protected $qBuilder; |
||
| 19 | |||
| 20 | protected $fields; |
||
| 21 | |||
| 22 | protected $filtering; |
||
| 23 | |||
| 24 | protected $orFiltering; |
||
| 25 | |||
| 26 | protected $relationEntityAlias; |
||
| 27 | |||
| 28 | protected $sorting; |
||
| 29 | |||
| 30 | protected $joins; |
||
| 31 | |||
| 32 | protected $rel; |
||
| 33 | |||
| 34 | protected $printing; |
||
| 35 | |||
| 36 | protected $page; |
||
| 37 | |||
| 38 | protected $pageLength; |
||
| 39 | |||
| 40 | protected $select; |
||
| 41 | |||
| 42 | /** @todo move this file in configuration */ |
||
| 43 | /** @todo type number|text */ |
||
| 44 | private static $operatorMap = [ |
||
| 45 | //'eq' => [ |
||
| 46 | //'filtro' => '=', |
||
| 47 | //'tipo' => 'data|numero|stringa', |
||
| 48 | //'meta' => '%{foo}%' |
||
| 49 | //], |
||
| 50 | 'eq' => [ |
||
| 51 | 'meta' => '=', |
||
| 52 | ], |
||
| 53 | 'neq' => [ |
||
| 54 | 'meta' => '!=', |
||
| 55 | ], |
||
| 56 | 'gt' => [ |
||
| 57 | 'meta' => '>', |
||
| 58 | ], |
||
| 59 | 'gte' => [ |
||
| 60 | 'meta' => '>=', |
||
| 61 | ], |
||
| 62 | 'lt' => [ |
||
| 63 | 'meta' => '<', |
||
| 64 | ], |
||
| 65 | 'lte' => [ |
||
| 66 | 'meta' => '<=', |
||
| 67 | ], |
||
| 68 | 'startswith' => [ |
||
| 69 | 'meta' => 'LIKE', |
||
| 70 | 'substitution_pattern' => '{string}%' |
||
| 71 | ], |
||
| 72 | 'contains' => [ |
||
| 73 | 'meta' => 'LIKE', |
||
| 74 | 'substitution_pattern' => '%{string}%' |
||
| 75 | ], |
||
| 76 | 1 | 'notcontains' => [ |
|
| 77 | 'meta' => 'NOT LIKE', |
||
| 78 | 1 | 'substitution_pattern' => '%{string}%' |
|
| 79 | ], |
||
| 80 | 'endswith' => [ |
||
| 81 | 'meta' => 'LIKE', |
||
| 82 | 'substitution_pattern' => '%{string}' |
||
| 83 | ], |
||
| 84 | 'list' => [ |
||
| 85 | 'meta' => 'IN', |
||
| 86 | 'substitution_pattern' => '({string})', |
||
| 87 | ], |
||
| 88 | 'field_eq' => [ |
||
| 89 | 'meta' => '=', |
||
| 90 | ], |
||
| 91 | ]; |
||
| 92 | |||
| 93 | public function getAvailableFilters() |
||
| 94 | { |
||
| 95 | return array_keys(static::$operatorMap); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setFields(array $fields = []) |
||
| 99 | { |
||
| 100 | $this->fields = $fields; |
||
| 101 | |||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getFields() |
||
| 106 | { |
||
| 107 | if (null === $this->fields) { |
||
| 108 | throw new \RuntimeException( |
||
| 109 | 'Oops! Fields are not defined' |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $this->fields; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function setFilters(array $filtering = []) |
||
| 121 | } |
||
| 122 | |||
| 123 | public function setOrFilters(array $orFiltering = []) |
||
| 124 | { |
||
| 125 | $this->orFiltering = $orFiltering; |
||
| 126 | |||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function setSorting(array $sorting = []) |
||
| 131 | { |
||
| 132 | $this->sorting = $sorting; |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getFilters() |
||
| 138 | { |
||
| 139 | return $this->filtering; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getOrFilters() |
||
| 143 | { |
||
| 144 | return $this->orFiltering; |
||
| 145 | } |
||
| 146 | |||
| 147 | private function noExistsJoin($prevEntityAlias, $currentEntityAlias) |
||
| 148 | { |
||
| 149 | if (null === $this->joins) { |
||
| 150 | $this->joins = []; |
||
| 151 | } |
||
| 152 | |||
| 153 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
| 154 | |||
| 155 | return ! in_array($needle, $this->joins); |
||
| 156 | } |
||
| 157 | |||
| 158 | private function storeJoin($prevEntityAlias, $currentEntityAlias) |
||
| 159 | { |
||
| 160 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
| 161 | $this->joins[$needle] = $needle; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name) |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function join(String $relation) |
||
| 217 | } |
||
| 218 | |||
| 219 | public function filter() |
||
| 220 | { |
||
| 221 | if (null === $this->filtering) { |
||
| 222 | throw new \RuntimeException( |
||
| 256 | } |
||
| 257 | |||
| 258 | private function applyFilterAnd($filter, $value) |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | private function applyFilterOr($filter, $value, $orCondition) |
||
| 369 | { |
||
| 370 | $whereCondition = null; |
||
| 371 | $filterAndOperator = explode('|',$filter); |
||
| 372 | |||
| 373 | $fieldName = $filterAndOperator[0]; |
||
| 374 | $fieldName = $this->parser->camelize($fieldName); |
||
| 375 | |||
| 376 | $operator = self::$operatorMap[self::DEFAULT_OPERATOR]; |
||
| 377 | if(isset($filterAndOperator[1])){ |
||
| 378 | $operator = self::$operatorMap[$filterAndOperator[1]]; |
||
| 379 | } |
||
| 380 | |||
| 381 | // controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
||
| 382 | // esempio per users: filtering[username|contains]=mado |
||
| 383 | if (in_array($fieldName, $this->fields)) { |
||
| 384 | |||
| 385 | $salt = ''; |
||
| 386 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
| 387 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
| 388 | $salt = '_' . rand(111, 999); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | if ($salt == '') { |
||
| 393 | $salt = '_' . rand(111, 999); |
||
| 394 | } |
||
| 395 | |||
| 396 | // filtering[foo|bar] |
||
| 397 | // $filterAndOperator[0] = 'foo' |
||
| 398 | // $filterAndOperator[1] = 'bar' |
||
| 399 | View Code Duplication | if (isset($filterAndOperator[1])) { |
|
| 400 | if ('list' == $filterAndOperator[1]) { |
||
| 401 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
| 402 | } else if ('field_eq' == $filterAndOperator[1]) { |
||
| 403 | $whereCondition = |
||
| 404 | $this->entityAlias . '.' . $fieldName . ' '. |
||
| 405 | $operator['meta'] . '' . |
||
| 406 | $this->entityAlias . '.' . $value |
||
| 407 | ; |
||
| 408 | //} else { |
||
| 409 | //throw new \RuntimeException( |
||
| 410 | //'Oops! Eccezzione' |
||
| 411 | //); |
||
| 412 | } else { |
||
| 413 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
| 414 | } |
||
| 415 | } else { |
||
| 416 | $whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
||
| 417 | } |
||
| 418 | |||
| 419 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
| 420 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
| 421 | } else { |
||
| 422 | $orCondition['orCondition'] = $whereCondition; |
||
| 423 | } |
||
| 424 | |||
| 425 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
| 426 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
| 427 | $value = explode(',', $value); |
||
| 428 | } else { |
||
| 429 | $value = str_replace( |
||
| 430 | '{string}', |
||
| 431 | $value, |
||
| 432 | $operator['substitution_pattern'] |
||
| 433 | ); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | $orCondition['parameters'][] = [ |
||
| 438 | 'field' => 'field_' . $fieldName . $salt, |
||
| 439 | 'value' => $value |
||
| 440 | ]; |
||
| 441 | } else { |
||
| 442 | $isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
||
| 443 | if ($isNotARelation) { |
||
| 444 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
||
| 445 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
| 446 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
| 447 | } else { |
||
| 448 | $orCondition['orCondition'] = $whereCondition; |
||
| 449 | } |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
||
| 454 | // esempio per users: filtering[_embedded.groups.name|eq]=admin |
||
| 455 | if (strstr($filter, '_embedded.')) { |
||
| 456 | |||
| 457 | $this->join($filter); |
||
| 458 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
| 459 | |||
| 460 | $embeddedFields = explode('.', $fieldName); |
||
| 461 | $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
||
| 462 | |||
| 463 | $salt = ''; |
||
| 464 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
| 465 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
| 466 | $salt = '_' . rand(111, 999); |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | if ($salt == '') { |
||
| 471 | $salt = '_' . rand(111, 999); |
||
| 472 | } |
||
| 473 | |||
| 474 | View Code Duplication | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
| 475 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
| 476 | } else { |
||
| 477 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
| 478 | } |
||
| 479 | |||
| 480 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
| 481 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
| 482 | } else { |
||
| 483 | $orCondition['orCondition'] = $whereCondition; |
||
| 484 | } |
||
| 485 | |||
| 486 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
| 487 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
| 488 | $value = explode(',', $value); |
||
| 489 | } else { |
||
| 490 | $value = str_replace( |
||
| 491 | '{string}', |
||
| 492 | $value, |
||
| 493 | $operator['substitution_pattern'] |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | $orCondition['parameters'][] = [ |
||
| 499 | 'field' => 'field_' . $fieldName . $salt, |
||
| 500 | 'value' => $value |
||
| 501 | ]; |
||
| 502 | } |
||
| 503 | |||
| 504 | return $orCondition; |
||
| 505 | } |
||
| 506 | |||
| 507 | public function sort() |
||
| 508 | { |
||
| 509 | if (!$this->fields) { |
||
| 510 | throw new \RuntimeException( |
||
| 511 | 'Oops! Fields are not defined' |
||
| 512 | ); |
||
| 513 | } |
||
| 514 | |||
| 515 | if (null === $this->sorting) { |
||
| 516 | throw new \RuntimeException( |
||
| 517 | 'Oops! Sorting is not defined' |
||
| 518 | ); |
||
| 519 | } |
||
| 520 | |||
| 521 | foreach ($this->sorting as $sort => $val) { |
||
| 522 | $val = strtolower($val); |
||
| 523 | |||
| 524 | $fieldName = $this->parser->camelize($sort); |
||
| 525 | |||
| 526 | if (in_array($fieldName, $this->fields)) { |
||
| 527 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
| 528 | $this->qBuilder->addOrderBy($this->entityAlias .'.'. $fieldName, $direction); |
||
| 529 | } |
||
| 530 | |||
| 531 | if (strstr($sort, '_embedded.')) { |
||
| 532 | $this->join($sort); |
||
| 533 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
| 534 | |||
| 535 | $embeddedFields = explode('.', $sort); |
||
| 536 | $fieldName = $this->parser->camelize($embeddedFields[2]); |
||
| 537 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
| 538 | |||
| 539 | $this->qBuilder->addOrderBy($relationEntityAlias.'.'.$fieldName, $direction); |
||
| 540 | } |
||
| 541 | |||
| 542 | } |
||
| 543 | |||
| 544 | return $this; |
||
| 545 | } |
||
| 546 | |||
| 547 | public function getQueryBuilder() |
||
| 548 | { |
||
| 549 | if (!$this->qBuilder) { |
||
| 550 | throw new \RuntimeException( |
||
| 551 | "Oops! Query builder was never initialized! call ::createQueryBuilder('entityName', 'alias') to start." |
||
| 552 | ); |
||
| 553 | } |
||
| 554 | |||
| 555 | return $this->qBuilder; |
||
| 556 | } |
||
| 557 | |||
| 558 | public function buildSelectValue() : string |
||
| 559 | { |
||
| 560 | if ("" == $this->getSelect()) { |
||
| 561 | return $this->getEntityAlias( |
||
| 562 | $this->getEntityName() |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | |||
| 566 | return $this->getSelect(); |
||
| 567 | } |
||
| 568 | |||
| 569 | private function setRelationEntityAlias(string $relationEntityAlias) |
||
| 570 | { |
||
| 571 | $this->relationEntityAlias = $relationEntityAlias; |
||
| 572 | } |
||
| 573 | |||
| 574 | private function getRelationEntityAlias() |
||
| 575 | { |
||
| 576 | return $this->relationEntityAlias; |
||
| 577 | } |
||
| 578 | |||
| 579 | public function setRel($rel) |
||
| 580 | { |
||
| 581 | $this->rel = $rel; |
||
| 582 | |||
| 583 | return $this; |
||
| 584 | } |
||
| 585 | |||
| 586 | public function getRel() |
||
| 587 | { |
||
| 588 | return $this->rel; |
||
| 589 | } |
||
| 590 | |||
| 591 | public function setPrinting($printing) |
||
| 592 | { |
||
| 593 | $this->printing = $printing; |
||
| 594 | |||
| 595 | return $this; |
||
| 596 | } |
||
| 597 | |||
| 598 | public function getPrinting() |
||
| 601 | } |
||
| 602 | |||
| 603 | public function setPage($page) |
||
| 604 | { |
||
| 605 | $this->page = $page; |
||
| 606 | |||
| 607 | return $this; |
||
| 608 | } |
||
| 609 | |||
| 610 | public function getPage() |
||
| 611 | { |
||
| 612 | return $this->page; |
||
| 613 | } |
||
| 614 | |||
| 615 | public function setPageLength($pageLength) |
||
| 616 | { |
||
| 617 | $this->pageLength = $pageLength; |
||
| 618 | |||
| 619 | return $this; |
||
| 620 | } |
||
| 621 | |||
| 622 | public function getPageLength() |
||
| 623 | { |
||
| 624 | return $this->pageLength; |
||
| 625 | } |
||
| 626 | |||
| 627 | public function setSelect( $select) : QueryBuilderFactory |
||
| 628 | { |
||
| 629 | $this->select = $select; |
||
| 630 | |||
| 631 | return $this; |
||
| 632 | } |
||
| 633 | |||
| 634 | public function getSelect() |
||
| 637 | } |
||
| 638 | |||
| 639 | public function getEntityManager() |
||
| 640 | { |
||
| 641 | return $this->manager; |
||
| 642 | } |
||
| 643 | } |
||
| 644 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths