Ini::loadSettings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * This file is part of Configuration
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Configuration\Driver;
11
12
use Slick\Configuration\ConfigurationInterface;
13
use Slick\Configuration\Exception\ParserErrorException;
14
15
/**
16
 * Ini configuration driver
17
 *
18
 * @package Slick\Configuration\Driver
19
 */
20
class Ini implements ConfigurationInterface
21
{
22
23
    use CommonDriverMethods;
24
25
    /**
26
     * @var string
27 6
     */
28
    private string $lastError = '';
29 6
30 6
    /**
31 3
     * Creates an ini configuration driver
32 3
     *
33 3
     * @param string $filePath
34
     */
35 3
    public function __construct(string $filePath)
36 3
    {
37
        $this->checkFile($filePath);
38
39
        $this->loadSettings($filePath);
40
41
        if (!is_array($this->data)) {
0 ignored issues
show
introduced by
The condition is_array($this->data) is always true.
Loading history...
42
            throw new ParserErrorException(
43
                "Parse error: $this->lastError"
44
            );
45
        }
46
    }
47
48
    private function loadSettings(string $filePath): void
49
    {
50
        set_error_handler(function (int $errorNumber, string $message): bool {
51
            $this->lastError = "$errorNumber: $message";
52
            return true;
53
        });
54
        $parsedFile = parse_ini_file($filePath, true, INI_SCANNER_TYPED);
55
        $this->data = is_array($parsedFile) ? $parsedFile : 0;
0 ignored issues
show
introduced by
The condition is_array($parsedFile) is always true.
Loading history...
56
        restore_error_handler();
57
    }
58
}
59