Completed
Push — master ( 8e7dca...6ec0d0 )
by frank
01:41
created

autoptimizeVersionUpdatesHandler   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 197
Duplicated Lines 5.08 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 197
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B run_needed_major_upgrades() 0 27 6
A check_installed_and_update() 0 15 3
A on_major_version_update() 0 9 2
A upgrade_from_1_6() 0 19 2
B upgrade_from_1_7() 10 27 7
A upgrade_from_1_9() 0 17 3
A upgrade_from_2_2() 0 15 3
A do_2_2_settings_update() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Handles version updates and should only be instantiated in autoptimize.php if/when needed.
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
class autoptimizeVersionUpdatesHandler
11
{
12
    /**
13
     * The current major version string.
14
     *
15
     * @var string
16
     */
17
    protected $current_major_version = null;
18
19
    public function __construct( $current_version )
20
    {
21
        $this->current_major_version = substr( $current_version, 0, 3 );
22
    }
23
24
    /**
25
     * Runs all needed upgrade procedures (depending on the
26
     * current major version specified during class instantiation)
27
     */
28
    public function run_needed_major_upgrades()
29
    {
30
        $major_update = false;
31
32
        switch ( $this->current_major_version ) {
33
            case '1.6':
34
                $this->upgrade_from_1_6();
35
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
                // No break, intentionally, so all upgrades are ran during a single request...
37
            case '1.7':
38
                $this->upgrade_from_1_7();
39
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
                // No break, intentionally, so all upgrades are ran during a single request...
41
            case '1.9':
42
                $this->upgrade_from_1_9();
43
                $major_update = true;
0 ignored issues
show
Unused Code introduced by
$major_update is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
                // No break, intentionally, so all upgrades are ran during a single request...
45
            case '2.2':
46
                $this->upgrade_from_2_2();
47
                $major_update = true;
48
                // No break, intentionally, so all upgrades are ran during a single request...
49
        }
50
51
        if ( true === $major_update ) {
52
            $this->on_major_version_update();
53
        }
54
    }
55
56
    /**
57
     * Checks specified version against the one stored in the database under `autoptimize_version` and performs
58
     * any major upgrade routines if needed.
59
     * Updates the database version to the specified $target if it's different to the one currently stored there.
60
     *
61
     * @param string $target Target version to check against (ie., the currently running one).
62
     */
63
    public static function check_installed_and_update( $target )
64
    {
65
        $db_version = get_option( 'autoptimize_version', 'none' );
66
        if ( $db_version !== $target ) {
67
            if ( 'none' === $db_version ) {
68
                add_action( 'admin_notices', 'autoptimizeMain::notice_installed' );
69
            } else {
70
                $updater = new self( $db_version );
71
                $updater->run_needed_major_upgrades();
72
            }
73
74
            // Versions differed, upgrades happened if needed, store the new version.
75
            update_option( 'autoptimize_version', $target );
76
        }
77
    }
78
79
    /**
80
     * Called after any major version update (and it's accompanying upgrade procedure)
81
     * has happened. Clears cache and sets an admin notice.
82
     */
83
    protected function on_major_version_update()
84
    {
85
        // The transients guard here prevents stale object caches from busting the cache on every request.
86
        if ( false == get_transient( 'autoptimize_stale_option_buster' ) ) {
87
            set_transient( 'autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS );
88
            autoptimizeCache::clearall();
89
            add_action( 'admin_notices', 'autoptimizeMain::notice_updated' );
90
        }
91
    }
92
93
    /**
94
     * From back in the days when I did not yet consider multisite.
95
     */
96
    private function upgrade_from_1_6()
97
    {
98
        // If user was on version 1.6.x, force advanced options to be shown by default.
99
        update_option( 'autoptimize_show_adv', '1' );
100
101
        // And remove old options.
102
        $to_delete_options = array(
103
            'autoptimize_cdn_css',
104
            'autoptimize_cdn_css_url',
105
            'autoptimize_cdn_js',
106
            'autoptimize_cdn_js_url',
107
            'autoptimize_cdn_img',
108
            'autoptimize_cdn_img_url',
109
            'autoptimize_css_yui',
110
        );
111
        foreach ( $to_delete_options as $del_opt ) {
112
            delete_option( $del_opt );
113
        }
114
    }
115
116
    /**
117
     * Forces WP 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8
118
     *
119
     * @global $wpdb
120
     */
121
    private function upgrade_from_1_7()
122
    {
123
        if ( ! is_multisite() ) {
124
            $css_exclude = get_option( 'autoptimize_css_exclude' );
125 View Code Duplication
            if ( empty( $css_exclude ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
                $css_exclude = 'admin-bar.min.css, dashicons.min.css';
127
            } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
128
                $css_exclude .= ', dashicons.min.css';
129
            }
130
            update_option( 'autoptimize_css_exclude', $css_exclude );
131
        } else {
132
            global $wpdb;
133
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
134
            $original_blog_id = get_current_blog_id();
135
            foreach ( $blog_ids as $blog_id ) {
136
                switch_to_blog( $blog_id );
137
                $css_exclude = get_option( 'autoptimize_css_exclude' );
138 View Code Duplication
                if ( empty( $css_exclude ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
                    $css_exclude = 'admin-bar.min.css, dashicons.min.css';
140
                } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
141
                    $css_exclude .= ', dashicons.min.css';
142
                }
143
                update_option( 'autoptimize_css_exclude', $css_exclude );
144
            }
145
            switch_to_blog( $original_blog_id );
146
        }
147
    }
148
149
    /**
150
     * 2.0 will not aggregate inline CSS/JS by default, but we want users
151
     * upgrading from 1.9 to keep their inline code aggregated by default.
152
     *
153
     * @global $wpdb
154
     */
155
    private function upgrade_from_1_9()
156
    {
157
        if ( ! is_multisite() ) {
158
            update_option( 'autoptimize_css_include_inline', 'on' );
159
            update_option( 'autoptimize_js_include_inline', 'on' );
160
        } else {
161
            global $wpdb;
162
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
163
            $original_blog_id = get_current_blog_id();
164
            foreach ( $blog_ids as $blog_id ) {
165
                switch_to_blog( $blog_id );
166
                update_option( 'autoptimize_css_include_inline', 'on' );
167
                update_option( 'autoptimize_js_include_inline', 'on' );
168
            }
169
            switch_to_blog( $original_blog_id );
170
        }
171
    }
172
173
    /**
174
     * 2.3 has no "remove google fonts" in main screen, moved to "extra"
175
     *
176
     * @global $wpdb
177
     */
178
    private function upgrade_from_2_2()
179
    {
180
        if ( ! is_multisite() ) {
181
            $this->do_2_2_settings_update();
182
        } else {
183
            global $wpdb;
184
            $blog_ids         = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
185
            $original_blog_id = get_current_blog_id();
186
            foreach ( $blog_ids as $blog_id ) {
187
                switch_to_blog( $blog_id );
188
                $this->do_2_2_settings_update();
189
            }
190
            switch_to_blog( $original_blog_id );
191
        }
192
    }
193
194
    /**
195
     * Helper for 2.2 autoptimize_extra_settings upgrade to avoid duplicate code
196
     */
197
    private function do_2_2_settings_update()
198
    {
199
        $nogooglefont    = get_option( 'autoptimize_css_nogooglefont', '' );
200
        $ao_extrasetting = get_option( 'autoptimize_extra_settings', '' );
201
        if ( ( $nogooglefont ) && ( empty( $ao_extrasetting ) ) ) {
202
            update_option( 'autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options() );
203
        }
204
        delete_option( 'autoptimize_css_nogooglefont' );
205
    }
206
}
207