Completed
Pull Request — master (#677)
by Sebastian
01:51
created

ConnectionFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B createConnection() 0 23 6
A getDatabasePlatform() 0 15 2
A initializeTypes() 0 14 4
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 Doctrine\Common\EventManager;
18
use Doctrine\DBAL\Configuration;
19
use Doctrine\DBAL\Connection;
20
use Doctrine\DBAL\DriverManager;
21
use Doctrine\DBAL\Exception\DriverException;
22
use Doctrine\DBAL\Types\Type;
23
24
/**
25
 * Connection
26
 */
27
class ConnectionFactory
28
{
29
    private $typesConfig = array();
30
    private $commentedTypes = array();
31
    private $initialized = false;
32
33
    /**
34
     * Construct.
35
     *
36
     * @param array $typesConfig
37
     */
38
    public function __construct(array $typesConfig)
39
    {
40
        $this->typesConfig = $typesConfig;
41
    }
42
43
    /**
44
     * Create a connection by name.
45
     *
46
     * @param array         $params
47
     * @param Configuration $config
0 ignored issues
show
Documentation introduced by
Should the type for parameter $config not be null|Configuration?

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...
48
     * @param EventManager  $eventManager
0 ignored issues
show
Documentation introduced by
Should the type for parameter $eventManager not be null|EventManager?

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
     * @param array         $mappingTypes
50
     *
51
     * @return \Doctrine\DBAL\Connection
52
     */
53
    public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
54
    {
55
        if (!$this->initialized) {
56
            $this->initializeTypes();
57
        }
58
59
        $connection = DriverManager::getConnection($params, $config, $eventManager);
60
61
        if (!empty($mappingTypes)) {
62
            $platform = $this->getDatabasePlatform($connection);
63
            foreach ($mappingTypes as $dbType => $doctrineType) {
64
                $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
65
            }
66
        }
67
        if (!empty($this->commentedTypes)) {
68
            $platform = $this->getDatabasePlatform($connection);
69
            foreach ($this->commentedTypes as $type) {
70
                $platform->markDoctrineTypeCommented(Type::getType($type));
71
            }
72
        }
73
74
        return $connection;
75
    }
76
77
    /**
78
     * Try to get the database platform.
79
     *
80
     * This could fail if types should be registered to an predefined/unused connection
81
     * and the platform version is unknown.
82
     * For details have a look at DoctrineBundle issue #673.
83
     *
84
     * @param  \Doctrine\DBAL\Connection $connection
85
     *
86
     * @return \Doctrine\DBAL\Platforms\AbstractPlatform
87
     * @throws \Doctrine\DBAL\Exception\DriverException
88
     */
89
    private function getDatabasePlatform(Connection $connection)
90
    {
91
        try {
92
            return $connection->getDatabasePlatform();
93
        } catch (DriverException $driverException) {
94
            throw new DriverException(
95
                "An exception occured while establishing a connection to figure out your platform version." . PHP_EOL .
96
                "You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
97
                "For further information have a look at:" . PHP_EOL .
98
                "https://github.com/doctrine/DoctrineBundle/issues/673",
99
                0,
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a object<Doctrine\DBAL\Driver\DriverException>.

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...
100
                $driverException
0 ignored issues
show
Unused Code introduced by
The call to DriverException::__construct() has too many arguments starting with $driverException.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
            );
102
        }
103
    }
104
105
    /**
106
     * initialize the types
107
     */
108
    private function initializeTypes()
109
    {
110
        foreach ($this->typesConfig as $type => $typeConfig) {
111
            if (Type::hasType($type)) {
112
                Type::overrideType($type, $typeConfig['class']);
113
            } else {
114
                Type::addType($type, $typeConfig['class']);
115
            }
116
            if ($typeConfig['commented']) {
117
                $this->commentedTypes[] = $type;
118
            }
119
        }
120
        $this->initialized = true;
121
    }
122
}
123