Utils::updateSchema()   A
last analyzed

Complexity

Conditions 2
Paths 7

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 2
b 0
f 0
nc 7
nop 0
dl 0
loc 16
rs 9.8666
1
<?php
0 ignored issues
show
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...
Coding Style introduced by
Class found in ".php" file; use ".inc" extension instead
Loading history...
2
3
/**
4
 * src/Utility/Utils.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\ORM\{ EntityManager, Tools\SchemaTool };
13
use Dotenv\Dotenv;
14
use Throwable;
15
16
/**
17
 * Class Utils
18
 */
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...
19
class Utils
20
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Utils
Loading history...
21
    /**
22
     * Load the environment/configuration variables
23
     * defined in .env file + (.env.docker || .env.local)
24
     *
25
     * @param string $dir   project root directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
26
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
27
    public static function loadEnv(string $dir): void
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
28
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
29
        require_once $dir . '/vendor/autoload.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
30
31
        if (!class_exists(Dotenv::class)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
32
            fwrite(STDERR, 'ERROR: No se ha cargado la clase Dotenv' . 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
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
        try {
37
            // Load environment variables from .env file
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
38
            if (file_exists($dir . '/.env')) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
39
                $dotenv = Dotenv::createMutable($dir, '.env');
40
                $dotenv->load();
41
            } else {
42
                fwrite(STDERR, 'ERROR: no existe el fichero .env' . PHP_EOL);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
43
                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...
44
            }
45
46
            // Overload (if they exist) with .env.docker or .env.local
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
47
            if (isset($_SERVER['DOCKER']) && file_exists($dir . '/.env.docker')) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
48
                $dotenv = Dotenv::createMutable($dir, '.env.docker');
49
                $dotenv->load();
50
            } elseif (file_exists($dir . '/.env.local')) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
51
                $dotenv = Dotenv::createMutable($dir, '.env.local');
52
                $dotenv->load();
53
            }
54
55
            // Requiring Variables to be set
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
56
            $dotenv->required([ 'DATABASE_NAME', 'DATABASE_USER', 'DATABASE_PASSWD', 'SERVER_VERSION' ]);
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
57
            $dotenv->required([ 'ENTITY_DIR' ]);
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
58
        } catch (Throwable $e) {
59
            fwrite(
60
                STDERR,
61
                'EXCEPCIÓN: ' . $e->getCode() . ' - ' . $e->getMessage()
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
62
            );
63
            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...
64
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end try"
Loading history...
65
    }
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 //end loadEnv()
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
66
67
    /**
68
     * Drop & Update database schema
69
     *
70
     * @return void
71
     */
72
    public static function updateSchema(): void
73
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
74
        try {
75
            /** @var EntityManager $e_manager */
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...
76
            $e_manager = DoctrineConnector::getEntityManager();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
77
            $e_manager->clear();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
78
            $metadata = $e_manager->getMetadataFactory()->getAllMetadata();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
79
            $sch_tool = new SchemaTool($e_manager);
0 ignored issues
show
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
80
            $sch_tool->dropDatabase();
0 ignored issues
show
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
81
            $sch_tool->updateSchema($metadata);
0 ignored issues
show
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
82
        } catch (Throwable $e) {
83
            fwrite(
84
                STDERR,
85
                'EXCEPCIÓN: ' . $e->getCode() . ' - ' . $e->getMessage()
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
86
            );
87
            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...
88
        }
89
    }
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 updateSchema()
Loading history...
90
}
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...
91