Passed
Push — master ( 53bb62...283d0c )
by Rougin
08:28
created

Config::get()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 12.0193

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 36
c 1
b 0
f 0
nc 32
nop 3
dl 0
loc 60
ccs 37
cts 39
cp 0.9487
crap 12.0193
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Combustor\Common;
4
5
/**
6
 * Config
7
 *
8
 * A simple object-oriented interface for handling CodeIgniter's configurations.
9
 *
10
 * @package Combustor
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
class Config
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $config;
19
20
    /**
21
     * @var string
22
     */
23
    public $fileName;
24
25
    /**
26
     * @var array
27
     */
28
    protected $lines;
29
30
    /**
31
     * @param string $config
32
     * @param string $configPath
33
     */
34 24
    public function __construct($config, $configPath)
35
    {
36 24
        $this->config = $config;
37 24
        $this->fileName = $configPath . '/' . $config . '.php';
38
39 24
        $content = file_get_contents($this->fileName);
40
41 24
        $this->lines = preg_split("/\\r\\n|\\r|\\n/", $content);
0 ignored issues
show
Documentation Bug introduced by
It seems like preg_split('/\r\n|\r|\n/', $content) can also be of type false. However, the property $lines is declared as type array. 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...
42 24
    }
43
44
    /**
45
     * Returns the specified value from the config item.
46
     *
47
     * @param  string       $item
48
     * @param  integer      $line
49
     * @param  string  $dataType
50
     * @return mixed
51
     */
52 24
    public function get($item, $line, $dataType)
53
    {
54 24
        $result = null;
55 24
        $value = null;
56
57
        switch ($dataType) {
58 24
            case 'array':
59 24
                $value = 'array\((.*?)\)';
60
61 24
                break;
62 6
            case 'boolean':
63 6
                $value = '(true|TRUE|false|FALSE)';
64
65 6
                break;
66 6
            case 'string':
67 6
                $value = '(.*?)';
68
69 6
                break;
70
        }
71
72 24
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $value . ';/';
73
74 24
        preg_match_all($pattern, $this->lines[$line], $match);
75
76
        switch ($dataType) {
77 24
            case 'array':
78 24
                $result = array_filter($match[1]);
79 24
                $data = '';
80
81 24
                if (! empty($result[0])) {
82 3
                    $data = $result[0];
83 2
                }
84
85 24
                $result = array_filter(explode(',', $data));
86 24
                $length = count($result);
87
88 24
                for ($i = 0; $i < $length; $i++) {
89 3
                    $result[$i] = str_replace(['\'', '"'], '', trim($result[$i]));
90 2
                }
91
92 24
                break;
93 6
            case 'boolean':
94 6
                $result = $match[1][0] == 'TRUE';
95
96 6
                break;
97 6
            case 'string':
98 6
                $result = $match[1][0];
99
100 6
                if ($result == '\'\'') {
101
                    $result = null;
102
                }
103
104 6
                if ($result[0] == '\'' && $result[strlen($result) - 1] == '\'') {
105 6
                    $result = substr($result, 1, strlen($result) - 2);
106 4
                }
107
108 6
                break;
109
        }
110
111 24
        return $result;
112
    }
113
114
    /**
115
     * Sets an value to an item in the config.
116
     *
117
     * @param string  $item
118
     * @param integer $line
119
     * @param mixed   $value
120
     * @param string  $dataType
121
     * @param boolean $exact
122
     */
123 21
    public function set($item, $line, $value, $dataType, $exact = false)
124
    {
125 21
        $data = null;
126 21
        $format = null;
127
128
        switch ($dataType) {
129 21
            case 'array':
130 21
                $length = count($value);
131
132 21
                for ($i = 0; $i < $length; $i++) {
133 21
                    $value[$i] = '\'' . $value[$i] . '\'';
134 14
                }
135
136 21
                $data = 'array(' . implode(', ', $value) . ')';
137 21
                $format = 'array\([^)]*\)';
138
139 21
                break;
140 3
            case 'boolean':
141 3
                $data = ($value) ? 'TRUE' : 'FALSE';
142 3
                $format = '[^)]*';
143
144 3
                break;
145 3
            case 'string':
146 3
                $data = '\'' . $value . '\'';
147 3
                $format = '(.*?)';
148
149 3
                break;
150
        }
151
152 21
        if ($exact) {
153
            $data = $value;
154
        }
155
156 21
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $format . ';/';
157 21
        $replacement = '$' . $this->config . '[\'' . $item . '\'] = ' . $data . ';';
158
159 21
        $result = preg_replace($pattern, $replacement, $this->lines[$line]);
160
161 21
        $this->lines[$line] = $result;
162 21
    }
163
164
    /**
165
     * Saves the current config.
166
     *
167
     * @return void
168
     */
169 21
    public function save()
170
    {
171 21
        file_put_contents($this->fileName, (string) $this);
172 21
    }
173
174
    /**
175
     * Returns the whole class into a string.
176
     *
177
     * @return string
178
     */
179 21
    public function __toString()
180
    {
181 21
        return implode(PHP_EOL, $this->lines);
182
    }
183
}
184