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 |
||
| 22 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html |
||
| 23 | */ |
||
| 24 | class QueryStringQuery implements BuilderInterface |
||
| 25 | { |
||
| 26 | use ParametersTrait; |
||
| 27 | |||
| 28 | public function __construct(private string $query, array $parameters = []) |
||
|
|
|||
| 29 | { |
||
| 30 | $this->setParameters($parameters); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getType(): string |
||
| 34 | { |
||
| 35 | return 'query_string'; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function toArray(): array |
||
| 39 | { |
||
| 40 | $query = [ |
||
| 41 | 'query' => $this->query, |
||
| 42 | ]; |
||
| 43 | |||
| 44 | $output = $this->processArray($query); |
||
| 45 | |||
| 46 | return [$this->getType() => $output]; |
||
| 47 | } |
||
| 48 | } |
||
| 49 |