Test Failed
Push — master ( 9bfc37...d346fd )
by Dominik
02:14
created

NormalizationLinkMappingBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 59
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 11 1
A setGroups() 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 2
    private function __construct()
27
    {
28 2
    }
29
30
    /**
31
     * @param string                  $name
32
     * @param LinkNormalizerInterface $linkNormalizer
33
     *
34
     * @return NormalizationLinkMappingBuilderInterface
35
     */
36 2
    public static function create(
37
        string $name,
38
        LinkNormalizerInterface $linkNormalizer
39
    ): NormalizationLinkMappingBuilderInterface {
40 2
        $self = new self();
41 2
        $self->name = $name;
42 2
        $self->groups = [];
43 2
        $self->linkNormalizer = $linkNormalizer;
44
45 2
        return $self;
46
    }
47
48
    /**
49
     * @param array $groups
50
     *
51
     * @return NormalizationLinkMappingBuilderInterface
52
     */
53 1
    public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface
54
    {
55 1
        $this->groups = $groups;
56
57 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...
58
    }
59
60
    /**
61
     * @return NormalizationLinkMappingInterface
62
     */
63 2
    public function getMapping(): NormalizationLinkMappingInterface
64
    {
65 2
        return new NormalizationLinkMapping($this->name, $this->groups, $this->linkNormalizer);
66
    }
67
}
68