PluginState::isDevMode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\Composer\Entities;
2
3
use Composer\Composer;
4
5
/**
6
 * Class     PluginState
7
 *
8
 * @package  Arcanedev\Composer\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class PluginState
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var \Composer\Composer */
19
    protected $composer;
20
21
    /** @var array */
22
    protected $includes = [];
23
24
    /** @var array */
25
    protected $requires = [];
26
27
    /** @var array */
28
    protected $duplicateLinks = [];
29
30
    /** @var bool */
31
    protected $devMode = false;
32
33
    /** @var bool */
34
    protected $recurse = true;
35
36
    /** @var bool */
37
    protected $replace = false;
38
39
    /** @var bool $ignore */
40
    protected $ignore = false;
41
42
    /**
43
     * Whether to merge the -dev sections.
44
     *
45
     * @var bool
46
     */
47
    protected $mergeDev = true;
48
49
    /**
50
     * Whether to merge the extra section.
51
     *
52
     * By default, the extra section is not merged and there will be many cases where
53
     * the merge of the extra section is performed too late to be of use to other plugins.
54
     * When enabled, merging uses one of two strategies - either 'first wins' or 'last wins'.
55
     * When enabled, 'first wins' is the default behaviour. If Replace mode is activated
56
     * then 'last wins' is used.
57
     *
58
     * @var bool
59
     */
60
    protected $mergeExtra = false;
61
62
    /**
63
     * Whether to merge the scripts section.
64
     *
65
     * @var bool $mergeScripts
66
     */
67
    protected $mergeScripts = false;
68
69
    /**
70
     * Whether to merge the extra section in a deep / recursive way.
71
     *
72
     * By default the extra section is merged with array_merge() and duplicate keys are ignored.
73
     * When enabled this allows to merge the arrays recursively using the following rule:
74
     *   Integer keys are merged, while array values are replaced where the later values overwrite the former.
75
     *
76
     * This is useful especially for the extra section when plugins use larger structures like a 'patches' key with
77
     * the packages as sub-keys and the patches as values.
78
     *
79
     * When 'replace' mode is activated the order of array merges is exchanged.
80
     *
81
     * @var bool
82
     */
83
    protected $mergeExtraDeep = false;
84
85
    /** @var bool */
86
    protected $firstInstall = false;
87
88
    /** @var bool */
89
    protected $locked = false;
90
91
    /** @var bool */
92
    protected $dumpAutoloader = false;
93
94
    /** @var bool */
95
    protected $optimizeAutoloader = false;
96
97
    /* -----------------------------------------------------------------
98
     |  Constructor
99
     | -----------------------------------------------------------------
100
     */
101
102
    /**
103
     * Make PluginState instance.
104
     *
105
     * @param  \Composer\Composer  $composer
106
     */
107 123
    public function __construct(Composer $composer)
108
    {
109 123
        $this->composer = $composer;
110 123
    }
111
112
    /* -----------------------------------------------------------------
113
     |  Getters & Setters
114
     | -----------------------------------------------------------------
115
     */
116
117
    /**
118
     * Get list of filenames and/or glob patterns to include.
119
     *
120
     * @return array
121
     */
122 102
    public function getIncludes()
123
    {
124 102
        return $this->includes;
125
    }
126
127
    /**
128
     * Set the first install flag.
129
     *
130
     * @param  bool  $flag
131
     *
132
     * @return self
133
     */
134 6
    public function setFirstInstall($flag)
135
    {
136 6
        $this->firstInstall = (bool) $flag;
137
138 6
        return $this;
139
    }
140
141
    /**
142
     * Is this the first time that the plugin has been installed ?
143
     *
144
     * @return bool
145
     */
146 108
    public function isFirstInstall()
147
    {
148 108
        return $this->firstInstall;
149
    }
150
151
    /**
152
     * Set the locked flag.
153
     *
154
     * @param  bool  $flag
155
     *
156
     * @return self
157
     */
158 9
    public function setLocked($flag)
159
    {
160 9
        $this->locked = (bool) $flag;
161
162 9
        return $this;
163
    }
164
165
    /**
166
     * Was a lockfile present when the plugin was installed ?
167
     *
168
     * @return bool
169
     */
170 12
    public function isLocked()
171
    {
172 12
        return $this->locked;
173
    }
174
175
    /**
176
     * Should an update be forced ?
177
     *
178
     * @return bool
179
     */
180 3
    public function forceUpdate()
181
    {
182 3
        return ! $this->isLocked();
183
    }
184
185
    /**
186
     * Set the devMode flag.
187
     *
188
     * @param  bool  $flag
189
     *
190
     * @return self
191
     */
192 102
    public function setDevMode($flag)
193
    {
194 102
        $this->devMode = (bool) $flag;
195
196 102
        return $this;
197
    }
198
199
    /**
200
     * Should devMode settings be processed ?
201
     *
202
     * @return bool
203
     */
204 99
    public function isDevMode()
205
    {
206 99
        return $this->shouldMergeDev() && $this->devMode;
207
    }
208
209
    /**
210
     * Should devMode settings be merged?
211
     *
212
     * @return bool
213
     */
214 99
    public function shouldMergeDev()
215
    {
216 99
        return $this->mergeDev;
217
    }
218
219
    /**
220
     * Set the dumpAutoloader flag.
221
     *
222
     * @param  bool  $flag
223
     *
224
     * @return self
225
     */
226 102
    public function setDumpAutoloader($flag)
227
    {
228 102
        $this->dumpAutoloader = (bool) $flag;
229
230 102
        return $this;
231
    }
