GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 430bba...26150f )
by Miguel A.
03:06
created

IniFile   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 60.29%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 188
ccs 41
cts 68
cp 0.6029
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A load() 0 4 1
B save() 0 22 6
A addSection() 0 21 4
A hasSection() 0 4 1
A getSection() 0 9 2
A get() 0 6 1
A set() 0 7 1
A toArray() 0 10 2
A toString() 0 10 2
1
<?php
2
3
namespace Retrinko\Ini;
4
5
use Retrinko\Ini\Exceptions\FileException;
6
use Retrinko\Ini\Exceptions\InvalidDataException;
7
8
class IniFile
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $file;
14
    /**
15
     * @var IniParser;
16
     */
17
    protected $parser;
18
    /**
19
     * @var IniSection[]
20
     */
21
    protected $sections = [];
22
23
    /**
24
     * IniFile constructor.
25
     *
26
     * @param string|null $file
27
     *
28
     * @throws FileException
29
     */
30 16
    public function __construct($file = null)
31
    {
32 16
        $rawContents = '';
33 16
        $this->parser = IniParser::i();
34 16
        if (!is_null($file))
35 16
        {
36 11
            $this->file = IniFileLocator::i()->locate($file);
37 10
            $rawContents = file_get_contents($this->file);
38 10
        }
39 15
        $this->sections = $this->parser->parseIniString($rawContents);
40 15
    }
41
42
    /**
43
     * @param string $file
44
     *
45
     * @return IniFile
46
     * @throws FileException
47
     */
48
    public static function load($file)
49
    {
50
        return new self($file);
51
    }
52
53
    /**
54
     * @param null|string $outputFile
55
     *
56
     * @throws FileException
57
     */
58
    public function save($outputFile = null)
59
    {
60
        if (is_null($outputFile))
61
        {
62
            if (is_null($this->file))
63
            {
64
                throw new FileException('No output file set! Please, set an output file.');
65
            }
66
            $outputFile = $this->file;
67
        }
68
69
        if(is_file($outputFile) && !is_writable($outputFile))
70
        {
71
            throw new FileException(sprintf('Output file "%s" is not writable!', $outputFile));
72
        }
73
74
        $result = file_put_contents($outputFile, $this->toString());
75
        if (false === $result)
76
        {
77
            throw new FileException(sprintf('Error writing file "%s"!', $outputFile));
78
        }
79
    }
80
81
    /**
82
     * @param IniSection $section
83
     *
84
     * @return $this
85
     * @throws InvalidDataException
86
     */
87 1
    public function addSection(IniSection $section)
88
    {
89 1
        if ($this->hasSection($section->getName()))
90 1
        {
91
            throw new InvalidDataException(sprintf('Section "%s" already exists!',
92
                                                   $section->getName()));
93
        }
94
95 1
        if ($section->hasParent())
96 1
        {
97 1
            if (!isset($this->sections[$section->getParent()->getName()]))
98 1
            {
99
                throw new InvalidDataException(sprintf('Parent section "%s" does not exists!',
100
                                                     $section->getParent()->getName()));
101
            }
102 1
        }
103
104 1
        $this->sections[$section->getName()] = $section;
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * @param string $sectionName
111
     *
112
     * @return bool
113
     */
114 8
    public function hasSection($sectionName)
115
    {
116 8
        return isset($this->sections[$sectionName]);
117
    }
118
119
    /**
120
     * @param string $sectionName
121
     *
122
     * @return IniSection
123
     * @throws InvalidDataException
124
     */
125 8
    public function getSection($sectionName)
126
    {
127 8
        if (!$this->hasSection($sectionName))
128 8
        {
129 1
            throw new InvalidDataException(sprintf('Section "%s" does not exists!', $sectionName));
130
        }
131
132 7
        return $this->sections[$sectionName];
133
    }
134
135
    /**
136
     * Get normalized item value
137
     *
138
     * @param string $sectionName
139
     * @param string $itemName
140
     * @param mixed $defaultValue
141
     *
142
     * @return mixed
143
     * @throws InvalidDataException
144
     */
145 8
    public function get($sectionName, $itemName, $defaultValue = null)
146
    {
147 8
        $section = $this->getSection($sectionName);
148
149 7
        return $section->get($itemName, $defaultValue);
150
    }
151
152
    /**
153
     * @param string $sectionName
154
     * @param string $itemName
155
     * @param string $itemValue
156
     *
157
     * @return $this
158
     * @throws InvalidDataException
159
     */
160
    public function set($sectionName, $itemName, $itemValue)
161
    {
162
        $section = $this->getSection($sectionName);
163
        $section->set($itemName, $itemValue);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return array
170
     */
171 3
    public function toArray()
172
    {
173 3
        $data = [];
174 3
        foreach ($this->sections as $sectionName => $section)
175
        {
176 1
            $data[$sectionName] = $section->toArray();
177 3
        }
178
179 3
        return $data;
180
    }
181
182
    /**
183
     * @return string
184
     */
185 2
    public function toString()
186
    {
187 2
        $contents = [];
188 2
        foreach ($this->sections as $section)
189
        {
190
            $contents[] = $section->toString();
191 2
        }
192
193 2
        return implode(PHP_EOL, $contents);
194
    }
195
}