Php   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 104
ccs 19
cts 19
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 31 5
A getSupportedFileExtensions() 0 4 1
A loadFile() 0 6 2
A exportFormat() 0 6 1
A __toString() 0 4 1
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
 * Konfig's PHP parser class.
25
 *
26
 * @category FileParser
27
 * @package  Konfig
28
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
29
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
30
 * @link     https://xeriab.github.io/projects/konfig
31
 *
32
 * @implements Exen\Konfig\FileParser\AbstractFileParser
33
 */
34
class Php extends AbstractFileParser
35
{
36
    /**
37
     * Loads a PHP file and gets its contents as an array.
38
     *
39
     * @param string $path File path
40
     *
41
     * @throws ParseException             If the PHP file throws an exception
42
     * @throws UnsupportedFormatException If the PHP file does not return an array
43
     *
44
     * @return array The parsed data
45
     *
46
     * @since 0.1.0
47
     */
48 12
    public function parse($path)
49
    {
50 12
        $data = null;
51
52
        // Require the file, if it throws an exception, rethrow it
53
        try {
54 12
            $data = $this->loadFile($path);
55 6
        } catch (Exception $ex) {
56 3
            throw new ParseException(
57
                [
58 3
                'message' => 'PHP file threw an exception',
59 3
                'file' => $path,
60 3
                'exception' => $ex,
61
                ]
62 1
            );
63
        }
64
65
        // If we have a callable, run it and expect an array back
66 9
        if (is_callable($data)) {
67 3
            $data = call_user_func($data);
68 1
        }
69
70
        // Check for array, if its anything else, throw an exception
71 9
        if (empty($data) || !is_array($data)) {
72 3
            throw new UnsupportedFileFormatException(
73 2
                'PHP file does not return an array'
74 1
            );
75
        }
76
77 6
        return $data;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @return array Supported extensions
84
     *
85
     * @since 0.1.0
86
     */
87 3
    public function getSupportedFileExtensions()
88
    {
89 3
        return ['php', 'inc'];
90
    }
91
92
    /**
93
     * Loads in the given file and parses it.
94
     *
95
     * @param string|bool|null $file File to load
96
     *
97
     * @return array The parsed file data
98
     *
99
     * @since              0.2.4
100
     * @codeCoverageIgnore
101
     */
102
    protected function loadFile($file = null)
103
    {
104
        $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...
105
106
        return include $this->file;
107
    }
108
109
    /**
110
     * Returns the formatted configuration file contents.
111
     *
112
     * @param array $contents configuration array
113
     *
114
     * @return string formatted configuration file contents
115
     *
116
     * @since              0.2.4
117
     * @codeCoverageIgnore
118
     */
119
    protected function exportFormat($contents = null)
120
    {
121
        throw new Exception(
122
            'Saving configuration to `PHP` is not supported at this time'
123
        );
124
    }
125
126
    /**
127
     * __toString.
128
     *
129
     * @return             string
130
     * @since              0.1.2
131
     * @codeCoverageIgnore
132
     */
133
    public function __toString()
134
    {
135
        return 'Exen\Konfig\FileParser\Php' . PHP_EOL;
136
    }
137
}
138
139
// END OF ./src/FileParser/Php.php FILE
140