AbstractAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setRawData() 0 5 1
A getRawData() 0 4 1
A setTree() 0 5 1
A getTree() 0 4 1
1
<?php
2
namespace ezTreeBuilder\Base;
3
4
/**
5
 * The base of an Adapter
6
 *
7
 * @package ezTreeBuilder
8
 * @author Adrian Tilita <[email protected]>
9
 * @abstract
10
 * @version 1.0
11
 */
12
abstract class AbstractAdapter implements Adapter
13
{
14
    /**
15
     * Raw branches if needed
16
     * @var array
17
     */
18
    private $rawData;
19
20
    /**
21
     * Builded tree
22
     * @var array
23
     */
24
    private $tree;
25
26
    /**
27
     * Sets the raw branches
28
     * @param array $data
29
     * @return Adapter
30
     */
31
    public function setRawData(array $data)
32
    {
33
        $this->rawData = $data;
34
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (ezTreeBuilder\Base\AbstractAdapter) is incompatible with the return type declared by the interface ezTreeBuilder\Base\Adapter::setRawData of type TreeBuilder\Base\Adapter.

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
    /**
38
     * Returnes the raw data
39
     * @return array
40
     */
41
    protected function getRawData()
42
    {
43
        return $this->rawData;
44
    }
45
46
    /**
47
     * Sets the tree data
48
     * @param array $data
49
     * @return Adapter
50
     */
51
    public function setTree(array $data)
52
    {
53
        $this->tree = $data;
54
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (ezTreeBuilder\Base\AbstractAdapter) is incompatible with the return type declared by the interface ezTreeBuilder\Base\Adapter::setTree of type TreeBuilder\Base\Adapter.

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...
55
    }
56
57
    /**
58
     * Get the tree array
59
     * @return array
60
     */
61
    protected function getTree()
62
    {
63
        return $this->tree;
64
    }
65
}
66