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

lib/Doctrine/ODM/MongoDB/Query/FieldExtractor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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'))) {
56 6
                foreach ($v as $q) {
57 6
                    $test = new self($q);
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'))) {
77 2
                foreach ($value as $q) {
78 2
                    $test = new self($q);
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