Failed Conditions
Pull Request — master (#17)
by Yo
03:03 queued 17s
created

ConfigurationFileUpdater   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 120
ccs 77
cts 77
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A update() 0 11 2
A merge() 0 13 1
A mergeConfiguration() 0 53 1
A mergeKeyList() 0 4 1
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Updater;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
5
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
6
7
class ConfigurationFileUpdater
8
{
9
    /** @var PlainValueUpdater */
10
    private $plainValueUpdater;
11
    /** @var KeywordListUpdater */
12
    private $keywordListUpdater;
13
    /** @var ListUpdater */
14
    private $listUpdater;
15
    /** @var AuthorListUpdater */
16
    private $authorListUpdater;
17
18 1
    public function __construct(
19
        PlainValueUpdater $plainValueUpdater,
20
        KeywordListUpdater $keywordListUpdater,
21
        ListUpdater $listUpdater,
22
        AuthorListUpdater $authorListUpdater
23
    ) {
24 1
        $this->plainValueUpdater = $plainValueUpdater;
25 1
        $this->keywordListUpdater = $keywordListUpdater;
26 1
        $this->listUpdater = $listUpdater;
27 1
        $this->authorListUpdater = $authorListUpdater;
28 1
    }
29
30
    /**
31
     * @param ConfigurationFile[] $configurationFileList
32
     *
33
     * @return ConfigurationFile
34
     */
35 1
    public function update(array $configurationFileList)
36
    {
37 1
        $newConfigurationFile = array_pop($configurationFileList);
38
39 1
        while (count($configurationFileList) > 0) {
40 1
            $baseConfigurationFile = array_pop($configurationFileList);
41 1
            $newConfigurationFile = $this->merge($baseConfigurationFile, $newConfigurationFile);
1 ignored issue
show
Bug introduced by
It seems like $newConfigurationFile can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42 1
        }
43
44 1
        return $newConfigurationFile;
45
    }
46
47
48
    /**
49
     * @param ConfigurationFile $baseConfigurationFile
50
     * @param ConfigurationFile $newConfigurationFile
51
     *
52
     * @return ConfigurationFile
53
     */
54 1
    public function merge(ConfigurationFile $baseConfigurationFile, ConfigurationFile $newConfigurationFile)
55
    {
56 1
        return new ConfigurationFile(
57 1
            $this->mergeConfiguration(
58 1
                $baseConfigurationFile->getConfiguration(),
59 1
                $newConfigurationFile->getConfiguration()
60 1
            ),
61 1
            $this->mergeKeyList(
62 1
                $baseConfigurationFile->getKeyList(),
63 1
                $newConfigurationFile->getKeyList()
64 1
            )
65 1
        );
66
    }
67
68 1
    protected function mergeConfiguration(Configuration $baseConfiguration, Configuration $newConfiguration)
69
    {
70 1
        return new Configuration(
71 1
            $this->plainValueUpdater->update(
72 1
                $newConfiguration->getPackageName(),
73 1
                $baseConfiguration->getPackageName()
74 1
            ),
75 1
            $this->plainValueUpdater->update(
76 1
                $newConfiguration->getType(),
77 1
                $baseConfiguration->getType()
78 1
            ),
79 1
            $this->plainValueUpdater->update(
80 1
                $newConfiguration->getLicense(),
81 1
                $baseConfiguration->getLicense()
82 1
            ),
83 1
            $this->plainValueUpdater->update(
84 1
                $newConfiguration->getPackageVersion(),
85 1
                $baseConfiguration->getPackageVersion()
86 1
            ),
87 1
            $this->plainValueUpdater->update(
88 1
                $newConfiguration->getDescription(),
89 1
                $baseConfiguration->getDescription()
90 1
            ),
91 1
            $this->keywordListUpdater->update(
92 1
                $newConfiguration->getKeywordList(),
93 1
                $baseConfiguration->getKeywordList()
94 1
            ),
95 1
            $this->authorListUpdater->update($newConfiguration->getAuthorList(), $baseConfiguration->getAuthorList()),
96 1
            $this->listUpdater->update(
97 1
                $newConfiguration->getProvidedPackageList(),
98 1
                $baseConfiguration->getProvidedPackageList()
99 1
            ),
100 1
            $this->listUpdater->update(
101 1
                $newConfiguration->getSuggestedPackageList(),
102 1
                $baseConfiguration->getSuggestedPackageList()
103 1
            ),
104 1
            $this->listUpdater->update($newConfiguration->getSupportList(), $baseConfiguration->getSupportList()),
105 1
            $this->listUpdater->update($newConfiguration->getAutoloadList(), $baseConfiguration->getAutoloadList()),
106 1
            $this->listUpdater->update(
107 1
                $newConfiguration->getAutoloadDevList(),
108 1
                $baseConfiguration->getAutoloadDevList()
109 1
            ),
110 1
            $this->listUpdater->update(
111 1
                $newConfiguration->getRequiredPackageList(),
112 1
                $baseConfiguration->getRequiredPackageList()
113 1
            ),
114 1
            $this->listUpdater->update(
115 1
                $newConfiguration->getRequiredDevPackageList(),
116 1
                $baseConfiguration->getRequiredDevPackageList()
117 1
            ),
118 1
            $this->listUpdater->update($newConfiguration->getScriptList(), $baseConfiguration->getScriptList())
119 1
        );
120
    }
121
122 1
    protected function mergeKeyList(array $baseKeyList, array $newKeyList)
123
    {
124 1
        return array_replace($baseKeyList, $newKeyList);
125
    }
126
}
127