Passed
Push — master ( b992df...2b2d28 )
by F. Javier
12:32
created

DoctrineConnector::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Common\Proxy\AbstractProxyFactory;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Doctrine\ORM\ORMSetup;
16
use Exception;
17
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
18
use Throwable;
19
20
/**
21
 * Class DoctrineConnector
22
 */
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...
23
final class DoctrineConnector
24
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class DoctrineConnector
Loading history...
25
    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...
26
27
    /**
28
     * Generate the Entity Manager
29
     *
30
     * @return EntityManagerInterface|null
31
     */
32
    public static function getEntityManager(): ?EntityManagerInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
33
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
34
        if (self::$instance instanceof EntityManager) {
35
            return self::$instance;
36
        }
37
38
        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...
39
            !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...
40
                $_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...
41
                $_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...
42
                $_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...
43
                $_ENV['ENTITY_DIR']
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...
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
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...
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
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...
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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 3 space(s) but found 4
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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 5 space(s) but found 6
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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 2
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...
Coding Style introduced by
Array double arrow not aligned correctly; expected 3 space(s) but found 4
Loading history...
58
            '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...
59
        ];
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...
60
61
        $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...
62
        // $debug = $_ENV['DEBUG'] ?? false;
63
        $queryCache = new PhpFilesAdapter('doctrine_queries');
64
        // $metadataCache = new PhpFilesAdapter('doctrine_metadata');
65
        $resultsCache = new PhpFilesAdapter('doctrine_results');
66
        $config = ORMSetup::createAttributeMetadataConfiguration(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
67
            [ $entityDir ],            // paths to mapped entities
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...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
68
            true,                      // developper mode
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
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of true please use TRUE.
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
69
            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...
70
        );
71
        $config->setQueryCache($queryCache);
72
        // $config->setMetadataCache($metadataCache);
73
        $config->setResultCache($resultsCache);
74
        $config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED);
75
        // if ($debug) {
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
76
        //     $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
0 ignored issues
show
Coding Style introduced by
Expected 1 space before comment text but found 5; use block comment if you need indentation
Loading history...
77
        // }
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
Coding Style introduced by
There should be no blank line after an inline comment.
Loading history...
78
79
        try {
80
            $entityManager = EntityManager::create($dbParams, $config);
81
        } catch (Throwable $e) {
82
            $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage());
83
            fwrite(STDERR, $msg . PHP_EOL);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
84
            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...
85
        }
86
87
        return $entityManager;
88
    }
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...
89
90
    protected function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
91
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
92
    }
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...
93
94
    protected function __clone()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __clone()
Loading history...
95
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
96
    }
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...
97
98
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
     * @throws Exception
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
100
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
101
    public function __wakeup()
102
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
103
        throw new Exception("Cannot unserialize a Singleton.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cannot unserialize a Singleton. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
104
    }
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...
105
}
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...
106