Completed
Pull Request — master (#858)
by
unknown
02:28
created

FallbackServiceRepositoryCompilePass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class FallbackServiceRepositoryCompilePass implements CompilerPassInterface
12
{
13
	/**
14
	 * @param ContainerBuilder $container
15
	 *
16
	 * @throws \Exception
17
	 */
18
	public function process(ContainerBuilder $container)
19
	{
20
		foreach (array_keys($container->getDefinitions()) as $id) {
21
			if (!preg_match('/^doctrine\.orm\.[^.]+_entity_manager$/', $id)) {
22
				continue;
23
			}
24
25
			$this->processMetadata(
26
				$container,
27
				$id,
28
				$container->get($id)->getMetadataFactory()
29
			);
30
		}
31
	}
32
33
	/**
34
	 * @param ContainerBuilder     $container
35
	 * @param string               $entityManagerServiceId
36
	 * @param ClassMetadataFactory $factory
37
	 */
38
	protected function processMetadata(
39
		ContainerBuilder $container,
40
        $entityManagerServiceId,
41
		ClassMetadataFactory $factory
42
	) {
43
		foreach ($factory->getAllMetadata() as $metadata) {
44
			$repositoryClassName = null;
45
			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...
46
				$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...
47
			}
48
49
			if ($repositoryClassName === null) {
50
				continue;
51
			}
52
53
			if (!$container->has($repositoryClassName)) {
54
				$repositoryDefinition = new Definition(
55
					$repositoryClassName,
56
					[
57
						$metadata->getName()
58
					]
59
				);
60
				$repositoryDefinition->setFactory([
61
					new Reference($entityManagerServiceId),
62
					'getRepository'
63
				]);
64
                $repositoryDefinition->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG);
65
				$container->setDefinition(
66
					$repositoryClassName,
67
					$repositoryDefinition
68
				);
69
			}
70
		}
71
	}
72
}
73