AliasCollection::readFromFile()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
1
<?php
2
/*
3
 * This file is part of the slince/composer-alias package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Slince\ComposerAlias;
12
13
use Composer\Config\JsonConfigSource;
14
use Composer\Json\JsonFile;
15
use Composer\Util\Silencer;
16
17
class AliasCollection implements \IteratorAggregate
18
{
19
    /**
20
     * @var JsonFile
21
     */
22
    protected $configFile;
23
24
    /**
25
     * @var JsonConfigSource
26
     */
27
    protected $configSource;
28
29
    /**
30
     * @var array
31
     */
32
    protected $aliases;
33
34
    /**
35
     * @param JsonFile $configFile
36
     *
37
     * @throws \Exception
38
     */
39
    public function __construct(JsonFile $configFile)
40
    {
41
        $this->configFile = $configFile;
42
        $this->configSource = new JsonConfigSource($this->configFile);
43
        if (!$this->configFile->exists()) {
44
            touch($this->configFile->getPath());
45
            $this->configFile->write(['config' => new \ArrayObject()]);
46
            Silencer::call('chmod', $this->configFile->getPath(), 0600);
47
        }
48
    }
49
50
    /**
51
     * Gets all aliases.
52
     *
53
     * @return array
54
     */
55
    public function all()
56
    {
57
        $this->readFromFile();
58
59
        return $this->aliases;
60
    }
61
62
    /**
63
     * Add an alias.
64
     *
65
     * @param string $alias
66
     * @param string $raw
67
     *
68
     * @return static
69
     */
70
    public function add($alias, $raw)
71
    {
72
        $this->readFromFile();
73
        $this->aliases[$alias] = $raw;
74
        $this->configSource->addConfigSetting('_alias', $this->aliases);
0 ignored issues
show
Documentation introduced by
$this->aliases is of type array<string,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
        return $this;
77
    }
78
79
    /**
80
     * Removes an alias.
81
     *
82
     * @param string $alias
83
     *
84
     * @return $this
85
     */
86
    public function remove($alias)
87
    {
88
        $this->readFromFile();
89
        unset($this->aliases[$alias]);
90
        $this->configSource->addConfigSetting('_alias', $this->aliases);
0 ignored issues
show
Documentation introduced by
$this->aliases is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
92
        return $this;
93
    }
94
95
    /**
96
     * Gets the alias.
97
     *
98
     * @param string $alias
99
     *
100
     * @return string
101
     */
102
    public function get($alias)
103
    {
104
        $this->readFromFile();
105
106
        return isset($this->aliases[$alias]) ? $this->aliases[$alias] : null;
107
    }
108
109
    /**
110
     * Checks whether the alias exists.
111
     *
112
     * @param string $alias
113
     *
114
     * @return bool
115
     */
116
    public function has($alias)
117
    {
118
        return isset($this->aliases[$alias]);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getIterator()
125
    {
126
        return new \ArrayIterator($this->all());
127
    }
128
129
    protected function readFromFile()
130
    {
131
        if ($this->aliases) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aliases 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...
132
            return;
133
        }
134
        $config = $this->configFile->read();
135
        if (!isset($config['config']['_alias']) || !is_array($config['config']['_alias'])) {
136
            $aliases = [];
137
        } else {
138
            $aliases = $config['config']['_alias'];
139
        }
140
        $this->aliases = $aliases;
141
    }
142
}
143