Completed
Pull Request — develop (#120)
by Matthew
04:27
created

Php::parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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 12
    public function parseFile($filename)
29
    {
30
        // Run the fileEval the string, if it throws an exception, rethrow it
31
        try {
32 12
            $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 9
        $parsed = $this->parse($data, $filename);
44
        return $parsed === null ? [] : $parsed;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     * Loads a PHP string and gets its' contents as an array
50
     *
51
     * @throws ParseException             If the PHP string throws an exception
52
     * @throws UnsupportedFormatException If the PHP string does not return an array
53 9
     */
54
    public function parseString($config)
55
    {
56 9
        // Handle PHP start tag
57 9
        $config = trim($config);
58 9
        if (substr($config, 0, 2) === '<?') {
59
            $config = '?>' . $config;
60
        }
61
62
        // Eval the string, if it throws an exception, rethrow it
63 9
        try {
64 3
            $data = $this->isolate($config);
65 3
        } catch (Exception $exception) {
66
            throw new ParseException(
67 3
                [
68 3
                    'message'   => 'PHP string threw an exception',
69
                    'exception' => $exception,
70
                ]
71
            );
72
        }
73
74 6
        // Complete parsing
75
        $parsed = $this->parse($data);
76
        return $parsed === null ? [] : $parsed;
77
    }
78
79
    /**
80
     * Completes parsing of PHP data
81
     *
82
     * @param  array $data
83
     * @param  string $filename
84
     *
85 9
     * @return array|null
86
     * @throws UnsupportedFormatException
87
     */
88 9
    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...
89 3
    {
90
        // If we have a callable, run it and expect an array back
91
        if (is_callable($data)) {
92
            $data = call_user_func($data);
93 9
        }
94 3
95
        // Check for array, if its anything else, throw an exception
96
        if (!is_array($data)) {
97 6
            throw new UnsupportedFormatException('PHP data does not return an array');
98
        }
99
100
        return $data;
101
    }
102
103 3
    /**
104
     * Runs PHP string in isolated method
105 3
     *
106
     * @param  string $EGsfKPdue7ahnMTy
107
     *
108
     * @return array
109
     */
110
    protected function isolate($EGsfKPdue7ahnMTy)
111
    {
112
        return eval($EGsfKPdue7ahnMTy);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118
    public static function getSupportedExtensions()
119
    {
120
        return ['php'];
121
    }
122
}
123