Completed
Push — master ( 33953d...4a13d7 )
by Xeriab
03:37
created

Neon   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 91
ccs 12
cts 12
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 18 2
A getSupportedFileExtensions() 0 4 1
A loadFile() 0 7 1
A exportFormat() 0 6 1
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exception;
20
use Exen\Konfig\Exception\ParseException;
21
use Exen\Konfig\Utils;
22
use Nette\Neon\Neon as NeonParser;
23
24
/**
25
 * Neon
26
 * Konfig's NEON parser class.
27
 *
28
 * @category FileParser
29
 * @package  Konfig
30
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
31
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
32
 * @link     https://xeriab.github.io/projects/konfig
33
 *
34
 * @implements Exen\Konfig\FileParser\AbstractFileParser
35
 */
36
class Neon extends AbstractFileParser
37
{
38
    /**
39
     * Loads a NEON file as an array.
40
     *
41
     * @param string $path File path
42
     *
43
     * @throws ParseException If there is an error parsing NEON file
44
     *
45
     * @return array The parsed data
46
     *
47
     * @since 0.1.0
48
     */
49 6
    public function parse($path)
50
    {
51 6
        $data = null;
52
53
        try {
54 6
            $data = $this->loadFile($path);
55 5
        } catch (Exception $ex) {
56 3
            throw new ParseException(
57
                [
58 3
                'message' => 'Error parsing NEON file',
59 3
                'file' => $path,
60 3
                'exception' => $ex,
61
                ]
62 2
            );
63
        }
64
65 3
        return $data;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return array Supported extensions
72
     *
73
     * @since 0.1.0
74
     */
75 3
    public function getSupportedFileExtensions()
76
    {
77 3
        return ['neon'];
78
    }
79
80
    /**
81
     * Loads in the given file and parses it.
82
     *
83
     * @param string $file File to load
84
     *
85
     * @return array The parsed file data
86
     *
87
     * @since              0.2.4
88
     * @codeCoverageIgnore
89
     */
90
    protected function loadFile($file = null)
91
    {
92
        $this->file = $file;
93
        $contents = $this->parseVars(Utils::getContent($file));
94
95
        return NeonParser::decode($contents);
96
    }
97
98
    /**
99
     * Returns the formatted configuration file contents.
100
     *
101
     * @param array $contents configuration array
102
     *
103
     * @return string formatted configuration file contents
104
     *
105
     * @since              0.2.4
106
     * @codeCoverageIgnore
107
     */
108
    protected function exportFormat($contents = null)
109
    {
110
        $this->prepVars($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 108 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...
111
112
        return NeonParser::encode($contents);
113
    }
114
115
    /**
116
     * __toString.
117
     *
118
     * @return             string
119
     * @since              0.1.2
120
     * @codeCoverageIgnore
121
     */
122
    public function __toString()
123
    {
124
        return 'Exen\Konfig\FileParser\Neon' . PHP_EOL;
125
    }
126
}
127
128
// END OF ./src/FileParser/Neon.php FILE
129