Completed
Push — master ( 16ddda...303f06 )
by Derek
07:23 queued 05:08
created

MySql   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
wmc 4
lcom 1
cbo 1
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 16 2
A disconnect() 0 12 2
1
<?php
2
3
namespace Subreality\Dilmun\Enki;
4
5
use Subreality\Dilmun\LoggedClassTrait;
6
7
/**
8
 * Class MySql
9
 * @package Subreality\Dilmun\Enki
10
 */
11
class MySql implements EnkiInterface
12
{
13
    use LoggedClassTrait;
14
15
    private $pdo;
16
17
    /**
18
     * @param $host
19
     * @param $user
20
     * @param $pass
21
     * @param $name
22
     *
23
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
24
     */
25 3
    public function connect($host, $user, $pass, $name)
26
    {
27
        try {
28 3
            $this->pdo = new \PDO("mysql:{$host};dbname={$name}", $user, $pass);
29
30 2
            $connected = true;
31 3
        } catch (\PDOException $pdo_e) {
32 1
            $context["exception"] = $pdo_e;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$context was never initialized. Although not strictly required by PHP, it is generally a good practice to add $context = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
33
34 1
            $this->updateLog("error", "MySQL connection failed with the following message: {message}", $context);
35
            
36 1
            $connected = false;
37
        }
38
39 3
        return $connected;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45 2
    public function disconnect()
46
    {
47 2
        if ($this->pdo instanceof \PDO) {
48 1
            $this->pdo = null;
49
50 1
            $disconnected = true;
51 1
        } else {
52 1
            $disconnected = false;
53
        }
54
55 2
        return $disconnected;
56
    }
57
}
58