Passed
Push — master ( 265f32...0e6955 )
by Dominik
02:02 queued 12s
created

DenormalizationFieldMappingBuilder::setPolicy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     * @var FieldDenormalizerInterface
36
     */
37
    private $fieldDenormalizer;
38
39
    /**
40
     * @var PolicyInterface|null
41
     */
42
    private $policy;
43
44
    /**
45
     * @param string $name
46
     */
47 15
    private function __construct(string $name)
48
    {
49 15
        $this->name = $name;
50 15
    }
51
52
    /**
53
     * @param string $name
54
     * @param bool   $emptyToNull
55
     * @param FieldDenormalizerInterface|null
56
     *
57
     * @return DenormalizationFieldMappingBuilderInterface
58
     */
59 4 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
        bool $emptyToNull = false,
62
        FieldDenormalizerInterface $fieldDenormalizer = null
63
    ): DenormalizationFieldMappingBuilderInterface {
64 4
        if (null === $fieldDenormalizer) {
65 3
            $fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull);
66
        }
67
68 4
        $self = new self($name);
69 4
        $self->fieldDenormalizer = $fieldDenormalizer;
70
71 4
        return $self;
72
    }
73
74
    /**
75
     * @param string   $name
76
     * @param callable $callback
77
     *
78
     * @return DenormalizationFieldMappingBuilderInterface
79
     */
80 1
    public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface
81
    {
82 1
        $self = new self($name);
83 1
        $self->fieldDenormalizer = new CallbackFieldDenormalizer($callback);
84
85 1
        return $self;
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param string $type
91
     * @param bool   $emptyToNull
92
     *
93
     * @return DenormalizationFieldMappingBuilderInterface
94
     */
95 2
    public static function createConvertType(
96
        string $name,
97
        string $type,
98
        bool $emptyToNull = false
99
    ): DenormalizationFieldMappingBuilderInterface {
100 2
        $self = new self($name);
101 2
        $self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull);
102
103 2
        return $self;
104
    }
105
106
    /**
107
     * @param string        $name
108
     * @param bool          $emptyToNull
109
     * @param \DateTimeZone $dateTimeZone
110
     *
111
     * @return DenormalizationFieldMappingBuilderInterface
112
     */
113 3 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
        string $name,
115
        bool $emptyToNull = false,
116
        \DateTimeZone $dateTimeZone = null
117
    ): DenormalizationFieldMappingBuilderInterface {
118 3
        $self = new self($name);
119 3
        $self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull, $dateTimeZone);
120
121 3
        return $self;
122
    }
123
124
    /**
125
     * @param string $name
126
     * @param string $class
127
     *
128
     * @return DenormalizationFieldMappingBuilderInterface
129
     */
130 1 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 1
        $self = new self($name);
133 1
        $self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name));
134
135 1
        return $self;
136
    }
137
138
    /**
139
     * @param string $name
140
     * @param string $class
141
     *
142
     * @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 1
        $self = new self($name);
147 1
        $self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name));
148
149 1
        return $self;
150
    }
151
152
    /**
153
     * @param string   $name
154
     * @param callable $repository
155
     *
156
     * @return DenormalizationFieldMappingBuilderInterface
157
     */
158 1
    public static function createReferenceMany(
159
        string $name,
160
        callable $repository
161
    ): DenormalizationFieldMappingBuilderInterface {
162 1
        $self = new self($name);
163 1
        $self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name));
164
165 1
        return $self;
166
    }
167
168
    /**
169
     * @param string   $name
170
     * @param callable $repository
171
     * @param bool     $emptyToNull
172
     *
173
     * @return DenormalizationFieldMappingBuilderInterface
174
     */
175 2
    public static function createReferenceOne(
176
        string $name,
177
        callable $repository,
178
        bool $emptyToNull = false
179
    ): DenormalizationFieldMappingBuilderInterface {
180 2
        $self = new self($name);
181 2
        $self->fieldDenormalizer = new ReferenceOneFieldDenormalizer(
182 2
            $repository,
183 2
            new PropertyAccessor($name),
184 2
            $emptyToNull
185
        );
186
187 2
        return $self;
188
    }
189
190
    /**
191
     * @deprecated
192
     *
193
     * @param array $groups
194
     *
195
     * @return DenormalizationFieldMappingBuilderInterface
196
     */
197 1
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
198
    {
199 1
        $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
201 1
        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
    }
203
204
    /**
205
     * @param FieldDenormalizerInterface $fieldDenormalizer
206
     *
207
     * @return DenormalizationFieldMappingBuilderInterface
208
     */
209 1
    public function setFieldDenormalizer(
210
        FieldDenormalizerInterface $fieldDenormalizer
211
    ): DenormalizationFieldMappingBuilderInterface {
212 1
        @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 1
            'Utilize third parameter of create method instead',
214 1
            E_USER_DEPRECATED
215
        );
216
217 1
        $this->fieldDenormalizer = $fieldDenormalizer;
218
219 1
        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 1
    public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface
228
    {
229 1
        $this->policy = $policy;
230
231 1
        return $this;
232
    }
233
234
    /**
235
     * @return DenormalizationFieldMappingInterface
236
     */
237 15
    public function getMapping(): DenormalizationFieldMappingInterface
238
    {
239 15
        return new DenormalizationFieldMapping(
240 15
            $this->name,
241 15
            $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 15
            $this->fieldDenormalizer,
243 15
            $this->policy ?? new NullPolicy()
244
        );
245
    }
246
}
247