Ini   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 2
b 0
f 0
dl 0
loc 37
rs 10
ccs 8
cts 8
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A loadSettings() 0 9 2
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