Completed
Pull Request — master (#1448)
by Andreas
11:03
created

Expr::ensureArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
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\Aggregation;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\MongoDB\Aggregation\Expr as BaseExpr;
24
use Doctrine\ODM\MongoDB\DocumentManager;
25
use Doctrine\ODM\MongoDB\Types\Type;
26
27
/**
28
 * Fluent interface for building aggregation pipelines.
29
 *
30
 * @author alcaeus <[email protected]>
31
 */
32
class Expr extends BaseExpr
33
{
34
    /**
35
     * @var DocumentManager
36
     */
37
    private $dm;
38
39
    /**
40
     * @var ClassMetadata
41
     */
42
    private $class;
43
44
    /**
45
     * @inheritDoc
46
     */
47 4
    public function __construct(DocumentManager $dm, ClassMetadata $class)
48
    {
49 4
        $this->dm = $dm;
50 4
        $this->class = $class;
51 4
    }
52
53
    /**
54
     * @param string $fieldName
55
     * @return self
56
     */
57 4
    public function field($fieldName)
58
    {
59 4
        $fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName);
60
61 4
        parent::field($fieldName);
62
63 4
        return $this;
64
    }
65
66
    /**
67
     * @param mixed|self $expression
68
     * @return mixed
69
     */
70 4
    protected function ensureArray($expression)
71
    {
72
        // Convert field names in expressions
73 4
        if (is_string($expression) && substr($expression, 0, 1) === '$') {
74 2
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
75
        }
76
77
        // Convert PHP types to MongoDB types for everything else
78 4
        return Type::convertPHPToDatabaseValue(parent::ensureArray($expression));
79
    }
80
81
    /**
82
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
83
     */
84 4
    private function getDocumentPersister()
85
    {
86 4
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
    }
88
}
89