Completed
Push — master ( d576ea...caa4fc )
by Michael
06:09 queued 03:25
created

update_function.php ➔ xoops_module_update_news()   F

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 168
Code Lines 93

Duplication

Lines 81
Ratio 48.21 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 31
eloc 93
c 2
b 0
f 0
nc 44789760
nop 0
dl 81
loc 168
rs 2

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
 * News functions
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   {@link http://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Voltan
15
 * @package     News
16
 */
17
18
function xoops_module_update_news()
19
{
20
    include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php';
21
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
22
    $errors = 0;
23
24
    //0) Rename all tables
25
26
    if (news_TableExists($xoopsDB->prefix('stories_files'))) {
27
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files'));
28
        $result = $xoopsDB->queryF($sql);
29
        if (!$result) {
30
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
31
            ++$errors;
32
        }
33 View Code Duplication
    } else {
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...
34
35
        // 1) Create, if it does not exists, the stories_files table
36
        if (!news_TableExists($xoopsDB->prefix('news_stories_files'))) {
37
            $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_files') . " (
38
              fileid int(8) unsigned NOT NULL auto_increment,
39
              filerealname varchar(255) NOT NULL default '',
40
              storyid int(8) unsigned NOT NULL default '0',
41
              date int(10) NOT NULL default '0',
42
              mimetype varchar(64) NOT NULL default '',
43
              downloadname varchar(255) NOT NULL default '',
44
              counter int(8) unsigned NOT NULL default '0',
45
              PRIMARY KEY  (fileid),
46
              KEY storyid (storyid)
47
            ) ENGINE=MyISAM;";
48
            if (!$xoopsDB->queryF($sql)) {
49
                echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED1;
50
                ++$errors;
51
            }
52
        }
53
    }
54
55 View Code Duplication
    if (news_TableExists($xoopsDB->prefix('stories'))) {
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...
56
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories') . ' RENAME ' . $xoopsDB->prefix('news_stories'));
57
        $result = $xoopsDB->queryF($sql);
58
        if (!$result) {
59
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
60
            ++$errors;
61
        }
62
    }
63
64 View Code Duplication
    if (news_TableExists($xoopsDB->prefix('topics'))) {
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...
65
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('topics') . ' RENAME ' . $xoopsDB->prefix('news_topics'));
66
        $result = $xoopsDB->queryF($sql);
67
        if (!$result) {
68
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
69
            ++$errors;
70
        }
71
    }
72
73 View Code Duplication
    if (news_TableExists($xoopsDB->prefix('stories_files'))) {
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...
74
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files'));
75
        $result = $xoopsDB->queryF($sql);
76
        if (!$result) {
77
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
78
            ++$errors;
79
        }
80
    }
81
82
    // 2) Change the topic title's length, in the topics table
83
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' CHANGE topic_title topic_title VARCHAR( 255 ) NOT NULL;');
84
    $result = $xoopsDB->queryF($sql);
85
    if (!$result) {
86
        echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
87
        ++$errors;
88
    }
89
90
    // 2.1) Add the new fields to the topic table
