Completed
Pull Request — master (#8)
by ARCANEDEV
02:14
created

ExtraTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeExtra() 0 12 3
A getExtra() 0 20 4
A mergeExtraArray() 0 6 2
unwrapIfNeeded() 0 3 ?
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
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /** @var \Arcanedev\Composer\Utilities\Logger $logger */
20
    protected $logger;
21
22
    /** @var \Composer\Package\CompletePackage $package */
23
    protected $package;
24
25
    /** @var string $path */
26
    protected $path;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Main Functions
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * Merge extra config into a RootPackage.
34
     *
35
     * @param  \Composer\Package\RootPackageInterface    $root
36
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
37
     */
38
    protected function mergeExtra(RootPackageInterface $root, PluginState $state)
39
    {
40
        $extra = $this->package->getExtra();
41
        unset($extra['merge-plugin']);
42
43
        if ($state->shouldMergeExtra() && ! empty($extra)) {
44
            $unwrapped = static::unwrapIfNeeded($root, 'setExtra');
45
            $unwrapped->setExtra(
46
                $this->getExtra($unwrapped, $state, $extra)
47
            );
48
        }
49
    }
50
51
    /**
52
     * Get extra config.
53
     *
54
     * @param  \Composer\Package\RootPackageInterface    $root
55
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
56
     * @param  array                                     $extra
57
     *
58
     * @return array
59
     */
60
    private function getExtra(
61
        RootPackageInterface $root, PluginState $state, $extra
62
    ) {
63
        $rootExtra   = $root->getExtra();
64
65
        if ($state->replaceDuplicateLinks()) {
66
            return self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra);
67
        }
68
69
        if ( ! $state->shouldMergeExtraDeep()) {
70
            foreach (array_intersect(array_keys($extra), array_keys($rootExtra)) as $key) {
71
                $this->logger->info(
72
                    "Ignoring duplicate <comment>{$key}</comment> in ".
73
                    "<comment>{$this->path}</comment> extra config."
74
                );
75
            }
76
        }
77
78
        return self::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra);
79
    }
80
81
    /**
82
     * Merges two arrays either via arrayMergeDeep or via array_merge.
83
     *
84
     * @param  bool  $mergeDeep
85
     * @param  array  $array1
86
     * @param  array  $array2
87
     *
88
     * @return array
89
     */
90
    private static function mergeExtraArray($mergeDeep, $array1, $array2)
91
    {
92
        return $mergeDeep
93
            ? NestedArray::mergeDeep($array1, $array2)
94
            : array_merge($array1, $array2);
95
    }
96
97
    /**
98
     * Get a full featured Package from a RootPackageInterface.
99
     *
100
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
101
     * @param  string                                                                $method
102
     *
103
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
104
     */
105
    abstract protected function unwrapIfNeeded(
106
        RootPackageInterface $root, $method = 'setExtra'
107
    );
108
}
109