Completed
Push — develop ( f33748...a2b993 )
by Hassan
03:56
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 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
        return $this->parse($data, $filename);
0 ignored issues
show
Documentation introduced by
$filename is of type string, but the function expects a object<Noodlehaus\Parser\strring>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 9
    public function parseString($config)
54
    {
55
        // Handle PHP start tag
56 9
        $config = trim($config);
57 9
        if (substr($config, 0, 2) === '<?') {
58 9
            $config = '?>' . $config;
59
        }
60
61
        // Eval the string, if it throws an exception, rethrow it
62
        try {
63 9
            $data = eval($config);
64 3
        } 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...
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 6
        return $this->parse($data);
75
    }
76
77
    /**
78
     * Completes parsing of PHP data
79
     *
80
     * @param  array   $data
81
     * @param  strring $filename
82
     *
83
     * @throws ParseException If there is an error parsing the PHP data
84
     */
85 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...
86
    {
87
        // If we have a callable, run it and expect an array back
88 9
        if (is_callable($data)) {
89 3
            $data = call_user_func($data);
90
        }
91
92
        // Check for array, if its anything else, throw an exception
93 9
        if (!is_array($data)) {
94 3
            throw new UnsupportedFormatException('PHP data does not return an array');
95
        }
96
97 6
        return $data;
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 3
    public static function getSupportedExtensions()
104
    {
105 3
        return ['php'];
106
    }
107
}
108