Completed
Push — master ( f657d9...210110 )
by ARCANEDEV
8s
created

Package::getLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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