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

RequiresTrait   A

Complexity

Total Complexity 9

Size/Duplication

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeRequires() 0 20 2
A mergeDevRequires() 0 20 2
A mergeStabilityFlags() 0 11 1
A mergeLinks() 0 16 4
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Arcanedev\Composer\Entities\PluginState;
4
use Arcanedev\Composer\Entities\StabilityFlags;
5
use Composer\Package\RootPackageInterface;
6
7
/**
8
 * Trait     RequiresTrait
9
 *
10
 * @package  Arcanedev\Composer\Entities\PackageTraits
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait RequiresTrait
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Merge require into a RootPackage.
21
     *
22
     * @param  \Composer\Package\RootPackageInterface    $root
23
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
24
     */
25
    protected function mergeRequires(RootPackageInterface $root, PluginState $state)
26
    {
27
        if ( ! empty($requires = $this->package->getRequires())) {
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
            $this->mergeStabilityFlags($root, $requires);
29
30
            $duplicateLinks = [];
31
            $requires       = $this->replaceSelfVersionDependencies(
0 ignored issues
show
Bug introduced by
It seems like replaceSelfVersionDependencies() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
                'require', $requires, $root
33
            );
34
35
            $root->setRequires($this->mergeLinks(
36
                $root->getRequires(),
37
                $requires,
38
                $state->replaceDuplicateLinks(),
39
                $duplicateLinks
40
            ));
41
42
            $state->addDuplicateLinks('require', $duplicateLinks);
43
        }
44
    }
45
46
    /**
47
     * Merge require-dev into RootPackage.
48
     *
49
     * @param  \Composer\Package\RootPackageInterface    $root
50
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
51
     */
52
    protected function mergeDevRequires(RootPackageInterface $root, PluginState $state)
53
    {
54
        if ( ! empty($requires = $this->package->getDevRequires())) {
55
            $this->mergeStabilityFlags($root, $requires);
56
57
            $duplicateLinks = [];
58
            $requires       = $this->replaceSelfVersionDependencies(
0 ignored issues
show
Bug introduced by
It seems like replaceSelfVersionDependencies() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
                'require-dev', $requires, $root
60
            );
61
62
            $root->setDevRequires($this->mergeLinks(
63
                $root->getDevRequires(),
64
                $requires,
65
                $state->replaceDuplicateLinks(),
66
                $duplicateLinks
67
            ));
68
69
            $state->addDuplicateLinks('require-dev', $duplicateLinks);
70
        }
71
    }
72
73
    /* ------------------------------------------------------------------------------------------------
74
     |  Other Functions
75
     | ------------------------------------------------------------------------------------------------
76
     */
77
    /**
78
     * Extract and merge stability flags from the given collection of
79
     * requires and merge them into a RootPackage.
80
     *
81
     * @param  \Composer\Package\RootPackageInterface  $root
82
     * @param  \Composer\Package\Link[]                $requires
83
     */
84
    protected function mergeStabilityFlags(RootPackageInterface $root, array $requires)
85
    {
86
        $flags = StabilityFlags::extract(
87
            $root->getStabilityFlags(),
88
            $root->getMinimumStability(),
89
            $requires
90
        );
91
92
        self::unwrapIfNeeded($root, 'setStabilityFlags')
93
            ->setStabilityFlags($flags);
94
    }
95
96
    /**
97
     * Merge two collections of package links and collect duplicates for subsequent processing.
98
     *
99
     * @param  \Composer\Package\Link[]  $origin          Primary collection
100
     * @param  array                     $merge           Additional collection
101
     * @param  bool                      $replace         Replace existing links ?
102
     * @param  array                     $duplicateLinks  Duplicate storage
103
     *
104
     * @return \Composer\Package\Link[]                   Merged collection
105
     */
106
    private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks)
107
    {
108
        foreach ($merge as $name => $link) {
109
            if ( ! isset($origin[$name]) || $replace) {
110
                $this->logger->info("Merging <comment>{$name}</comment>");
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...
111
                $origin[$name] = $link;
112
            }
113
            else {
114
                // Defer to solver.
115
                $this->logger->info("Deferring duplicate <comment>{$name}</comment>");
116
                $duplicateLinks[] = $link;
117
            }
118
        }
119
120
        return $origin;
121
    }
122
}
123