Completed
Pull Request — master (#29)
by Dominik
06:47 queued 01:11
created

createCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
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\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 14
    private function __construct(string $name)
45
    {
46 14
        $this->name = $name;
47 14
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return DenormalizationFieldMappingBuilderInterface
53
     */
54 3 View Code Duplication
    public static function create(string $name, bool $emptyToNull = false): 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...
55
    {
56 3
        $self = new self($name);
57 3
        $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull);
58
59 3
        return $self;
60
    }
61
62
    /**
63
     * @param string   $name
64
     * @param callable $callback
65
     *
66
     * @return DenormalizationFieldMappingBuilderInterface
67
     */
68 1
    public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface
69
    {
70 1
        $self = new self($name);
71 1
        $self->fieldDenormalizer = new CallbackFieldDenormalizer($callback);
72
73 1
        return $self;
74
    }
75
76
    /**
77
     * @param string $name
78
     * @param string $type
79
     * @param bool   $emptyToNull
80
     *
81
     * @return DenormalizationFieldMappingBuilderInterface
82
     */
83 2 View Code Duplication
    public static function createConvertType(
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...
84
        string $name,
85
        string $type,
86
        bool $emptyToNull = false
87
    ): DenormalizationFieldMappingBuilderInterface {
88 2
        $self = new self($name);
89 2
        $self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull);
90
91 2
        return $self;
92
    }
93
94
    /**
95
     * @param string        $name
96
     * @param bool          $emptyToNull
97
     * @param \DateTimeZone $dateTimeZone
98
     *
99
     * @return DenormalizationFieldMappingBuilderInterface
100
     */
101 3
    public static function createDateTime(
102
        string $name,
103
        bool $emptyToNull = false,
104
        \DateTimeZone $dateTimeZone = null
105
    ): DenormalizationFieldMappingBuilderInterface {
106 3
        $self = new self($name);
107 3
        $self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull, $dateTimeZone);
108
109 3
        return $self;
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param string $class
115
     *
116
     * @return DenormalizationFieldMappingBuilderInterface
117
     */
118 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...
119
    {
120 1
        $self = new self($name);
121 1
        $self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name));
122
123 1
        return $self;
124
    }
125
126
    /**
127
     * @param string $name
128
     * @param string $class
129
     *
130
     * @return DenormalizationFieldMappingBuilderInterface
131
     */
132 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...
133
    {
134 1
        $self = new self($name);
135 1
        $self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name));
136
137 1
        return $self;
138
    }
139
140
    /**
141
     * @param string   $name
142
     * @param callable $repository
143
     *
144
     * @return DenormalizationFieldMappingBuilderInterface
145
     */
146 1
    public static function createReferenceMany(
147
        string $name,
148
        callable $repository
149
    ): DenormalizationFieldMappingBuilderInterface {
150 1
        $self = new self($name);
151 1
        $self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name));
152
153 1
        return $self;
154
    }
155
156
    /**
157
     * @param string   $name
158
     * @param callable $repository
159
     * @param bool     $emptyToNull
160
     *
161
     * @return DenormalizationFieldMappingBuilderInterface
162
     */
163 2
    public static function createReferenceOne(
164
        string $name,
165
        callable $repository,
166
        bool $emptyToNull = false
167
    ): DenormalizationFieldMappingBuilderInterface {
168 2
        $self = new self($name);
169 2
        $self->fieldDenormalizer = new ReferenceOneFieldDenormalizer(
170 2
            $repository,
171 2
            new PropertyAccessor($name),
172 2
            $emptyToNull
173
        );
174
175 2
        return $self;
176
    }
177
178
    /**
179
     * @deprecated
180
     *
181
     * @param array $groups
182
     *
183
     * @return DenormalizationFieldMappingBuilderInterface
184
     */
185 1
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
186
    {
187 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...
188
189 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...
190
    }
191
192
    /**
193
     * @param FieldDenormalizerInterface $fieldDenormalizer
194
     *
195
     * @return DenormalizationFieldMappingBuilderInterface
196
     */
197 1
    public function setFieldDenormalizer(
198
        FieldDenormalizerInterface $fieldDenormalizer
199
    ): DenormalizationFieldMappingBuilderInterface {
200 1
        $this->fieldDenormalizer = $fieldDenormalizer;
201
202 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...
203
    }
204
205
    /**
206
     * @param PolicyInterface $policy
207
     *
208
     * @return DenormalizationFieldMappingBuilderInterface
209
     */
210 1
    public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface
211
    {
212 1
        $this->policy = $policy;
213
214 1
        return $this;
215
    }
216
217
    /**
218
     * @return DenormalizationFieldMappingInterface
219
     */
220 14
    public function getMapping(): DenormalizationFieldMappingInterface
221
    {
222 14
        return new DenormalizationFieldMapping(
223 14
            $this->name,
224 14
            $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...
225 14
            $this->fieldDenormalizer,
226 14
            $this->policy ?? new NullPolicy()
227
        );
228
    }
229
}
230