Passed
Pull Request — develop (#340)
by Felipe
04:56
created

getAppInstance()   C

Complexity

Conditions 12
Paths 97

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 31
c 0
b 0
f 0
nc 97
nop 0
dl 0
loc 52
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
/**
4
 * PHPPgAdmin 6.0.0
5
 */
6
7
\defined('BASE_PATH') || \define('BASE_PATH', \dirname(__DIR__));
8
9
\defined('THEME_PATH') || \define('THEME_PATH', \dirname(__DIR__) . '/assets/themes');
10
// Enforce PHP environment
11
\ini_set('arg_separator.output', '&amp;');
12
13
if (!\is_writable(\dirname(__DIR__) . '/temp')) {
14
    die('Your temp folder must have write permissions (use chmod 777 temp -R on linux)');
15
}
16
17
require_once \dirname(__DIR__) . '/vendor/autoload.php';
18
19
$shouldSetSession = (\defined('PHP_SESSION_ACTIVE') ? \PHP_SESSION_ACTIVE !== \session_status() : !\session_id())
20
    && !\headers_sent()
21
    && !\ini_get('session.auto_start');
22
23
if ($shouldSetSession && \PHP_SAPI !== 'cli') {
24
    \session_set_cookie_params(0, '/', $_SERVER['HTTP_HOST'], isset($_SERVER['HTTPS']));
25
    \session_name('PPA_ID');
26
    \session_start();
27
}
28
if (!\defined('ADODB_ERROR_HANDLER_TYPE')) {
29
    \define('ADODB_ERROR_HANDLER_TYPE', \E_USER_ERROR);
30
}
31
32
if (!\defined('ADODB_ERROR_HANDLER')) {
33
    \define('ADODB_ERROR_HANDLER', '\PHPPgAdmin\ADOdbException::adodb_throw');
34
}
35
function getAppInstance() {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
36
    $subfolder = '';
37
    // Check to see if the configuration file exists, if not, explain
38
    if (!\file_exists(\dirname(__DIR__) . '/config.inc.php')) {
39
        die('Configuration error: Copy config.inc.php-dist to config.inc.php and edit appropriately.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
    }
41
$conf = [];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
42
43
include_once \dirname(__DIR__) . '/config.inc.php';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
44
45
if (isset($conf['subfolder']) && \is_string($conf['subfolder'])) {
46
    $subfolder = $conf['subfolder'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
47
} elseif (\PHP_SAPI === 'cli-server') {
48
    $subfolder = '/index.php';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
49
} elseif (isset($_SERVER['DOCUMENT_ROOT'])) {
50
    $subfolder = \str_replace(
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
51
        $_SERVER['DOCUMENT_ROOT'],
52
        '',
53
        \dirname(__DIR__)
54
    );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
55
}
56
57
$conf['subfolder'] = $subfolder;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
58
59
60
$conf['debugmode'] = (!isset($conf['debugmode'])) ? false : (bool) ($conf['debugmode']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
61
62
63
64
if ($conf['debugmode']) {
65
    \ini_set('display_errors', 'On');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
66
67
    \ini_set('display_startup_errors', 'On');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
68
    \ini_set('opcache.revalidate_freq', '0');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
69
    \error_reporting(\E_ALL);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
70
71
    if (\array_key_exists('register_debuggers', $conf) && \is_callable($conf['register_debuggers'])) {
72
        $conf['register_debuggers']();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
73
    }
74
}
75
76
77
$conf['BASE_PATH'] = BASE_PATH;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
78
$conf['theme_path'] = BASE_PATH . '/assets/themes';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
79
\defined('IN_TEST') || \define('IN_TEST', false);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
80
$conf['IN_TEST'] = IN_TEST;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
81
\ini_set('display_errors', strval($conf['debugmode']));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
82
\defined('ADODB_ASSOC_CASE') || \define('ADODB_ASSOC_CASE', ADODB_ASSOC_CASE_NATIVE);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
83
84
// Fetch App and DI Container
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
85
$app = \PHPPgAdmin\ContainerUtils::getAppInstance($conf);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
86
return $app;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
87
};
88
89
function containerInstance(): \PHPPgAdmin\ContainerUtils
90
{
91
    $app=getAppInstance();
92
    $container = $app->getContainer();
93
                if (!$container instanceof \PHPPgAdmin\ContainerUtils) {
94
                    \trigger_error('App Container must be an instance of \\Slim\\Container', \E_USER_ERROR);
95
                }
96
    return  $container;
97
}
98
99
function requestInstance(): \Slim\Http\Request
100
{
101
    return  \containerInstance()->request;
102
}
103
104
function responseInstance(): \Slim\Http\Response
105
{
106
    return \containerInstance()->response;
107
}
108
$app=getAppInstance();
109
$container=$app->getContainer();
110
111
// This should be deprecated once we're sure no php scripts are required directly
112
$container->offsetSet('server', $_REQUEST['server'] ?? null);
113
$container->offsetSet('database', $_REQUEST['database'] ?? null);
114
$container->offsetSet('schema', $_REQUEST['schema'] ?? null);
115
116
$container['haltHandler'] = static function (\PHPPgAdmin\ContainerUtils $c) {
117
    return static function ($request, $response, $exits, $status = 500) use ($c) {
118
        $title = 'PHPPgAdmin Error';
119
120
        $html = '<p>The application could not run because of the following error:</p>';
121
122
        $output = \sprintf(
123
            "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
124
                '<title>%s</title><style>' .
125
                'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' .
126
                'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' .
127
                'span{display:inline-block;font-size:16px;}' .
128
                '</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>',
129
            $title,
130
            $title,
131
            $html,
132
            \implode('<br>', $exits)
133
        );
134
135
        $body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+'));
136
        $body->write($output);
137
138
        return $response
139
            ->withStatus($status)
140
            ->withHeader('Content-type', 'text/html')
141
            ->withBody($body);
142
    };
143
};
144
145
// Set the requestobj and responseobj properties of the container
146
// as the value of $request and $response, which already contain the route
147
$app->add(new \PHPPgAdmin\Middleware\PopulateRequestResponse($container));
148
149
$container['action'] = $_REQUEST['action'] ?? '';
150
151
if (!isset($msg)) {
152
    $msg = '';
153
}
154
155
$container['msg'] = $msg;
156
//ddd($container->misc);
157