Database::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace EDouna\LaravelDBBackup\Databases;
4
5
use EDouna\LaravelDBBackup\ProcessHandler;
6
use Illuminate\Support\Facades\Config;
7
use SQLiteDatabase;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EDouna\LaravelDBBackup\Databases\SQLiteDatabase. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class Database
10
{
11
    /**
12
     * @var mixed
13
     */
14
    protected $database;
15
16
    /**
17
     * @var array
18
     */
19
    public $realDatabase;
20
21
    protected $processHandler;
22
23
    public $backupFilename;
24
25
    protected $storageFolder;
26
27
    /**
28
     * @var array
29
     */
30
    protected $supportedDatabaseDrivers = ['mysql', 'sqlite'];
31
32
    public function __construct()
33
    {
34
        $this->database = Config::get('database.default');
35
        $this->realDatabase = Config::get('database.connections.'.$this->database);
36
        $this->processHandler = new ProcessHandler();
37
    }
38
39
    public function buildDatabaseClass(): void
40
    {
41
        switch ($this->realDatabase['driver']) {
42
            case 'mysql':
43
                $this->realDatabase = $this->buildMySQL($this->realDatabase);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->buildMySQL($this->realDatabase) of type EDouna\LaravelDBBackup\Databases\MySQLDatabase is incompatible with the declared type array of property $realDatabase.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
                break;
45
            case 'sqlite':
46
                $this->realDatabase = $this->buildSQLite($this->realDatabase);
47
                break;
48
        }
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function isDatabaseSupported(): bool
55
    {
56
        return (in_array($this->realDatabase->getDatabaseIdentifier(), $this->supportedDatabaseDrivers)) ? true : false;
57
    }
58
59
    /**
60
     * @return object
61
     */
62
    public function getRealDatabase(): object
63
    {
64
        return $this->realDatabase;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->realDatabase returns the type array which is incompatible with the type-hinted return object.
Loading history...
65
    }
66
67
    public function setBackupFilename(string $backupFilename): void
68
    {
69
        $this->backupFilename = $backupFilename;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getBackupFilename(): string
76
    {
77
        return $this->backupFilename;
78
    }
79
80
    /**
81
     * @param array $database
82
     *
83
     * @return MySQLDatabase
84
     */
85
    protected function buildMySQL(array $database): MySQLDatabase
86
    {
87
        $this->database = new MySQLDatabase(
88
            $database['database'],
89
            $database['username'],
90
            $database['password'],
91
            $database['host'],
92
            $database['port'],
93
            $this->processHandler
94
        );
95
96
        return $this->database;
97
    }
98
99
    /**
100
     * Create an SQLite database instance.
101
     *
102
     * @param array $database
103
     *
104
     * @return Databases\SQLiteDatabase
0 ignored issues
show
Bug introduced by
The type EDouna\LaravelDBBackup\D...atabases\SQLiteDatabase 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...
105
     */
106
    protected function buildSQLite(array $database): SQLiteDatabase
107
    {
108
        $this->database = new SQLiteDatabase($database['database']);
109
110
        $this->generateBackupFilename();
0 ignored issues
show
Bug introduced by
The method generateBackupFilename() does not exist on EDouna\LaravelDBBackup\Databases\Database. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        $this->/** @scrutinizer ignore-call */ 
111
               generateBackupFilename();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
112
        return $this->database;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->database returns the type SQLiteDatabase which is incompatible with the documented return type EDouna\LaravelDBBackup\D...atabases\SQLiteDatabase.
Loading history...
113
    }
114
}
115