Test Failed
Pull Request — master (#29)
by Dominik
01:54
created

DenormalizationFieldMappingBuilder::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\Deserialization\Mapping;
6
7
use Chubbyphp\Deserialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Deserialization\Denormalizer\CallbackFieldDenormalizer;
9
use Chubbyphp\Deserialization\Denormalizer\ConvertTypeFieldDenormalizer;
10
use Chubbyphp\Deserialization\Denormalizer\DateTimeFieldDenormalizer;
11
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer;
12
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
13
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedManyFieldDenormalizer;
14
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer;
15
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer;
16
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer;
17
use Chubbyphp\Deserialization\Policy\NullPolicy;
18
use Chubbyphp\Deserialization\Policy\PolicyInterface;
19
20
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * @deprecated
29
     *
30
     * @var array
31
     */
32
    private $groups = [];
33
34
    /**
35 14
     * @var FieldDenormalizerInterface
36
     */
37 14
    private $fieldDenormalizer;
38 14
39
    /**
40
     * @var PolicyInterface|null
41
     */
42
    private $policy;
43
44
    /**
45 3
     * @param string $name
46
     */
47 3
    private function __construct(string $name)
48 3
    {
49
        $this->name = $name;
50 3
    }
51
52
    /**
53
     * @param string $name
54
     * @param bool   $emptyToNull
55
     * @param FieldDenormalizerInterface|null
56
     *
57
     * @return DenormalizationFieldMappingBuilderInterface
58
     */
59 1 View Code Duplication
    public static function create(
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...
60
        string $name,
61 1
        bool $emptyToNull = false,
62 1
        FieldDenormalizerInterface $fieldDenormalizer = null
63
    ): DenormalizationFieldMappingBuilderInterface {
64 1
        if (null === $fieldDenormalizer) {
65
            $fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull);
66
        }
67
68
        $self = new self($name);
69
        $self->fieldDenormalizer = $fieldDenormalizer;
70
71
        return $self;
72
    }
73
74 2
    /**
75
     * @param string   $name
76
     * @param callable $callback
77
     *
78
     * @return DenormalizationFieldMappingBuilderInterface
79 2
     */
80 2
    public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface
81
    {
82 2
        $self = new self($name);
83
        $self->fieldDenormalizer = new CallbackFieldDenormalizer($callback);
84
85
        return $self;
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param string $type
91
     * @param bool   $emptyToNull
92 3
     *
93
     * @return DenormalizationFieldMappingBuilderInterface
94
     */
95
    public static function createConvertType(
96
        string $name,
97 3
        string $type,
98 3
        bool $emptyToNull = false
99
    ): DenormalizationFieldMappingBuilderInterface {
100 3
        $self = new self($name);
101
        $self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull);
102
103
        return $self;
104
    }
105
106
    /**
107
     * @param string        $name
108
     * @param bool          $emptyToNull
109 1
     * @param \DateTimeZone $dateTimeZone
110
     *
111 1
     * @return DenormalizationFieldMappingBuilderInterface
112 1
     */
113 View Code Duplication
    public static function createDateTime(
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...
114 1
        string $name,
115
        bool $emptyToNull = false,
116
        \DateTimeZone $dateTimeZone = null
117
    ): DenormalizationFieldMappingBuilderInterface {
118
        $self = new self($name);
119
        $self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull, $dateTimeZone);
120
121
        return $self;
122
    }
123 1
124
    /**
125 1
     * @param string $name
126 1
     * @param string $class
127
     *
128 1
     * @return DenormalizationFieldMappingBuilderInterface
129
     */
130 View Code Duplication
    public static function createEmbedMany(string $name, string $class): DenormalizationFieldMappingBuilderInterface
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...
131
    {
132
        $self = new self($name);
133
        $self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name));
134
135
        return $self;
136
    }
137 1
138
    /**
139
     * @param string $name
140
     * @param string $class
141 1
     *
142 1
     * @return DenormalizationFieldMappingBuilderInterface
143
     */
144 1 View Code Duplication
    public static function createEmbedOne(string $name, string $class): DenormalizationFieldMappingBuilderInterface
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...
145
    {
146
        $self = new self($name);
147
        $self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name));
148
149
        return $self;
150
    }
151
152
    /**
153
     * @param string   $name
154 2
     * @param callable $repository
155
     *
156
     * @return DenormalizationFieldMappingBuilderInterface
157
     */
158
    public static function createReferenceMany(
159 2
        string $name,
160 2
        callable $repository
161 2
    ): DenormalizationFieldMappingBuilderInterface {
162 2
        $self = new self($name);
163 2
        $self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name));
164
165
        return $self;
166 2
    }
167
168
    /**
169
     * @param string   $name
170
     * @param callable $repository
171
     * @param bool     $emptyToNull
172
     *
173
     * @return DenormalizationFieldMappingBuilderInterface
174 1
     */
175
    public static function createReferenceOne(
176 1
        string $name,
177
        callable $repository,
178 1
        bool $emptyToNull = false
179
    ): DenormalizationFieldMappingBuilderInterface {
180
        $self = new self($name);
181
        $self->fieldDenormalizer = new ReferenceOneFieldDenormalizer(
182
            $repository,
183
            new PropertyAccessor($name),
184
            $emptyToNull
185
        );
186 1
187
        return $self;
188
    }
189 1
190
    /**
191 1
     * @deprecated
192
     *
193
     * @param array $groups
194
     *
195
     * @return DenormalizationFieldMappingBuilderInterface
196
     */
197 14
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
198
    {
199 14
        $this->groups = $groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...MappingBuilder::$groups has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
200 14
201 14
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...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...
202 14
    }
203
204
    /**
205
     * @param FieldDenormalizerInterface $fieldDenormalizer
206
     *
207
     * @return DenormalizationFieldMappingBuilderInterface
208
     */
209
    public function setFieldDenormalizer(
210
        FieldDenormalizerInterface $fieldDenormalizer
211
    ): DenormalizationFieldMappingBuilderInterface {
212
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
213
            'Utilize third parameter of create method instead',
214
            E_USER_DEPRECATED
215
        );
216
217
        $this->fieldDenormalizer = $fieldDenormalizer;
218
219
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...e::setFieldDenormalizer 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...
220
    }
221
222
    /**
223
     * @param PolicyInterface $policy
224
     *
225
     * @return DenormalizationFieldMappingBuilderInterface
226
     */
227
    public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface
228
    {
229
        $this->policy = $policy;
230
231
        return $this;
232
    }
233
234
    /**
235
     * @return DenormalizationFieldMappingInterface
236
     */
237
    public function getMapping(): DenormalizationFieldMappingInterface
238
    {
239
        return new DenormalizationFieldMapping(
240
            $this->name,
241
            $this->groups,
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...MappingBuilder::$groups has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
242
            $this->fieldDenormalizer,
243
            $this->policy ?? new NullPolicy()
244
        );
245
    }
246
}
247