Completed
Push — master ( 5d896f...588e78 )
by Peter
10:12 queued 13s
created

RepositoryFactory   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
wmc 1
lcom 0
cbo 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRepository() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification\Repository;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\EntityRepository;
18
use Doctrine\ORM\Repository\RepositoryFactory as DoctrineRepositoryFactory;
19
use Happyr\DoctrineSpecification\EntitySpecificationRepository;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Happyr\DoctrineSpecifica...SpecificationRepository.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
21
/**
22
 * Factory class for creating EntitySpecificationRepository instances.
23
 *
24
 * Provides an implementation of RepositoryFactory so that the
25
 * default repository type in Doctrine can easily be replaced.
26
 */
27
class RepositoryFactory implements DoctrineRepositoryFactory
28
{
29
    /**
30
     * Gets the repository for an entity class.
31
     *
32
     * @param EntityManagerInterface $entityManager the EntityManager instance
33
     * @param string                 $entityName    the name of the entity
34
     *
35
     * @return EntityRepository
36
     */
37
    public function getRepository(EntityManagerInterface $entityManager, $entityName)
38
    {
39
        return new EntitySpecificationRepository(
40
            $entityManager,
41
            $entityManager->getClassMetadata($entityName)
0 ignored issues
show
Compatibility introduced by
$entityManager->getClassMetadata($entityName) of type object<Doctrine\Persiste...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\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...
42
        );
43
    }
44
}
45