DoctrineConnector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 37
c 11
b 0
f 0
dl 0
loc 83
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __wakeup() 0 3 1
A __construct() 0 2 1
A __clone() 0 2 1
A getEntityManager() 0 58 4
1
<?php
0 ignored issues
show
Coding Style introduced by
Class found in ".php" file; use ".inc" extension instead
Loading history...
Coding Style introduced by
There must be no blank lines before the file comment
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Filename "DoctrineConnector.php" doesn't match the expected filename "doctrineconnector.php"
Loading history...
2
3
/**
4
 * src/Utility/DoctrineConnector.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
5
 *
6
 * @license https://opensource.org/licenses/MIT MIT License
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
7
 * @link    https://miw.etsisi.upm.es/ ETS de Ingeniería de Sistemas Informáticos
0 ignored issues
show
Coding Style introduced by
The tag in position 2 should be the @subpackage tag
Loading history...
8
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style Documentation introduced by
Missing @package tag in file comment
Loading history...
Coding Style Documentation introduced by
Missing @subpackage tag in file comment
Loading history...
Coding Style Documentation introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
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
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
17
use Throwable;
18
19
/**
20
 * Class DoctrineConnector
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
final class DoctrineConnector
23
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class DoctrineConnector
Loading history...
24
    private static EntityManager|null $instance = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
Coding Style introduced by
Private member variable "instance" must contain a leading underscore
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
Coding Style introduced by
Private member variable "instance" must be prefixed with an underscore
Loading history...
25
26
    /**
27
     * Generate the Entity Manager
28
     *
29
     * @return EntityManagerInterface
30
     */
31
    public static function getEntityManager(): EntityManagerInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
32
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
33
        if (self::$instance instanceof EntityManager) {
34
            return self::$instance;
35
        }
36
37
        if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
38
            !isset(
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
39
                $_ENV['DATABASE_NAME'],
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
40
                $_ENV['DATABASE_USER'],
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
41
                $_ENV['DATABASE_PASSWD'],
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
42
                $_ENV['ENTITY_DIR'],
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
43
                $_ENV['SERVER_VERSION'],
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
44
            )
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
45
        ) {
46
            fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
47
            exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
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
    }
}
Loading history...
48
        }
49
50
        // Cargar configuración de la conexión.
51
        $dbParams = [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
52
            'host'          => $_ENV['DATABASE_HOST'] ?? '127.0.0.1',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
53
            'port'          => $_ENV['DATABASE_PORT'] ?? 3306,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
54
            'dbname'        => $_ENV['DATABASE_NAME'],
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
55
            'user'          => $_ENV['DATABASE_USER'],
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
56
            'password'      => $_ENV['DATABASE_PASSWD'],
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
57
            'driver'        => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
58
            'serverVersion' => $_ENV['SERVER_VERSION'],
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
59
            'charset'       => $_ENV['DATABASE_CHARSET'] ?? 'UTF8',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
60
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 20 space(s), but found 8.
Loading history...
61
62
        $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR'];
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
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
Coding Style introduced by
Comments may not appear after statements
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
68
            isDevMode: true,                  // developper mode
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of true please use TRUE.
Loading history...
Coding Style introduced by
Comments may not appear after statements
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
69
            proxyDir: (string) ini_get('sys_temp_dir') // Proxy dir
0 ignored issues
show
Coding Style introduced by
Comments may not appear after statements
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
Coding Style introduced by
A cast statement should not be followed as per the coding-style.
Loading history...
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
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
77
        /** @var Connection $connection */
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
85
            exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
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
    }
}
Loading history...
86
        }
87
88
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance returns the type null which is incompatible with the type-hinted return Doctrine\ORM\EntityManagerInterface.
Loading history...
89
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected //end getEntityManager()
Loading history...
90
91
    protected function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
92
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
94
95
    protected function __clone()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __clone()
Loading history...
96
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __clone()
Loading history...
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
100
     * @throws Exception
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function __wakeup()
103
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
104
        throw new Exception('Cannot unserialize a Singleton.');
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __wakeup()
Loading history...
106
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
107