Completed
Push — master ( 1015df...614308 )
by Rougin
02:40
created

SparkPlug::setCharSet()   D

Complexity

Conditions 10
Paths 96

Size

Total Lines 44
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.4632

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 44
ccs 20
cts 24
cp 0.8333
rs 4.8196
cc 10
eloc 17
nc 96
nop 0
crap 10.4632

How to fix   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\SparkPlug;
4
5
use CI_Controller;
6
use FilesystemIterator;
7
use RecursiveIteratorIterator;
8
use RecursiveDirectoryIterator;
9
10
/**
11
 * Spark Plug Class
12
 *
13
 * Returns the CodeIgniter's instance.
14
 * 
15
 * @package SparkPlug
16
 * @author  Rougin Royce Gutib <[email protected]>
17
 */
18
class SparkPlug
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $globals = [];
24
25
    /**
26
     * @var array
27
     */
28
    protected $server = [];
29
30
    /**
31
     * @var string
32
     */
33
    protected $path = '';
34
35
    /**
36
     * @param array  $globals
37
     * @param array  $server
38
     * @param string $path
39
     */
40 3
    public function __construct(array &$globals, array $server, $path = '')
41
    {
42 3
        $this->globals =& $globals;
43 3
        $this->server = $server;
44 3
        $this->path = $path;
45
46 3
        $this->setPaths();
47 3
        $this->setEnvironment();
48 3
        $this->loadConstants();
49 3
        $this->loadClasses();
50 3
        $this->setCharSet();
51 3
    }
52
53
    /**
54
     * Gets an instance of CodeIgniter.
55
     * 
56
     * @return CodeIgniter
57
     */
58 3
    public function getCodeIgniter()
59
    {
60
        // Sets global configurations
61 3
        $this->globals['CFG'] =& load_class('Config', 'core');
62 3
        $this->globals['UNI'] =& load_class('Utf8', 'core');
63 3
        $this->globals['SEC'] =& load_class('Security', 'core');
64
65
        // Loads the CodeIgniter's core classes
66 3
        load_class('Loader', 'core');
67 3
        load_class('Router', 'core');
68 3
        load_class('Input', 'core');
69 3
        load_class('Lang', 'core');
70
71 3
        require 'get_instance.php';
72
73 3
        return new CI_Controller;
74
    }
75
76
    /**
77
     * Loads the Common and the Base Controller class.
78
     * 
79
     * @return void
80
     */
81 3
    protected function loadClasses()
82
    {
83 3
        require BASEPATH . 'core/Common.php';
84
85 3
        if ( ! class_exists('CI_Controller')) {
86 3
            require BASEPATH . 'core/Controller.php';
87 3
        }
88 3
    }
89
90
    /**
91
     * Loads the framework constants.
92
     * 
93
     * @return void
94
     */
95 3
    protected function loadConstants()
96
    {
97 3
        if (defined('FILE_READ_MODE')) {
98
            return;
99
        }
100
101 3
        $constants = APPPATH . 'config/' . ENVIRONMENT . '/constants.php';
102
103 3
        if (file_exists($constants)) {
104
            return require $constants;
105
        }
106
107 3
        return require APPPATH . 'config/constants.php';
108
    }
109
110
    /**
111
     * Sets up important charset-related stuff.
112
     *
113
     * @return void
114
     */
115 3
    protected function setCharSet()
116
    {
117 3
        $charset = strtoupper(config_item('charset'));
118
119 3
        ini_set('default_charset', $charset);
120
121 3
        if ( ! extension_loaded('mbstring')) {
122
            define('MB_ENABLED', FALSE);
123
        }
124
125 3
        if ( ! defined('MB_ENABLED')) {
126 3
            define('MB_ENABLED', TRUE);
127 3
        }
128
129
        // mbstring.internal_encoding is deprecated starting with PHP 5.6
130
        // and it's usage triggers E_DEPRECATED messages.
131
        
132 3
        if ( ! is_php('5.6') && ! ini_get('mbstring.internal_encoding')) {
133 2
            ini_set('mbstring.internal_encoding', $charset);
134 2
        }
135
136
        // This is required for mb_convert_encoding() to strip invalid
137
        // characters. That's utilized by CI_Utf8, but it's also done for
138
        // consistency with iconv.
139 3
        mb_substitute_character('none');
140
141
        // There's an ICONV_IMPL constant, but the PHP manual says that using
142
        // iconv's predefined constants is "strongly discouraged".
143 3
        if ( ! defined('ICONV_ENABLED')) {
144 3
            $isEnabled = extension_loaded('iconv') ? TRUE: FALSE;
145
146 3
            define('ICONV_ENABLED', $isEnabled);
147 3
        }
148
149
        // iconv.internal_encoding is deprecated starting with PHP 5.6
150
        // and it's usage triggers E_DEPRECATED messages.
151 3
        if ( ! is_php('5.6') && ! ini_get('iconv.internal_encoding')) {
152
            ini_set('iconv.internal_encoding', $charset);
153
        }
154
155 3
        if (is_php('5.6')) {
156 1
            ini_set('php.internal_encoding', $charset);
157 1
        }
158 3
    }
159
160
    /**
161
     * Sets up the current environment.
162
     *
163
     * @return void
164
     */
165 3
    protected function setEnvironment()
166
    {
167 3
        if ( ! defined('ENVIRONMENT')) {
168 3
            $environment = isset($this->server['CI_ENV'])
169 3
                ? $this->server['CI_ENV']
170 3
                : 'development';
171
172 3
            define('ENVIRONMENT', $environment);
173 3
        }
174 3
    }
175
176
    /**
177
     * Sets up the APPPATH, VENDOR, and BASEPATH constants.
178
     * 
179
     * @return void
180
     */
181 3
    protected function setPaths()
182
    {
183 3
        $applicationPath = realpath('application');
184 3
        $vendorPath = realpath('vendor');
185
186 3
        if ($this->path) {
187 3
            $vendorPath = $this->path . '/vendor';
188 3
            $applicationPath = $this->path . '/application';
189 3
        }
190
191 3
        if ( ! defined('APPPATH')) {
192 3
            define('APPPATH', $applicationPath . '/');
193 3
        }
194
195 3
        if ( ! defined('VENDOR')) {
196 3
            define('VENDOR', $vendorPath . '/');
197 3
        }
198
199 3
        if ( ! defined('VIEWPATH')) {
200 3
            define('VIEWPATH', APPPATH . '/views/');
201 3
        }
202
203 3
        if (defined('BASEPATH')) {
204
            return;
205
        }
206
207 3
        $iterator = new RecursiveIteratorIterator(
208 3
            new RecursiveDirectoryIterator(getcwd())
209 3
        );
210
211 3
        foreach ($iterator as $file) {
212 3
            $core = 'core' . DIRECTORY_SEPARATOR . 'CodeIgniter.php';
213
214 3
            if (strpos($file->getPathname(), $core) !== FALSE) {
215 3
                $path = str_replace($core, '', $file->getPathname());
216
217 3
                define('BASEPATH', $path);
218
219 3
                break;
220
            }
221 3
        }
222 3
    }
223
}
224