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
Pull Request — master (#4)
by Flemming
02:51
created

IniFile::save()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 0
cts 16
cp 0
rs 8.6737
cc 6
eloc 10
nc 7
nop 1
crap 42
1
<?php
2
3
namespace FlmBus\Ini;
4
5
use FlmBus\Ini\Exceptions\FileException;
6
use FlmBus\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 21
    public function __construct($file = null)
31
    {
32 21
        $rawContents = '';
33 21
        $this->parser = IniParser::i();
34 21
        if (!is_null($file))
35 21
        {
36 16
            $this->file = IniFileLocator::i()->locate($file);
37 15
            $rawContents = file_get_contents($this->file);
38 15
        }
39 20
        $this->sections = $this->parser->parseIniString($rawContents);
40 20
    }
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 13
    public function hasSection($sectionName)
115
    {
116 13
        return isset($this->sections[$sectionName]);
117
    }
118
119
    /**
120
     * @param string $sectionName
121
     *
122
     * @return IniSection
123
     * @throws InvalidDataException
124
     */
125 13 View Code Duplication
    public function getSection($sectionName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127 13
        if (!$this->hasSection($sectionName))
128 13
        {
129 3
            throw new InvalidDataException(sprintf('Section "%s" does not exists!', $sectionName));
130
        }
131
132 10
        return $this->sections[$sectionName];
133
    }
134
135
    /**
136
     * @param string $sectionName
137
     *
138
     * @throws InvalidDataException
139
     */
140 View Code Duplication
    public function deleteSection($sectionName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        if (!$this->hasSection($sectionName))
143
        {
144
            throw new InvalidDataException(sprintf('Section "%s" does not exists!', $sectionName));
145
        }
146
147
        unset($this->sections[$sectionName]);
148
    }
149
150
    /**
151
     * Get normalized item value
152
     *
153
     * @param string $sectionName
154
     * @param string $itemName
155
     * @param mixed $defaultValue
156
     *
157
     * @return mixed
158
     * @throws InvalidDataException
159
     */
160 8
    public function get($sectionName, $itemName, $defaultValue = null)
161
    {
162 8
        $section = $this->getSection($sectionName);
163
164 7
        return $section->get($itemName, $defaultValue);
165
    }
166
167
    /**
168
     * @param string $sectionName
169
     * @param string $itemName
170
     * @param string $itemValue
171
     *
172
     * @return $this
173
     * @throws InvalidDataException
174
     */
175
    public function set($sectionName, $itemName, $itemValue)
176
    {
177
        $section = $this->getSection($sectionName);
178
        $section->set($itemName, $itemValue);
179
180
        return $this;
181
    }
182
183
    /**
184
     * @param string $sectionName
185
     * @param string $itemName
186
     *
187
     * @return $this
188
     * @throws InvalidDataException
189
     */
190 5
    public function delete($sectionName, $itemName)
191
    {
192 5
        $section = $this->getSection($sectionName);
193 3
        $section->delete($itemName);
194
195 2
        return $this;
196
    }
197
198
    /**
199
     * @return array
200
     */
201 3
    public function toArray()
202
    {
203 3
        $data = [];
204 3
        foreach ($this->sections as $sectionName => $section)
205
        {
206 1
            $data[$sectionName] = $section->toArray();
207 3
        }
208
209 3
        return $data;
210
    }
211
212
    /**
213
     * @return string
214
     */
215 2
    public function toString()
216
    {
217 2
        $contents = [];
218 2
        foreach ($this->sections as $section)
219
        {
220
            $contents[] = $section->toString();
221 2
        }
222
223 2
        return implode(PHP_EOL, $contents);
224
    }
225
}