Completed
Push — develop ( 930556...77256d )
by Hassan
9s
created

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 20 3
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * JSON 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 Json implements ParserInterface
18
{
19
    /**
20
     * {@inheritDoc}
21
     * Loads a JSON string as an array
22
     *
23
     * @throws ParseException If there is an error parsing the JSON string
24
     */
25 6
    public function parse($config, $filename = null)
26
    {
27 6
        $data = json_decode($config, true);
28
29 6
        if (json_last_error() !== JSON_ERROR_NONE) {
30 3
            $error_message  = 'Syntax error';
31 3
            if (function_exists('json_last_error_msg')) {
32 3
                $error_message = json_last_error_msg();
33 1
            }
34
35
            $error = [
36 3
                'message' => $error_message,
37 3
                'type'    => json_last_error(),
38 3
                'file'    => $filename,
39 1
            ];
40 3
            throw new ParseException($error);
41
        }
42
43 3
        return $data;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 3
    public static function getSupportedExtensions()
50
    {
51 3
        return ['json'];
52
    }
53
}
54