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

BucketAutoOutput   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
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 48
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 buckets() 0 4 1
A granularity() 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\Stage;
24
25
/**
26
 * Fluent interface for adding an output specification to a bucket stage.
27
 *
28
 * @author alcaeus <[email protected]>
29
 * @since 1.5
30
 */
31
class BucketAutoOutput extends AbstractOutput
32
{
33
    /**
34
     * @param Builder $builder
35
     * @param Stage\BucketAuto $bucket
36
     */
37 3
    public function __construct(Builder $builder, Stage\BucketAuto $bucket)
38
    {
39 3
        parent::__construct($builder, $bucket);
40 3
    }
41
42
    /**
43
     * An expression to group documents by. To specify a field path, prefix the
44
     * field name with a dollar sign $ and enclose it in quotes.
45
     *
46
     * @return Stage\BucketAuto
47
     */
48
    public function groupBy($expression)
49
    {
50
        return $this->bucket->groupBy($expression);
51
    }
52
53
    /**
54
     * A positive 32-bit integer that specifies the number of buckets into which input documents are grouped.
55
     *
56
     * @param int $buckets
57
     *
58
     * @return Stage\BucketAuto
59
     */
60
    public function buckets($buckets)
61
    {
62
        return $this->bucket->buckets($buckets);
0 ignored issues
show
Bug introduced by
The method buckets() 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...
63
    }
64
65
    /**
66
     * A string that specifies the preferred number series to use to ensure that
67
     * the calculated boundary edges end on preferred round numbers or their
68
     * powers of 10.
69
     *
70
     * @param string $granularity
71
     *
72
     * @return Stage\BucketAuto
73
     */
74
    public function granularity($granularity)
75
    {
76
        return $this->bucket->granularity($granularity);
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 granularity() does only exist in the following sub-classes of Doctrine\ODM\MongoDB\Agg...on\Stage\AbstractBucket: Doctrine\ODM\MongoDB\Aggregation\Stage\BucketAuto. 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...
77
    }
78
}
79