Completed
Push — master ( a280db...0a4184 )
by ARCANEDEV
10s
created

Package::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
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\ScriptsTrait,
30
        PackageTraits\DevTrait,
31
        PackageTraits\ReferencesTrait;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Properties
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /** @var \Composer\Composer $composer */
38
    protected $composer;
39
40
    /** @var \Arcanedev\Composer\Utilities\Logger $logger */
41
    protected $logger;
42
43
    /** @var \Composer\Package\CompletePackage $package */
44
    protected $package;
45
46
    /** @var string $path */
47
    protected $path;
48
49
    /** @var \Composer\Package\Version\VersionParser $versionParser */
50
    protected $versionParser;
51
52
    /** @var array $json */
53
    protected $json;
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Constructor
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Make a Package instance.
61
     *
62
     * @param  string                                $path
63
     * @param  \Composer\Composer                    $composer
64
     * @param  \Arcanedev\Composer\Utilities\Logger  $logger
65
     */
66 93
    public function __construct($path, Composer $composer, Logger $logger)
67
    {
68 93
        $this->path          = $path;
69 93
        $this->composer      = $composer;
70 93
        $this->logger        = $logger;
71 93
        $this->json          = PackageJson::read($path);
72 93
        $this->package       = PackageJson::convert($this->json);
73 93
        $this->versionParser = new VersionParser;
74 93
    }
75
76
    /* ------------------------------------------------------------------------------------------------
77
     |  Getters & Setters
78
     | ------------------------------------------------------------------------------------------------
79
     */
80
    /**
81
     * Get list of additional packages to require if precessing recursively.
82
     *
83
     * @return array
84
     */
85 90
    public function getRequires()
86
    {
87 90
        return isset($this->getJson()['extra']['merge-plugin']['require'])
88 30
            ? $this->getJson()['extra']['merge-plugin']['require']
89 90
            : [];
90
    }
91
92
    /**
93
     * Get list of additional packages to include if precessing recursively.
94
     *
95
     * @return array
96
     */
97 90
    public function getIncludes()
98
    {
99 90
        return isset($this->getJson()['extra']['merge-plugin']['include'])
100 40
            ? $this->getJson()['extra']['merge-plugin']['include']
101 90
            : [];
102
    }
103
104
    /**
105
     * Get composer.
106
     *
107
     * @return \Composer\Composer
108
     */
109 12
    public function getComposer()
110
    {
111 12
        return $this->composer;
112
    }
113
114
    /**
115
     * Get the Logger.
116
     *
117
     * @return \Arcanedev\Composer\Utilities\Logger
118
     */
119 54
    public function getLogger()
120
    {
121 54
        return $this->logger;
122
    }
123
124
    /**
125
     * Get the json.
126
     *
127
     * @return array
128
     */
129 93
    public function getJson()
130
    {
131 93
        return $this->json;
132
    }
133
134
    /**
135
     * Get the package.
136
     *
137
     * @return \Composer\Package\CompletePackage $package
138
     */
139 93
    public function getPackage()
140
    {
141 93
        return $this->package;
142
    }
143
144
    /**
145
     * Get the path.
146
     *
147
     * @return string
148
     */
149 12
    public function getPath()
150
    {
151 12
        return $this->path;
152
    }
153
154
    /* ------------------------------------------------------------------------------------------------
155
     |  Main Functions
156
     | ------------------------------------------------------------------------------------------------
157
     */
158
    /**
159
     * Merge this package into a RootPackage.
160
     *
161
     * @param  \Composer\Package\RootPackageInterface    $root
162
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
163
     */
164 93
    public function mergeInto(RootPackageInterface $root, PluginState $state)
165
    {
166 93
        $this->prependRepositories($root);
167 93
        $this->mergeRequires($root, $state);
168 93
        $this->mergeAutoload($root);
169 93
        $this->mergePackageLinks('conflict', $root);
170 93
        $this->mergePackageLinks('replace',  $root);
171 93
        $this->mergePackageLinks('provide',  $root);
172 93
        $this->mergeSuggests($root);
173 93
        $this->mergeExtra($root, $state);
174 93
        $this->mergeScripts($root, $state);
175
176 93
        $state->isDevMode()
177 79
            ? $this->mergeDevInto($root, $state)
178 45
            : $this->mergeReferences($root);
179 93
    }
180
181
    /* ------------------------------------------------------------------------------------------------
182
     |  Other Functions
183
     | ------------------------------------------------------------------------------------------------
184
     */
185
    /**
186
     * Update Links with a 'self.version' constraint with the root package's version.
187
     *
188
     * @param  string                                  $type
189
     * @param  array                                   $links
190
     * @param  \Composer\Package\RootPackageInterface  $root
191
     *
192
     * @return array
193
     */
194 57
    protected function replaceSelfVersionDependencies(
195
        $type, array $links, RootPackageInterface $root
196
    ) {
197 57
        $linkType      = BasePackage::$supportedLinkTypes[$type];
198 57
        $version       = $root->getVersion();
199 57
        $prettyVersion = $root->getPrettyVersion();
200 57
        $vp            = $this->versionParser;
201 57
        $packages      = $root->{'get' . ucfirst($linkType['method'])}();
202
203 57
        return array_map(function (Link $link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
204 57
            if ($link->getPrettyConstraint() !== 'self.version') {
205 57
                return $link;
206
            }
207
208 9
            if (isset($packages[$link->getSource()])) {
209
                /** @var  \Composer\Package\Link  $package */
210 6
                $package       = $packages[$link->getSource()];
211 6
                $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...
212 6
                $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...
213 2
            }
214
215 9
            return new Link(
216 9
                $link->getSource(),
217 9
                $link->getTarget(),
218 9
                $vp->parseConstraints($version),
219 9
                $linkType['description'],
220
                $prettyVersion
221 3
            );
222 57
        }, $links);
223
    }
224
225
    /**
226
     * Get a full featured Package from a RootPackageInterface.
227
     *
228
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
229
     * @param  string                                                                $method
230
     *
231
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
232
     */
233 93
    protected static function unwrapIfNeeded(
234
        RootPackageInterface $root, $method = 'setExtra'
235
    ) {
236 93
        return ($root instanceof RootAliasPackage && ! method_exists($root, $method))
237 35
            ? $root->getAliasOf()
238 93
            : $root;
239
    }
240
}
241