Completed
Pull Request — master (#1)
by Rougin
07:32
created

DatabaseDriver::parseConfiguration()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 16
nop 1
crap 5
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
/**
6
 * Database Driver
7
 *
8
 * A database driver for using available database drivers.
9
 *
10
 * @package  Describe
11
 * @category Driver
12
 * @author   Rougin Royce Gutib <[email protected]>
13
 */
14
class DatabaseDriver implements DriverInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $configuration = [];
20
21
    /**
22
     * @var string
23
     */
24
    protected $driver = '';
25
26
    /**
27
     * @param string $driver
28
     * @param array  $configuration
29
     */
30 18
    public function __construct($driver, $configuration = [])
31
    {
32 18
        $this->driver = $driver;
33 18
        $this->configuration = $configuration;
34 18
    }
35
36
    /**
37
     * Gets the specified driver from the specified database connection.
38
     *
39
     * @param string $driverName
40
     * @param array  $configuration
41
     * @return \Rougin\Describe\Driver\DriverInterface|null
42
     */
43 18
    public function getDriver($driverName, $configuration = [])
0 ignored issues
show
Unused Code introduced by
The parameter $driverName 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...
44
    {
45 18
        $mysql  = [ 'mysql', 'mysqli' ];
46 18
        $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ];
47
48 18
        list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration);
49
50 18
        if (in_array($database['dbdriver'], $mysql)) {
51
            $dsn    = 'mysql:host=' . $hostname . ';dbname=' . $database;
52
            $pdo    = new \PDO($dsn, $username, $password);
53
            $driver = new MySQLDriver($pdo, $database);
54
        }
55
56 9 View Code Duplication
        if (in_array($database['dbdriver'], $sqlite)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $pdo    = new \PDO($hostname);
58
            $driver = new SQLiteDriver($pdo);
59
        }
60
61 9
        return $driver;
0 ignored issues
show
Bug introduced by
The variable $driver does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
62
    }
63
64
    /**
65
     * Returns the result.
66
     *
67
     * @return array
68
     */
69 12
    public function getTable($table)
70
    {
71 12
        $driver = $this->getDriver($this->driver, $this->configuration);
72
73
        return $driver->getTable($table);
74
    }
75
76
    /**
77
     * Shows the list of tables.
78
     *
79
     * @return array
80
     */
81 6
    public function showTables()
82
    {
83 6
        $driver = $this->getDriver($this->driver, $this->configuration);
84
85
        return $driver->showTables();
86
    }
87
88
    /**
89
     * Parses the configuration into separate variables.
90
     *
91
     * @param  array  $configuration
92
     * @return array
93
     */
94 18
    protected function parseConfiguration(array $configuration)
95
    {
96 18
        $driver   = null;
0 ignored issues
show
Unused Code introduced by
$driver 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...
97 18
        $database = null;
98 18
        $hostname = null;
99 18
        $password = null;
100 18
        $username = null;
101
102 18
        if (isset($configuration['database'])) {
103 9
            $database = $configuration['database'];
104 9
        }
105
106 18
        if (isset($configuration['hostname'])) {
107 18
            $hostname = $configuration['hostname'];
108 18
        }
109
110 18
        if (isset($configuration['password'])) {
111 9
            $password = $configuration['password'];
112 9
        }
113
114 18
        if (isset($configuration['username'])) {
115 9
            $username = $configuration['username'];
116 9
        }
117
118 18
        return [ $database, $hostname, $username, $password ];
119
    }
120
}
121