Passed
Push — master ( 59e837...beea65 )
by F. Javier
15:37
created

Utils::updateSchema()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 15
rs 9.9
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 Dotenv\Dotenv;
13
use Throwable;
14
15
/**
16
 * Class Utils
17
 */
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...
18
class Utils
19
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Utils
Loading history...
20
    /**
21
     * Load the environment/configuration variables
22
     * defined in .env file + (.env.docker || .env.local)
23
     *
24
     * @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 end with a full stop
Loading history...
introduced by
Parameter comment must start with a capital letter
Loading history...
25
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
26
    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...
27
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
28
        require_once $dir . '/vendor/autoload.php';
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
29
30
        if (!class_exists(Dotenv::class)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
31
            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...
32
            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...
33
        }
34
35
        try {
36
            // 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...
37
            if (file_exists($dir . '/.env')) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
38
                $dotenv = Dotenv::createMutable($dir, '.env');
39
                $dotenv->load();
40
            } else {
41
                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...
42
                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...
43
            }
44
45
            // 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...
46
            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...
47
                $dotenv = Dotenv::createMutable($dir, '.env.docker');
48
                $dotenv->load();
49
            } 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...
50
                $dotenv = Dotenv::createMutable($dir, '.env.local');
51
                $dotenv->load();
52
            }
53
54
            // 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...
55
            $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...
56
            $dotenv->required([ 'ENTITY_DIR' ]);
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
57
        } catch (Throwable $e) {
58
            fwrite(
59
                STDERR,
60
                'EXCEPCIÓN: ' . $e->getCode() . ' - ' . $e->getMessage()
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
61
            );
62
            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...
63
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end try"
Loading history...
64
    }
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...
65
66
    /**
67
     * Drop & Update database schema
68
     *
69
     * @return void
70
     */
71
    public static function updateSchema(): void
72
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
73
        try {
74
            /** @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...
75
            $e_manager = DoctrineConnector::getEntityManager();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
76
            $metadata = $e_manager->getMetadataFactory()->getAllMetadata();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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
Variable "e_manager" is not in valid camel caps format
Loading history...
77
            $sch_tool = new SchemaTool($e_manager);
0 ignored issues
show
Bug introduced by
The type MiW\DemoDoctrine\Utility\SchemaTool was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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
Variable "e_manager" is not in valid camel caps format
Loading history...
78
            $sch_tool->dropDatabase();
0 ignored issues
show
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
79
            $sch_tool->updateSchema($metadata);
0 ignored issues
show
Coding Style introduced by
Variable "sch_tool" is not in valid camel caps format
Loading history...
80
        } catch (Throwable $e) {
81
            fwrite(
82
                STDERR,
83
                'EXCEPCIÓN: ' . $e->getCode() . ' - ' . $e->getMessage()
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
84
            );
85
            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...
86
        }
87
    }
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 updateSchema()
Loading history...
88
89
    /**
90
     * Load user data fixtures
91
     *
92
     * @param string $username user name
0 ignored issues
show
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
93
     * @param string $email user email
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 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...
94
     * @param string $password user password
0 ignored issues
show
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
95
     * @param bool $isWriter isAdmin
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
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...
96
     *
97
     * @return int user_id
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
98
     */
99
    public static function loadUserData(
100
        string $username,
101
        string $email,
102
        string $password,
103
        bool $isWriter = false
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...
104
    ): int {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
105
        assert($username !== '');
106
        $user = new User(
0 ignored issues
show
Bug introduced by
The type MiW\DemoDoctrine\Utility\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
107
            username: $username,
108
            email: $email,
109
            password: $password,
110
            role: $isWriter ? Role::WRITER : Role::READER
0 ignored issues
show
Bug introduced by
The type MiW\DemoDoctrine\Utility\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
111
        );
112
        try {
113
            $e_manager = DoctrineConnector::getEntityManager();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
114
            $e_manager?->persist($user);
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
115
            $e_manager?->flush();
0 ignored issues
show
Coding Style introduced by
Variable "e_manager" is not in valid camel caps format
Loading history...
116
        } catch (Throwable $e) {
117
            fwrite(
118
                STDERR,
119
                'EXCEPCIÓN: ' . $e->getCode() . ' - ' . $e->getMessage()
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
120
            );
121
            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 integer. 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...
122
        }
123
124
        return $user->getId();
125
    }
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 loadUserData()
Loading history...
126
}
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...
127