Completed
Branch master (36e7ce)
by Xeriab
01:56
created

Php::parse()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 8.439
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * Konfig
4
 *
5
 * Yet another simple configuration loader library.
6
 *
7
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
8
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
9
 * @link    https://xeriab.github.io/projects/konfig
10
 */
11
12
namespace Exen\Konfig\FileParser;
13
14
use Exception;
15
use Exen\Konfig\Exception\ParseException;
16
use Exen\Konfig\Exception\UnsupportedFileFormatException;
17
18
class Php extends AbstractFileParser
19
{
20
    /**
21
     * {@inheritDoc}
22
     * Loads a PHP file and gets its' contents as an array
23
     *
24
     * @throws ParseException If the PHP file throws an exception
25
     * @throws UnsupportedFormatException If the PHP file does not return an array
26
     * @since 0.1
27
     */
28 12
    public function parse($path)
29
    {
30
        // Require the file, if it throws an exception, rethrow it
31
        try {
32 12
            $temp = require $path;
33 9
        } catch (Exception $ex) {
34 3
            throw new ParseException(
35
                [
36 3
                    'message'   => 'PHP file threw an exception',
37 3
                    'exception' => $ex,
38
                ]
39 2
            );
40
        }
41
42
        // If we have a callable, run it and expect an array back
43 9
        if (is_callable($temp)) {
44 3
            $temp = call_user_func($temp);
45 2
        }
46
47
        // Check for array, if its anything else, throw an exception
48 9
        if (empty($temp) || !is_array($temp)) {
49 3
            throw new UnsupportedFileFormatException('PHP file does not return an array');
50
        }
51
52 6
        return $temp;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @since 0.1
58
     */
59 3
    public function getSupportedFileExtensions()
60
    {
61 3
        return ['php', 'inc'];
62
    }
63
}
64
65
#: END OF ./src/FileParser/Php.php FILE
66