Completed
Push — develop ( 3fad72...7847b0 )
by Davide
03:35
created

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 71.43%

Importance

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

2 Methods

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