Completed
Push — master ( 7b9f4b...1cd743 )
by Andreas
13s queued 10s
created

lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Out.php (1 issue)

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;
6
7
use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
8
use Doctrine\ODM\MongoDB\Aggregation\Builder;
9
use Doctrine\ODM\MongoDB\Aggregation\Stage;
10
use Doctrine\ODM\MongoDB\DocumentManager;
11
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
12
use Doctrine\ODM\MongoDB\Mapping\MappingException;
13
14
class Out extends Stage
15
{
16
    /** @var DocumentManager */
17
    private $dm;
18
19
    /** @var string */
20
    private $collection;
21
22 6
    public function __construct(Builder $builder, string $collection, DocumentManager $documentManager)
23
    {
24 6
        parent::__construct($builder);
25
26 6
        $this->dm = $documentManager;
27 6
        $this->out($collection);
28 5
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 5
    public function getExpression() : array
34
    {
35
        return [
36 5
            '$out' => $this->collection,
37
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 6
    public function out(string $collection) : Stage\Out
44
    {
45
        try {
46 6
            $class = $this->dm->getClassMetadata($collection);
47 4
        } catch (BaseMappingException $e) {
48 4
            $this->collection = $collection;
49 4
            return $this;
50
        }
51
52 2
        $this->fromDocument($class);
0 ignored issues
show
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
53 1
        return $this;
54
    }
55
56 2
    private function fromDocument(ClassMetadata $classMetadata) : void
57
    {
58 2
        if ($classMetadata->isSharded()) {
59 1
            throw MappingException::cannotUseShardedCollectionInOutStage($classMetadata->name);
60
        }
61
62 1
        $this->collection = $classMetadata->getCollection();
63 1
    }
64
}
65