Completed
Push — master ( 5c0ad8...d30cdc )
by Filip
08:38 queued 07:33
created

Php::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Exception;
6
use Noodlehaus\Exception\ParseException;
7
use Noodlehaus\Exception\UnsupportedFormatException;
8
9
/**
10
 * PHP parser
11
 *
12
 * @package    Config
13
 * @author     Jesus A. Domingo <[email protected]>
14
 * @author     Hassan Khan <[email protected]>
15
 * @author     Filip Š <[email protected]>
16
 * @link       https://github.com/noodlehaus/config
17
 * @license    MIT
18
 */
19
class Php implements ParserInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     * Loads a PHP file and gets its' contents as an array
24
     *
25
     * @throws ParseException             If the PHP file throws an exception
26
     * @throws UnsupportedFormatException If the PHP file does not return an array
27
     */
28 15
    public function parseFile($filename)
29
    {
30
        // Run the fileEval the string, if it throws an exception, rethrow it
31
        try {
32 15
            $data = require $filename;
33 3
        } catch (Exception $exception) {
34 3
            throw new ParseException(
35
                [
36 3
                    'message'   => 'PHP file threw an exception',
37 3
                    'exception' => $exception,
38
                ]
39
            );
40
        }
41
42
        // Complete parsing
43 12
        return (array)$this->parse($data, $filename);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * Loads a PHP string and gets its' contents as an array
49
     *
50
     * @throws ParseException             If the PHP string throws an exception
51
     * @throws UnsupportedFormatException If the PHP string does not return an array
52
     */
53 12
    public function parseString($config)
54
    {
55
        // Handle PHP start tag
56 12
        $config = trim($config);
57 12
        if (substr($config, 0, 2) === '<?') {
58 12
            $config = '?>' . $config;
59
        }
60
61
        // Eval the string, if it throws an exception, rethrow it
62
        try {
63 12
            $data = $this->isolate($config);
64 3
        } catch (Exception $exception) {
65 3
            throw new ParseException(
66
                [
67 3
                    'message'   => 'PHP string threw an exception',
68 3
                    'exception' => $exception,
69
                ]
70
            );
71
        }
72
73
        // Complete parsing
74 9
        return (array)$this->parse($data);
75
    }
76
77
    /**
78
     * Completes parsing of PHP data
79
     *
80
     * @param  array $data
81
     * @param  string $filename
82
     *
83
     * @return array|null
84
     * @throws UnsupportedFormatException
85
     */
86 12
    protected function parse($data = null, $filename = null)
0 ignored issues
show
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        // If we have a callable, run it and expect an array back
89 12
        if (is_callable($data)) {
90 3
            $data = call_user_func($data);
91
        }
92
93
        // Check for array, if its anything else, throw an exception
94 12
        if (!is_array($data)) {
95 3
            throw new UnsupportedFormatException('PHP data does not return an array');
96
        }
97
98 9
        return $data;
99
    }
100
101
    /**
102
     * Runs PHP string in isolated method
103
     *
104
     * @param  string $EGsfKPdue7ahnMTy
105
     *
106
     * @return array
107
     */
108 12
    protected function isolate($EGsfKPdue7ahnMTy)
109
    {
110 12
        return eval($EGsfKPdue7ahnMTy);
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 3
    public static function getSupportedExtensions()
117
    {
118 3
        return ['php'];
119
    }
120
}
121