Test Failed
Push — master ( 8a2b60...8e4848 )
by Dominik
03:00
created

NormalizationFieldMappingBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 9 1
A setGroups() 0 6 1
A setFieldNormalizer() 0 7 1
A getMapping() 0 4 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\FieldNormalizer;
9
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
10
11
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var array
20
     */
21
    private $groups;
22
23
    /**
24
     * @var FieldNormalizerInterface
25
     */
26
    private $fieldNormalizer;
27
28
    private function __construct()
29
    {
30
    }
31
32
    /**
33
     * @param string $name
34
     *
35
     * @return NormalizationFieldMappingBuilderInterface
36
     */
37
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
38
    {
39
        $self = new self();
40
        $self->name = $name;
41
        $self->groups = [];
42
        $self->fieldNormalizer = new FieldNormalizer(new PropertyAccessor($name));
43
44
        return $self;
45
    }
46
47
    /**
48
     * @param array $groups
49
     *
50
     * @return NormalizationFieldMappingBuilderInterface
51
     */
52
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
53
    {
54
        $this->groups = $groups;
55
56
        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...
57
    }
58
59
    /**
60
     * @param FieldNormalizerInterface $fieldNormalizer
61
     *
62
     * @return NormalizationFieldMappingBuilderInterface
63
     */
64
    public function setFieldNormalizer(
65
        FieldNormalizerInterface $fieldNormalizer
66
    ): NormalizationFieldMappingBuilderInterface {
67
        $this->fieldNormalizer = $fieldNormalizer;
68
69
        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...
70
    }
71
72
    /**
73
     * @return NormalizationFieldMappingInterface
74
     */
75
    public function getMapping(): NormalizationFieldMappingInterface
76
    {
77
        return new NormalizationFieldMapping($this->name, $this->groups, $this->fieldNormalizer);
78
    }
79
}
80