Passed
Push — master ( 2f127a...1f1008 )
by Dominik
03:40 queued 01:11
created

NormalizationFieldMappingBuilder::createDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

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