Completed
Pull Request — master (#1624)
by Abir
08:47
created

Expr::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
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
class Expr extends BaseExpr
31
{
32
    /**
33
     * @var DocumentManager
34
     */
35
    private $dm;
36
37
    /**
38
     * @var ClassMetadata
39
     */
40
    private $class;
41
42
    /**
43
     * @inheritDoc
44
     */
45 4
    public function __construct(DocumentManager $dm, ClassMetadata $class)
46
    {
47 4
        $this->dm = $dm;
48 4
        $this->class = $class;
49 4
    }
50
51
    /**
52
     * @param string $fieldName
53
     * @return self
54
     */
55 4
    public function field($fieldName)
56
    {
57 4
        $fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName);
58
59 4
        parent::field($fieldName);
60
61 4
        return $this;
62
    }
63
64
    /**
65
     * @param mixed|self $expression
66
     * @return mixed
67
     */
68
    protected function ensureArray($expression)
69
    {
70
        // Convert field names in expressions
71
        if (is_string($expression) && substr($expression, 0, 1) === '$') {
72
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
73
        }
74
75
        // Convert PHP types to MongoDB types for everything else
76
        return Type::convertPHPToDatabaseValue(parent::ensureArray($expression));
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\MongoDB\Aggregation\Expr::ensureArray() has been deprecated with message: Deprecated in favor of convertExpression

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
77
    }
78
79
    /**
80
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
81
     */
82 4
    private function getDocumentPersister()
83
    {
84 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...
85
    }
86
}
87