Issues (169)

src/Utility/DoctrineConnector.php (103 issues)

1
<?php
0 ignored issues
show
Class found in ".php" file; use ".inc" extension instead
Loading history...
There must be no blank lines before the file comment
Loading history...
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Filename "DoctrineConnector.php" doesn't match the expected filename "doctrineconnector.php"
Loading history...
2
3
/**
4
 * src/Utility/DoctrineConnector.php
0 ignored issues
show
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
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
The tag in position 2 should be the @subpackage tag
Loading history...
8
 */
0 ignored issues
show
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...
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...
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
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @author tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
22
final class DoctrineConnector
23
{
0 ignored issues
show
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
Expected 1 blank line(s) before first member var; 0 found
Loading history...
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
Private member variable "instance" must contain a leading underscore
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
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
Expected 2 blank lines before function; 1 found
Loading history...
32
    {
0 ignored issues
show
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
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
38
            !isset(
0 ignored issues
show
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Expected 1 space(s) after NOT operator; 0 found
Loading history...
39
                $_ENV['DATABASE_NAME'],
0 ignored issues
show
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
40
                $_ENV['DATABASE_USER'],
0 ignored issues
show
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
41
                $_ENV['DATABASE_PASSWD'],
0 ignored issues
show
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
42
                $_ENV['ENTITY_DIR'],
0 ignored issues
show
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
43
                $_ENV['SERVER_VERSION'],
0 ignored issues
show
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Multi-line IF statement not indented correctly; expected 12 spaces but found 16
Loading history...
44
            )
0 ignored issues
show
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
Concat operator must not be surrounded by spaces
Loading history...
47
            exit(1);
0 ignored issues
show
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
Short array syntax is not allowed
Loading history...
52
            'host'          => $_ENV['DATABASE_HOST'] ?? '127.0.0.1',
0 ignored issues
show
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Operation must be bracketed
Loading history...
53
            'port'          => $_ENV['DATABASE_PORT'] ?? 3306,
0 ignored issues
show
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Operation must be bracketed
Loading history...
54
            'dbname'        => $_ENV['DATABASE_NAME'],
0 ignored issues
show
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
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
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
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Operation must be bracketed
Loading history...
58
            'serverVersion' => $_ENV['SERVER_VERSION'],
0 ignored issues
show
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
This array key does not seem to be aligned correctly; expected 21 spaces, but found 12.
Loading history...
Operation must be bracketed
Loading history...
60
        ];
0 ignored issues
show
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
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
Comments may not appear after statements
Loading history...
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
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
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of true please use TRUE.
Loading history...
Comments may not appear after statements
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
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
Comments may not appear after statements
Loading history...
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
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...
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
77
        /** @var Connection $connection */
0 ignored issues
show
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Block comments must be started with /*
Loading history...
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
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
Concat operator must not be surrounded by spaces
Loading history...
85
            exit(1);
0 ignored issues
show
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
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
Expected //end getEntityManager()
Loading history...
90
91
    protected function __construct()
0 ignored issues
show
Missing doc comment for function __construct()
Loading history...
92
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
93
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end __construct()
Loading history...
94
95
    protected function __clone()
0 ignored issues
show
Missing doc comment for function __clone()
Loading history...
96
    {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
97
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end __clone()
Loading history...
98
99
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
100
     * @throws Exception
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
101
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
102
    public function __wakeup()
103
    {
0 ignored issues
show
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
Expected 2 blank lines after function; 0 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected //end __wakeup()
Loading history...
106
}
0 ignored issues
show
Expected //end class
Loading history...
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