Completed
Push — master ( 5649ba...c37d73 )
by recca
03:09
created

SqliteConnectionResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A connection() 0 15 3
A getDefaultConnection() 0 4 1
A setDefaultConnection() 0 4 1
A getInstance() 0 8 2
1
<?php
2
3
namespace Recca0120\Repository;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\ConnectionResolverInterface;
7
use Illuminate\Database\Connectors\ConnectionFactory;
8
9
class SqliteConnectionResolver implements ConnectionResolverInterface
10
{
11
    /**
12
     * All of the registered connections.
13
     *
14
     * @var array
15
     */
16
    protected $connections = [];
17
18
    /**
19
     * The default connection name.
20
     *
21
     * @var string
22
     */
23
    protected $default = 'default';
24
    /**
25
     * The current globally available container (if any).
26
     *
27
     * @var static
28
     */
29
    private static $instance;
30
31
    public function __construct(ConnectionFactory $factory = null)
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $this->factory = $connectionFactory = new ConnectionFactory(
0 ignored issues
show
Bug introduced by
The property factory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$connectionFactory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
            Container::getInstance() ?: new Container
35
        );
36
    }
37
38
    /**
39
     * Get a database connection instance.
40
     *
41
     * @param  string  $name
42
     * @return \Illuminate\Database\ConnectionInterface
43
     */
44
    public function connection($name = null)
45
    {
46
        if (is_null($name)) {
47
            $name = $this->getDefaultConnection();
48
        }
49
50
        if (isset($this->connections[$name]) === false) {
51
            $this->connections[$name] = $this->factory->make([
52
                'driver' => 'sqlite',
53
                'database' => ':memory:',
54
            ], $name);
55
        }
56
57
        return $this->connections[$name];
58
    }
59
60
    /**
61
     * Get the default connection name.
62
     *
63
     * @return string
64
     */
65
    public function getDefaultConnection()
66
    {
67
        return $this->default;
68
    }
69
70
    /**
71
     * Set the default connection name.
72
     *
73
     * @param  string  $name
74
     * @return void
75
     */
76
    public function setDefaultConnection($name)
77
    {
78
        $this->default = $name;
79
    }
80
81
    /**
82
     * Set the globally available instance of the SqliteConnectionResolver.
83
     *
84
     * @return static
85
     */
86
    public static function getInstance(ConnectionFactory $factory = null)
87
    {
88
        if (is_null(static::$instance)) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
89
            static::$instance = new static($factory);
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
90
        }
91
92
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
93
    }
94
}
95