Composer::forceAutoload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Util;
13
14
class Composer extends Executable
15
{
16
    /** @var array Content of composer.json decoded */
17
    protected $config = [];
18
19
    /** @var string The binary executable */
20
    protected $binary = 'composer';
21
22
    public function createProject($project, $using)
23
    {
24
        $this->runCommand(sprintf('create-project %s %s', $using, $project));
25
26
        return $this;
27
    }
28
29
    public function install()
30
    {
31
        $this->runCommand('install --prefer-dist --optimize-autoloader --no-suggest');
32
33
        return $this;
34
    }
35
36
    public function update()
37
    {
38
        $this->runCommand('update --prefer-dist --optimize-autoloader --no-suggest');
39
40
        return $this;
41
    }
42
43
    public function dumpAutoload()
44
    {
45
        $this->runCommand('dump-autoload --optimize');
46
47
        return $this;
48
    }
49
50
    public function config(string $key, $default = null)
51
    {
52
        if (!$this->config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
            $this->config = (new Path)->readAsJson($this->workDir . '/composer.json');
54
        }
55
56
        $temp = $this->config;
57
        foreach (\explode('.', $key) as $part) {
58
            if (\is_array($temp) && \array_key_exists($part, $temp)) {
59
                $temp = $temp[$part];
60
            } else {
61
                return $default;
62
            }
63
        }
64
65
        return $temp;
66
    }
67
68
    public function forceAutoload()
69
    {
70
        $autoloader = $this->workDir . '/vendor/autoload.php';
71
72
        if (\array_search($autoloader, \get_included_files()) === false) {
73
            require_once $autoloader;
74
        }
75
    }
76
}
77