Passed
Branch master (432af5)
by compolom
04:49 queued 02:52
created

IniObject::removeSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 5
cp 0.6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.2559
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\IniObject;
6
7
use InvalidArgumentException;
8
use SplFileObject;
9
10
class IniObject
11
{
12
    private $filename;
13
14
    protected $config;
15
16
    private $sections;
17
18
    /**
19
     * IniObject constructor.
20
     *
21
     * @param string|null $filename
22
     * @param array $config
23
     */
24 10
    public function __construct(?string $filename = null, array $config = [])
25
    {
26 10
        $data = false;
27
28 10
        if (! count($config)) {
29 10
            $this->initDefaultConfig();
30
        }
31 10
        if ($filename && file_exists($filename)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32 10
            $data = parse_ini_file(
33 10
                $filename,
34 10
                true,
35 10
                $this->config['strict'] ? INI_SCANNER_TYPED : INI_SCANNER_NORMAL
36
            );
37
        }
38 10
        if ($data) {
39 10
            $this->sectionLoad($data);
40 10
            $this->setFilename($filename);
41
        }
42 10
    }
43
44
    /**
45
     * @param array $data
46
     * @throws InvalidArgumentException
47
     */
48 10
    private function sectionLoad(array $data): void
49
    {
50 10
        if (! count($data)) {
51
            throw new InvalidArgumentException('Data is not set');
52
        }
53
54 10
        $sections = [];
55 10
        foreach ($data as $sectionName => $params) {
56 10
            $sections[$sectionName] = new Section($sectionName, $params);
57
        }
58
59 10
        $this->sections = $sections;
60 10
    }
61
62
    /**
63
     * @param string $name
64
     * @return Section
65
     */
66 6
    public function getSection(string $name): Section
67
    {
68 6
        if (! isset($this->sections[$name])) {
69 1
            throw new InvalidArgumentException('Section not found');
70
        }
71
72 6
        return $this->sections[$name];
73
    }
74
75
    /**
76
     * @param string $name
77
     */
78 1
    public function removeSection(string $name): void
79
    {
80 1
        if (! isset($this->sections[$name])) {
81 1
            throw new InvalidArgumentException('Section not found for remove');
82
        }
83
        unset($this->sections[$name]);
84
    }
85
86 3
    public function addSection(string $name, array $section): void
87
    {
88 3
        if (isset($this->sections[$name])) {
89 1
            throw new InvalidArgumentException('Overwrite section denied');
90
        }
91 3
        $this->sections[$name] = new Section($name, $section);
92 3
    }
93
94 1
    public function updateSection(string $name, array $section): void
95
    {
96 1
        if (! isset($this->sections[$name])) {
97 1
            throw new InvalidArgumentException('Section not found for update');
98
        }
99 1
        $this->sections[$name] = new Section($name, $section);
100 1
    }
101
102
    /**
103
     * default config
104
     */
105 10
    private function initDefaultConfig(): void
106
    {
107 10
        $this->config = [
108
            'strict'    => false,
109
            'overwrite' => true,
110
        ];
111 10
    }
112
113
    /**
114
     * @param string $filename
115
     */
116 10
    private function setFilename(?string $filename): void
117
    {
118 10
        $this->filename = $filename;
119 10
    }
120
121 1
    public function __toString()
122
    {
123 1
        $return = '';
124
125 1
        foreach ($this->sections as $section) {
126 1
            $return .= $section;
127
        }
128
129 1
        return trim($return) . PHP_EOL;
130
    }
131
132
    /**
133
     * @param string|null $filename
134
     * @return bool
135
     */
136 1
    public function save(string $filename = null): bool
137
    {
138 1
        if (! $this->config['overwrite'] && file_exists($filename)) {
139 1
            throw new InvalidArgumentException('Overwrite file protection');
140
        }
141 1
        if ($filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142 1
            $this->setFilename($filename);
143
        }
144
145 1
        return (bool) (new SPLFileObject($this->filename, 'w+b'))->fwrite((string) $this);
146
    }
147
}
148