Passed
Pull Request — master (#19)
by Dominik
02:18
created

createReferenceMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
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\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
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
        $self = new self($name);
46 2
        $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name));
47
48 2
        return $self;
49
    }
50
51
    /**
52
     * @param string $name
53
     *
54
     * @return DenormalizationFieldMappingBuilderInterface
55
     */
56 1
    public static function createDateTime(string $name): DenormalizationFieldMappingBuilderInterface
57
    {
58 1
        $self = new self($name);
59 1
        $self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name));
60
61 1
        return $self;
62
    }
63
64
    /**
65
     * @param string $name
66
     * @param string $class
67
     *
68
     * @return DenormalizationFieldMappingBuilderInterface
69
     */
70 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...
71
    {
72 1
        $self = new self($name);
73 1
        $self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name));
74
75 1
        return $self;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @param string $class
81
     *
82
     * @return DenormalizationFieldMappingBuilderInterface
83
     */
84 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...
85
    {
86 1
        $self = new self($name);
87 1
        $self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name));
88
89 1
        return $self;
90
    }
91
92
    /**
93
     * @param string   $name
94
     * @param callable $repository
95
     *
96
     * @return DenormalizationFieldMappingBuilderInterface
97
     */
98 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...
99
        string $name,
100
        callable $repository
101
    ): DenormalizationFieldMappingBuilderInterface {
102 1
        $self = new self($name);
103 1
        $self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name));
104
105 1
        return $self;
106
    }
107
108
    /**
109
     * @param string   $name
110
     * @param callable $repository
111
     *
112
     * @return DenormalizationFieldMappingBuilderInterface
113
     */
114 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...
115
        string $name,
116
        callable $repository
117
    ): DenormalizationFieldMappingBuilderInterface {
118 1
        $self = new self($name);
119 1
        $self->fieldDenormalizer = new ReferenceOneFieldDenormalizer($repository, new PropertyAccessor($name));
120
121 1
        return $self;
122
    }
123
124
    /**
125
     * @param array $groups
126
     *
127
     * @return DenormalizationFieldMappingBuilderInterface
128
     */
129 1
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
130
    {
131 1
        $this->groups = $groups;
132
133 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...
134
    }
135
136
    /**
137
     * @param FieldDenormalizerInterface $fieldDenormalizer
138
     *
139
     * @return DenormalizationFieldMappingBuilderInterface
140
     */
141 1
    public function setFieldDenormalizer(
142
        FieldDenormalizerInterface $fieldDenormalizer
143
    ): DenormalizationFieldMappingBuilderInterface {
144 1
        $this->fieldDenormalizer = $fieldDenormalizer;
145
146 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...
147
    }
148
149
    /**
150
     * @return DenormalizationFieldMappingInterface
151
     */
152 7
    public function getMapping(): DenormalizationFieldMappingInterface
153
    {
154 7
        return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer);
155
    }
156
}
157