Passed
Pull Request — master (#19)
by Dominik
02:54 queued 37s
created

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\Deserialization\Mapping;
6
7
use Chubbyphp\Deserialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Deserialization\Denormalizer\DateTimeFieldDenormalizer;
9
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer;
10
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
11
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedManyFieldDenormalizer;
12
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer;
13
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer;
14
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer;
15
16
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var array
25
     */
26
    private $groups = [];
27
28
    /**
29
     * @var FieldDenormalizerInterface|null
30
     */
31
    private $fieldDenormalizer;
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 DenormalizationFieldMappingBuilderInterface
42
     */
43 2
    public static function create(string $name): DenormalizationFieldMappingBuilderInterface
44
    {
45 2
        return new self($name);
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return DenormalizationFieldMappingBuilderInterface
52
     */
53 1
    public static function createDateTime(string $name): DenormalizationFieldMappingBuilderInterface
54
    {
55 1
        $self = new self($name);
56 1
        $self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name));
57
58 1
        return $self;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param string $class
64
     *
65
     * @return DenormalizationFieldMappingBuilderInterface
66
     */
67 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...
68
    {
69 1
        $self = new self($name);
70 1
        $self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name));
71
72 1
        return $self;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param string $class
78
     *
79
     * @return DenormalizationFieldMappingBuilderInterface
80
     */
81 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...
82
    {
83 1
        $self = new self($name);
84 1
        $self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name));
85
86 1
        return $self;
87
    }
88
89
    /**
90
     * @param string   $name
91
     * @param callable $repository
92
     *
93
     * @return DenormalizationFieldMappingBuilderInterface
94
     */
95 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...
96
        string $name,
97
        callable $repository
98
    ): DenormalizationFieldMappingBuilderInterface {
99 1
        $self = new self($name);
100 1
        $self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name));
101
102 1
        return $self;
103
    }
104
105
    /**
106
     * @param string   $name
107
     * @param callable $repository
108
     *
109
     * @return DenormalizationFieldMappingBuilderInterface
110
     */
111 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...
112
        string $name,
113
        callable $repository
114
    ): DenormalizationFieldMappingBuilderInterface {
115 1
        $self = new self($name);
116 1
        $self->fieldDenormalizer = new ReferenceOneFieldDenormalizer($repository, new PropertyAccessor($name));
117
118 1
        return $self;
119
    }
120
121
    /**
122
     * @param array $groups
123
     *
124
     * @return DenormalizationFieldMappingBuilderInterface
125
     */
126 1
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
127
    {
128 1
        $this->groups = $groups;
129
130 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...
131
    }
132
133
    /**
134
     * @param FieldDenormalizerInterface $fieldDenormalizer
135
     *
136
     * @return DenormalizationFieldMappingBuilderInterface
137
     */
138 1
    public function setFieldDenormalizer(
139
        FieldDenormalizerInterface $fieldDenormalizer
140
    ): DenormalizationFieldMappingBuilderInterface {
141 1
        $this->fieldDenormalizer = $fieldDenormalizer;
142
143 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...
144
    }
145
146
    /**
147
     * @return DenormalizationFieldMappingInterface
148
     */
149 7
    public function getMapping(): DenormalizationFieldMappingInterface
150
    {
151 7
        return new DenormalizationFieldMapping(
152 7
            $this->name,
153 7
            $this->groups,
154 7
            $this->fieldDenormalizer ?? new FieldDenormalizer(new PropertyAccessor($this->name))
155
        );
156
    }
157
}
158