232
233
    /**
234
     * Is the autoloader file supposed to be written out ?
235
     *
236
     * @return bool
237
     */
238 3
    public function shouldDumpAutoloader()
239
    {
240 3
        return $this->dumpAutoloader;
241
    }
242
243
    /**
244
     * Set the optimizeAutoloader flag.
245
     *
246
     * @param  bool  $flag
247
     *
248
     * @return self
249
     */
250 102
    public function setOptimizeAutoloader($flag)
251
    {
252 102
        $this->optimizeAutoloader = (bool) $flag;
253
254 102
        return $this;
255
    }
256
257
    /**
258
     * Should the autoloader be optimized ?
259
     *
260
     * @return bool
261
     */
262 3
    public function shouldOptimizeAutoloader()
263
    {
264 3
        return $this->optimizeAutoloader;
265
    }
266
267
    /**
268
     * Add duplicate packages.
269
     *
270
     * @param  string  $type
271
     * @param  array   $packages
272
     *
273
     * @return self
274
     */
275 54
    public function addDuplicateLinks($type, array $packages)
276
    {
277 54
        if ( ! isset($this->duplicateLinks[$type])) {
278 54
            $this->duplicateLinks[$type] = [];
279
        }
280
281 54
        $this->duplicateLinks[$type] = array_merge(
282 54
            $this->duplicateLinks[$type],
283 36
            $packages
284
        );
285
286 54
        return $this;
287
    }
288
289
    /**
290
     * Should includes be recursively processed ?
291
     *
292
     * @return bool
293
     */
294 99
    public function recurseIncludes()
295
    {
296 99
        return $this->recurse;
297
    }
298
299
    /**
300
     * Get list of filenames and/or glob patterns to require
301
     *
302
     * @return array
303
     */
304 102
    public function getRequires()
305
    {
306 102
        return $this->requires;
307
    }
308
309
    /**
310
     * Get duplicate packages.
311
     *
312
     * @param  string  $type
313
     *
314
     * @return array
315
     */
316 99
    public function getDuplicateLinks($type)
317
    {
318 99
        return $this->duplicateLinks[$type] ?? [];
319
    }
320
321
    /**
322
     * Should duplicate links be replaced in a 'last definition wins' order ?
323
     *
324
     * @return bool
325
     */
326 51
    public function replaceDuplicateLinks()
327
    {
328 51
        return $this->replace;
329
    }
330
331
    /**
332
     * Should duplicate links be ignored?
333
     *
334
     * @return bool
335
     */
336 54
    public function ignoreDuplicateLinks()
337
    {
338 54
        return $this->ignore;
339
    }
340
341
    /**
342
     * Should the extra section be merged ?
343
     *
344
     * By default, the extra section is not merged and there will be many cases where
345
     * the merge of the extra section is performed too late to be of use to other plugins.
346
     * When enabled, merging uses one of two strategies - either 'first wins' or 'last wins'.
347
     * When enabled, 'first wins' is the default behaviour. If Replace mode is activated
348
     * then 'last wins' is used.
349
     *
350
     * @return bool
351
     */
352 99
    public function shouldMergeExtra()
353
    {
354 99
        return $this->mergeExtra;
355
    }
356
357
    /**
358
     * Should the extra section be merged deep / recursively?
359
     *
360
     * @return bool
361
     */
362 18
    public function shouldMergeExtraDeep()
363
    {
364 18
        return $this->mergeExtraDeep;
365
    }
366
367
    /**
368
     * Should the scripts section be merged?
369
     *
370
     * @return bool
371
     */
372 99
    public function shouldMergeScripts()
373
    {
374 99
        return $this->mergeScripts;
375
    }
376
377
    /* ------------------------------------------------------------------------------------------------
378
     |  Main Functions
379
     | ------------------------------------------------------------------------------------------------
380
     */
381
    /**
382
     * Load plugin settings.
383
     */
384 102
    public function loadSettings()
385
    {
386 102
        $extra                = $this->composer->getPackage()->getExtra();
387 102
        $config               = static::mergeConfig($extra);
0 ignored issues
show
Bug introduced by
Since mergeConfig() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of mergeConfig() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
388 102
        $this->includes       = is_array($config['include']) ? $config['include'] : [$config['include']];
389 102
        $this->requires       = is_array($config['require']) ? $config['require'] : [$config['require']];
390 102
        $this->recurse        = (bool) $config['recurse'];
391 102
        $this->replace        = (bool) $config['replace'];
392 102
        $this->ignore         = (bool) $config['ignore-duplicates'];
393 102
        $this->mergeDev       = (bool) $config['merge-dev'];
394 102
        $this->mergeExtra     = (bool) $config['merge-extra'];
395 102
        $this->mergeExtraDeep = (bool) $config['merge-extra-deep'];
396 102
        $this->mergeScripts   = (bool) $config['merge-scripts'];
397 102
    }
398
399
    /* -----------------------------------------------------------------
400
     |  Other Methods
401
     | -----------------------------------------------------------------
402
     */
403
404
    /**
405
     * Merge config.
406
     *
407
     * @param  array  $extra
408
     *
409
     * @return array
410
     */
411 102
    private static function mergeConfig(array $extra)
412
    {
413 102
        return array_merge([
414 102
            'include'              => [],
415
            'require'              => [],
416
            'recurse'              => true,
417
            'replace'              => false,
418
            'ignore-duplicates'    => false,
419
            'prepend-repositories' => false,
420
            'merge-dev'            => true,
421
            'merge-extra'          => false,
422
            'merge-extra-deep'     => false,
423
            'merge-scripts'        => false,
424 102
        ], $extra['merge-plugin'] ?? []);
425
    }
426
}
427