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

Package::mergeDevRequires()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.2
cc 2
eloc 13
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\Composer\Entities;
2
3
use Arcanedev\Composer\Utilities\Logger;
4
use Composer\Composer;
5
use Composer\Package\BasePackage;
6
use Composer\Package\Link;
7
use Composer\Package\RootAliasPackage;
8
use Composer\Package\RootPackageInterface;
9
use Composer\Package\Version\VersionParser;
10
11
/**
12
 * Class     Package
13
 *
14
 * @package  Arcanedev\Composer\Entities
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Package
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Traits
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    use PackageTraits\RepositoriesTrait,
24
        PackageTraits\RequiresTrait,
25
        PackageTraits\AutoloadTrait,
26
        PackageTraits\LinksTrait,
27
        PackageTraits\SuggestsTrait,
28
        PackageTraits\ExtraTrait,
29
        PackageTraits\DevTrait,
30
        PackageTraits\ReferencesTrait;
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Properties
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /** @var \Composer\Composer $composer */
37
    protected $composer;
38
39
    /** @var \Arcanedev\Composer\Utilities\Logger $logger */
40
    protected $logger;
41
42
    /** @var \Composer\Package\CompletePackage $package */
43
    protected $package;
44
45
    /** @var string $path */
46
    protected $path;
47
48
    /** @var \Composer\Package\Version\VersionParser $versionParser */
49
    protected $versionParser;
50
51
    /** @var array $json */
52
    protected $json;
53
54
    /* ------------------------------------------------------------------------------------------------
55 135
     |  Constructor
56
     | ------------------------------------------------------------------------------------------------
57 135
     */
58 135
    /**
59 135
     * Make a Package instance.
60 135
     *
61 135
     * @param  string                                $path
62 135
     * @param  \Composer\Composer                    $composer
63 135
     * @param  \Arcanedev\Composer\Utilities\Logger  $logger
64
     */
65
    public function __construct($path, Composer $composer, Logger $logger)
66
    {
67
        $this->path          = $path;
68
        $this->composer      = $composer;
69
        $this->logger        = $logger;
70
        $this->json          = PackageJson::read($path);
71
        $this->package       = PackageJson::convert($this->json);
72
        $this->versionParser = new VersionParser;
73
    }
74 130
75
    /* ------------------------------------------------------------------------------------------------
76 130
     |  Getters & Setters
77 104
     | ------------------------------------------------------------------------------------------------
78 130
     */
79
    /**
80
     * Get list of additional packages to require if precessing recursively.
81
     *
82
     * @return array
83
     */
84
    public function getRequires()
85
    {
86 130
        return isset($this->json['extra']['merge-plugin']['require'])
87
            ? $this->json['extra']['merge-plugin']['require']
88 130
            : [];
89 109
    }
90 130
91
    /**
92
     * Get list of additional packages to include if precessing recursively.
93
     *
94
     * @return array
95
     */
96
    public function getIncludes()
97
    {
98
        return isset($this->json['extra']['merge-plugin']['include'])
99
            ? $this->json['extra']['merge-plugin']['include']
100
            : [];
101
    }
102
103 135
    /* ------------------------------------------------------------------------------------------------
104
     |  Main Functions
105 135
     | ------------------------------------------------------------------------------------------------
106
     */
107 135
    /**
108 135
     * Merge this package into a RootPackage.
109 135
     *
110 135
     * @param  \Composer\Package\RootPackageInterface    $root
111 135
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
112 135
     */
113 135
    public function mergeInto(RootPackageInterface $root, PluginState $state)
114
    {
115 135
        $this->addRepositories($root, $state);
116 129
117
        $this->mergeRequires($root, $state);
118 30
        $this->mergeAutoload($root);
119 135
        $this->mergePackageLinks('conflict', $root);
120
        $this->mergePackageLinks('replace',  $root);
121
        $this->mergePackageLinks('provide',  $root);
122
        $this->mergeSuggests($root);
123
        $this->mergeExtra($root, $state);
124
125
        if ($state->isDevMode())
126
            $this->mergeDevInto($root, $state);
127 135
        else
128
            $this->mergeReferences($root);
129 135
    }
130 135
131 135
    /* ------------------------------------------------------------------------------------------------
132 135
     |  Other Functions
133
     | ------------------------------------------------------------------------------------------------
134
     */
135
    /**
136
     * Update Links with a 'self.version' constraint with the root package's version.
137
     *
138
     * @param  string                                  $type
139
     * @param  array                                   $links
140
     * @param  \Composer\Package\RootPackageInterface  $root
141 135
     *
142
     * @return array
143 135
     */
144
    protected function replaceSelfVersionDependencies(
145 20
        $type, array $links, RootPackageInterface $root
146 20
    ) {
147
        $linkType      = BasePackage::$supportedLinkTypes[$type];
148 20
        $version       = $root->getVersion();
149 20
        $prettyVersion = $root->getPrettyVersion();
150 16
        $vp            = $this->versionParser;
151
        $packages      = $root->{'get' . ucfirst($linkType['method'])}();
152 20
153 4
        return array_map(function (Link $link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
154 17
            if ($link->getPrettyConstraint() !== 'self.version') {
155 20
                return $link;
156
            }
157 20
158 20
            if (isset($packages[$link->getSource()])) {
159
                /** @var  \Composer\Package\Link  $package */
160
                $package       = $packages[$link->getSource()];
161
                $version       = $package->getConstraint()->getPrettyString();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $version, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
162
                $prettyVersion = $package->getPrettyConstraint();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $prettyVersion, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
163
            }
164
165
            return new Link(
166
                $link->getSource(),
167
                $link->getTarget(),
168 20
                $vp->parseConstraints($version),
169
                $linkType['description'],
170
                $prettyVersion
171
            );
172
        }, $links);
173
    }
174 20
175
    /**
176 20
     * Get a full featured Package from a RootPackageInterface.
177 20
     *
178 16
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
179
     * @param  string                                                                $method
180 20
     *
181
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
182 4
     */
183 17
    protected static function unwrapIfNeeded(
184 19
        RootPackageInterface $root, $method = 'setExtra'
185
    ) {
186 20
        return ($root instanceof RootAliasPackage && ! method_exists($root, $method))
187 20
            ? $root->getAliasOf()
188
            : $root;
189
    }
190
}
191