1 | <?php |
||
15 | class ConnectionFactory |
||
16 | { |
||
17 | /** @var mixed[][] */ |
||
18 | private $typesConfig = []; |
||
19 | |||
20 | /** @var bool */ |
||
21 | private $initialized = false; |
||
22 | |||
23 | /** |
||
24 | * @param mixed[][] $typesConfig |
||
25 | */ |
||
26 | public function __construct(array $typesConfig) |
||
27 | { |
||
28 | $this->typesConfig = $typesConfig; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Create a connection by name. |
||
33 | * |
||
34 | * @param mixed[] $params |
||
35 | * @param string[]|Type[] $mappingTypes |
||
36 | * |
||
37 | * @return Connection |
||
38 | */ |
||
39 | public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = []) |
||
40 | { |
||
41 | if (! $this->initialized) { |
||
42 | $this->initializeTypes(); |
||
43 | } |
||
44 | |||
45 | $connection = DriverManager::getConnection($params, $config, $eventManager); |
||
46 | |||
47 | if (!isset($params['pdo']) && !isset($params['charset'])) { |
||
48 | $params = $connection->getParams(); |
||
49 | $params['charset'] = 'utf8'; |
||
50 | |||
51 | if ($connection instanceof AbstractMySQLDriver) { |
||
52 | $params['charset'] = 'utf8mb4'; |
||
53 | $params['defaultTableOptions']['collate'] = $params['defaultTableOptions']['collate'] ?? 'utf8mb4_unicode_ci'; |
||
54 | } |
||
55 | |||
56 | $connectionClass = \get_class($connection); |
||
57 | $connection = new $connectionClass($params, $connection->getDriver(), $connection->getConfiguration(), $connection->getEventManager()); |
||
58 | } |
||
59 | |||
60 | if (! empty($mappingTypes)) { |
||
61 | $platform = $this->getDatabasePlatform($connection); |
||
62 | foreach ($mappingTypes as $dbType => $doctrineType) { |
||
63 | $platform->registerDoctrineTypeMapping($dbType, $doctrineType); |
||
|
|||
64 | } |
||
65 | } |
||
66 | |||
67 | return $connection; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Try to get the database platform. |
||
72 | * |
||
73 | * This could fail if types should be registered to an predefined/unused connection |
||
74 | * and the platform version is unknown. |
||
75 | * For details have a look at DoctrineBundle issue #673. |
||
76 | * |
||
77 | * @return AbstractPlatform |
||
78 | * |
||
79 | * @throws DBALException |
||
80 | */ |
||
81 | private function getDatabasePlatform(Connection $connection) |
||
96 | |||
97 | /** |
||
98 | * initialize the types |
||
99 | */ |
||
100 | private function initializeTypes() |
||
112 | } |
||
113 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.