1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | /** |
||
4 | * src/Utility/DoctrineConnector.php |
||
0 ignored issues
–
show
|
|||
5 | * |
||
6 | * @license https://opensource.org/licenses/MIT MIT License |
||
0 ignored issues
–
show
|
|||
7 | * @link https://miw.etsisi.upm.es/ ETS de IngenierÃa de Sistemas Informáticos |
||
0 ignored issues
–
show
|
|||
8 | */ |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | namespace MiW\DemoDoctrine\Utility; |
||
11 | |||
12 | use Doctrine\DBAL\{ Connection, DriverManager }; |
||
13 | use Doctrine\ORM\{ EntityManager, EntityManagerInterface, ORMSetup }; |
||
14 | use Doctrine\ORM\Proxy\ProxyFactory; |
||
15 | use Exception; |
||
16 | // use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
||
0 ignored issues
–
show
|
|||
17 | use Throwable; |
||
18 | |||
19 | /** |
||
20 | * Class DoctrineConnector |
||
21 | */ |
||
0 ignored issues
–
show
|
|||
22 | final class DoctrineConnector |
||
23 | { |
||
0 ignored issues
–
show
|
|||
24 | private static EntityManager|null $instance = null; |
||
0 ignored issues
–
show
|
|||
25 | |||
26 | /** |
||
27 | * Generate the Entity Manager |
||
28 | * |
||
29 | * @return EntityManagerInterface |
||
30 | */ |
||
31 | public static function getEntityManager(): EntityManagerInterface |
||
0 ignored issues
–
show
|
|||
32 | { |
||
0 ignored issues
–
show
|
|||
33 | if (self::$instance instanceof EntityManager) { |
||
34 | return self::$instance; |
||
35 | } |
||
36 | |||
37 | if ( |
||
0 ignored issues
–
show
|
|||
38 | !isset( |
||
0 ignored issues
–
show
|
|||
39 | $_ENV['DATABASE_NAME'], |
||
0 ignored issues
–
show
|
|||
40 | $_ENV['DATABASE_USER'], |
||
0 ignored issues
–
show
|
|||
41 | $_ENV['DATABASE_PASSWD'], |
||
0 ignored issues
–
show
|
|||
42 | $_ENV['ENTITY_DIR'], |
||
0 ignored issues
–
show
|
|||
43 | $_ENV['SERVER_VERSION'], |
||
0 ignored issues
–
show
|
|||
44 | ) |
||
0 ignored issues
–
show
|
|||
45 | ) { |
||
46 | fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
||
0 ignored issues
–
show
|
|||
47 | exit(1); |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return Doctrine\ORM\EntityManagerInterface . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
48 | } |
||
49 | |||
50 | // Cargar configuración de la conexión. |
||
51 | $dbParams = [ |
||
0 ignored issues
–
show
|
|||
52 | 'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
||
0 ignored issues
–
show
|
|||
53 | 'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
||
0 ignored issues
–
show
|
|||
54 | 'dbname' => $_ENV['DATABASE_NAME'], |
||
0 ignored issues
–
show
|
|||
55 | 'user' => $_ENV['DATABASE_USER'], |
||
0 ignored issues
–
show
|
|||
56 | 'password' => $_ENV['DATABASE_PASSWD'], |
||
0 ignored issues
–
show
|
|||
57 | 'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
||
0 ignored issues
–
show
|
|||
58 | 'serverVersion' => $_ENV['SERVER_VERSION'], |
||
0 ignored issues
–
show
|
|||
59 | 'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
||
0 ignored issues
–
show
|
|||
60 | ]; |
||
0 ignored issues
–
show
|
|||
61 | |||
62 | $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
||
0 ignored issues
–
show
|
|||
63 | // $queryCache = new PhpFilesAdapter('doctrine_queries'); |
||
64 | // $metadataCache = new PhpFilesAdapter('doctrine_metadata'); |
||
65 | // $resultsCache = new PhpFilesAdapter('doctrine_results'); |
||
66 | $config = ORMSetup::createAttributeMetadataConfiguration( |
||
67 | paths: [ $entityDir ], // paths to mapped entities |
||
0 ignored issues
–
show
|
|||
68 | isDevMode: true, // developper mode |
||
0 ignored issues
–
show
|
|||
69 | proxyDir: (string) ini_get('sys_temp_dir') // Proxy dir |
||
0 ignored issues
–
show
|
|||
70 | ); |
||
71 | // $config->setQueryCache($queryCache); |
||
72 | // $config->setMetadataCache($metadataCache); |
||
73 | // $config->setResultCache($resultsCache); |
||
74 | $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED); |
||
75 | |||
76 | // configuring the database connection |
||
0 ignored issues
–
show
|
|||
77 | /** @var Connection $connection */ |
||
0 ignored issues
–
show
|
|||
78 | $connection = DriverManager::getConnection($dbParams, $config); |
||
79 | |||
80 | try { |
||
81 | self::$instance = new EntityManager($connection, $config); |
||
82 | } catch (Throwable $e) { |
||
83 | $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
||
84 | fwrite(STDERR, $msg . PHP_EOL); |
||
0 ignored issues
–
show
|
|||
85 | exit(1); |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return Doctrine\ORM\EntityManagerInterface . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
86 | } |
||
87 | |||
88 | return self::$instance; |
||
0 ignored issues
–
show
|
|||
89 | } |
||
0 ignored issues
–
show
|
|||
90 | |||
91 | protected function __construct() |
||
0 ignored issues
–
show
|
|||
92 | { |
||
0 ignored issues
–
show
|
|||
93 | } |
||
0 ignored issues
–
show
|
|||
94 | |||
95 | protected function __clone() |
||
0 ignored issues
–
show
|
|||
96 | { |
||
0 ignored issues
–
show
|
|||
97 | } |
||
0 ignored issues
–
show
|
|||
98 | |||
99 | /** |
||
0 ignored issues
–
show
|
|||
100 | * @throws Exception |
||
0 ignored issues
–
show
|
|||
101 | */ |
||
0 ignored issues
–
show
|
|||
102 | public function __wakeup() |
||
103 | { |
||
0 ignored issues
–
show
|
|||
104 | throw new Exception('Cannot unserialize a Singleton.'); |
||
105 | } |
||
0 ignored issues
–
show
|
|||
106 | } |
||
0 ignored issues
–
show
|
|||
107 |