Completed
Pull Request — master (#8)
by ARCANEDEV
04:13
created

Package::addRepository()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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