JsonParser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * slince config library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Config\Parser;
7
8
use Slince\Config\Exception\ParseException;
9
10
class JsonParser implements ParserInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function parse($file)
16
    {
17
        $data = json_decode(file_get_contents($file), true);
18
        if (json_last_error() != JSON_ERROR_NONE) {
19
            throw new ParseException(sprintf('The file (%s)  need to contain a valid json string', $file));
20
        }
21
        return $data;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function dump($file, array $data)
28
    {
29
        $string = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
30
        @mkdir(dirname($file), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
31
        return @file_put_contents($file, $string) !== false;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public static function getSupportedExtensions()
38
    {
39
        return ['json'];
40
    }
41
}
42