| Total Complexity | 41 |
| Total Lines | 148 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Filter 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 Filter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Filter extends FilterAbstract |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @param Builder $builder |
||
| 13 | * @param $descriptors |
||
| 14 | * @return mixed |
||
| 15 | */ |
||
| 16 | public function filter(Builder $builder, $descriptors) |
||
| 17 | { |
||
| 18 | $logic = array_get($descriptors, 'logic', 'and'); |
||
| 19 | $filters = array_get($descriptors, 'filters', []); |
||
| 20 | |||
| 21 | if (! is_array($filters)) { |
||
| 22 | return $builder; |
||
| 23 | } |
||
| 24 | |||
| 25 | foreach ($filters as $filter) { |
||
| 26 | $this->handle($builder, $filter, $logic); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param Builder $builder |
||
| 32 | * @param $descriptor |
||
| 33 | * @param $logic |
||
| 34 | * @return \Illuminate\Database\Query\Builder|Builder |
||
| 35 | */ |
||
| 36 | protected function handle(Builder $builder, $descriptor, $logic) |
||
| 37 | { |
||
| 38 | if (! is_array($descriptor) || ! array_key_exists('field', $descriptor)) { |
||
| 39 | return $builder; |
||
| 40 | } |
||
| 41 | |||
| 42 | $logic = $logic === 'or' ? 'or' : 'and'; |
||
| 43 | |||
| 44 | if (array_key_exists('operator', $descriptor)) { |
||
| 45 | if ($descriptor['operator'] === 'isnull') { |
||
| 46 | return $builder->whereNull($descriptor['field'], $logic); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($descriptor['operator'] === 'isnotnull') { |
||
| 50 | return $builder->whereNotNull($descriptor['field'], $logic); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return $builder->where( |
||
| 55 | $this->resolveField($descriptor), |
||
| 56 | $this->resolveOperator($descriptor), |
||
| 57 | $this->resolveValue($descriptor), |
||
| 58 | $logic |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $descriptor |
||
| 64 | * @return \Illuminate\Database\Query\Expression |
||
| 65 | */ |
||
| 66 | protected function resolveField($descriptor) |
||
| 67 | { |
||
| 68 | return $this->isBinary($descriptor) ? DB::raw('BINARY '.$descriptor['field']) : $descriptor['field']; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $descriptor |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | protected function resolveOperator($descriptor) |
||
| 76 | { |
||
| 77 | if (! array_key_exists('operator', $descriptor)) { |
||
| 78 | return '='; |
||
| 79 | } |
||
| 80 | |||
| 81 | switch ($descriptor['operator']) { |
||
| 82 | case 'eq': |
||
| 83 | case 'isempty': |
||
| 84 | return '='; |
||
| 85 | |||
| 86 | case 'neq': |
||
| 87 | case 'isnotempty': |
||
| 88 | return '!='; |
||
| 89 | |||
| 90 | case 'lt': |
||
| 91 | return '<'; |
||
| 92 | |||
| 93 | case 'lte': |
||
| 94 | return '<='; |
||
| 95 | |||
| 96 | case 'gt': |
||
| 97 | return '>'; |
||
| 98 | |||
| 99 | case 'gte': |
||
| 100 | return '>='; |
||
| 101 | |||
| 102 | case 'startswith': |
||
| 103 | case 'endswith': |
||
| 104 | case 'contains': |
||
| 105 | return 'LIKE'; |
||
| 106 | |||
| 107 | case 'doesnotcontain': |
||
| 108 | return 'NOT LIKE'; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $descriptor |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | protected function resolveValue($descriptor) |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $descriptor |
||
| 148 | * @param $sensitive |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | protected function isBinary($descriptor) |
||
| 159 |