Conditions | 26 |
Paths | 15169 |
Total Lines | 152 |
Lines | 17 |
Ratio | 11.18 % |
Changes | 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 |
||
34 | function apcal_minical_ex_show($options) |
||
35 | { |
||
36 | global $xoopsConfig, $xoopsDB, $xoopsUser; |
||
37 | |||
38 | // speed check |
||
39 | //global $GIJ_common_time ; |
||
40 | //list($usec, $sec) = explode(" ",microtime()); |
||
41 | //echo ((float) $sec + (float) $usec) - $GIJ_common_time ; |
||
42 | |||
43 | // get bid |
||
44 | if (substr(XOOPS_VERSION, 6, 3) > 2.0) { |
||
45 | // XOOPS 2.1/2.2 |
||
46 | // instanceid as bid from block_instance |
||
47 | $bid = @$GLOBALS['apcal_blockinstance_id']; |
||
48 | } else { |
||
49 | // XOOPS 2.0.x |
||
50 | if (is_object($GLOBALS['block_arr'][$GLOBALS['i']])) { |
||
51 | // bid from newblocks |
||
52 | $bid = $GLOBALS['block_arr'][$GLOBALS['i']]->getVar('bid'); |
||
53 | } else { |
||
54 | return array(); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | $moduleDirName = empty($options[0]) ? basename(dirname(__DIR__)) : $options[0]; |
||
59 | $gifaday = empty($options[1]) ? 2 : (int)$options[1]; |
||
60 | $just1gif = empty($options[2]) ? 0 : 1; |
||
61 | // $plugins_tmp = empty( $options[3] ) ? array() : explode( ',' , $options[3] ) ; |
||
62 | // robots mode (arrows in minicalex will point not a current page but APCal) |
||
63 | $robots_mode = preg_match('/(msnbot|Googlebot|Yahoo! Slurp)/i', $_SERVER['HTTP_USER_AGENT']); |
||
64 | |||
65 | // GET URL extraction |
||
66 | // Only integer values are valid (for preventing from XSS) |
||
67 | $additional_get = ''; |
||
68 | if (!$robots_mode) { |
||
69 | foreach ($_GET as $g_key => $g_val) { |
||
70 | if ($g_key === 'caldate' || $g_key == session_name()) { |
||
71 | continue; |
||
72 | } |
||
73 | if ((int)$g_val != $g_val) { |
||
74 | $additional_get = ''; |
||
75 | break; |
||
76 | } else { |
||
77 | $additional_get .= '&' . urlencode($g_key) . '=' . (int)$g_val; |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // cache enable or not |
||
83 | if (empty($_POST['apcal_jumpcaldate']) |
||
84 | && (empty($_GET['caldate']) |
||
85 | || in_array(substr($_GET['caldate'], 0, 4), array(date('Y'), date('Y') - 1))) |
||
86 | ) { |
||
87 | $enable_cache = true; |
||
88 | // $enable_cache = false ; |
||
89 | } else { |
||
90 | $enable_cache = false; |
||
91 | } |
||
92 | |||
93 | // cache read |
||
94 | if ($enable_cache) { |
||
95 | if (empty($_GET['caldate'])) { |
||
96 | $Ym = date('Ym'); |
||
97 | } else { |
||
98 | list($Y, $m) = explode('-', $_GET['caldate']); |
||
99 | if (empty($m)) { |
||
100 | $Ym = date('Ym'); |
||
101 | } else { |
||
102 | $Ym = sprintf('%04d%02d', $Y, $m); |
||
103 | } |
||
104 | } |
||
105 | $bid_hash = substr(md5($bid . XOOPS_DB_PREFIX), -6); |
||
106 | $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
107 | $cache_file = XOOPS_CACHE_PATH . "/{$moduleDirName }_minical_ex_{$bid_hash}_{$xoopsConfig['language']}_"; |
||
108 | if (file_exists($cache_file . $Ym)) { |
||
109 | $cache_bodies = file($cache_file . $Ym); |
||
110 | if (count($cache_bodies) == 3) { |
||
111 | $expire = (int)$cache_bodies[0]; |
||
112 | $prev_uid = (int)$cache_bodies[1]; |
||
113 | if ($expire > time() && $prev_uid == $uid) { |
||
114 | $block = unserialize($cache_bodies[2]); |
||
115 | View Code Duplication | if ($robots_mode) { |
|
|
|||
116 | $block['root_url'] = $block['mod_url'] . '/'; |
||
117 | // $block['php_self'] = '/' ; |
||
118 | $block['additional_get'] = ''; |
||
119 | } else { |
||
120 | $block['root_url'] = ''; |
||
121 | // $block['php_self'] = $_SERVER['SCRIPT_NAME'] ; |
||
122 | $block['additional_get'] = $additional_get; |
||
123 | } |
||
124 | // speed check |
||
125 | //list($usec, $sec) = explode(" ",microtime()); |
||
126 | //echo ((float) $sec + (float) $usec) - $GIJ_common_time ; |
||
127 | return $block; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // MyTextSanitizer |
||
134 | $myts = MyTextSanitizer::getInstance(); |
||
135 | |||
136 | // setting physical & virtual paths |
||
137 | $mod_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName"; |
||
138 | $mod_url = XOOPS_URL . "/modules/$moduleDirName"; |
||
139 | |||
140 | // defining class of APCal |
||
141 | if (!class_exists('APCal_xoops')) { |
||
142 | require_once "$mod_path/class/APCal.php"; |
||
143 | require_once "$mod_path/class/APCal_xoops.php"; |
||
144 | } |
||
145 | |||
146 | // creating an instance of APCal |
||
147 | $cal = new APCal_xoops('', $xoopsConfig['language'], true); |
||
148 | |||
149 | // ignoring cid from GET |
||
150 | $cal->now_cid = 0; |
||
151 | |||
152 | // setting properties of APCal |
||
153 | $cal->conn = $GLOBALS['xoopsDB']->conn; |
||
154 | include "$mod_path/include/read_configs.php"; |
||
155 | $cal->base_url = $mod_url; |
||
156 | $cal->base_path = $mod_path; |
||
157 | $cal->images_url = "$mod_url/assets/images/$skin_folder"; |
||
158 | $cal->images_path = "$mod_path/assets/images/$skin_folder"; |
||
159 | |||
160 | $block = $cal->get_minical_ex($gifaday, $just1gif, $cal->get_plugins("mcx{$bid}")); |
||
161 | |||
162 | // speed check |
||
163 | // global $GIJ_common_time ; |
||
164 | // list($usec, $sec) = explode(" ",microtime()); |
||
165 | // echo ((float) $sec + (float) $usec) - $GIJ_common_time ; |
||
166 | |||
167 | if ($enable_cache) { |
||
168 | $fp = fopen($cache_file . sprintf('%04d%02d', $cal->year, $cal->month), 'w'); |
||
169 | fwrite($fp, (time() + 300) . "\n"); // 5 mininutes (hard coded) |
||
170 | fwrite($fp, (int)$uid . "\n"); |
||
171 | fwrite($fp, serialize($block)); |
||
172 | fclose($fp); |
||
173 | } |
||
174 | |||
175 | View Code Duplication | if ($robots_mode) { |
|
176 | $block['root_url'] = $block['mod_url'] . '/'; |
||
177 | // $block['php_self'] = '/' ; |
||
178 | $block['additional_get'] = ''; |
||
179 | } else { |
||
180 | $block['root_url'] = ''; |
||
181 | $block['additional_get'] = $additional_get; |
||
182 | } |
||
183 | |||
184 | return $block; |
||
185 | } |
||
186 | |||
235 |
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.