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 |
||
| 21 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html |
||
| 22 | */ |
||
| 23 | class SpanOrQuery implements SpanQueryInterface |
||
| 24 | { |
||
| 25 | use ParametersTrait; |
||
| 26 | |||
| 27 | private array $queries = []; |
||
|
|
|||
| 28 | |||
| 29 | public function __construct(array $parameters = []) |
||
| 30 | { |
||
| 31 | $this->setParameters($parameters); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function addQuery(SpanQueryInterface $query): static |
||
| 35 | { |
||
| 36 | $this->queries[] = $query; |
||
| 37 | |||
| 38 | return $this; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function getQueries(): array |
||
| 42 | { |
||
| 43 | return $this->queries; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function getType(): string |
||
| 47 | { |
||
| 48 | return 'span_or'; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function toArray(): array |
||
| 52 | { |
||
| 53 | $query = []; |
||
| 54 | foreach ($this->queries as $type) { |
||
| 55 | $query['clauses'][] = $type->toArray(); |
||
| 56 | } |
||
| 57 | $output = $this->processArray($query); |
||
| 58 | |||
| 59 | return [$this->getType() => $output]; |
||
| 60 | } |
||
| 61 | } |
||
| 62 |