Completed
Push — master ( 3599e8...a4b34c )
by Rougin
05:04
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
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 Royce 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 string
27
     */
28
    protected $lines;
29
30
    /**
31
     * @param string $config
32
     * @param string $configPath
33
     */
34 36
    public function __construct($config, $configPath)
35
    {
36 36
        $this->config = $config;
37 36
        $this->fileName = $configPath . '/' . $config . '.php';
38
39 36
        $content = file_get_contents($this->fileName);
40
41 36
        $this->lines = explode(PHP_EOL, $content);
0 ignored issues
show
Documentation Bug introduced by
It seems like explode(PHP_EOL, $content) of type array is incompatible with the declared type string of property $lines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42 36
    }
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 36
    public function get($item, $line, $dataType)
53
    {
54 36
        $result = null;
55 36
        $value = null;
56
57
        switch ($dataType) {
58 36
            case 'array':
59 36
                $value = 'array\((.*?)\)';
60
61 36
                break;
62 12
            case 'boolean':
63 6
                $value = '(TRUE|FALSE)';
64
65 6
                break;
66 12
            case 'string':
67 12
                $value = '(.*?)';
68
69 12
                break;
70
        }
71
72 36
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $value . ';/';
73
74 36
        preg_match_all($pattern, $this->lines[$line], $match);
75
76
        switch ($dataType) {
77 36
            case 'array':
78 36
                $result = array_filter($match[1]);
79 36
                $data = '';
80
81 36
                if (! empty($result[0])) {
82 15
                    $data = $result[0];
83 15
                }
84
85 36
                $result = array_filter(explode(',', $data));
86
87 36
                for ($i = 0; $i < count($result); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
88 15
                    $result[$i] = str_replace(['\'', '"'], '', trim($result[$i]));
89 15
                }
90
91 36
                break;
92 12
            case 'boolean':
93 6
                $result = $match[1][0] == 'TRUE';
94
95 6
                break;
96 12
            case 'string':
97 12
                $result = $match[1][0];
98
99 12
                if ($result == '\'\'') {
100 6
                    $result = null;
101 6
                }
102
103 12
                if ($result[0] == '\'' && $result[strlen($result) - 1] == '\'') {
104 12
                    $result = substr($result, 1, strlen($result) - 2);
105 12
                }
106
107 12
                break;
108
        }
109
110 36
        return $result;
111
    }
112
113
    /**
114
     * Sets an value to an item in the config.
115
     * 
116
     * @param string  $item
117
     * @param integer $line
118
     * @param mixed   $value
119
     * @param string  $dataType
120
     * @param boolean $exact
121
     */
122 33
    public function set($item, $line, $value, $dataType, $exact = false)
123
    {
124 33
        $data = null;
125 33
        $format = null;
126
127
        switch ($dataType) {
128 33
            case 'array':
129 33
                for ($i = 0; $i < count($value); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
130 33
                    $value[$i] = '\'' . $value[$i] . '\'';
131 33
                }
132
133 33
                $data = 'array(' . implode(', ', $value) . ')';
134 33
                $format = 'array\([^)]*\)';
135
136 33
                break;
137 33
            case 'boolean':
138 3
                $data = ($value) ? 'TRUE' : 'FALSE';
139 3
                $format = '[^)]*';
140
141 3
                break;
142 33
            case 'string':
143 33
                $data = '\'' . $value . '\'';
144 33
                $format = '(.*?)';
145
146 33
                break;
147
        }
148
149 33
        if ($exact) {
150 30
            $data = $value;
151 30
        }
152
153 33
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $format . ';/';
154 33
        $replacement = '$' . $this->config . '[\'' . $item . '\'] = ' . $data . ';';
155
156 33
        $result = preg_replace($pattern, $replacement, $this->lines[$line]);
157
158 33
        $this->lines[$line] = $result;
159 33
    }
160
161
    /**
162
     * Saves the current config.
163
     * 
164
     * @return void
165
     */
166 33
    public function save()
167
    {
168 33
        file_put_contents($this->fileName, (string) $this);
169 33
    }
170
171
    /**
172
     * Returns the whole class into a string.
173
     * 
174
     * @return string
175
     */
176 33
    public function __toString()
177
    {
178 33
        return implode(PHP_EOL, $this->lines);
179
    }
180
}
181