Completed
Push — master ( eee136...1365cb )
by Oleg
03:55
created

ConnectionInjector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 4 1
A build() 0 10 2
1
<?php /** MicroConnectionInjector */
2
3
namespace Micro\Db;
4
5
use Micro\Base\Exception;
6
use Micro\Base\Injector;
7
use Micro\Db\Drivers\IDriver;
8
9
/**
10
 * Class ConnectionInjector
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Db
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class ConnectionInjector extends Injector
22
{
23
    /**
24
     * Get DB driver
25
     *
26
     * @access public
27
     * @return IDriver
28
     * @throws Exception
29
     */
30
    public function getDriver()
31
    {
32
        return $this->build()->getDriver();
33
    }
34
35
    /**
36
     * @access public
37
     * @return IConnection
38
     * @throws Exception
39
     */
40
    public function build()
41
    {
42
        $connection = $this->get('connection');
43
44
        if (!($connection instanceof IConnection)) {
45
            throw new Exception('Component `connection` not configured');
46
        }
47
48
        return $connection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $connection; (Micro\Db\IConnection) is incompatible with the return type declared by the interface Micro\Base\IInjector::build of type Micro\Base\IDispatcher.

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...
49
    }
50
}