Completed
Push — master ( 33953d...4a13d7 )
by Xeriab
03:37
created

Php::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exception;
20
use Exen\Konfig\Exception\ParseException;
21
use Exen\Konfig\Exception\UnsupportedFileFormatException;
22
23
/**
24
 * Php
25
 * Konfig's PHP parser class.
26
 *
27
 * @category FileParser
28
 * @package  Konfig
29
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
30
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
31
 * @link     https://xeriab.github.io/projects/konfig
32
 *
33
 * @implements Exen\Konfig\FileParser\AbstractFileParser
34
 */
35
class Php extends AbstractFileParser
36
{
37
    /**
38
     * Loads a PHP file and gets its contents as an array.
39
     *
40
     * @param string $path File path
41
     *
42
     * @throws ParseException             If the PHP file throws an exception
43
     * @throws UnsupportedFormatException If the PHP file does not return an array
44
     *
45
     * @return array The parsed data
46
     *
47
     * @since 0.1.0
48
     */
49 12
    public function parse($path)
50
    {
51 12
        $data = null;
52
53
        // Require the file, if it throws an exception, rethrow it
54
        try {
55 12
            $data = $this->loadFile($path);
56 9
        } catch (Exception $ex) {
57 3
            throw new ParseException(
58
                [
59 3
                'message' => 'PHP file threw an exception',
60 3
                'file' => $path,
61 3
                'exception' => $ex,
62
                ]
63 2
            );
64
        }
65
66
        // If we have a callable, run it and expect an array back
67 9
        if (is_callable($data)) {
68 3
            $data = call_user_func($data);
69 2
        }
70
71
        // Check for array, if its anything else, throw an exception
72 9
        if (empty($data) || !is_array($data)) {
73 3
            throw new UnsupportedFileFormatException(
74 1
                'PHP file does not return an array'
75 2
            );
76
        }
77
78 6
        return $data;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @return array Supported extensions
85
     *
86
     * @since 0.1.0
87
     */
88 3
    public function getSupportedFileExtensions()
89
    {
90 3
        return ['php', 'inc'];
91
    }
92
93
    /**
94
     * Loads in the given file and parses it.
95
     *
96
     * @param string|bool|null $file File to load
97
     *
98
     * @return array The parsed file data
99
     *
100
     * @since              0.2.4
101
     * @codeCoverageIgnore
102
     */
103
    protected function loadFile($file = null)
104
    {
105
        $this->file = is_file($file) ? $file : false;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_file($file) ? $file : false can also be of type boolean. However, the property $file is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
106
107
        return include $this->file;
108
    }
109
110
    /**
111
     * Returns the formatted configuration file contents.
112
     *
113
     * @param array $contents configuration array
114
     *
115
     * @return string formatted configuration file contents
116
     *
117
     * @since              0.2.4
118
     * @codeCoverageIgnore
119
     */
120
    protected function exportFormat($contents = null)
121
    {
122
        throw new Exception(
123
            'Saving configuration to `PHP` is not supported at this time'
124
        );
125
    }
126
127
    /**
128
     * __toString.
129
     *
130
     * @return             string
131
     * @since              0.1.2
132
     * @codeCoverageIgnore
133
     */
134
    public function __toString()
135
    {
136
        return 'Exen\Konfig\FileParser\Php' . PHP_EOL;
137
    }
138
}
139
140
// END OF ./src/FileParser/Php.php FILE
141