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

Package::mergePackageLinks()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.8571
cc 3
eloc 14
nc 3
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->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
     * 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
     |  Main Functions
115 135
     | ------------------------------------------------------------------------------------------------
116 129
     */
117
    /**
118 30
     * Merge this package into a RootPackage.
119 135
     *
120
     * @param  \Composer\Package\RootPackageInterface    $root
121
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
122
     */
123
    public function mergeInto(RootPackageInterface $root, PluginState $state)
124
    {
125
        $this->addRepositories($root, $state);
126
127 135
        $this->mergeRequires($root, $state);
128
        $this->mergeAutoload($root);
129 135
        $this->mergePackageLinks('conflict', $root);
130 135
        $this->mergePackageLinks('replace',  $root);
131 135
        $this->mergePackageLinks('provide',  $root);
132 135
        $this->mergeSuggests($root);
133
        $this->mergeExtra($root, $state);
134
135
        if ($state->isDevMode())
136
            $this->mergeDevInto($root, $state);
137
        else
138
            $this->mergeReferences($root);
139
    }
140
141 135
    /* ------------------------------------------------------------------------------------------------
142
     |  Other Functions
143 135
     | ------------------------------------------------------------------------------------------------
144
     */
145 20
    /**
146 20
     * Update Links with a 'self.version' constraint with the root package's version.
147
     *
148 20
     * @param  string                                  $type
149 20
     * @param  array                                   $links
150 16
     * @param  \Composer\Package\RootPackageInterface  $root
151
     *
152 20
     * @return array
153 4
     */
154 17
    protected function replaceSelfVersionDependencies(
155 20
        $type, array $links, RootPackageInterface $root
156
    ) {
157 20
        $linkType      = BasePackage::$supportedLinkTypes[$type];
158 20
        $version       = $root->getVersion();
159
        $prettyVersion = $root->getPrettyVersion();
160
        $vp            = $this->versionParser;
161
        $packages      = $root->{'get' . ucfirst($linkType['method'])}();
162
163
        return array_map(function (Link $link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
164
            if ($link->getPrettyConstraint() !== 'self.version') {
165
                return $link;
166
            }
167
168 20
            if (isset($packages[$link->getSource()])) {
169
                /** @var  \Composer\Package\Link  $package */
170
                $package       = $packages[$link->getSource()];
171
                $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...
172
                $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...
173
            }
174 20
175
            return new Link(
176 20
                $link->getSource(),
177 20
                $link->getTarget(),
178 16
                $vp->parseConstraints($version),
179
                $linkType['description'],
180 20
                $prettyVersion
181
            );
182 4
        }, $links);
183 17
    }
184 19
185
    /**
186 20
     * Get a full featured Package from a RootPackageInterface.
187 20
     *
188
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
189
     * @param  string                                                                $method
190
     *
191
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
192
     */
193
    protected static function unwrapIfNeeded(
194
        RootPackageInterface $root, $method = 'setExtra'
195 135
    ) {
196
        return ($root instanceof RootAliasPackage && ! method_exists($root, $method))
197 135
            ? $root->getAliasOf()
198
            : $root;
199 135
    }
200
}
201