Completed
Pull Request — master (#750)
by Mike
11:44
created

Registry::getDefaultEntityManagerName()   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
namespace Doctrine\Bundle\DoctrineBundle;
4
5
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Bridge\Doctrine\RegistryInterface;
8
use Symfony\Bridge\Doctrine\ManagerRegistry;
9
use Doctrine\ORM\ORMException;
10
use Doctrine\ORM\EntityManager;
11
12
/**
13
 * References all Doctrine connections and entity managers in a given Container.
14
 *
15
 * @author Fabien Potencier <[email protected]>
16
 */
17
class Registry extends ManagerRegistry implements RegistryInterface
18
{
19
    /**
20
     * Construct.
21
     *
22
     * @param ContainerInterface $container
23
     * @param array              $connections
24
     * @param array              $entityManagers
25
     * @param string             $defaultConnection
26
     * @param string             $defaultEntityManager
27
     */
28
    public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
29
    {
30
        $parentTraits = class_uses(parent::class);
31
        if (isset($parentTraits[ContainerAwareTrait::class])) {
32
            // this case should be removed when Symfony 3.4 becomes the lowest supported version
33
            // and then also, the constructor should type-hint Psr\Container\ContainerInterface
34
            $this->setContainer($container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not seem to exist on object<Doctrine\Bundle\DoctrineBundle\Registry>.

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...
35
        } else {
36
            $this->container = $container;
37
        }
38
39
        parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
40
    }
41
42
    /**
43
     * Gets the default entity manager name.
44
     *
45
     * @return string The default entity manager name
46
     *
47
     * @deprecated
48
     */
49
    public function getDefaultEntityManagerName()
50
    {
51
        @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...
52
53
        return $this->getDefaultManagerName();
54
    }
55
56
    /**
57
     * Gets a named entity manager.
58
     *
59
     * @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...
60
     *
61
     * @return EntityManager
62
     *
63
     * @deprecated
64
     */
65
    public function getEntityManager($name = null)
66
    {
67
        @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...
68
69
        return $this->getManager($name);
70
    }
71
72
    /**
73
     * Gets an array of all registered entity managers
74
     *
75
     * @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...
76
     *
77
     * @deprecated
78
     */
79
    public function getEntityManagers()
80
    {
81
        @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...
82
83
        return $this->getManagers();
84
    }
85
86
    /**
87
     * Resets a named entity manager.
88
     *
89
     * This method is useful when an entity manager has been closed
90
     * because of a rollbacked transaction AND when you think that
91
     * it makes sense to get a new one to replace the closed one.
92
     *
93
     * Be warned that you will get a brand new entity manager as
94
     * the existing one is not usable anymore. This means that any
95
     * other object with a dependency on this entity manager will
96
     * hold an obsolete reference. You can inject the registry instead
97
     * to avoid this problem.
98
     *
99
     * @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...
100
     *
101
     * @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...
102
     *
103
     * @deprecated
104
     */
105
    public function resetEntityManager($name = null)
106
    {
107
        @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...
108
109
        $this->resetManager($name);
110
    }
111
112
    /**
113
     * Resolves a registered namespace alias to the full namespace.
114
     *
115
     * This method looks for the alias in all registered entity managers.
116
     *
117
     * @param string $alias The alias
118
     *
119
     * @return string The full namespace
120
     *
121
     * @deprecated
122
     */
123
    public function getEntityNamespace($alias)
124
    {
125
        @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...
126
127
        return $this->getAliasNamespace($alias);
128
    }
129
130
    /**
131
     * Resolves a registered namespace alias to the full namespace.
132
     *
133
     * This method looks for the alias in all registered entity managers.
134
     *
135
     * @param string $alias The alias
136
     *
137
     * @return string The full namespace
138
     *
139
     * @see Configuration::getEntityNamespace
140
     */
141
    public function getAliasNamespace($alias)
142
    {
143
        foreach (array_keys($this->getManagers()) as $name) {
144
            try {
145
                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...
146
            } catch (ORMException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
147
            }
148
        }
149
150
        throw ORMException::unknownEntityNamespace($alias);
151
    }
152
153
    /**
154
     * Gets all connection names.
155
     *
156
     * @return array An array of connection names
157
     *
158
     * @deprecated
159
     */
160
    public function getEntityManagerNames()
161
    {
162
        @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...
163
164
        return $this->getManagerNames();
165
    }
166
167
    /**
168
     * Gets the entity manager associated with a given class.
169
     *
170
     * @param string $class A Doctrine Entity class name
171
     *
172
     * @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...
173
     *
174
     * @deprecated
175
     */
176
    public function getEntityManagerForClass($class)
177
    {
178
        @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...
179
180
        return $this->getManagerForClass($class);
181
    }
182
}
183