Completed
Push — master ( c03726...c0099d )
by Andreas
01:46 queued 10s
created

Registry.php (14 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\ORM\EntityManager;
6
use Doctrine\ORM\ORMException;
7
use Psr\Container\ContainerInterface;
8
use Symfony\Bridge\Doctrine\ManagerRegistry;
9
use Symfony\Bridge\Doctrine\RegistryInterface;
10
11
/**
12
 * References all Doctrine connections and entity managers in a given Container.
13
 */
14
class Registry extends ManagerRegistry implements RegistryInterface
15
{
16
    /**
17
     * @param string[] $connections
18
     * @param string[] $entityManagers
19
     * @param string   $defaultConnection
20
     * @param string   $defaultEntityManager
21
     */
22
    public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
23
    {
24
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Psr\Container\ContainerInterface>, but the property $container was declared to be of type object<Symfony\Component...ncyInjection\Container>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

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