91
    if (!news_FieldExists('menu', $xoopsDB->prefix('news_topics'))) {
92
        news_AddField("menu TINYINT( 1 ) DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_topics'));
93
    }
94
    if (!news_FieldExists('topic_frontpage', $xoopsDB->prefix('news_topics'))) {
95
        news_AddField("topic_frontpage TINYINT( 1 ) DEFAULT '1' NOT NULL", $xoopsDB->prefix('news_topics'));
96
    }
97
    if (!news_FieldExists('topic_rssurl', $xoopsDB->prefix('news_topics'))) {
98
        news_AddField('topic_rssurl VARCHAR( 255 ) NOT NULL', $xoopsDB->prefix('news_topics'));
99
    }
100
    if (!news_FieldExists('topic_description', $xoopsDB->prefix('news_topics'))) {
101
        news_AddField('topic_description TEXT NOT NULL', $xoopsDB->prefix('news_topics'));
102
    }
103
    if (!news_FieldExists('topic_color', $xoopsDB->prefix('news_topics'))) {
104
        news_AddField("topic_color varchar(6) NOT NULL default '000000'", $xoopsDB->prefix('news_topics'));
105
    }
106
107
    // 3) If it does not exists, create the table stories_votedata
108 View Code Duplication
    if (!news_TableExists($xoopsDB->prefix('news_stories_votedata'))) {
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...
109
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_votedata') . " (
110
              ratingid int(11) unsigned NOT NULL auto_increment,
111
              storyid int(8) unsigned NOT NULL default '0',
112
              ratinguser int(11) NOT NULL default '0',
113
              rating tinyint(3) unsigned NOT NULL default '0',
114
              ratinghostname varchar(60) NOT NULL default '',
115
              ratingtimestamp int(10) NOT NULL default '0',
116
              PRIMARY KEY  (ratingid),
117
              KEY ratinguser (ratinguser),
118
              KEY ratinghostname (ratinghostname),
119
              KEY storyid (storyid)
120
            ) ENGINE=MyISAM;";
121
        if (!$xoopsDB->queryF($sql)) {
122
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED3;
123
            ++$errors;
124
        }
125
    }
126
127
    // 4) Create the four new fields for the votes in the story table
128
    if (!news_FieldExists('rating', $xoopsDB->prefix('news_stories'))) {
129
        news_AddField("rating DOUBLE( 6, 4 ) DEFAULT '0.0000' NOT NULL", $xoopsDB->prefix('news_stories'));
130
    }
131
    if (!news_FieldExists('votes', $xoopsDB->prefix('news_stories'))) {
132
        news_AddField("votes INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_stories'));
133
    }
134
    if (!news_FieldExists('keywords', $xoopsDB->prefix('news_stories'))) {
135
        news_AddField('keywords VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
136
    }
137
    if (!news_FieldExists('description', $xoopsDB->prefix('news_stories'))) {
138
        news_AddField('description VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
139
    }
140
    if (!news_FieldExists('pictureinfo', $xoopsDB->prefix('news_stories'))) {
141
        news_AddField('pictureinfo VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
142
    }
143
    if (!news_FieldExists('subtitle', $xoopsDB->prefix('news_stories'))) {
144
        news_AddField('subtitle VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
145
    }
146
147
    // 5) Add some indexes to the topics table
148
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `topic_title` );');
149
    $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
$result 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...
150
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `menu` );');
151
    $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
$result 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...
152
153
    // 6) Make files and folders
154
    $dir = XOOPS_ROOT_PATH . '/uploads/news';
155 View Code Duplication
    if (!is_dir($dir)) {
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...
156
        mkdir($dir);
157
        chmod($dir, 0777);
158
    } elseif (!is_writable($dir)) {
159
        chmod($dir, 0777);
160
    }
161
162
    $dir = XOOPS_ROOT_PATH . '/uploads/news/file';
163 View Code Duplication
    if (!is_dir($dir)) {
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...
164
        mkdir($dir);
165
        chmod($dir, 0777);
166
    } elseif (!is_writable($dir)) {
167
        chmod($dir, 0777);
168
    }
169
170
    $dir = XOOPS_ROOT_PATH . '/uploads/news/image';
171 View Code Duplication
    if (!is_dir($dir)) {
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...
172
        mkdir($dir);
173
        chmod($dir, 0777);
174
    } elseif (!is_writable($dir)) {
175
        chmod($dir, 0777);
176
    }
177
178
    // Copy index.html files on uploads folders
179
    $indexFile = XOOPS_ROOT_PATH . '/modules/news/include/index.html';
180
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/index.html');
181
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/file/index.html');
182
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/image/index.html');
183
184
    return true;
185
}
186