Completed
Pull Request — master (#1448)
by Andreas
10:09
created

Expr   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 57
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A field() 0 8 1
A ensureArray() 0 10 3
A getDocumentPersister() 0 4 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 4
    protected function ensureArray($expression)
69
    {
70
        // Convert field names in expressions
71 4
        if (is_string($expression) && substr($expression, 0, 1) === '$') {
72 2
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
73
        }
74
75
        // Convert PHP types to MongoDB types for everything else
76 4
        return Type::convertPHPToDatabaseValue(parent::ensureArray($expression));
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