| Conditions | 5 |
| Paths | 6 |
| Total Lines | 52 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public static function getEntityManager(): EntityManagerInterface |
||
| 30 | { |
||
| 31 | if (null !== self::$instance) { |
||
| 32 | return self::$instance; |
||
| 33 | } |
||
| 34 | |||
| 35 | if ( |
||
| 36 | !isset( |
||
| 37 | $_ENV['DATABASE_NAME'], |
||
| 38 | $_ENV['DATABASE_USER'], |
||
| 39 | $_ENV['DATABASE_PASSWD'], |
||
| 40 | $_ENV['ENTITY_DIR'] |
||
| 41 | ) |
||
| 42 | ) { |
||
| 43 | fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
||
| 44 | exit(1); |
||
| 45 | } |
||
| 46 | |||
| 47 | // Cargar configuración de la conexión |
||
| 48 | $dbParams = [ |
||
| 49 | 'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
||
| 50 | 'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
||
| 51 | 'dbname' => $_ENV['DATABASE_NAME'], |
||
| 52 | 'user' => $_ENV['DATABASE_USER'], |
||
| 53 | 'password' => $_ENV['DATABASE_PASSWD'], |
||
| 54 | 'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
||
| 55 | 'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
||
| 59 | $debug = $_ENV['DEBUG'] ?? false; |
||
| 60 | $config = Setup::createAnnotationMetadataConfiguration( |
||
| 61 | [ $entityDir ], // paths to mapped entities |
||
| 62 | $debug, // developper mode |
||
| 63 | ini_get('sys_temp_dir'), // Proxy dir |
||
| 64 | null, // Cache implementation |
||
| 65 | false // use Simple Annotation Reader |
||
| 66 | ); |
||
| 67 | $config->setAutoGenerateProxyClasses(true); |
||
| 68 | if ($debug) { |
||
| 69 | $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); |
||
| 70 | } |
||
| 71 | |||
| 72 | try { |
||
| 73 | $entityManager = EntityManager::create($dbParams, $config); |
||
| 74 | } catch (Throwable $e) { |
||
| 75 | $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
||
| 76 | fwrite(STDERR, $msg . PHP_EOL); |
||
| 77 | exit(1); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $entityManager; |
||
| 81 | } |
||
| 95 |