Completed
Push — master ( 8d39d6...58e1d9 )
by Xeriab
02:50
created

Json::loadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Konfig
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
9
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
10
 * @link    https://xeriab.github.io/projects/konfig
11
 */
12
13
namespace Exen\Konfig\FileParser;
14
15
use Exen\Konfig\Utils;
16
use Exen\Konfig\Exception\Exception;
17
use Exen\Konfig\Exception\ParseException;
18
19
class Json extends AbstractFileParser
20
{
21
    /**
22
     * {@inheritDoc}
23
     * Loads a JSON file as an array
24
     *
25
     * @throws ParseException If there is an error parsing JSON file
26 6
     * @since 0.1.0
27
     */
28 6
    public function parse($path)
29
    {
30 6
        $data = $this->loadFile($path);
31 6
32 4
        if (function_exists('json_last_error_msg')) {
33
            $error_message = json_last_error_msg();
34
        } else {
35
            $error_message = 'Syntax error';
36 6
        }
37 3
38 3
        if (json_last_error() !== JSON_ERROR_NONE) {
39 3
            throw new ParseException([
40 3
                'message' => $error_message,
41 2
                'type' => json_last_error(),
42
                'file' => $path,
43
            ]);
44 3
        }
45
46
        return $data;
47
    }
48
49
    /**
50 3
     * {@inheritDoc}
51
     */
52 3
    public function getSupportedFileExtensions()
53
    {
54
        return ['json'];
55
    }
56
57
    /**
58
     * Loads in the given file and parses it.
59
     *
60
     * @param   string  $file File to load
61
     * @return  array
62
     * @since 0.2.4
63
     * @codeCoverageIgnore
64
     */
65
    protected function loadFile($file = null)
66
    {
67
        $this->file = $file;
68
        $contents = $this->parseVars(Utils::getContent($file));
69
        return json_decode($contents, true);
70
    }
71
72
    /**
73
     * Returns the formatted configuration file contents.
74
     *
75
     * @param   array   $content  configuration array
0 ignored issues
show
Documentation introduced by
There is no parameter named $content. Did you maybe mean $contents?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
76
     * @return  string  formatted configuration file contents
77
     * @since 0.2.4
78
     * @codeCoverageIgnore
79
     */
80
    protected function exportFormat($contents = null)
81
    {
82
        $this->prepVars($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 80 can also be of type null; however, Exen\Konfig\FileParser\A...tFileParser::prepVars() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
        return json_encode($contents);
84
    }
85
}
86
87
// END OF ./src/FileParser/Json.php FILE
88