Completed
Pull Request — master (#16)
by Yo
02:35
created

ConfigurationUpdater::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 51
cts 51
cp 1
rs 9.5797
c 0
b 0
f 0
cc 1
eloc 39
nc 1
nop 2
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Updater;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
5
6
class ConfigurationUpdater
7
{
8
    /** @var PlainValueUpdater */
9
    private $plainValueUpdater;
10
    /** @var KeywordListUpdater */
11
    private $keywordListUpdater;
12
    /** @var ListUpdater */
13
    private $listUpdater;
14
    /** @var AuthorListUpdater */
15
    private $authorListUpdater;
16
17 1
    public function __construct(
18
        PlainValueUpdater $plainValueUpdater,
19
        KeywordListUpdater $keywordListUpdater,
20
        ListUpdater $listUpdater,
21
        AuthorListUpdater $authorListUpdater
22
    ) {
23 1
        $this->plainValueUpdater = $plainValueUpdater;
24 1
        $this->keywordListUpdater = $keywordListUpdater;
25 1
        $this->listUpdater = $listUpdater;
26 1
        $this->authorListUpdater = $authorListUpdater;
27 1
    }
28
29
    /**
30
     * @param Configuration[] $configurationList
31
     *
32
     * @return Configuration
33
     */
34 1
    public function update(array $configurationList)
35
    {
36 1
        $newConfiguration = array_pop($configurationList);
37
38 1
        while (count($configurationList) > 0) {
39 1
            $baseConfiguration = array_pop($configurationList);
40 1
            $newConfiguration = $this->merge($baseConfiguration, $newConfiguration);
1 ignored issue
show
Bug introduced by
It seems like $newConfiguration 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...
41 1
        }
42
43 1
        return $newConfiguration;
44
    }
45
46
47
    /**
48
     * @param Configuration $baseConfiguration
49
     * @param Configuration $newConfiguration
50
     *
51
     * @return Configuration
52
     */
53 1
    public function merge(Configuration $baseConfiguration, Configuration $newConfiguration)
54
    {
55 1
        return new Configuration(
56 1
            $this->plainValueUpdater->update(
57 1
                $newConfiguration->getPackageName(),
58 1
                $baseConfiguration->getPackageName()
59 1
            ),
60 1
            $this->plainValueUpdater->update(
61 1
                $newConfiguration->getType(),
62 1
                $baseConfiguration->getType()
63 1
            ),
64 1
            $this->plainValueUpdater->update(
65 1
                $newConfiguration->getLicense(),
66 1
                $baseConfiguration->getLicense()
67 1
            ),
68 1
            $this->plainValueUpdater->update(
69 1
                $newConfiguration->getPackageVersion(),
70 1
                $baseConfiguration->getPackageVersion()
71 1
            ),
72 1
            $this->plainValueUpdater->update(
73 1
                $newConfiguration->getDescription(),
74 1
                $baseConfiguration->getDescription()
75 1
            ),
76 1
            $this->keywordListUpdater->update(
77 1
                $newConfiguration->getKeywordList(),
78 1
                $baseConfiguration->getKeywordList()
79 1
            ),
80 1
            $this->authorListUpdater->update($newConfiguration->getAuthorList(), $baseConfiguration->getAuthorList()),
81 1
            $this->listUpdater->update(
82 1
                $newConfiguration->getProvidedPackageList(),
83 1
                $baseConfiguration->getProvidedPackageList()
84 1
            ),
85 1
            $this->listUpdater->update(
86 1
                $newConfiguration->getSuggestedPackageList(),
87 1
                $baseConfiguration->getSuggestedPackageList()
88 1
            ),
89 1
            $this->listUpdater->update($newConfiguration->getSupportList(), $baseConfiguration->getSupportList()),
90 1
            $this->listUpdater->update($newConfiguration->getAutoloadList(), $baseConfiguration->getAutoloadList()),
91 1
            $this->listUpdater->update(
92 1
                $newConfiguration->getAutoloadDevList(),
93 1
                $baseConfiguration->getAutoloadDevList()
94 1
            ),
95 1
            $this->listUpdater->update(
96 1
                $newConfiguration->getRequiredPackageList(),
97 1
                $baseConfiguration->getRequiredPackageList()
98 1
            ),
99 1
            $this->listUpdater->update(
100 1
                $newConfiguration->getRequiredDevPackageList(),
101 1
                $baseConfiguration->getRequiredDevPackageList()
102 1
            ),
103 1
            $this->listUpdater->update($newConfiguration->getScriptList(), $baseConfiguration->getScriptList())
104 1
        );
105
    }
106
}
107