Test Failed
Push — master ( ad5f1c...9bfc37 )
by Dominik
02:03
created

LinkBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 66
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 9 1
A setRels() 0 6 1
A setAttributes() 0 6 1
A getLink() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Link;
6
7
use Psr\Link\LinkInterface;
8
9
final class LinkBuilder implements LinkBuilderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $href;
15
16
    /**
17
     * @var string[]
18
     */
19
    private $rels;
20
21
    /**
22
     * @var array
23
     */
24
    private $attributes;
25
26 2
    private function __construct()
27
    {
28 2
    }
29
30
    /**
31
     * @param string $href
32
     *
33
     * @return LinkBuilderInterface
34
     */
35 2
    public static function create(string $href): LinkBuilderInterface
36
    {
37 2
        $self = new self;
38 2
        $self->href = $href;
39 2
        $self->rels = [];
40 2
        $self->attributes = [];
41
42 2
        return $self;
43
    }
44
45
    /**
46
     * @param string[] $rels
47
     * @return LinkBuilderInterface
48
     */
49 1
    public function setRels(array $rels): LinkBuilderInterface
50
    {
51 1
        $this->rels = $rels;
52
53 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\Link\LinkBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...ilderInterface::setRels of type self.

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...
54
    }
55
56
    /**
57
     * @param array $attributes
58
     * @return LinkBuilderInterface
59
     */
60 1
    public function setAttributes(array $attributes): LinkBuilderInterface
61
    {
62 1
        $this->attributes = $attributes;
63
64 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\Link\LinkBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...nterface::setAttributes of type self.

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...
65
    }
66
67
    /**
68
     * @return LinkInterface
69
     */
70 2
    public function getLink(): LinkInterface
71
    {
72 2
        return new Link($this->href, $this->rels, $this->attributes);
73
    }
74
}
75