JsonReader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readFromFile() 0 4 1
A getError() 0 10 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Reader;
16
17
/**
18
 * Read & parse json formatted file and return the result
19
 *
20
 * @package Phossa2\Shared
21
 * @author  Hong Zhang <[email protected]>
22
 * @version 2.0.1
23
 * @since   2.0.1 added
24
 */
25
class JsonReader extends ReaderAbstract
26
{
27
    /**
28
     * default json error
29
     *
30
     * @var    array
31
     * @access protected
32
     * @staticvar
33
     */
34
    protected static $error = [
35
        \JSON_ERROR_NONE => 'No error',
36
        \JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
37
        \JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
38
        \JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
39
        \JSON_ERROR_SYNTAX => 'Syntax error',
40
        \JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
41
    ];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected static function readFromFile($path)
47
    {
48
        return @json_decode(file_get_contents($path), true);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    protected static function getError(/*# string */ $path)/*#: string */
55
    {
56
        if (function_exists('json_last_error_msg')) {
57
            return json_last_error_msg();
58
        } else {
59
            $error = json_last_error();
60
            return isset(static::$error[$error]) ?
61
                static::$error[$error] : 'JSON parse error';
62
        }
63
    }
64
}
65