Completed
Pull Request — develop (#114)
by Filip
02:36
created

Json::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
     * Parses an JSON file as an array
22
     *
23
     * @throws ParseException If there is an error parsing the JSON file
24
     */
25 6
    public function parseFile($filename)
26
    {
27 6
        $data = json_decode(file_get_contents($filename), true);
28 6
        return $this->parse($data, $filename);
0 ignored issues
show
Documentation introduced by
$filename is of type string, but the function expects a object<Noodlehaus\Parser\strring>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     * Parses an JSON string as an array
34
     *
35
     * @throws ParseException If there is an error parsing the JSON string
36
     */
37 3
    public function parseString($config)
38
    {
39 3
        $data = json_decode($config, true);
40 3
        return $this->parse($data);
41
    }
42
43
    /**
44
     * Completes parsing of JSON data
45
     *
46
     * @param  array   $data
47
     * @param  strring $filename
48
     *
49
     * @throws ParseException If there is an error parsing the JSON data
50
     */
51 6
    protected function parse($data = null, $filename = null)
52
    {
53 6
        if (json_last_error() !== JSON_ERROR_NONE) {
54 3
            $error_message  = 'Syntax error';
55 3
            if (function_exists('json_last_error_msg')) {
56 3
                $error_message = json_last_error_msg();
57 1
            }
58
59
            $error = [
60 3
                'message' => $error_message,
61 3
                'type'    => json_last_error(),
62 3
                'file'    => $filename,
63 1
            ];
64 3
            throw new ParseException($error);
65
        }
66
67 3
        return $data;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 3
    public static function getSupportedExtensions()
74
    {
75 3
        return ['json'];
76
    }
77
}
78