Completed
Push — master ( 965662...ac7f34 )
by Xeriab
07:12
created

Json::parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
rs 9.2
cc 3
eloc 12
nc 4
nop 1
crap 3.004
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exen\Konfig\Exception\ParseException;
20
use Exen\Konfig\Utils;
21
22
/**
23
 * Json
24
 * Konfig's JSON parser class.
25
 *
26
 * @category FileParser
27
 * @package  Konfig
28
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
29
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
30
 * @link     https://xeriab.github.io/projects/konfig
31
 *
32
 * @implements Exen\Konfig\FileParser\AbstractFileParser
33
 */
34
class Json extends AbstractFileParser
35
{
36
    /**
37
     * Loads a JSON file as an array.
38
     *
39
     * @param string $path File path
40
     *
41
     * @throws ParseException If there is an error parsing JSON file
42
     *
43
     * @return array The parsed data
44
     *
45
     * @since 0.1.0
46
     */
47 6
    public function parse($path)
48
    {
49 6
        $data = $this->loadFile($path);
50
51 6
        if (function_exists('json_last_error_msg')) {
52 6
            $error_message = json_last_error_msg();
53 4
        } else {
54
            $error_message = 'Syntax error';
55
        }
56
57 6
        if (json_last_error() !== JSON_ERROR_NONE) {
58 3
            throw new ParseException(
59
                [
60 3
                'message' => $error_message,
61 3
                'type' => json_last_error(),
62 3
                'file' => $path,
63
                ]
64 2
            );
65
        }
66
67 3
        return $data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @return array Supported extensions
74
     *
75
     * @since 0.1.0
76
     */
77 3
    public function getSupportedFileExtensions()
78
    {
79 3
        return ['json'];
80
    }
81
82
    /**
83
     * Loads in the given file and parses it.
84
     *
85
     * @param string $file File to load
86
     *
87
     * @return array The parsed file data
88
     *
89
     * @since              0.2.4
90
     * @codeCoverageIgnore
91
     */
92
    protected function loadFile($file = null)
93
    {
94
        $this->file = $file;
95
        $contents = $this->parseVars(Utils::getContent($file));
96
97
        return json_decode($contents, true);
98
    }
99
100
    /**
101
     * Returns the formatted configuration file contents.
102
     *
103
     * @param array $contents configuration array
104
     *
105
     * @return string formatted configuration file contents
106
     *
107
     * @since              0.2.4
108
     * @codeCoverageIgnore
109
     */
110
    protected function exportFormat($contents = null)
111
    {
112
        $this->prepVars($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 110 can also be of type null; however, Exen\Konfig\FileParser\A...tFileParser::prepVars() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
113
114
        return json_encode($contents);
115
    }
116
}
117
118
// END OF ./src/FileParser/Json.php FILE
119