IlluminateDBAdapter::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Analogue\ORM\Drivers;
4
5
use Illuminate\Database\Connection;
6
7
/**
8
 * Illuminate Driver for Analogue ORM. If multiple DB connections are
9
 * involved, we'll treat each underlyin driver as a separate instance.
10
 */
11
class IlluminateDBAdapter implements DBAdapter
12
{
13
    /**
14
     * @var Connection
15
     */
16
    protected $connection;
17
18
    /**
19
     * IlluminateDBAdapter constructor.
20
     * @param Connection $connection
21
     */
22
    public function __construct(Connection $connection)
23
    {
24
        $this->connection = $connection;
25
    }
26
27
    /**
28
     * Return a new Query instance for this driver
29
     *
30
     * @return QueryAdapter
31
     */
32
    public function getQuery()
33
    {
34
        $connection = $this->connection;
35
36
        $grammar = $connection->getQueryGrammar();
37
38
        return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Analogue\ORM...n->getPostProcessor()); (Analogue\ORM\Drivers\IlluminateQueryBuilder) is incompatible with the return type declared by the interface Analogue\ORM\Drivers\DBAdapter::getQuery of type Analogue\ORM\Drivers\Que...\IlluminateQueryAdapter.

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...
39
    }
40
41
    /**
42
     * Get the date format supported by the current connection
43
     *
44
     * @return string
45
     */
46
    public function getDateFormat()
47
    {
48
        return $this->connection->getQueryGrammar()->getDateFormat();
49
    }
50
51
    /**
52
     * Start a DB transaction on driver that supports it.
53
     * @return void
54
     */
55
    public function beginTransaction()
56
    {
57
        $this->connection->beginTransaction();
58
    }
59
60
    /**
61
     * Commit a DB transaction on driver that supports it.
62
     * @return void
63
     */
64
    public function commit()
65
    {
66
        $this->connection->commit();
67
    }
68
69
    /**
70
     * Rollback a DB transaction
71
     * @return void
72
     */
73
    public function rollback()
74
    {
75
        $this->connection->rollBack();
76
    }
77
}
78