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.
Test Failed
Pull Request — master (#3)
by
unknown
03:22
created

IniFile::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 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 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 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
     * @param string $sectionName
137
     *
138
     * @return $this
139
     * @throws InvalidDataException
140
     */
141 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...
142
    {
143
        if (!$this->hasSection($sectionName))
144
        {
145 8
            throw new InvalidDataException(sprintf('Section "%s" does not exists!', $sectionName));
146
        }
147 8
        unset($this->sections[$sectionName]);
148
        return $this;
149 7
    }
150
151
    /**
152
     * Get normalized item value
153
     *
154
     * @param string $sectionName
155
     * @param string $itemName
156
     * @param mixed $defaultValue
157
     *
158
     * @return mixed
159
     * @throws InvalidDataException
160
     */
161
    public function get($sectionName, $itemName, $defaultValue = null)
162
    {
163
        $section = $this->getSection($sectionName);
164
165
        return $section->get($itemName, $defaultValue);
166
    }
167
168
    /**
169
     * @param string $sectionName
170
     * @param string $itemName
171 3
     * @param string $itemValue
172
     *
173 3
     * @return $this
174 3
     * @throws InvalidDataException
175
     */
176 1
    public function set($sectionName, $itemName, $itemValue)
177 3
    {
178
        $section = $this->getSection($sectionName);
179 3
        $section->set($itemName, $itemValue);
180
181
        return $this;
182
    }
183
184
    /**
185 2
     * @param string $sectionName
186
     * @param string $itemName
187 2
     *
188 2
     * @return $this
189
     * @throws InvalidDataException
190
     */
191 2
    public function delete($sectionName, $itemName)
192
    {
193 2
        if (!$this->hasSection($sectionName))
194
        {
195
            throw new InvalidDataException(sprintf('Section "%s" does not exists!', $sectionName));
196
        }
197
        $section = $this->getSection($sectionName);
198
        $section->delete($itemName);
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Retrinko\Ini\IniSection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
        return $this;
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public function toArray()
206
    {
207
        $data = [];
208
        foreach ($this->sections as $sectionName => $section)
209
        {
210
            $data[$sectionName] = $section->toArray();
211
        }
212
213
        return $data;
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function toString()
220
    {
221
        $contents = [];
222
        foreach ($this->sections as $section)
223
        {
224
            $contents[] = $section->toString();
225
        }
226
227
        return implode(PHP_EOL, $contents);
228
    }
229
}
230