Completed
Pull Request — master (#1654)
by Andreas
08:52
created

ReplaceRoot   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 6
dl 39
loc 39
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A convertExpression() 10 10 4
A getDocumentPersister() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
4
5
use Doctrine\MongoDB\Aggregation\Builder;
6
use \Doctrine\MongoDB\Aggregation\Stage as BaseStage;
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9
use Doctrine\ODM\MongoDB\Types\Type;
10
11 View Code Duplication
class ReplaceRoot extends BaseStage\ReplaceRoot
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
12
{
13
    /**
14
     * @var DocumentManager
15
     */
16
    private $dm;
17
18
    /**
19
     * @var ClassMetadata
20
     */
21
    private $class;
22
23 6
    public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class, $expression = null)
24
    {
25 6
        $this->dm = $documentManager;
26 6
        $this->class = $class;
27
28 6
        parent::__construct($builder, $expression);
29 6
    }
30
31 3
    protected function convertExpression($expression)
32
    {
33 3
        if (is_array($expression)) {
34
            return array_map([$this, 'convertExpression'], $expression);
35 3
        } elseif (is_string($expression) && substr($expression, 0, 1) === '$') {
36 2
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
37
        } else {
38 1
            return Type::convertPHPToDatabaseValue(parent::convertExpression($expression));
39
        }
40
    }
41
42
    /**
43
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
44
     */
45 2
    private function getDocumentPersister()
46
    {
47 2
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
48
    }
49
}
50