Completed
Pull Request — master (#603)
by Christophe
02:27
created

Registry::getEntityManagers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle;
16
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Bridge\Doctrine\RegistryInterface;
19
use Symfony\Bridge\Doctrine\ManagerRegistry;
20
use Doctrine\ORM\ORMException;
21
use Doctrine\ORM\EntityManager;
22
23
/**
24
 * References all Doctrine connections and entity managers in a given Container.
25
 *
26
 * @author Fabien Potencier <[email protected]>
27
 */
28
class Registry extends ManagerRegistry implements RegistryInterface
29
{
30
    /**
31
     * Construct.
32
     *
33
     * @param ContainerInterface $container
34
     * @param array              $connections
35
     * @param array              $entityManagers
36
     * @param string             $defaultConnection
37
     * @param string             $defaultEntityManager
38
     */
39
    public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
40
    {
41
        $this->setContainer($container);
42
43
        parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
44
    }
45
46
    /**
47
     * Gets the default entity manager name.
48
     *
49
     * @return string The default entity manager name
50
     *
51
     * @deprecated
52
     */
53
    public function getDefaultEntityManagerName()
54
    {
55
        @trigger_error('getDefaultEntityManagerName is deprecated since Symfony 2.1. Use getDefaultManagerName 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...
56
57
        return $this->getDefaultManagerName();
58
    }
59
60
    /**
61
     * Gets a named entity manager.
62
     *
63
     * @param string $name The entity manager name (null for the default one)
0 ignored issues
show
Documentation introduced by
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...
64
     *
65
     * @return EntityManager
66
     *
67
     * @deprecated
68
     */
69
    public function getEntityManager($name = null)
70
    {
71
        @trigger_error('getEntityManager is deprecated since Symfony 2.1. Use getManager 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...
72
73
        return $this->getManager($name);
74
    }
75
76
    /**
77
     * Gets an array of all registered entity managers
78
     *
79
     * @return EntityManager[] an array of all EntityManager instances
0 ignored issues
show
Documentation introduced by
Should the return type not be \Doctrine\Common\Persistence\ObjectManager[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     *
81
     * @deprecated
82
     */
83
    public function getEntityManagers()
84
    {
85
        @trigger_error('getEntityManagers is deprecated since Symfony 2.1. Use getManagers 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...
86
87
        return $this->getManagers();
88
    }
89
90
    /**
91
     * Resets a named entity manager.
92
     *
93
     * This method is useful when an entity manager has been closed
94
     * because of a rollbacked transaction AND when you think that
95
     * it makes sense to get a new one to replace the closed one.
96
     *
97
     * Be warned that you will get a brand new entity manager as
98
     * the existing one is not usable anymore. This means that any
99
     * other object with a dependency on this entity manager will
100
     * hold an obsolete reference. You can inject the registry instead
101
     * to avoid this problem.
102
     *
103
     * @param string $name The entity manager name (null for the default one)
0 ignored issues
show
Documentation introduced by
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...
104
     *
105
     * @return EntityManager
0 ignored issues
show
Documentation introduced by
Should the return type not be EntityManager|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

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

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
177
     *
178
     * @deprecated
179
     */
180
    public function getEntityManagerForClass($class)
181
    {
182
        @trigger_error('getEntityManagerForClass is deprecated since Symfony 2.1. Use getManagerForClass 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...
183
184
        return $this->getManagerForClass($class);
185
    }
186
}
187