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

ConfigurationFileUpdater::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 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