Completed
Pull Request — master (#12)
by Dominik
07:37 queued 14s
created

NormalizationLinkMappingBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 81.25%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 11 1
A setGroups() 0 6 1
A setPermission() 0 6 1
A getMapping() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface;
8
9
final class NormalizationLinkMappingBuilder implements NormalizationLinkMappingBuilderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
16
    /**
17
     * @var array
18
     */
19
    private $groups;
20
21
    /**
22
     * @var LinkNormalizerInterface
23
     */
24
    private $linkNormalizer;
25
26
    /**
27
     * @var object|string|null
28
     */
29
    private $permission;
30
31 2
    private function __construct()
32
    {
33 2
    }
34
35
    /**
36
     * @param string                  $name
37
     * @param LinkNormalizerInterface $linkNormalizer
38
     *
39
     * @return NormalizationLinkMappingBuilderInterface
40
     */
41 2
    public static function create(
42
        string $name,
43
        LinkNormalizerInterface $linkNormalizer
44
    ): NormalizationLinkMappingBuilderInterface {
45 2
        $self = new self();
46 2
        $self->name = $name;
47 2
        $self->groups = [];
48 2
        $self->linkNormalizer = $linkNormalizer;
49
50 2
        return $self;
51
    }
52
53
    /**
54
     * @param array $groups
55
     *
56
     * @return NormalizationLinkMappingBuilderInterface
57
     */
58 1
    public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface
59
    {
60 1
        $this->groups = $groups;
61
62 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...ationLinkMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...derInterface::setGroups 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...
63
    }
64
65
    /**
66
     * @param object|string|null $permission
67
     *
68
     * @return NormalizationLinkMappingBuilderInterface
69
     */
70
    public function setPermission($permission): NormalizationLinkMappingBuilderInterface
71
    {
72
        $this->permission = $permission;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return NormalizationLinkMappingInterface
79
     */
80 2
    public function getMapping(): NormalizationLinkMappingInterface
81
    {
82 2
        return new NormalizationLinkMapping($this->name, $this->groups, $this->linkNormalizer, $this->permission);
83
    }
84
}
85