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

BucketOutput   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 55
ccs 3
cts 9
cp 0.3333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A groupBy() 0 4 1
A boundaries() 0 4 1
A defaultBucket() 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\Stage\Bucket;
21
22
use Doctrine\ODM\MongoDB\Aggregation\Builder;
23
use Doctrine\ODM\MongoDB\Aggregation\Expr;
24
use Doctrine\ODM\MongoDB\Aggregation\Stage;
25
26
/**
27
 * Fluent interface for adding an output specification to a bucket stage.
28
 *
29
 * @author alcaeus <[email protected]>
30
 * @since 1.5
31
 */
32
class BucketOutput extends AbstractOutput
33
{
34
    /**
35
     * @param Builder $builder
36
     * @param Stage\Bucket $bucket
37
     */
38 3
    public function __construct(Builder $builder, Stage\Bucket $bucket)
39
    {
40 3
        parent::__construct($builder, $bucket);
41 3
    }
42
43
    /**
44
     * An expression to group documents by. To specify a field path, prefix the
45
     * field name with a dollar sign $ and enclose it in quotes.
46
     *
47
     * @param mixed|Expr $expression
48
     * @return Stage\Bucket
49
     */
50
    public function groupBy($expression)
51
    {
52
        return $this->bucket->groupBy($expression);
53
    }
54
55
    /**
56
     * An array of values based on the groupBy expression that specify the
57
     * boundaries for each bucket.
58
     *
59
     * Each adjacent pair of values acts as the inclusive lower boundary and the
60
     * exclusive upper boundary for the bucket. You must specify at least two
61
     * boundaries. The specified values must be in ascending order and all of
62
     * the same type. The exception is if the values are of mixed numeric types.
63
     *
64
     * @param array ...$boundaries
65
     *
66
     * @return Stage\Bucket
67
     */
68
    public function boundaries(...$boundaries)
69
    {
70
        return $this->bucket->boundaries(...$boundaries);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Doctrine\ODM\MongoDB\Agg...on\Stage\AbstractBucket as the method boundaries() does only exist in the following sub-classes of Doctrine\ODM\MongoDB\Agg...on\Stage\AbstractBucket: Doctrine\ODM\MongoDB\Aggregation\Stage\Bucket. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
71
    }
72
73
    /**
74
     * A literal that specifies the _id of an additional bucket that contains
75
     * all documents whose groupBy expression result does not fall into a bucket
76
     * specified by boundaries.
77
     *
78
     * @param mixed $default
79
     *
80
     * @return Stage\Bucket
81
     */
82
    public function defaultBucket($default)
83
    {
84
        return $this->bucket->defaultBucket($default);
0 ignored issues
show
Bug introduced by
The method defaultBucket() does not exist on Doctrine\ODM\MongoDB\Agg...on\Stage\AbstractBucket. Did you maybe mean bucket()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
    }
86
}
87