Completed
Push — develop ( 930556...77256d )
by Hassan
9s
created

Php   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 49
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 31 4
A getSupportedExtensions() 0 4 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 string and gets its' contents as an array
24
     *
25
     * @throws ParseException             If the PHP string throws an exception
26
     * @throws UnsupportedFormatException If the PHP string does not return an array
27
     */
28 12
    public function parse($config, $filename = null)
29
    {
30
        // Strip PHP start and end tags
31 12
        $config = str_replace('<?php', '', $config);
32 12
        $config = str_replace('<?', '', $config);
33 12
        $config = str_replace('?>', '', $config);
34
35
        // Eval the string, if it throws an exception, rethrow it
36
        try {
37 12
            $temp = eval($config);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
38 6
        } catch (Exception $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $excep...ion' => $exception)); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
39 3
            throw new ParseException(
40
                [
41 3
                    'message'   => 'PHP string threw an exception',
42 3
                    'exception' => $exception,
43
                ]
44 1
            );
45
        }
46
47
        // If we have a callable, run it and expect an array back
48 9
        if (is_callable($temp)) {
49 3
            $temp = call_user_func($temp);
50 1
        }
51
52
        // Check for array, if its anything else, throw an exception
53 9
        if (!is_array($temp)) {
54 3
            throw new UnsupportedFormatException('PHP string does not return an array');
55
        }
56
57 6
        return $temp;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 3
    public static function getSupportedExtensions()
64
    {
65 3
        return ['php'];
66
    }
67
}
68