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

AbstractRepositoryFactory::instantiateRepository()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
10
use function ltrim;
11
use function spl_object_hash;
12
13
/**
14
 * Abstract factory for creating document repositories.
15
 */
16
abstract class AbstractRepositoryFactory implements RepositoryFactory
17
{
18
    /**
19
     * The list of DocumentRepository instances.
20
     *
21
     * @var ObjectRepository[]
22
     */
23
    private $repositoryList = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 350
    public function getRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
29
    {
30 350
        $metadata = $documentManager->getClassMetadata($documentName);
31 350
        $hashKey  = $metadata->getName() . spl_object_hash($documentManager);
32
33 350
        if (isset($this->repositoryList[$hashKey])) {
34 167
            return $this->repositoryList[$hashKey];
35
        }
36
37 350
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
38
39 350
        $this->repositoryList[$hashKey] = $repository;
40
41 350
        return $repository;
42
    }
43
44
    /**
45
     * Create a new repository instance for a document class.
46
     *
47
     * @return ObjectRepository|GridFSRepository
48
     */
49 350
    protected function createRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
50
    {
51 350
        $metadata = $documentManager->getClassMetadata($documentName);
52
53 350
        if ($metadata->customRepositoryClassName) {
0 ignored issues
show
Bug introduced by
Accessing customRepositoryClassName on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54 13
            $repositoryClassName = $metadata->customRepositoryClassName;
0 ignored issues
show
Bug introduced by
Accessing customRepositoryClassName on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
55 342
        } elseif ($metadata->isFile) {
0 ignored issues
show
Bug introduced by
Accessing isFile on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56 10
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName();
57
        } else {
58 333
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
59
        }
60
61 350
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
0 ignored issues
show
Compatibility introduced by
$metadata 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...
62
    }
63
64
    /**
65
     * Instantiates requested repository.
66
     */
67
    abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository;
68
}
69