Completed
Push — master ( f657d9...210110 )
by ARCANEDEV
8s
created

ExtraTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeExtra() 0 12 3
A getExtra() 0 20 4
A mergeExtraArray() 0 6 2
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Arcanedev\Composer\Entities\PluginState;
4
use Arcanedev\Composer\Utilities\NestedArray;
5
use Composer\Package\RootPackageInterface;
6
7
/**
8
 * Trait     ExtraTrait
9
 *
10
 * @package  Arcanedev\Composer\Entities\PackageTraits
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait ExtraTrait
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Traits
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    use PackageTrait;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Main Functions
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Merge extra config into a RootPackage.
27
     *
28
     * @param  \Composer\Package\RootPackageInterface    $root
29
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
30
     */
31
    protected function mergeExtra(RootPackageInterface $root, PluginState $state)
32
    {
33
        $extra = $this->getPackage()->getExtra();
34
        unset($extra['merge-plugin']);
35
36
        if ($state->shouldMergeExtra() && ! empty($extra)) {
37
            $unwrapped = static::unwrapIfNeeded($root, 'setExtra');
38
            $unwrapped->setExtra(
39
                $this->getExtra($unwrapped, $state, $extra)
40
            );
41
        }
42
    }
43
44
    /**
45
     * Get extra config.
46
     *
47
     * @param  \Composer\Package\RootPackageInterface    $root
48
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
49
     * @param  array                                     $extra
50
     *
51
     * @return array
52
     */
53
    private function getExtra(
54
        RootPackageInterface $root, PluginState $state, $extra
55
    ) {
56
        $rootExtra   = $root->getExtra();
57
58
        if ($state->replaceDuplicateLinks()) {
59
            return self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra);
60
        }
61
62
        if ( ! $state->shouldMergeExtraDeep()) {
63
            foreach (array_intersect(array_keys($extra), array_keys($rootExtra)) as $key) {
64
                $this->getLogger()->info(
65
                    "Ignoring duplicate <comment>{$key}</comment> in ".
66
                    "<comment>{$this->getPath()}</comment> extra config."
67
                );
68
            }
69
        }
70
71
        return static::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra);
72
    }
73
74
    /**
75
     * Merges two arrays either via arrayMergeDeep or via array_merge.
76
     *
77
     * @param  bool  $mergeDeep
78
     * @param  array  $array1
79
     * @param  array  $array2
80
     *
81
     * @return array
82
     */
83
    private static function mergeExtraArray($mergeDeep, $array1, $array2)
84
    {
85
        return $mergeDeep
86
            ? NestedArray::mergeDeep($array1, $array2)
87
            : array_merge($array1, $array2);
88
    }
89
}
90