Completed
Push — master ( bf0cd7...00bb35 )
by Sébastien
04:20 queued 14s
created

PdoMysqlConnection::getCurrentSchema()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 9
nc 4
nop 0
crap 4.25
1
<?php
2
3
namespace Soluble\DbWrapper\Connection;
4
5
use Soluble\DbWrapper\Adapter\PdoMysqlAdapter;
6
use PDO;
7
8
class PdoMysqlConnection implements ConnectionInterface
9
{
10
11
    /**
12
     *
13
     * @var PdoMysqlAdapter
14
     */
15
    protected $adapter;
16
17
18
    /**
19
     *
20
     * @var PDO
21
     */
22
    protected $resource;
23
24
    /**
25
     *
26
     * @param PdoMysqlAdapter $adapter
27
     * @param PDO $resource
28
     */
29 12
    public function __construct(PdoMysqlAdapter $adapter, PDO $resource)
30
    {
31 12
        $this->adapter = $adapter;
32 12
        $this->resource = $resource;
33 12
    }
34
35
36
    /**
37
     * {@inheritdoc}
38
     * @return PDO
39
     */
40 2
    public function getResource()
41
    {
42 2
        return $this->resource;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function getHost()
49
    {
50 1
        $infos = explode(' ', trim($this->resource->getAttribute(PDO::ATTR_CONNECTION_STATUS)));
51 1
        return strtolower($infos[0]);
52
    }
53
54
    /**
55
     * Return current schema/database name
56
     *
57
     * @throws Exception\RuntimeException
58
     * @return string
59
     */
60 2
    public function getCurrentSchema()
61
    {
62 2
        $query = 'SELECT DATABASE() as current_schema';
63
        try {
64 2
            $results = $this->adapter->query($query);
65 2
            if (count($results) == 0 || $results[0]['current_schema'] === null) {
66
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Soluble\DbWrapper\Connec...ction::getCurrentSchema of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
            }
68 2
        } catch (\Exception $e) {
69
            throw new Exception\RuntimeException($e->getMessage());
70
        }
71 2
        return $results[0]['current_schema'];
72
    }
73
}
74