Completed
Push — master ( 6246c1...4e6532 )
by Andreas
01:48 queued 10s
created

DoctrineBundle.php (3 issues)

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
namespace Doctrine\Bundle\DoctrineBundle;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
6
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
7
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
8
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
9
use Doctrine\Common\Util\ClassUtils;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Proxy\Autoloader;
12
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
13
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
14
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
15
use Symfony\Component\Console\Application;
16
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\HttpKernel\Bundle\Bundle;
19
20
class DoctrineBundle extends Bundle
21
{
22
    /** @var callable|null */
23
    private $autoloader;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function build(ContainerBuilder $container)
29
    {
30
        parent::build($container);
31
32
        $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
33
34
        if ($container->hasExtension('security')) {
35
            $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
0 ignored issues
show
The method addUserProviderFactory() does not seem to exist on object<Symfony\Component...ion\ExtensionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        }
37
38
        $container->addCompilerPass(new DoctrineValidationPass('orm'));
39
        $container->addCompilerPass(new EntityListenerPass());
40
        $container->addCompilerPass(new ServiceRepositoryCompilerPass());
41
        $container->addCompilerPass(new WellKnownSchemaFilterPass());
42
        $container->addCompilerPass(new DbalSchemaFilterPass());
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function boot()
49
    {
50
        // Register an autoloader for proxies to avoid issues when unserializing them
51
        // when the ORM is used.
52
        if (! $this->container->hasParameter('doctrine.orm.proxy_namespace')) {
53
            return;
54
        }
55
56
        $namespace      = $this->container->getParameter('doctrine.orm.proxy_namespace');
57
        $dir            = $this->container->getParameter('doctrine.orm.proxy_dir');
58
        $proxyGenerator = null;
59
60
        if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
61
            // See https://github.com/symfony/symfony/pull/3419 for usage of references
62
            $container = &$this->container;
63
64
            $proxyGenerator = static function ($proxyDir, $proxyNamespace, $class) use (&$container) {
65
                $originalClassName = ClassUtils::getRealClass($class);
66
                /** @var Registry $registry */
67
                $registry = $container->get('doctrine');
68
69
                // Tries to auto-generate the proxy file
70
                /** @var EntityManager $em */
71
                foreach ($registry->getManagers() as $em) {
72
                    if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConfiguration() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
73
                        continue;
74
                    }
75
76
                    $metadataFactory = $em->getMetadataFactory();
77
78
                    if ($metadataFactory->isTransient($originalClassName)) {
79
                        continue;
80
                    }
81
82
                    $classMetadata = $metadataFactory->getMetadataFor($originalClassName);
83
84
                    $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getProxyFactory() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
85
86
                    clearstatcache(true, Autoloader::resolveFile($proxyDir, $proxyNamespace, $class));
87
88
                    break;
89
                }
90
            };
91
        }
92
93
        $this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function shutdown()
100
    {
101
        if ($this->autoloader !== null) {
102
            spl_autoload_unregister($this->autoloader);
103
            $this->autoloader = null;
104
        }
105
106
        // Clear all entity managers to clear references to entities for GC
107
        if ($this->container->hasParameter('doctrine.entity_managers')) {
108
            foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
109
                if (! $this->container->initialized($id)) {
110
                    continue;
111
                }
112
113
                $this->container->get($id)->clear();
114
            }
115
        }
116
117
        // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
118
        if (! $this->container->hasParameter('doctrine.connections')) {
119
            return;
120
        }
121
122
        foreach ($this->container->getParameter('doctrine.connections') as $id) {
123
            if (! $this->container->initialized($id)) {
124
                continue;
125
            }
126
127
            $this->container->get($id)->close();
128
        }
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function registerCommands(Application $application)
135
    {
136
    }
137
}
138