Completed
Pull Request — master (#1376)
by Bilge
30:49 queued 28:17
created

FieldExtractor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 67
Duplicated Lines 20.9 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%
Metric Value
wmc 19
lcom 1
cbo 0
dl 14
loc 67
ccs 0
cts 56
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getFields() 6 22 9
B getFieldsFromElemMatch() 8 19 5
A isOperator() 0 12 4

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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Query;
21
22
/**
23
 * Class responsible for extracting an array of field names that are involved in
24
 * a given mongodb query. Used for checking if query is indexed.
25
 *
26
 * @see Doctrine\ODM\MongoDB\Query::isIndexed()
27
 */
28
class FieldExtractor
29
{
30
    private $query;
31
    private $sort;
32
33
    public function __construct(array $query, array $sort = array())
34
    {
35
        $this->query = $query;
36
        $this->sort = $sort;
37
    }
38
39
    public function getFields()
40
    {
41
        $fields = array();
42
43
        foreach ($this->query as $k => $v) {
44
            if (is_array($v) && isset($v['$elemMatch']) && is_array($v['$elemMatch'])) {
45
                $elemMatchFields = $this->getFieldsFromElemMatch($v['$elemMatch']);
46
                foreach ($elemMatchFields as $field) {
47
                    $fields[] = $k.'.'.$field;
48
                }
49 View Code Duplication
            } elseif ($this->isOperator($k, array('and', 'or'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
                foreach ($v as $q) {
51
                    $test = new self($q);
52
                    $fields = array_merge($fields, $test->getFields());
53
                }
54
            } elseif ($k[0] !== '$') {
55
                $fields[] = $k;
56
            }
57
        }
58
        $fields = array_unique(array_merge($fields, array_keys($this->sort)));
59
        return $fields;
60
    }
61
62
    private function getFieldsFromElemMatch(array $elemMatch)
63
    {
64
        $fields = array();
65
        foreach ($elemMatch as $fieldName => $value) {
66
            if ($this->isOperator($fieldName, 'where')) {
67
                continue;
68
            }
69
70 View Code Duplication
            if ($this->isOperator($fieldName, array('and', 'or'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
                foreach ($value as $q) {
72
                    $test = new self($q);
73
                    $fields = array_merge($fields, $test->getFields());
74
                }
75
            } else {
76
                $fields[] = $fieldName;
77
            }
78
        }
79
        return $fields;
80
    }
81
82
    private function isOperator($fieldName, $operator)
83
    {
84
        if ( ! is_array($operator)) {
85
            $operator = array($operator);
86
        }
87
        foreach ($operator as $op) {
88
            if ($fieldName === '$' . $op) {
89
                return true;
90
            }
91
        }
92
        return false;
93
    }
94
}
95