Completed
Push — master ( 3597c2...ab3438 )
by Andreas
01:57 queued 10s
created

Registry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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 Psr\Container\ContainerInterface;
10
use Symfony\Bridge\Doctrine\ManagerRegistry;
11
use Symfony\Bridge\Doctrine\RegistryInterface;
12
13
/**
14
 * References all Doctrine connections and entity managers in a given Container.
15
 */
16
class Registry extends ManagerRegistry implements RegistryInterface
17
{
18
    /**
19
     * @param Connection[]             $connections
20
     * @param EntityManagerInterface[] $entityManagers
21
     * @param string                   $defaultConnection
22
     * @param string                   $defaultEntityManager
23
     */
24
    public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
25
    {
26
        $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...
27
28
        parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
0 ignored issues
show
Documentation introduced by
$connections is of type array<integer,object<Doctrine\DBAL\Connection>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$entityManagers is of type array<integer,object<Doc...ntityManagerInterface>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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