Test Failed
Pull Request — master (#10)
by Yo
02:29
created

ConfigurationDenormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 126
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A denormalize() 0 65 1
A getNormalizedOrDefault() 0 4 2
A valueOrNull() 0 4 2
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Serializer\Normalizer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
5
6
/**
7
 * Class ConfigurationDenormalizer
8
 */
9
class ConfigurationDenormalizer implements DenormalizerInterface
10
{
11
    /** @var AuthorListNormalizer */
12
    private $authorListNormalizer;
13
    /** @var PackageListNormalizer */
14
    private $packageListNormalizer;
15
    /** @var SuggestedPackageListNormalizer */
16
    private $suggestedPackageListNormalizer;
17
    /** @var SupportListNormalizer */
18
    private $supportListNormalizer;
19
    /** @var AutoloadListNormalizer */
20
    private $autoloadListNormalizer;
21
    /** @var ScriptListNormalizer */
22
    private $scriptListNormalizer;
23
24
    public function __construct(
25
        AuthorListNormalizer $authorListNormalizer,
26
        PackageListNormalizer $packageListNormalizer,
27
        SuggestedPackageListNormalizer $suggestedPackageListNormalizer,
28
        SupportListNormalizer $supportListNormalizer,
29
        AutoloadListNormalizer $autoloadListNormalizer,
30
        ScriptListNormalizer $scriptListNormalizer
31
    ) {
32
        $this->authorListNormalizer = $authorListNormalizer;
33
        $this->packageListNormalizer = $packageListNormalizer;
34
        $this->suggestedPackageListNormalizer = $suggestedPackageListNormalizer;
35
        $this->supportListNormalizer = $supportListNormalizer;
36
        $this->autoloadListNormalizer = $autoloadListNormalizer;
37
        $this->scriptListNormalizer = $scriptListNormalizer;
38
    }
39
40
    /**
41
     * @param array $configuration
42
     *
43
     * @return Configuration
44
     */
45
    public function denormalize(array $configuration)
46
    {
47
        return new Configuration(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Yoanm\Compos...:KEY_SCRIPT, array())); (Yoanm\ComposerConfigMana...ain\Model\Configuration) is incompatible with the return type declared by the interface Yoanm\ComposerConfigMana...rInterface::denormalize of type array.

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...
48
            $this->valueOrNull($configuration, ConfigurationNormalizer::KEY_NAME),
49
            $this->valueOrNull($configuration, ConfigurationNormalizer::KEY_TYPE),
50
            $this->valueOrNull($configuration, ConfigurationNormalizer::KEY_LICENSE),
51
            $this->valueOrNull($configuration, ConfigurationNormalizer::KEY_VERSION),
52
            $this->valueOrNull($configuration, ConfigurationNormalizer::KEY_DESCRIPTION),
53
            $configuration[ConfigurationNormalizer::KEY_KEYWORDS],
54
            $this->getNormalizedOrDefault(
55
                $this->authorListNormalizer,
56
                $configuration,
57
                ConfigurationNormalizer::KEY_AUTHORS,
58
                []
59
            ),
60
            $this->getNormalizedOrDefault(
61
                $this->packageListNormalizer,
62
                $configuration,
63
                ConfigurationNormalizer::KEY_PROVIDE,
64
                []
65
            ),
66
            $this->getNormalizedOrDefault(
67
                $this->suggestedPackageListNormalizer,
68
                $configuration,
69
                ConfigurationNormalizer::KEY_SUGGEST,
70
                []
71
            ),
72
            $this->getNormalizedOrDefault(
73
                $this->supportListNormalizer,
74
                $configuration,
75
                ConfigurationNormalizer::KEY_SUPPORT,
76
                []
77
            ),
78
            $this->getNormalizedOrDefault(
79
                $this->autoloadListNormalizer,
80
                $configuration,
81
                ConfigurationNormalizer::KEY_AUTOLOAD,
82
                []
83
            ),
84
            $this->getNormalizedOrDefault(
85
                $this->autoloadListNormalizer,
86
                $configuration,
87
                ConfigurationNormalizer::KEY_AUTOLOAD_DEV,
88
                []
89
            ),
90
            $this->getNormalizedOrDefault(
91
                $this->packageListNormalizer,
92
                $configuration,
93
                ConfigurationNormalizer::KEY_REQUIRE,
94
                []
95
            ),
96
            $this->getNormalizedOrDefault(
97
                $this->packageListNormalizer,
98
                $configuration,
99
                ConfigurationNormalizer::KEY_REQUIRE_DEV,
100
                []
101
            ),
102
            $this->getNormalizedOrDefault(
103
                $this->scriptListNormalizer,
104
                $configuration,
105
                ConfigurationNormalizer::KEY_SCRIPT,
106
                []
107
            )
108
        );
109
    }
110
111
    /**
112
     * @param DenormalizerInterface $denormalizer
113
     * @param array                 $configuration
114
     * @param string                $key
115
     * @param mixed                 $default
116
     *
117
     * @return array
118
     */
119
    protected function getNormalizedOrDefault(DenormalizerInterface $denormalizer, array $configuration, $key, $default)
120
    {
121
        return isset($configuration[$key]) ? $denormalizer->denormalize($configuration[$key]) : $default;
122
    }
123
124
    /**
125
     * @param array  $configuration
126
     * @param string $key
127
     *
128
     * @return mixed|null
129
     */
130
    protected function valueOrNull(array $configuration, $key)
131
    {
132
        return isset($configuration[$key]) ? $configuration[$key] : null;
133
    }
134
}
135