Php   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 28 4
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace Nip\Config\FileParser;
4
5
use Exception;
6
use Nip\Config\Exception\ParseException;
7
use Nip\Config\Exception\UnsupportedFormatException;
8
9
/**
10
 * Class Php
11
 * @package Nip\Config\FileParser
12
 */
13
class Php extends AbstractFileParser
14
{
15
    /**
16
     * {@inheritDoc}
17
     * Loads a PHP file and gets its' contents as an array
18
     *
19
     * @throws ParseException             If the PHP file throws an exception
20
     * @throws UnsupportedFormatException If the PHP file does not return an array
21
     */
22
    public function parse()
23
    {
24
        $path = $this->getPath();
25
        // Require the file, if it throws an exception, rethrow it
26
        try {
27
            /** @noinspection PhpIncludeInspection */
28
            $temp = require $path;
29
        } catch (Exception $exception) {
30
            throw new ParseException(
31
                [
32
                    'message' => 'PHP file threw an exception',
33
                    'exception' => $exception,
34
                ]
35
            );
36
        }
37
38
        // If we have a callable, run it and expect an array back
39
        if (is_callable($temp)) {
40
            $temp = call_user_func($temp);
41
        }
42
43
        // Check for array, if its anything else, throw an exception
44
        if (!is_array($temp)) {
45
            throw new UnsupportedFormatException('PHP file does not return an array');
46
        }
47
48
        return $temp;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function getSupportedExtensions()
55
    {
56
        return ['php'];
57
    }
58
}
59