Completed
Push — develop ( 3fad72...7847b0 )
by Davide
03:35
created

Ini   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 72.21%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 68
ccs 26
cts 36
cp 0.7221
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 23 4
A expandDottedKey() 0 18 4
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace Noodlehaus\FileParser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * INI file parser
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @link       https://github.com/noodlehaus/config
14
 * @license    MIT
15
 */
16
class Ini implements FileParserInterface
17
{
18
19
20
    /**
21
     * {@inheritDoc}
22
     * Parses an INI file as an array
23
     *
24
     * @throws ParseException If there is an error parsing the INI file
25
     */
26 8
    public function parse($path)
27
    {
28 8
        $data = @parse_ini_file($path, true);
29
30 8
        if (!$data) {
31 4
            $error = error_get_last();
32
33
            // parse_ini_file() may return NULL but set no error if the file contains no parsable data
34 4
            if (!is_array($error)) {
35 2
                $error["message"] = "No parsable content in file.";
36 2
            }
37
38
            // if file contains no parsable data, no error is set, resulting in any previous error
39
            // persisting in error_get_last(). in php 7 this can be addressed with error_clear_last()
40 4
            if (function_exists("error_clear_last")) {
41
                error_clear_last();
42
            }
43
44 4
            throw new ParseException($error);
45
        }
46
47 4
        return $this->expandDottedKey($data);
48
    }
49
50
    /**
51
     * Expand array with dotted keys to multidimensional array
52
     *
53
     * @param array $data
54
     *
55
     * @return array
56
     */
57 2
    protected function expandDottedKey($data)
58
    {
59 2
        foreach ($data as $key => $value) {
60 2
            if (($found = strpos($key, '.')) !== false) {
61 2
                $newKey = substr($key, 0, $found);
62 2
                $remainder = substr($key, $found + 1);
63
64 2
                $expandedValue = $this->expandDottedKey(array($remainder => $value));
65 2
                if (isset($data[$newKey])) {
66 2
                    $data[$newKey] = array_merge_recursive($data[$newKey], $expandedValue);
67 2
                } else {
68 2
                    $data[$newKey] = $expandedValue;
69
                }
70 2
                unset($data[$key]);
71 2
            }
72 2
        }
73 2
        return $data;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 2
    public static function getSupportedExtensions()
80
    {
81 2
        return array('ini');
82
    }
83
}
84