functions.php ➔ start()   C
last analyzed

Complexity

Conditions 7
Paths 48

Size

Total Lines 79
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 35
nc 48
nop 1
dl 0
loc 79
rs 6.5649
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * Defines some basic functionality used by the engine
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Init
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\init;
17
18
/**
19
 * This handles throwed exceptions which are not catched
20
 *
21
 * @param \Exception $exception Exception
22
 *
23
 * @return void
24
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
25
26
function handlerExceptions(\Exception $exception)
27
{
28
    // Create instance of error class and submit exception
29
    $controller = new \csphere\core\errors\Controller($exception);
30
31
    unset($controller);
32
}
33
34
/**
35
 * This handles classic php errors
36
 *
37
 * @param int    $errno   Error Number
38
 * @param string $errstr  Error String
39
 * @param string $errfile Error File
40
 * @param int    $errline Error Line
41
 *
42
 * @return void
43
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
44
45
function handlerErrors($errno, $errstr, $errfile, $errline)
46
{
47
    $exception = new \ErrorException($errstr, $errno, 0, $errfile, $errline);
48
49
    // Create instance of error class and submit exception plus continue
50
    $controller = new \csphere\core\errors\Controller($exception, true);
51
52
    unset($controller);
53
}
54
55
56
/**
57
 * Autoloader fallback for commandline and builtin webserver
58
 *
59
 * @param string $class Class
60
 *
61
 * @throws \Exception
62
 *
63
 * @return void
64
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
65
66
function handlerAutoloads($class)
67
{
68
    // Class var comes with namespace which makes it easy
69
    $class = strtolower(str_replace('\\', '/', $class));
70
71
    include \csphere\core\init\path() . $class . '.php';
72
}
73
74
/**
75
 * Check for errors on shutdown
76
 *
77
 * @return void
78
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
79
80
function handlerShutdown()
81
{
82
    // Errors displayed in here cause side-effects
83
    ini_set('display_errors', 0);
84
85
    // Get last error
86
    $error = error_get_last();
87
88
    if (is_array($error)) {
89
90
        $exception = new \ErrorException(
91
            $error['message'], $error['type'], 0, $error['file'], $error['line']
92
        );
93
94
        // Create instance of error class and submit exception
95
        $controller = new \csphere\core\errors\Controller($exception);
96
97
        unset($controller);
98
    }
99
}
100
101
/**
102
 * Determine the root directory where index.php is located
103
 *
104
 * @return string
105
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
106
107
function path()
108
{
109
    static $dir = '';
110
111
    // Look for the directory outside the csphere namespace
112
    if (empty($dir)) {
113
        $dir = str_replace('\\', '/', __DIR__);
114
        $dir = str_replace('csphere/core/init', '', $dir);
115
    }
116
117
    return $dir;
118
}
119
120
/**
121
 * Start up engine next to basic function definitions
122
 *
123
 * @param array $forcedConfig Array to force a Config Value
124
 *
125
 * @return void
126
 **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
127
128
function start(array $forcedConfig=[])
129
{
130
    // Report all errors
131
    ini_set('display_errors', 1);
132
    error_reporting(E_ALL | E_STRICT);
133
134
    // Set timezone to UTC so that times are consistent
135
    date_default_timezone_set('UTC');
136
137
    // Get current microtime for performance measurement
138
    $start = microtime(true);
139
140
    // File extensions for autoloads
141
    spl_autoload_extensions('.php');
142
143
    // Console, built-in-webserver and HHVM need some assistance
144
    $sapi = strtolower(php_sapi_name());
145
    $need = ['cli', 'cli-server', 'srv'];
146
147
    if (in_array($sapi, $need)) {
148
149
        // Special case for commandline and builtin webserver
150
        spl_autoload_register(__NAMESPACE__ . '\handlerAutoloads');
151
152
    } else {
153
154
        spl_autoload_register();
155
    }
156
157
    // Set error and exception handlers
158
    spl_autoload_call('csphere\core\errors\Controller');
159
    set_error_handler(__NAMESPACE__ . '\handlerErrors');
160
    set_exception_handler(__NAMESPACE__ . '\handlerExceptions');
161
    register_shutdown_function(__NAMESPACE__ . '\handlerShutdown');
162
163
    // Read config file and check for errors
164
    $conf   = new \csphere\core\init\Config();
165
    $error  = $conf->error();
166
    $config = $conf->get();
167
168
    //Overwrite default config values
169
    foreach ($forcedConfig as $cat=>$values) {
170
        foreach ($values as $key=>$value) {
171
            $config[$cat][$key]=$value;
172
        }
173
    }
174
175
    // Turn on display_errors if debug is activated
176
    $debug = (empty($config['view']['debug'])) ? 0 : 1;
177
178
    ini_set('display_errors', $debug);
179
180
    // Check and set config flags
181
    $config['view']['parsetime'] = $start;
182
183
    if (empty($config['view']['driver'])) {
184
185
        $config['view']['driver'] = 'html';
186
        $config['view']['theme']  = 'install';
187
    }
188
189
    // Pass config to service loader
190
    \csphere\core\service\Locator::start($config);
191
192
    // Prepare input for later usage
193
    \csphere\core\http\Input::prepare();
194
195
    // Execute request if no config error was found
196
    if ($error == []) {
197
198
        $router = new \csphere\core\router\Controller(true);
199
200
        $router->execute();
201
202
    } else {
203
204
        \csphere\core\errors\Startup::rescue($error);
205
    }
206
}
207