Completed
Pull Request — master (#111)
by Filip
07:42
created

Json   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

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