Completed
Push — master ( 8fc9bf...b852d4 )
by Andreas
17s queued 12s
created

AbstractRepositoryFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 9
loc 78
c 0
b 0
f 0
ccs 27
cts 30
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepository() 0 15 2
B createRepository() 9 39 10
instantiateRepository() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9
use Doctrine\ODM\MongoDB\Mapping\MappingException;
10
use Doctrine\ODM\MongoDB\MongoDBException;
11
use Doctrine\Persistence\ObjectRepository;
12
use function is_a;
13
use function ltrim;
14
use function spl_object_hash;
15
16
/**
17
 * Abstract factory for creating document repositories.
18
 */
19
abstract class AbstractRepositoryFactory implements RepositoryFactory
20
{
21
    /**
22
     * The list of DocumentRepository instances.
23
     *
24
     * @var ObjectRepository[]
25
     */
26
    private $repositoryList = [];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 371
    public function getRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
32
    {
33 371
        $metadata = $documentManager->getClassMetadata($documentName);
34 371
        $hashKey  = $metadata->getName() . spl_object_hash($documentManager);
0 ignored issues
show
Bug introduced by
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
35
36 371
        if (isset($this->repositoryList[$hashKey])) {
37 170
            return $this->repositoryList[$hashKey];
38
        }
39
40 371
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
41
42 367
        $this->repositoryList[$hashKey] = $repository;
43
44 367
        return $repository;
45
    }
46
47
    /**
48
     * Create a new repository instance for a document class.
49
     *
50
     * @return ObjectRepository|GridFSRepository|ViewRepository
51
     */
52 371
    protected function createRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
53
    {
54 371
        $metadata = $documentManager->getClassMetadata($documentName);
55
56 371
        $repositoryClassName = $metadata->isFile
57 12
            ? $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName()
58 371
            : $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
59
60 371
        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...
61 26
            $repositoryClassName = $metadata->customRepositoryClassName;
62
        }
63
64
        switch (true) {
65 371
            case $metadata->isFile:
66 12 View Code Duplication
                if (! is_a($repositoryClassName, GridFSRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, GridFSRepository::class);
68
                }
69 12
                break;
70
71 360
            case $metadata->isView():
72 15 View Code Duplication
                if (! is_a($repositoryClassName, ViewRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73 4
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, ViewRepository::class);
74
                }
75 11
                break;
76
77 346
            case $metadata->isEmbeddedDocument:
78
                throw MongoDBException::cannotCreateRepository($documentName);
79
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
81 346
            case $metadata->isMappedSuperclass:
82
            default:
83 346 View Code Duplication
                if (! is_a($repositoryClassName, DocumentRepository::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                    throw MappingException::invalidRepositoryClass($documentName, $repositoryClassName, DocumentRepository::class);
85
                }
86 346
                break;
87
        }
88
89 367
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
90
    }
91
92
    /**
93
     * Instantiates requested repository.
94
     */
95
    abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository;
96
}
97