Completed
Push — master ( 6c5d6a...127e8d )
by Oleg
03:38
created

Injector::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php /** MicroLoggerInjector */
2
3
namespace Micro\Logger;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Class LoggerInjector
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Logger
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class Injector extends \Micro\Base\Injector
20
{
21
    /**
22
     * @access public
23
     * @return Adapter
24
     * @throws Exception
25
     */
26
    public function build()
27
    {
28
        $logger = $this->get('logger');
29
30
        if (!($logger instanceof Adapter)) {
31
            throw new Exception('Component `logger` not configured');
32
        }
33
34
        return $logger;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $logger; (Micro\Logger\Adapter) 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...
35
    }
36
37
}