Json::exportFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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
 * Konfig's JSON parser class.
24
 *
25
 * @category FileParser
26
 * @package  Konfig
27
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
28
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
29
 * @link     https://xeriab.github.io/projects/konfig
30
 *
31
 * @implements Exen\Konfig\FileParser\AbstractFileParser
32
 */
33
class Json extends AbstractFileParser
34
{
35
    /**
36
     * Loads a JSON file as an array.
37
     *
38
     * @param string $path File path
39
     *
40
     * @throws ParseException If there is an error parsing JSON file
41
     *
42
     * @return array The parsed data
43
     *
44
     * @since 0.1.0
45
     */
46 6
    public function parse($path)
47
    {
48 6
        $data = $this->loadFile($path);
49
50 6
        if (function_exists('json_last_error_msg')) {
51 6
            $error_message = json_last_error_msg();
52 2
        } else {
53
            $error_message = 'Syntax error';
54
        }
55
56 6
        if (json_last_error() !== JSON_ERROR_NONE) {
57 3
            throw new ParseException(
58
                [
59 3
                'message' => $error_message,
60 3
                'type' => json_last_error(),
61 3
                'file' => $path,
62
                ]
63 1
            );
64
        }
65
66 3
        return $data;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @return array Supported extensions
73
     *
74
     * @since 0.1.0
75
     */
76 3
    public function getSupportedFileExtensions()
77
    {
78 3
        return ['json'];
79
    }
80
81
    /**
82
     * Loads in the given file and parses it.
83
     *
84
     * @param string $file File to load
85
     *
86
     * @return array The parsed file data
87
     *
88
     * @since              0.2.4
89
     * @codeCoverageIgnore
90
     */
91
    protected function loadFile($file = null)
92
    {
93
        $this->file = $file;
94
        $contents = $this->parseVars(Utils::getContent($file));
95
96
        return json_decode($contents, true);
97
    }
98
99
    /**
100
     * Returns the formatted configuration file contents.
101
     *
102
     * @param array $contents configuration array
103
     *
104
     * @return string formatted configuration file contents
105
     *
106
     * @since              0.2.4
107
     * @codeCoverageIgnore
108
     */
109
    protected function exportFormat($contents = null)
110
    {
111
        $this->prepVars($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 109 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...
112
        return json_encode($contents);
113
    }
114
115
    /**
116
     * __toString.
117
     *
118
     * @return             string
119
     * @since              0.1.2
120
     * @codeCoverageIgnore
121
     */
122
    public function __toString()
123
    {
124
        return 'Exen\Konfig\FileParser\Json' . PHP_EOL;
125
    }
126
}
127
128
// END OF ./src/FileParser/Json.php FILE
129