SessionInjector   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
c 1
b 0
f 0
wmc 2
lcom 0
cbo 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 10 2
1
<?php /** MicroSessionInjector */
2
3
namespace Micro\Web;
4
5
use Micro\Base\Exception;
6
use Micro\Base\Injector;
7
8
/**
9
 * Class SessionInjector
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/linpax/microphp-framework
13
 * @copyright Copyright (c) 2013 Oleg Lunegov
14
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
15
 * @package Micro
16
 * @subpackage Web
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class SessionInjector extends Injector
21
{
22
    /**
23
     * @access public
24
     * @return ISession
25
     * @throws Exception
26
     */
27
    public function build()
28
    {
29
        $session = $this->get('session');
30
31
        if (!($session instanceof ISession)) {
32
            throw new Exception('Component `session` not configured');
33
        }
34
35
        return $session;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $session; (Micro\Web\ISession) is incompatible with the return type declared by the interface Micro\Base\InjectorInterface::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...
36
    }
37
}