Completed
Push — master ( 3ed47e...11539a )
by Marco
17s
created

Registry.php (4 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\DBAL\Connection;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\ORMException;
9
use Symfony\Bridge\Doctrine\ManagerRegistry;
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * References all Doctrine connections and entity managers in a given Container.
16
 */
17
class Registry extends ManagerRegistry implements RegistryInterface
18
{
19
    /**
20
     * Construct.
21
     *
22
     * @param Connection[]             $connections
23
     * @param EntityManagerInterface[] $entityManagers
24
     * @param string                   $defaultConnection
25
     * @param string                   $defaultEntityManager
26
     */
27
    public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
28
    {
29
        $parentTraits = class_uses(parent::class);
30
        if (isset($parentTraits[ContainerAwareTrait::class])) {
31
            // this case should be removed when Symfony 3.4 becomes the lowest supported version
32
            // and then also, the constructor should type-hint Psr\Container\ContainerInterface
33
            $this->setContainer($container);
34
        } else {
35
            $this->container = $container;
36
        }
37
38
        parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
39
    }
40
41
    /**
42
     * Gets the default entity manager name.
43
     *
44
     * @return string The default entity manager name
45
     *
46
     * @deprecated
47
     */
48
    public function getDefaultEntityManagerName()
49
    {
50
        @trigger_error('getDefaultEntityManagerName is deprecated since Symfony 2.1. Use getDefaultManagerName instead', E_USER_DEPRECATED);
51
52
        return $this->getDefaultManagerName();
53
    }
54
55
    /**
56
     * Gets a named entity manager.
57
     *
58
     * @param string $name The entity manager name (null for the default one)
0 ignored issues
show
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
59
     *
60
     * @return EntityManager
61
     *
62
     * @deprecated
63
     */
64
    public function getEntityManager($name = null)
65
    {
66
        @trigger_error('getEntityManager is deprecated since Symfony 2.1. Use getManager instead', E_USER_DEPRECATED);
67
68
        return $this->getManager($name);
69
    }
70
71
    /**
72
     * Gets an array of all registered entity managers
73
     *
74
     * @return EntityManager[] an array of all EntityManager instances
75
     *
76
     * @deprecated
77
     */
78
    public function getEntityManagers()
79
    {
80
        @trigger_error('getEntityManagers is deprecated since Symfony 2.1. Use getManagers instead', E_USER_DEPRECATED);
81
82
        return $this->getManagers();
83
    }
84
85
    /**
86
     * Resets a named entity manager.
87
     *
88
     * This method is useful when an entity manager has been closed
89
     * because of a rollbacked transaction AND when you think that
90
     * it makes sense to get a new one to replace the closed one.
91
     *
92
     * Be warned that you will get a brand new entity manager as
93
     * the existing one is not usable anymore. This means that any
94
     * other object with a dependency on this entity manager will
95
     * hold an obsolete reference. You can inject the registry instead
96
     * to avoid this problem.
97
     *
98
     * @param string $name The entity manager name (null for the default one)
99
     *
100
     *
101
     * @deprecated
102
     */
103
    public function resetEntityManager($name = null)
104
    {
105
        @trigger_error('resetEntityManager is deprecated since Symfony 2.1. Use resetManager instead', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
106
107
        $this->resetManager($name);
108
    }
109
110
    /**
111
     * Resolves a registered namespace alias to the full namespace.
112
     *
113
     * This method looks for the alias in all registered entity managers.
114
     *
115
     * @param string $alias The alias
116
     *
117
     * @return string The full namespace
118
     *
119
     * @deprecated
120
     */
121
    public function getEntityNamespace($alias)
122
    {
123
        @trigger_error('getEntityNamespace is deprecated since Symfony 2.1. Use getAliasNamespace instead', E_USER_DEPRECATED);
124
125
        return $this->getAliasNamespace($alias);
126
    }
127
128
    /**
129
     * Resolves a registered namespace alias to the full namespace.
130
     *
131
     * This method looks for the alias in all registered entity managers.
132
     *
133
     * @param string $alias The alias
134
     *
135
     * @return string The full namespace
136
     *
137
     * @see Configuration::getEntityNamespace
138
     */
139
    public function getAliasNamespace($alias)
140
    {
141
        foreach (array_keys($this->getManagers()) as $name) {
142
            try {
143
                return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
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...
144
            } catch (ORMException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
145
            }
146
        }
147
148
        throw ORMException::unknownEntityNamespace($alias);
149
    }
150
151
    /**
152
     * Gets all connection names.
153
     *
154
     * @return string[] An array of connection names
155
     *
156
     * @deprecated
157
     */
158
    public function getEntityManagerNames()
159
    {
160
        @trigger_error('getEntityManagerNames is deprecated since Symfony 2.1. Use getManagerNames instead', E_USER_DEPRECATED);
161
162
        return $this->getManagerNames();
163
    }
164
165
    /**
166
     * Gets the entity manager associated with a given class.
167
     *
168
     * @param string $class A Doctrine Entity class name
169
     *
170
     * @return EntityManager|null
171
     *
172
     * @deprecated
173
     */
174
    public function getEntityManagerForClass($class)
175
    {
176
        @trigger_error('getEntityManagerForClass is deprecated since Symfony 2.1. Use getManagerForClass instead', E_USER_DEPRECATED);
177
178
        return $this->getManagerForClass($class);
179
    }
180
}
181