Test Failed
Pull Request — master (#15)
by
unknown
02:56
created

NormalizationFieldMappingBuilder::setPolicy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer;
9
use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizer;
11
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
12
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer;
13
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer;
14
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer;
15
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer;
16
use Chubbyphp\Serialization\Policy\NullPolicy;
17
use Chubbyphp\Serialization\Policy\PolicyInterface;
18
19
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var array
28
     */
29
    private $groups = [];
30
31
    /**
32
     * @var FieldNormalizerInterface|null
33
     */
34
    private $fieldNormalizer;
35
36
    /**
37 9
     * @var PolicyInterface|null
38
     */
39 9
    private $policy;
40 9
41
    /**
42
     * @param string $name
43
     */
44
    private function __construct(string $name)
45
    {
46
        $this->name = $name;
47 2
    }
48
49 2
    /**
50
     * @param string $name
51
     *
52
     * @return NormalizationFieldMappingBuilderInterface
53
     */
54
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
55
    {
56
        return new self($name);
57
    }
58 1
59
    /**
60 1
     * @param string   $name
61 1
     * @param callable $callback
62
     *
63 1
     * @return NormalizationFieldMappingBuilderInterface
64
     */
65
    public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface
66
    {
67
        $self = new self($name);
68
        $self->fieldNormalizer = new CallbackFieldNormalizer($callback);
69
70
        return $self;
71
    }
72 2
73
    /**
74 2
     * @param string $name
75 2
     * @param string $format
76
     *
77 2
     * @return NormalizationFieldMappingBuilderInterface
78
     */
79
    public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface
80
    {
81
        $self = new self($name);
82
        $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format);
83
84
        return $self;
85 1
    }
86
87 1
    /**
88 1
     * @param string $name
89
     *
90 1
     * @return NormalizationFieldMappingBuilderInterface
91
     */
92
    public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface
93
    {
94
        $self = new self($name);
95
        $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name));
96
97
        return $self;
98 1
    }
99
100 1
    /**
101 1
     * @param string $name
102
     *
103 1
     * @return NormalizationFieldMappingBuilderInterface
104
     */
105
    public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface
106
    {
107
        $self = new self($name);
108
        $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name));
109
110
        return $self;
111
    }
112 1
113
    /**
114
     * @param string $name
115
     * @param string $idName
116 1
     *
117 1
     * @return NormalizationFieldMappingBuilderInterface
118 1
     */
119 1 View Code Duplication
    public static function createReferenceMany(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
        string $name,
121
        string $idName = 'id'
122 1
    ): NormalizationFieldMappingBuilderInterface {
123
        $self = new self($name);
124
        $self->fieldNormalizer = new ReferenceManyFieldNormalizer(
125
            new PropertyAccessor($idName),
126
            new PropertyAccessor($name)
127
        );
128
129
        return $self;
130
    }
131 1
132
    /**
133
     * @param string $name
134
     * @param string $idName
135 1
     *
136 1
     * @return NormalizationFieldMappingBuilderInterface
137 1
     */
138 1 View Code Duplication
    public static function createReferenceOne(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
        string $name,
140
        string $idName = 'id'
141 1
    ): NormalizationFieldMappingBuilderInterface {
142
        $self = new self($name);
143
        $self->fieldNormalizer = new ReferenceOneFieldNormalizer(
144
            new PropertyAccessor($idName),
145
            new PropertyAccessor($name)
146
        );
147
148
        return $self;
149 1
    }
150
151 1
    /**
152
     * @deprecated
153 1
     *
154
     * @param array $groups
155
     *
156
     * @return NormalizationFieldMappingBuilderInterface
157
     */
158
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
159
    {
160
        $this->groups = $groups;
161 1
162
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) 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...
163
    }
164 1
165
    /**
166 1
     * @param FieldNormalizerInterface $fieldNormalizer
167
     *
168
     * @return NormalizationFieldMappingBuilderInterface
169
     */
170
    public function setFieldNormalizer(
171
        FieldNormalizerInterface $fieldNormalizer
172 9
    ): NormalizationFieldMappingBuilderInterface {
173
        $this->fieldNormalizer = $fieldNormalizer;
174 9
175 9
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...ace::setFieldNormalizer 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...
176 9
    }
177 9
178
    /**
179
     * @param PolicyInterface $policy
180
     *
181
     * @return NormalizationFieldMappingBuilderInterface
182
     */
183
    public function setPolicy(PolicyInterface $policy): NormalizationFieldMappingBuilderInterface
184
    {
185
        $this->policy = $policy;
186
187
        return $this;
188
    }
189
190
    /**
191
     * @return NormalizationFieldMappingInterface
192
     */
193
    public function getMapping(): NormalizationFieldMappingInterface
194
    {
195
        return new NormalizationFieldMapping(
196
            $this->name,
197
            $this->groups,
198
            $this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name)),
199
            $this->policy ?? new NullPolicy()
200
        );
201
    }
202
}
203