Completed
Push — master ( 80e8df...60de96 )
by Maciej
18s
created

FieldExtractor::getFields()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 6
Ratio 27.27 %

Code Coverage

Tests 15
CRAP Score 9

Importance

Changes 0
Metric Value
dl 6
loc 22
ccs 15
cts 15
cp 1
rs 6.412
c 0
b 0
f 0
cc 9
eloc 15
nc 5
nop 0
crap 9
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
 * @deprecated class was deprecated in 1.2 and will be removed in 2.0
29
 */
30
class FieldExtractor
31
{
32
    private $query;
33
    private $sort;
34
35 28
    public function __construct(array $query, array $sort = array())
36
    {
37 28
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
38 28
            sprintf('%s was deprecated in version 1.2 and will be removed altogether in 2.0.', __CLASS__),
39 28
            E_USER_DEPRECATED
40
        );
41 28
        $this->query = $query;
42 28
        $this->sort = $sort;
43 28
    }
44
45 28
    public function getFields()
46
    {
47 28
        $fields = array();
48
49 28
        foreach ($this->query as $k => $v) {
50 27
            if (is_array($v) && isset($v['$elemMatch']) && is_array($v['$elemMatch'])) {
51 5
                $elemMatchFields = $this->getFieldsFromElemMatch($v['$elemMatch']);
52 5
                foreach ($elemMatchFields as $field) {
53 5
                    $fields[] = $k.'.'.$field;
54
                }
55 25 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...
56 6
                foreach ($v as $q) {
57 6
                    $test = new self($q);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ODM\MongoDB\Query\FieldExtractor has been deprecated with message: class was deprecated in 1.2 and will be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
58 6
                    $fields = array_merge($fields, $test->getFields());
59
                }
60 25
            } elseif ($k[0] !== '$') {
61 25
                $fields[] = $k;
62
            }
63
        }
64 28
        $fields = array_unique(array_merge($fields, array_keys($this->sort)));
65 28
        return $fields;
66
    }
67
68 5
    private function getFieldsFromElemMatch(array $elemMatch)
69
    {
70 5
        $fields = array();
71 5
        foreach ($elemMatch as $fieldName => $value) {
72 5
            if ($this->isOperator($fieldName, 'where')) {
73 1
                continue;
74
            }
75
76 5 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...
77 2
                foreach ($value as $q) {
78 2
                    $test = new self($q);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ODM\MongoDB\Query\FieldExtractor has been deprecated with message: class was deprecated in 1.2 and will be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
79 2
                    $fields = array_merge($fields, $test->getFields());
80
                }
81
            } else {
82 5
                $fields[] = $fieldName;
83
            }
84
        }
85 5
        return $fields;
86
    }
87
88 27
    private function isOperator($fieldName, $operator)
89
    {
90 27
        if ( ! is_array($operator)) {
91 5
            $operator = array($operator);
92
        }
93 27
        foreach ($operator as $op) {
94 27
            if ($fieldName === '$' . $op) {
95 6
                return true;
96
            }
97
        }
98 27
        return false;
99
    }
100
}
101