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

MongoDB/Repository/AbstractRepositoryFactory.php (1 issue)

Check for loose comparison of strings.

Best Practice Bug Major

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\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 Best Practice introduced by
The expression $metadata->customRepositoryClassName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54 13
            $repositoryClassName = $metadata->customRepositoryClassName;
55 342
        } elseif ($metadata->isFile) {
56 10
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName();
57
        } else {
58 333
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
59
        }
60
61 350
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
62
    }
63
64
    /**
65
     * Instantiates requested repository.
66
     */
67
    abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository;
68
}
69