Completed
Push — master ( a8fe50...bce26f )
by Maciej
13s
created

Aggregation/Stage/Bucket/BucketAutoOutput.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Bucket;
6
7
use Doctrine\ODM\MongoDB\Aggregation\Builder;
8
use Doctrine\ODM\MongoDB\Aggregation\Stage;
9
10
/**
11
 * Fluent interface for adding an output specification to a bucket stage.
12
 *
13
 */
14
class BucketAutoOutput extends AbstractOutput
15
{
16 3
    public function __construct(Builder $builder, Stage\BucketAuto $bucket)
17
    {
18 3
        parent::__construct($builder, $bucket);
19 3
    }
20
21
    /**
22
     * An expression to group documents by. To specify a field path, prefix the
23
     * field name with a dollar sign $ and enclose it in quotes.
24
     */
25
    public function groupBy($expression): Stage\BucketAuto
26
    {
27
        return $this->bucket->groupBy($expression);
28
    }
29
30
    /**
31
     * A positive 32-bit integer that specifies the number of buckets into which input documents are grouped.
32
     */
33
    public function buckets(int $buckets): Stage\BucketAuto
34
    {
35
        return $this->bucket->buckets($buckets);
0 ignored issues
show
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...
36
    }
37
38
    /**
39
     * A string that specifies the preferred number series to use to ensure that
40
     * the calculated boundary edges end on preferred round numbers or their
41
     * powers of 10.
42
     */
43
    public function granularity(string $granularity): Stage\BucketAuto
44
    {
45
        return $this->bucket->granularity($granularity);
0 ignored issues
show
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...
46
    }
47
}
48