Completed
Push — master ( 11f27c...4713a5 )
by Andreas
08:33
created

AbstractBucket   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 10.75 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 10
loc 93
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A groupBy() 0 6 1
A getExpression() 0 14 2
getExtraPipelineFields() 0 1 ?
getStageName() 0 1 ?
A convertExpression() 10 10 4
A getDocumentPersister() 0 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\ODM\MongoDB\Aggregation\Builder;
6
use Doctrine\ODM\MongoDB\Aggregation\Expr;
7
use Doctrine\ODM\MongoDB\Aggregation\Stage;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
10
use Doctrine\ODM\MongoDB\Types\Type;
11
12
/**
13
 * Abstract class with common functionality for $bucket and $bucketAuto stages
14
 *
15
 * @internal
16
 * @author alcaeus <[email protected]>
17
 * @since 1.5
18
 */
19
abstract class AbstractBucket extends Stage
20
{
21
    /**
22
     * @var DocumentManager
23
     */
24
    private $dm;
25
26
    /**
27
     * @var ClassMetadata
28
     */
29
    private $class;
30
31
    /**
32
     * @var Bucket\BucketOutput|null
33
     */
34
    protected $output;
35
36
    /**
37
     * @var Expr
38
     */
39
    protected $groupBy;
40
41 8
    public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class)
42
    {
43 8
        $this->dm = $documentManager;
44 8
        $this->class = $class;
45
46 8
        parent::__construct($builder);
47 8
    }
48
49
    /**
50
     * An expression to group documents by. To specify a field path, prefix the
51
     * field name with a dollar sign $ and enclose it in quotes.
52
     *
53
     * @param array|Expr $expression
54
     * @return $this
55
     */
56 8
    public function groupBy($expression)
57
    {
58 8
        $this->groupBy = $expression;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expression can also be of type array. However, the property $groupBy is declared as type object<Doctrine\ODM\MongoDB\Aggregation\Expr>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
60 8
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 8
    public function getExpression()
67
    {
68
        $stage = [
69 8
            $this->getStageName() => [
70 8
                'groupBy' => $this->convertExpression($this->groupBy),
71 8
            ] + $this->getExtraPipelineFields(),
72
        ];
73
74 8
        if ($this->output !== null) {
75 6
            $stage[$this->getStageName()]['output'] = $this->output->getExpression();
76
        }
77
78 8
        return $stage;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    abstract protected function getExtraPipelineFields();
85
86
    /**
87
     * Returns the stage name with the dollar prefix
88
     *
89
     * @return string
90
     */
91
    abstract protected function getStageName();
92
93 8 View Code Duplication
    private function convertExpression($expression)
0 ignored issues
show
Duplication introduced by
This method 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...
94
    {
95 8
        if (is_array($expression)) {
96
            return array_map([$this, 'convertExpression'], $expression);
97 8
        } elseif (is_string($expression) && substr($expression, 0, 1) === '$') {
98 8
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
99
        } else {
100
            return Type::convertPHPToDatabaseValue(Expr::convertExpression($expression));
101
        }
102
    }
103
104
    /**
105
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
106
     */
107 8
    private function getDocumentPersister()
108
    {
109 8
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
110
    }
111
}
112