Completed
Pull Request — master (#91)
by
unknown
04:12
created

Php   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 44
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 26 4
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace mhndev\config\FileParser;
4
5
use Exception;
6
use mhndev\config\Exception\ParseException;
7
use mhndev\config\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 12
    public function parse($path)
28
    {
29
        // Require the file, if it throws an exception, rethrow it
30
        try {
31 12
            $temp = require $path;
32 12
        } catch (Exception $exception) {
33 3
            throw new ParseException(
34
                array(
35 3
                    'message'   => 'PHP file threw an exception',
36 3
                    'exception' => $exception,
37
                )
38 3
            );
39
        }
40
41
        // If we have a callable, run it and expect an array back
42 9
        if (is_callable($temp)) {
43 3
            $temp = call_user_func($temp);
44 3
        }
45
46
        // Check for array, if its anything else, throw an exception
47 9
        if (!is_array($temp)) {
48 3
            throw new UnsupportedFormatException('PHP file does not return an array');
49
        }
50
51 6
        return $temp;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 3
    public static function getSupportedExtensions()
58
    {
59 3
        return array('php');
60
    }
61
}
62