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:
| 1 | <?php |
||
| 6 | class Where |
||
| 7 | { |
||
| 8 | protected $field; |
||
| 9 | |||
| 10 | protected $query; |
||
| 11 | |||
| 12 | |||
| 13 | public function __construct(SearchQuery $query) |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param mixed $field |
||
| 20 | */ |
||
| 21 | public function setField($field) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param $value |
||
| 28 | * @return SearchQuery |
||
| 29 | */ |
||
| 30 | public function equal($value) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Добавить фильтр совпадения хотя бы одного значения из набора, этот фильтр не влияет на поле релевантности _score. |
||
| 38 | * |
||
| 39 | * @param $values - массив допустимых значений |
||
| 40 | * @example $query->where('channel')->in([1,2,3])->where('page.categoryId')->in([10,11]); |
||
| 41 | * @return SearchQuery; |
||
|
|
|||
| 42 | */ |
||
| 43 | View Code Duplication | public function in(array $values) |
|
| 44 | { |
||
| 45 | // потому что ES не понимает дырки в ключах |
||
| 46 | $values = array_values($values); |
||
| 47 | $this->query->addFilter('terms', [$this->field => $values]); |
||
| 48 | return $this->query; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Добавить фильтр вхождения значение в диапазон (обе границы включительно). |
||
| 53 | * Можно искать по диапазону дат. |
||
| 54 | * Этот фильтр не влияет на поле релевантности _score. |
||
| 55 | * |
||
| 56 | * @param $min - нижняя граница диапазона (включительно) |
||
| 57 | * @param $max - верхняя граница диапазона (включительно) |
||
| 58 | * @param $dateFormat - необязательное поле описание формата даты |
||
| 59 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-range-query.html |
||
| 60 | * @return SearchQuery; |
||
| 61 | */ |
||
| 62 | public function between($min, $max, $dateFormat = null) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Добавить фильтр "больше или равно" |
||
| 70 | * @param $value - значение |
||
| 71 | * @param null $dateFormat - необязательный формат даты |
||
| 72 | * @return SearchQuery |
||
| 73 | */ |
||
| 74 | public function greaterOrEqual($value, $dateFormat = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Добавить фильтр "больше чем" |
||
| 82 | * @param $value - значение |
||
| 83 | * @param null $dateFormat - необязательный формат даты |
||
| 84 | * @return SearchQuery |
||
| 85 | */ |
||
| 86 | public function greater($value, $dateFormat = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Добавить фильтр "меньше или равно" |
||
| 94 | * @param $value - значение |
||
| 95 | * @param null $dateFormat - необязательный формат даты |
||
| 96 | * @return SearchQuery |
||
| 97 | */ |
||
| 98 | public function lessOrEqual($value, $dateFormat = null) |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Добавить фильтр "меньше чем" |
||
| 107 | * @param $value - значение |
||
| 108 | * @param null $dateFormat - - необязательный формат даты |
||
| 109 | * @return SearchQuery |
||
| 110 | */ |
||
| 111 | public function less($value, $dateFormat = null) |
||
| 116 | |||
| 117 | |||
| 118 | protected function range($params, $dateFormat=null) |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Добавить фильтр полнотекстового поиска, этот фильтр влияет на поле релевантности _score. |
||
| 130 | * |
||
| 131 | * @param $text - поисковая фраза |
||
| 132 | * @return SearchQuery; |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function match($text) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Поле существует и имеет не null значение |
||
| 149 | * @return SearchQuery |
||
| 150 | */ |
||
| 151 | public function exists() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $value |
||
| 159 | * @return SearchQuery |
||
| 160 | */ |
||
| 161 | public function not($value) |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $values - массив допустимых значений |
||
| 170 | * @example $query->where('channel')->notIn([1,2,3]); |
||
| 171 | * @return SearchQuery; |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function notIn(array $values) |
|
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $min |
||
| 184 | * @param $max |
||
| 185 | * @param null $dateFormat |
||
| 186 | * @return SearchQuery |
||
| 187 | */ |
||
| 188 | public function notBetween($min, $max, $dateFormat = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $text |
||
| 200 | * @return SearchQuery |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function notMatch($text) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return SearchQuery |
||
| 217 | */ |
||
| 218 | public function notExists() |
||
| 223 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.