| Conditions | 11 |
| Paths | 8 |
| Total Lines | 54 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 68 | private function makeQuerySearchByTable(string $tableName, string $tableAlias): CompositeExpression |
||
| 69 | { |
||
| 70 | $fieldsToSearchWithin = $this->extractSearchableFieldsFromTable($tableName); |
||
| 71 | $searchTerm = (string)$this->searchDemand->getSearchTerm(); |
||
| 72 | $constraints = []; |
||
| 73 | |||
| 74 | $searchTermParts = str_getcsv($searchTerm, ' '); |
||
| 75 | foreach ($searchTermParts as $searchTermPart) { |
||
| 76 | $searchTermPart = trim($searchTermPart); |
||
| 77 | if ($searchTermPart === '') { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | $constraintsForParts = []; |
||
| 81 | $like = '%' . $this->queryBuilder->escapeLikeWildcards($searchTermPart) . '%'; |
||
| 82 | foreach ($fieldsToSearchWithin as $fieldName) { |
||
| 83 | if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | $fieldConfig = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']; |
||
| 87 | $fieldType = $fieldConfig['type']; |
||
| 88 | $evalRules = $fieldConfig['eval'] ?? ''; |
||
| 89 | |||
| 90 | // Check whether search should be case-sensitive or not |
||
| 91 | if (in_array('case', (array)($fieldConfig['search'] ?? []), true)) { |
||
| 92 | // case sensitive |
||
| 93 | $searchConstraint = $this->queryBuilder->expr()->andX( |
||
| 94 | $this->queryBuilder->expr()->like( |
||
| 95 | $tableAlias . '.' . $fieldName, |
||
| 96 | $this->queryBuilder->createNamedParameter($like, \PDO::PARAM_STR) |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | } else { |
||
| 100 | $searchConstraint = $this->queryBuilder->expr()->andX( |
||
| 101 | // case insensitive |
||
| 102 | $this->queryBuilder->expr()->comparison( |
||
| 103 | 'LOWER(' . $this->queryBuilder->quoteIdentifier($tableAlias . '.' . $fieldName) . ')', |
||
| 104 | 'LIKE', |
||
| 105 | $this->queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR) |
||
| 106 | ) |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Assemble the search condition only if the field makes sense to be searched |
||
| 111 | if ($fieldType === 'text' |
||
| 112 | || $fieldType === 'flex' |
||
| 113 | || ($fieldType === 'input' && (!$evalRules || !preg_match('/\b(?:date|time|int)\b/', $evalRules))) |
||
| 114 | ) { |
||
| 115 | $constraintsForParts[] = $searchConstraint; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $constraints[] = $this->queryBuilder->expr()->orX(...$constraintsForParts); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $this->queryBuilder->expr()->andX(...$constraints); |
||
| 122 | } |
||
| 153 |