Completed
Push — develop ( f33748...a2b993 )
by Hassan
03:56
created

Ini::parseString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * INI parser
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @author     Filip Š <[email protected]>
14
 * @link       https://github.com/noodlehaus/config
15
 * @license    MIT
16
 */
17
class Ini implements ParserInterface
18
{
19
    /**
20
     * {@inheritDoc}
21
     * Parses an INI file as an array
22
     *
23
     * @throws ParseException If there is an error parsing the INI file
24
     */
25 9
    public function parseFile($filename)
26
    {
27 9
        $data = @parse_ini_file($filename, true);
28 9
        return $this->parse($data, $filename);
0 ignored issues
show
Documentation introduced by
$filename is of type string, but the function expects a object<Noodlehaus\Parser\strring>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     * Parses an INI string as an array
34
     *
35
     * @throws ParseException If there is an error parsing the INI string
36
     */
37 9
    public function parseString($config)
38
    {
39 9
        $data = @parse_ini_string($config, true);
40 9
        return $this->parse($data);
41
    }
42
43
    /**
44
     * Completes parsing of INI data
45
     *
46
     * @param  array   $data
47
     * @param  strring $filename
48
     *
49
     * @throws ParseException If there is an error parsing the INI data
50
     */
51 12
    protected function parse($data = null, $filename = null)
52
    {
53 12
        if (!$data) {
54 6
            $error = error_get_last();
55
56
            // Parse functions may return NULL but set no error if the string contains no parsable data
57 6
            if (!is_array($error)) {
58 3
                $error["message"] = "No parsable content in data.";
59
            }
60
61 6
            $error["file"] = $filename;
62
63
            // if string contains no parsable data, no error is set, resulting in any previous error
64
            // persisting in error_get_last(). in php 7 this can be addressed with error_clear_last()
65 6
            if (function_exists("error_clear_last")) {
66 6
                error_clear_last();
67
            }
68
69 6
            throw new ParseException($error);
70
        }
71
72 6
        return $this->expandDottedKey($data);
73
    }
74
75
    /**
76
     * Expand array with dotted keys to multidimensional array
77
     *
78
     * @param array $data
79
     *
80
     * @return array
81
     */
82 3
    protected function expandDottedKey($data)
83
    {
84 3
        foreach ($data as $key => $value) {
85 3
            if (($found = strpos($key, '.')) !== false) {
86 3
                $newKey = substr($key, 0, $found);
87 3
                $remainder = substr($key, $found + 1);
88
89 3
                $expandedValue = $this->expandDottedKey([$remainder => $value]);
90 3
                if (isset($data[$newKey])) {
91 3
                    $data[$newKey] = array_merge_recursive($data[$newKey], $expandedValue);
92
                } else {
93 3
                    $data[$newKey] = $expandedValue;
94
                }
95 3
                unset($data[$key]);
96
            }
97
        }
98 3
        return $data;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 3
    public static function getSupportedExtensions()
105
    {
106 3
        return ['ini'];
107
    }
108
}
109