Passed
Push — master ( 9fcc02...c2b592 )
by F. Javier
02:46
created

Utils::getEntityManager()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 33
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 46
rs 9.392
1
<?php
0 ignored issues
show
Coding Style introduced by
Trait 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 "Utils.php" doesn't match the expected filename "utils.php"
Loading history...
introduced by
An error occurred during processing; checking has been aborted. The error message was: implode(): Passing glue string after array is deprecated. Swap the parameters in /home/scrutinizer/.analysis/phpcs/vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php on line 467
Loading history...
2
3
/**
4
 * PHP version 7.4
5
 * src/Utility/Utils.php
6
 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
7
8
namespace MiW\DemoDoctrine\Utility;
9
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Doctrine\ORM\Tools\Setup;
13
14
/**
15
 * Trait Utils
16
 */
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...
17
trait Utils
18
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for trait Utils
Loading history...
19
    /**
20
     * Generate the Entity Manager
21
     *
22
     * @return EntityManagerInterface
23
     */
24
    public static function getEntityManager(): EntityManagerInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
25
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
26
        if (!isset(
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
27
            $_ENV['DATABASE_NAME'],
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
28
            $_ENV['DATABASE_USER'],
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
29
            $_ENV['DATABASE_PASSWD'],
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
30
            $_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...
31
        )) {
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 12 spaces but found 8
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
32
            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...
33
            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...
34
        }
35
36
        // 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...
37
        $dbParams = [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
38
            '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...
39
            '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...
40
            '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...
41
            '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...
42
            '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...
43
            '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...
44
            '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...
45
        ];
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...
46
47
        $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...
48
        $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...
49
        $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...
50
            [ $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...
51
            $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...
52
            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...
53
            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...
54
            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...
55
        );
56
        $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...
57
        if ($debug) {
58
            $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

58
            $config->setSQLLogger(/** @scrutinizer ignore-deprecated */ new \Doctrine\DBAL\Logging\EchoSQLLogger());
Loading history...
59
        }
60
61
        try {
62
            $entityManager = EntityManager::create($dbParams, $config);
63
        } catch (\Throwable $e) {
64
            $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage());
65
            fwrite(STDERR, $msg . PHP_EOL);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
66
            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...
67
        }
68
69
        return $entityManager;
70
    }
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...
71
72
    /**
73
     * Load the environment/configuration variables
74
     * defined in .env file + (.env.docker || .env.local)
75
     *
76
     * @param string $dir   project root directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public static function loadEnv(string $dir): void
79
    {
80
        require_once $dir . '/vendor/autoload.php';
81
82
        if (!class_exists(\Dotenv\Dotenv::class)) {
83
            fwrite(STDERR, 'ERROR: No se ha cargado la clase Dotenv' . PHP_EOL);
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
        // Load environment variables from .env file
88
        if (file_exists($dir . '/.env')) {
89
            $dotenv = \Dotenv\Dotenv::createMutable($dir, '.env');
90
            $dotenv->load();
91
        } else {
92
            fwrite(STDERR, 'ERROR: no existe el fichero .env' . PHP_EOL);
93
            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...
94
        }
95
96
        // Overload (if they exist) with .env.docker or .env.local
97
        if (isset($_SERVER['DOCKER']) && file_exists($dir . '/.env.docker')) {
98
            $dotenv = \Dotenv\Dotenv::createMutable($dir, '.env.docker');
99
            $dotenv->load();
100
        } elseif (file_exists($dir . '/.env.local')) {
101
            $dotenv = \Dotenv\Dotenv::createMutable($dir, '.env.local');
102
            $dotenv->load();
103
        }
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 loadEnv()
Loading history...
105
}
0 ignored issues
show
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