Passed
Pull Request — master (#6)
by Dominik
02:17
created

NormalizationFieldMappingBuilder::createDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\DateTimeFieldNormalizer;
9
use Chubbyphp\Serialization\Normalizer\FieldNormalizer;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
11
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer;
12
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer;
13
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer;
14
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer;
15
16
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var array
25
     */
26
    private $groups = [];
27
28
    /**
29
     * @var FieldNormalizerInterface
30
     */
31
    private $fieldNormalizer;
32
33 2
    private function __construct(string $name)
34
    {
35 2
        $this->name = $name;
36 2
    }
37
38
    /**
39
     * @param string $name
40
     *
41
     * @return NormalizationFieldMappingBuilderInterface
42
     */
43 2
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
44
    {
45 2
        $self = new self($name);
46 2
        $self->fieldNormalizer = new FieldNormalizer(new PropertyAccessor($name));
47
48 2
        return $self;
49
    }
50
51
    /**
52
     * @param string $name
53
     *
54
     * @return NormalizationFieldMappingBuilderInterface
55
     */
56
    public static function createDateTime(string $name): NormalizationFieldMappingBuilderInterface
57
    {
58
        $self = new self($name);
59
        $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name));
60
61
        return $self;
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @return NormalizationFieldMappingBuilderInterface
68
     */
69
    public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface
70
    {
71
        $self = new self($name);
72
        $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name));
73
74
        return $self;
75
    }
76
77
    /**
78
     * @param string $name
79
     *
80
     * @return NormalizationFieldMappingBuilderInterface
81
     */
82
    public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface
83
    {
84
        $self = new self($name);
85
        $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name));
86
87
        return $self;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param string $idName
93
     *
94
     * @return NormalizationFieldMappingBuilderInterface
95
     */
96 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...
97
        string $name,
98
        string $idName = 'id'
99
    ): NormalizationFieldMappingBuilderInterface {
100
        $self = new self($name);
101
        $self->fieldNormalizer = new ReferenceManyFieldNormalizer(
102
            new PropertyAccessor($idName),
103
            new PropertyAccessor($name)
104
        );
105
106
        return $self;
107
    }
108
109
    /**
110
     * @param string $name
111
     * @param string $idName
112
     *
113
     * @return NormalizationFieldMappingBuilderInterface
114
     */
115 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...
116
        string $name,
117
        string $idName = 'id'
118
    ): NormalizationFieldMappingBuilderInterface {
119
        $self = new self($name);
120
        $self->fieldNormalizer = new ReferenceOneFieldNormalizer(
121
            new PropertyAccessor($idName),
122
            new PropertyAccessor($name)
123
        );
124
125
        return $self;
126
    }
127
128
    /**
129
     * @param array $groups
130
     *
131
     * @return NormalizationFieldMappingBuilderInterface
132
     */
133 1
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
134
    {
135 1
        $this->groups = $groups;
136
137 1
        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...
138
    }
139
140
    /**
141
     * @param FieldNormalizerInterface $fieldNormalizer
142
     *
143
     * @return NormalizationFieldMappingBuilderInterface
144
     */
145 1
    public function setFieldNormalizer(
146
        FieldNormalizerInterface $fieldNormalizer
147
    ): NormalizationFieldMappingBuilderInterface {
148 1
        $this->fieldNormalizer = $fieldNormalizer;
149
150 1
        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...
151
    }
152
153
    /**
154
     * @return NormalizationFieldMappingInterface
155
     */
156 2
    public function getMapping(): NormalizationFieldMappingInterface
157
    {
158 2
        return new NormalizationFieldMapping($this->name, $this->groups, $this->fieldNormalizer);
159
    }
160
}
161