Passed
Branch master (e9a9a2)
by F. Javier
06:49
created

DoctrineConnector::getEntityManager()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 36
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 52
rs 9.0328

How to fix   Long Method   

Long Method

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:

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...
Coding Style introduced by
Tag value for @license tag indented incorrectly; expected 1 spaces but found 2
Loading history...
7
 * @link     http://www.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...
Coding Style introduced by
Tag value for @link tag indented incorrectly; expected 4 spaces but found 5
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\ORM\EntityManager;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Doctrine\ORM\Tools\Setup;
15
use Throwable;
16
17
/**
18
 * Class DoctrineConnector
19
 */
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...
20
final class DoctrineConnector
21
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class DoctrineConnector
Loading history...
22
    private static ?EntityManager $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
Expected 1 space after "?"; 0 found
Loading history...
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
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...
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...
23
24
    /**
25
     * Generate the Entity Manager
26
     *
27
     * @return EntityManagerInterface
28
     */
29
    public static function getEntityManager(): EntityManagerInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Expected 1 space before ":"; 0 found
Loading history...
30
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
31
        if (null !== self::$instance) {
0 ignored issues
show
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...
32
            return self::$instance;
33
        }
34
35
        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...
36
            !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...
37
                $_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...
38
                $_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...
39
                $_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...
40
                $_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...
41
            )
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
42
        ) {
43
            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...
44
            exit(1);
0 ignored issues
show
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...
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...
45
        }
46
47
        // Cargar configuración de la conexión
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
48
        $dbParams = [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
49
            '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
Array double arrow not aligned correctly; expected 5 space(s) but found 6
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
50
            '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
Array double arrow not aligned correctly; expected 5 space(s) but found 6
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
51
            '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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 3 space(s) but found 4
Loading history...
52
            '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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 5 space(s) but found 6
Loading history...
53
            '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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 2
Loading history...
54
            '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
Array double arrow not aligned correctly; expected 3 space(s) but found 4
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
55
            '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
Array double arrow not aligned correctly; expected 2 space(s) but found 3
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
56
        ];
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...
57
58
        $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...
59
        $debug = $_ENV['DEBUG'] ?? false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
60
        $config = Setup::createAnnotationMetadataConfiguration(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61
            [ $entityDir ],            // paths to mapped entities
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
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...
62
            $debug,                    // developper mode
0 ignored issues
show
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...
63
            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...
64
            null,                      // Cache implementation
0 ignored issues
show
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
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...
65
            false                      // use Simple Annotation Reader
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
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...
66
        );
67
        $config->setAutoGenerateProxyClasses(true);
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...
68
        if ($debug) {
69
            $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Logging\EchoSQLLogger has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
            $config->setSQLLogger(/** @scrutinizer ignore-deprecated */ new \Doctrine\DBAL\Logging\EchoSQLLogger());
Loading history...
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);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
77
            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...
78
        }
79
80
        return $entityManager;
81
    }
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 getEntityManager()
Loading history...
82
83
    protected function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
84
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
85
    }
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...
86
87
    private function __clone()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __clone()
Loading history...
88
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
89
    }
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...
90
91
//    private function __wakeup()
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
Coding Style introduced by
Expected 1 space before comment text but found 4; use block comment if you need indentation
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
92
//    {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
Coding Style introduced by
Expected 1 space before comment text but found 4; use block comment if you need indentation
Loading history...
93
//    }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
Coding Style introduced by
Expected 1 space before comment text but found 4; use block comment if you need indentation
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
94
}
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...
95