| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Doctrine\Bundle\DoctrineBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\EventManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\DBAL\DBALException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\DBAL\Driver\AbstractMySQLDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\DBAL\DriverManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\DBAL\Exception\DriverException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Doctrine\DBAL\Platforms\AbstractPlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Doctrine\DBAL\Types\Type; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |             $driver = $connection->getDriver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($driver instanceof AbstractMySQLDriver) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $params['charset'] = 'utf8mb4'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $params['defaultTableOptions']['collate'] = $params['defaultTableOptions']['collate'] ?? 'utf8mb4_unicode_ci'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $connectionClass = \get_class($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $connection = new $connectionClass($params, $driver, $connection->getConfiguration(), $connection->getEventManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (! empty($mappingTypes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $platform = $this->getDatabasePlatform($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             foreach ($mappingTypes as $dbType => $doctrineType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $platform->registerDoctrineTypeMapping($dbType, $doctrineType); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $connection; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * Try to get the database platform. | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * This could fail if types should be registered to an predefined/unused connection | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * and the platform version is unknown. | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * For details have a look at DoctrineBundle issue #673. | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @return AbstractPlatform | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @throws DBALException | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private function getDatabasePlatform(Connection $connection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $connection->getDatabasePlatform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } catch (DriverException $driverException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             throw new DBALException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'An exception occured while establishing a connection to figure out your platform version.' . PHP_EOL . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 "You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'For further information have a look at:' . PHP_EOL . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 'https://github.com/doctrine/DoctrineBundle/issues/673', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $driverException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * initialize the types | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     private function initializeTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         foreach ($this->typesConfig as $typeName => $typeConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if (Type::hasType($typeName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 Type::overrideType($typeName, $typeConfig['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 Type::addType($typeName, $typeConfig['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->initialized = true; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.