Init::__construct()   F
last analyzed

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 100
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 870

Importance

Changes 0
Metric Value
cc 29
eloc 43
c 0
b 0
f 0
nc 663808
nop 1
dl 0
loc 100
ccs 0
cts 44
cp 0
crap 870
rs 0

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
 * YOURLS actions upon instantiating
5
 */
6
7
namespace YOURLS\Config;
8
9
class Init {
10
11
    /**
12
     * @param InitDefaults
0 ignored issues
show
Coding Style Documentation introduced by
@param tag is not allowed in member variable comment
Loading history...
13
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @var tag in member variable comment
Loading history...
14
    protected $actions;
15
16
    /**
17
     * @since  1.7.3
18
     *
19
     * @param InitDefaults $actions
20
     */
21
    public function __construct(InitDefaults $actions) {
22
23
        $this->actions = $actions;
24
25
        // Include core files
26
        if ($actions->include_core_funcs === true) {
27
            $this->include_core_functions();
28
        }
29
30
        // Enforce UTC timezone. Date/time can be adjusted with a plugin.
31
        if ($actions->default_timezone === true) {
32
            date_default_timezone_set( 'UTC' );
33
        }
34
35
        // Load locale
36
        if ($actions->load_default_textdomain === true) {
37
            yourls_load_default_textdomain();
38
        }
39
40
        // Check if we are in maintenance mode - if yes, it will die here.
41
        if ($actions->check_maintenance_mode === true) {
42
            yourls_check_maintenance_mode();
43
        }
44
45
        // Fix REQUEST_URI for IIS
46
        if ($actions->fix_request_uri === true) {
47
            yourls_fix_request_uri();
48
        }
49
50
        // If request for an admin page is http:// and SSL is required, redirect
51
        if ($actions->redirect_ssl === true) {
52
            $this->redirect_ssl_if_needed();
53
        }
54
55
        // Create the YOURLS object $ydb that will contain everything we globally need
56
        if ($actions->include_db === true) {
57
            $this->include_db_files();
58
        }
59
60
        // Allow early inclusion of a cache layer
61
        if ($actions->include_cache === true) {
62
            $this->include_cache_files();
63
        }
64
65
        // Abort initialization here if fast init wanted (for tests/debug/do not use)
66
        if ($actions->return_if_fast_init === true && defined('YOURLS_FAST_INIT') && YOURLS_FAST_INIT){
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 103 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
            return;
68
        }
69
70
        // Read options right from start
71
        if ($actions->get_all_options === true) {
72
            yourls_get_all_options();
73
        }
74
75
        // Register shutdown function
76
        if ($actions->register_shutdown === true) {
77
            register_shutdown_function( 'yourls_shutdown' );
78
        }
79
80
        // Core now loaded
81
        if ($actions->core_loaded === true) {
82
            yourls_do_action( 'init' ); // plugins can't see this, not loaded yet
83
        }
84
85
        // Check if need to redirect to install procedure
86
        if ($actions->redirect_to_install === true) {
87
            if (!yourls_is_installed() && !yourls_is_installing()) {
88
                yourls_no_cache_headers();
89
                yourls_redirect( yourls_admin_url('install.php'), 307 );
90
            }
91
        }
92
93
        // Check if upgrade is needed (bypassed if upgrading or installing)
94
        if ($actions->check_if_upgrade_needed === true) {
95
            if (!yourls_is_upgrading() && !yourls_is_installing() && yourls_upgrade_is_needed()) {
96
                yourls_no_cache_headers();
97
                yourls_redirect( yourls_admin_url('upgrade.php'), 307 );
98
            }
99
        }
100
101
        // Load all plugins
102
        if ($actions->load_plugins === true) {
103
            yourls_load_plugins();
104
        }
105
106
        // Trigger plugin loaded action
107
        if ($actions->plugins_loaded_action === true) {
108
            yourls_do_action( 'plugins_loaded' );
109
        }
110
111
        // Is there a new version of YOURLS ?
112
        if ($actions->check_new_version === true) {
113
            if (yourls_is_installed() && !yourls_is_upgrading()) {
114
                yourls_tell_if_new_version();
115
            }
116
        }
117
118
        if ($actions->init_admin === true) {
119
            if (yourls_is_admin()) {
120
                yourls_do_action( 'admin_init' );
121
            }
122
        }
123
124
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
125
126
    /**
127
     * @since  1.7.3
128
     * @return void
129
     */
130
    public function redirect_ssl_if_needed() {
131
        if (yourls_is_admin() && yourls_needs_ssl() && !yourls_is_ssl()) {
132
            if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
133
                yourls_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
            } else {
135
                yourls_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
136
            }
137
            exit();
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...
138
        }
139
    }
140
141
    /**
142
     * @since  1.7.3
143
     * @return void
144
     */
145
    public function include_db_files() {
146
        // Allow drop-in replacement for the DB engine
147
        if (file_exists(YOURLS_USERDIR.'/db.php')) {
148
            require_once YOURLS_USERDIR.'/db.php';
149
        } else {
150
            require_once YOURLS_INC.'/class-mysql.php';
151
            yourls_db_connect();
152
        }
153
    }
154
155
    /**
156
     * @since  1.7.3
157
     * @return void
158
     */
159
    public function include_cache_files() {
160
        if (file_exists(YOURLS_USERDIR.'/cache.php')) {
161
            require_once YOURLS_USERDIR.'/cache.php';
162
        }
163
    }
164
165
    /**
166
     * @since  1.7.3
167
     * @return void
168
     */
169
    public function include_core_functions() {
170
        require_once YOURLS_INC.'/version.php';
171
        require_once YOURLS_INC.'/functions.php';
172
        require_once YOURLS_INC.'/functions-geo.php';
173
        require_once YOURLS_INC.'/functions-shorturls.php';
174
        require_once YOURLS_INC.'/functions-debug.php';
175
        require_once YOURLS_INC.'/functions-options.php';
176
        require_once YOURLS_INC.'/functions-links.php';
177
        require_once YOURLS_INC.'/functions-plugins.php';
178
        require_once YOURLS_INC.'/functions-formatting.php';
179
        require_once YOURLS_INC.'/functions-api.php';
180
        require_once YOURLS_INC.'/functions-kses.php';
181
        require_once YOURLS_INC.'/functions-l10n.php';
182
        require_once YOURLS_INC.'/functions-compat.php';
183
        require_once YOURLS_INC.'/functions-html.php';
184
        require_once YOURLS_INC.'/functions-http.php';
185
        require_once YOURLS_INC.'/functions-infos.php';
186
        require_once YOURLS_INC.'/functions-deprecated.php';
187
        require_once YOURLS_INC.'/functions-auth.php';
188
189
        // Load install & upgrade functions if needed
190
        if ($this->actions->include_install_upgrade_funcs === true) {
191
            require_once YOURLS_INC.'/functions-upgrade.php';
192
            require_once YOURLS_INC.'/functions-install.php';
193
        }
194
    }
195
196
}
197