Completed
Pull Request — master (#8)
by ARCANEDEV
06:34
created

ExtraTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
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 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeExtra() 0 13 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
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Merge extra config into a RootPackage.
21
     *
22
     * @param  \Composer\Package\RootPackageInterface    $root
23
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
24
     */
25
    protected function mergeExtra(RootPackageInterface $root, PluginState $state)
26
    {
27
        $extra = $this->package->getExtra();
0 ignored issues
show
Bug introduced by
The property package does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        unset($extra['merge-plugin']);
29
30
        if ($state->shouldMergeExtra() && ! empty($extra)) {
31
            /** @var \Composer\Package\RootPackageInterface $unwrapped */
32
            $unwrapped = static::unwrapIfNeeded($root, 'setExtra');
33
            $unwrapped->setExtra(
34
                $this->getExtra($unwrapped, $state, $extra)
35
            );
36
        }
37
    }
38
39
    /**
40
     * Get extra config.
41
     *
42
     * @param  \Composer\Package\RootPackageInterface    $root
43
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
44
     * @param  array                                     $extra
45
     *
46
     * @return array
47
     */
48
    private function getExtra(
49
        RootPackageInterface $root, PluginState $state, $extra
50
    ) {
51
        $rootExtra   = $root->getExtra();
52
53
        if ($state->replaceDuplicateLinks()) {
54
            return self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra);
55
        }
56
57
        if ( ! $state->shouldMergeExtraDeep()) {
58
            foreach (array_intersect(array_keys($extra), array_keys($rootExtra)) as $key) {
59
                $this->logger->info(
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
                    "Ignoring duplicate <comment>{$key}</comment> in ".
61
                    "<comment>{$this->path}</comment> extra config."
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
                );
63
            }
64
        }
65
66
        return self::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra);
67
    }
68
69
    /**
70
     * Merges two arrays either via arrayMergeDeep or via array_merge.
71
     *
72
     * @param  bool  $mergeDeep
73
     * @param  array  $array1
74
     * @param  array  $array2
75
     *
76
     * @return array
77
     */
78
    private static function mergeExtraArray($mergeDeep, $array1, $array2)
79
    {
80
        return $mergeDeep
81
            ? NestedArray::mergeDeep($array1, $array2)
82
            : array_merge($array1, $array2);
83
    }
84
}
85