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

Package::mergeExtra()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 12
Bugs 1 Features 0
Metric Value
c 12
b 1
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 3
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->getJson()['extra']['merge-plugin']['require'])
87
            ? $this->getJson()['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->getJson()['extra']['merge-plugin']['include'])
99
            ? $this->getJson()['extra']['merge-plugin']['include']
100
            : [];
101
    }
102
103 135
    /**
104
     * Get composer.
105 135
     *
106
     * @return \Composer\Composer
107 135
     */
108 135
    public function getComposer()
109 135
    {
110 135
        return $this->composer;
111 135
    }
112 135
113 135
    /**
114
     * Get the Logger.
115 135
     *
116 129
     * @return \Arcanedev\Composer\Utilities\Logger
117
     */
118 30
    public function getLogger()
119 135
    {
120
        return $this->logger;
121
    }
122
123
    /**
124
     * Get the json.
125
     *
126
     * @return array
127 135
     */
128
    public function getJson()
129 135
    {
130 135
        return $this->json;
131 135
    }
132 135
133
    /* ------------------------------------------------------------------------------------------------
134
     |  Main Functions
135
     | ------------------------------------------------------------------------------------------------
136
     */
137
    /**
138
     * Merge this package into a RootPackage.
139
     *
140
     * @param  \Composer\Package\RootPackageInterface    $root
141 135
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
142
     */
143 135
    public function mergeInto(RootPackageInterface $root, PluginState $state)
144
    {
145 20
        $this->addRepositories($root, $state);
146 20
147
        $this->mergeRequires($root, $state);
148 20
        $this->mergeAutoload($root);
149 20
        $this->mergePackageLinks('conflict', $root);
150 16
        $this->mergePackageLinks('replace',  $root);
151
        $this->mergePackageLinks('provide',  $root);
152 20
        $this->mergeSuggests($root);
153 4
        $this->mergeExtra($root, $state);
154 17
155 20
        if ($state->isDevMode())
156
            $this->mergeDevInto($root, $state);
157 20
        else
158 20
            $this->mergeReferences($root);
159
    }
160
161
    /* ------------------------------------------------------------------------------------------------
162
     |  Other Functions
163
     | ------------------------------------------------------------------------------------------------
164
     */
165
    /**
166
     * Update Links with a 'self.version' constraint with the root package's version.
167
     *
168 20
     * @param  string                                  $type
169
     * @param  array                                   $links
170
     * @param  \Composer\Package\RootPackageInterface  $root
171
     *
172
     * @return array
173
     */
174 20
    protected function replaceSelfVersionDependencies(
175
        $type, array $links, RootPackageInterface $root
176 20
    ) {
177 20
        $linkType      = BasePackage::$supportedLinkTypes[$type];
178 16
        $version       = $root->getVersion();
179
        $prettyVersion = $root->getPrettyVersion();
180 20
        $vp            = $this->versionParser;
181
        $packages      = $root->{'get' . ucfirst($linkType['method'])}();
182 4
183 17
        return array_map(function (Link $link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
184 19
            if ($link->getPrettyConstraint() !== 'self.version') {
185
                return $link;
186 20
            }
187 20
188
            if (isset($packages[$link->getSource()])) {
189
                /** @var  \Composer\Package\Link  $package */
190
                $package       = $packages[$link->getSource()];
191
                $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...
192
                $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...
193
            }
194
195 135
            return new Link(
196
                $link->getSource(),
197 135
                $link->getTarget(),
198
                $vp->parseConstraints($version),
199 135
                $linkType['description'],
200
                $prettyVersion
201 80
            );
202
        }, $links);
203 80
    }
204 80
205 80
    /**
206 64
     * Get a full featured Package from a RootPackageInterface.
207
     *
208 80
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
209 80
     * @param  string                                                                $method
210 64
     *
211 80
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
212
     */
213 64
    protected static function unwrapIfNeeded(
214
        RootPackageInterface $root, $method = 'setExtra'
215 80
    ) {
216 80
        return ($root instanceof RootAliasPackage && ! method_exists($root, $method))
217
            ? $root->getAliasOf()
218
            : $root;
219
    }
220
}
221