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-dis-max-query.html |
||
23 | */ |
||
24 | class DisMaxQuery implements BuilderInterface |
||
25 | { |
||
26 | use ParametersTrait; |
||
27 | |||
28 | private array $queries = []; |
||
|
|||
29 | |||
30 | public function __construct(array $parameters = []) |
||
31 | { |
||
32 | $this->setParameters($parameters); |
||
33 | } |
||
34 | |||
35 | public function addQuery(BuilderInterface $query): static |
||
36 | { |
||
37 | $this->queries[] = $query; |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | public function getType(): string |
||
43 | { |
||
44 | return 'dis_max'; |
||
45 | } |
||
46 | |||
47 | public function toArray(): array |
||
48 | { |
||
49 | $query = []; |
||
50 | foreach ($this->queries as $type) { |
||
51 | $query[] = $type->toArray(); |
||
52 | } |
||
53 | $output = $this->processArray(['queries' => $query]); |
||
54 | |||
55 | return [$this->getType() => $output]; |
||
56 | } |
||
57 | } |
||
58 |