Completed
Push — develop ( 3fad72...7847b0 )
by Davide
03:35
created

Php::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1.125
1
<?php
2
3
namespace Noodlehaus\FileParser;
4
5
use Exception;
6
use Noodlehaus\Exception\ParseException;
7
use Noodlehaus\Exception\UnsupportedFormatException;
8
9
/**
10
 * PHP file parser
11
 *
12
 * @package    Config
13
 * @author     Jesus A. Domingo <[email protected]>
14
 * @author     Hassan Khan <[email protected]>
15
 * @link       https://github.com/noodlehaus/config
16
 * @license    MIT
17
 */
18
class Php implements FileParserInterface
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
     */
27 8
    public function parse($path)
28
    {
29
        // Require the file, if it throws an exception, rethrow it
30
        try {
31 8
            $temp = require $path;
32 8
        } catch (Exception $exception) {
33 2
            throw new ParseException(
34
                array(
35 2
                    'message'   => 'PHP file threw an exception',
36 2
                    'exception' => $exception,
37
                )
38 2
            );
39
        }
40
41
        // If we have a callable, run it and expect an array back
42 6
        if (is_callable($temp)) {
43 2
            $temp = call_user_func($temp);
44 2
        }
45
46
        // Check for array, if its anything else, throw an exception
47 6
        if (!is_array($temp)) {
48 2
            throw new UnsupportedFormatException('PHP file does not return an array');
49
        }
50
51 4
        return $temp;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 2
    public static function getSupportedExtensions()
58
    {
59 2
        return array('php');
60
    }
61
}
62