Passed
Branch master (5e7742)
by compolom
03:18 queued 01:36
created

IniObject::getSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
        $sections = [];
51 10
        foreach ($data as $sectionName => $params) {
52 10
            $sections[$sectionName] = new Section($sectionName, $params);
53
        }
54 10
        $this->sections = $sections;
55 10
    }
56
57
    /**
58
     * @param string $name
59
     * @return Section
60
     */
61 6
    public function getSection(string $name): Section
62
    {
63 6
        if (! isset($this->sections[$name])) {
64 1
            throw new InvalidArgumentException('Section not found');
65
        }
66
67 6
        return $this->sections[$name];
68
    }
69
70
    /**
71
     * @param string $name
72
     */
73 1
    public function removeSection(string $name): void
74
    {
75 1
        if (! isset($this->sections[$name])) {
76 1
            throw new InvalidArgumentException('Section not found for remove');
77
        }
78
        unset($this->sections[$name]);
79
    }
80
81 3
    public function addSection(string $name, array $section): void
82
    {
83 3
        if (isset($this->sections[$name])) {
84 1
            throw new InvalidArgumentException('Overwrite section denied');
85
        }
86 3
        $this->sections[$name] = new Section($name, $section);
87 3
    }
88
89 1
    public function updateSection(string $name, array $section): void
90
    {
91 1
        if (! isset($this->sections[$name])) {
92 1
            throw new InvalidArgumentException('Section not found for update');
93
        }
94 1
        $this->sections[$name] = new Section($name, $section);
95 1
    }
96
97
    /**
98
     * default config
99
     */
100 10
    private function initDefaultConfig(): void
101
    {
102 10
        $this->config = [
103
            'strict'    => false,
104
            'overwrite' => true,
105
        ];
106 10
    }
107
108
    /**
109
     * @param string $filename
110
     */
111 10
    private function setFilename(?string $filename): void
112
    {
113 10
        $this->filename = $filename;
114 10
    }
115
116 1
    public function __toString()
117
    {
118 1
        $return = '';
119
120 1
        foreach ($this->sections as $section) {
121 1
            $return .= $section;
122
        }
123
124 1
        return trim($return) . PHP_EOL;
125
    }
126
127
    /**
128
     * @param string|null $filename
129
     * @return bool
130
     */
131 1
    public function save(string $filename = null): bool
132
    {
133 1
        if (! $this->config['overwrite'] && file_exists($filename)) {
134 1
            throw new InvalidArgumentException('Overwrite file protection');
135
        }
136 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...
137 1
            $this->setFilename($filename);
138
        }
139
140 1
        return (bool) (new SPLFileObject($this->filename, 'w+b'))->fwrite((string) $this);
141
    }
142
}
143