Stub_Kernel::getSetting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
class Stub_Kernel extends Intraface_Kernel
3
{
4
    public $intranet;
5
    public $user;
6
    public $setting;
7
    public $session_id;
8
9
    function __construct($session_id = 'notreallyauniquesessionid')
10
    {
11
        $this->intranet = new Stub_Intranet;
12
        $this->user = new Stub_User;
13
        $this->setting = new Stub_Setting;
14
        $this->setting->set('intranet', 'contact.login_url', 'http://localhost/');
15
        $this->setting->set('intranet', 'webshop.confirmation_text', 'sometext');
16
        $this->setting->set('intranet', 'webshop.show_online', true);
17
        $this->setting->set('intranet', 'contact.login_email_text', 'sometext');
18
        $this->setting->set('intranet', 'cms.stylesheet.site', 'something');
19
        $this->session_id = $session_id;
20
    }
21
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
    function useShared()
23
    {
24
        throw new Exception('kernel->useShared should not be used in classes. Please rewrite the method');
25
    }
26
27
    function module()
28
    {
29
        throw new Exception('kernel->module should not be used in classes. Please rewrite the method!');
30
    }
31
32
    function useModule()
33
    {
34
        throw new Exception('kernel->useModule should not be used in classes. Please rewrite the method!');
35
    }
36
37
    function getModule()
38
    {
39
        throw new Exception('kernel->getModule should not be used in classes. Please rewrite the method!');
40
    }
41
    */
42
43
    public function getSessionId()
44
    {
45
        return $this->session_id;
46
    }
47
48
    function getSetting()
49
    {
50
        return $this->setting;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->setting; (Stub_Setting) is incompatible with the return type of the parent method Intraface_Kernel::getSetting of type Intraface_Setting.

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...
51
    }
52
}
53