Passed
Pull Request — master (#13)
by Dominik
03:26
created

DenormalizationFieldMappingBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 85
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 8 1
A setGroups() 0 6 1
A setFieldDenormalizer() 0 7 1
A setForceType() 0 6 1
A getMapping() 0 4 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\FieldDenormalizer;
9
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
10
11
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var array
20
     */
21
    private $groups = [];
22
23
    /**
24
     * @var FieldDenormalizerInterface
25
     */
26
    private $fieldDenormalizer;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $forceType;
32
33 2
    private function __construct()
34
    {
35 2
    }
36
37
    /**
38
     * @param string $name
39
     *
40
     * @return DenormalizationFieldMappingBuilderInterface
41
     */
42 2
    public static function create(string $name): DenormalizationFieldMappingBuilderInterface
43
    {
44 2
        $self = new self();
45 2
        $self->name = $name;
46 2
        $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name));
47
48 2
        return $self;
49
    }
50
51
    /**
52
     * @param array $groups
53
     *
54
     * @return DenormalizationFieldMappingBuilderInterface
55
     */
56 1
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
57
    {
58 1
        $this->groups = $groups;
59
60 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...
61
    }
62
63
    /**
64
     * @param FieldDenormalizerInterface $fieldDenormalizer
65
     *
66
     * @return DenormalizationFieldMappingBuilderInterface
67
     */
68 1
    public function setFieldDenormalizer(
69
        FieldDenormalizerInterface $fieldDenormalizer
70
    ): DenormalizationFieldMappingBuilderInterface {
71 1
        $this->fieldDenormalizer = $fieldDenormalizer;
72
73 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...
74
    }
75
76
    /**
77
     * @param string|null $forceType
78
     *
79
     * @return DenormalizationFieldMappingBuilderInterface
80
     */
81
    public function setForceType(string $forceType = null): DenormalizationFieldMappingBuilderInterface
82
    {
83
        $this->forceType = $forceType;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return DenormalizationFieldMappingInterface
90
     */
91 2
    public function getMapping(): DenormalizationFieldMappingInterface
92
    {
93 2
        return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer, $this->forceType);
94
    }
95
}
96