Conditions | 31 |
Paths | > 20000 |
Total Lines | 168 |
Code Lines | 93 |
Lines | 81 |
Ratio | 48.21 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | function xoops_module_update_news() |
||
19 | { |
||
20 | include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php'; |
||
21 | global $xoopsDB; |
||
|
|||
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 { |
|
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'))) { |
|
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'))) { |
|
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'))) { |
|
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'))) { |
|
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); |
||
150 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `menu` );'); |
||
151 | $result = $xoopsDB->queryF($sql); |
||
152 | |||
153 | // 6) Make files and folders |
||
154 | $dir = XOOPS_ROOT_PATH . '/uploads/news'; |
||
155 | View Code Duplication | if (!is_dir($dir)) { |
|
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)) { |
|
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)) { |
|
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 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state