Completed
Pull Request — master (#100)
by
unknown
03:36
created

AndQuery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 1
dl 52
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A addQuery() 4 4 1
A getType() 4 4 1
A toArray() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Query;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16
17
/**
18
 * Represents Elasticsearch "and" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-query.html
21
 */
22 View Code Duplication
class AndQuery implements BuilderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * @var array
26
     */
27
    private $queries;
28
29
    /**
30
     * @param array $queries
31
     */
32
    public function __construct(array $queries = [])
33
    {
34
        $this->queries = $queries;
35
    }
36
37
    /**
38
     * adds a query to join with AND operator
39
     *
40
     * @param BuilderInterface $query
41
     */
42
    public function addQuery(BuilderInterface $query)
43
    {
44
        $this->queries[] = $query;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getType()
51
    {
52
        return 'and';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function toArray()
59
    {
60
        $output = [];
61
        foreach ($this->queries as $query) {
62
            try {
63
                $output[] = $query->toArray();
64
            } catch (\Error $e) {
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
65
                throw new InvalidArgumentException(
66
                    'Queries given to `AND` query must be instances of BuilderInterface'
67
                );
68
            }
69
        }
70
71
        return [$this->getType() => $output];
72
    }
73
}
74