Passed
Pull Request — master (#15)
by
unknown
03:56
created

NormalizationLinkMappingBuilder::createCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer;
8
use Chubbyphp\Serialization\Normalizer\LinkNormalizer;
9
use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface;
10
use Chubbyphp\Serialization\Policy\PolicyInterface;
11
use Chubbyphp\Serialization\Policy\NullPolicy;
12
use Psr\Link\LinkInterface;
13
14
final class NormalizationLinkMappingBuilder implements NormalizationLinkMappingBuilderInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var array
23
     */
24
    private $groups = [];
25
26
    /**
27
     * @var LinkNormalizerInterface
28
     */
29
    private $linkNormalizer;
30
31
    /**
32
     * @var PolicyInterface|null
33
     */
34
    private $policy;
35
36
    /**
37
     * @param string $name
38
     */
39 4
    private function __construct(string $name)
40
    {
41 4
        $this->name = $name;
42 4
    }
43
44
    /**
45
     * @param string                  $name
46
     * @param LinkNormalizerInterface $linkNormalizer
47
     *
48
     * @return NormalizationLinkMappingBuilderInterface
49
     */
50 2
    public static function create(
51
        string $name,
52
        LinkNormalizerInterface $linkNormalizer
53
    ): NormalizationLinkMappingBuilderInterface {
54 2
        $self = new self($name);
55 2
        $self->linkNormalizer = $linkNormalizer;
56
57 2
        return $self;
58
    }
59
60
    /**
61
     * @param string   $name
62
     * @param callable $callback
63
     *
64
     * @return NormalizationLinkMappingBuilderInterface
65
     */
66 1
    public static function createCallback(
67
        string $name,
68
        callable $callback
69
    ): NormalizationLinkMappingBuilderInterface {
70 1
        $self = new self($name);
71 1
        $self->linkNormalizer = new CallbackLinkNormalizer($callback);
72
73 1
        return $self;
74
    }
75
76
    /**
77
     * @param string        $name
78
     * @param LinkInterface $link
79
     *
80
     * @return NormalizationLinkMappingBuilderInterface
81
     */
82 1
    public static function createLink(
83
        string $name,
84
        LinkInterface $link
85
    ): NormalizationLinkMappingBuilderInterface {
86 1
        $self = new self($name);
87 1
        $self->linkNormalizer = new LinkNormalizer($link);
88
89 1
        return $self;
90
    }
91
92
    /**
93
     * @deprecated
94
     *
95
     * @param array $groups
96
     *
97
     * @return NormalizationLinkMappingBuilderInterface
98
     */
99 1
    public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface
100
    {
101 1
        $this->groups = $groups;
102
103 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...
104
    }
105
106
    /**
107
     * @param PolicyInterface $policy
108
     *
109
     * @return NormalizationLinkMappingBuilderInterface
110
     */
111 1
    public function setPolicy(PolicyInterface $policy): NormalizationLinkMappingBuilderInterface
112
    {
113 1
        $this->policy = $policy;
114
115 1
        return $this;
116
    }
117
118
    /**
119
     * @return NormalizationLinkMappingInterface
120
     */
121 4
    public function getMapping(): NormalizationLinkMappingInterface
122
    {
123 4
        return new NormalizationLinkMapping(
124 4
            $this->name,
125 4
            $this->groups,
126 4
            $this->linkNormalizer,
127 4
            $this->policy ?? new NullPolicy()
128
        );
129
    }
130
}
131