Conditions | 19 |
Paths | 184 |
Total Lines | 92 |
Lines | 0 |
Ratio | 0 % |
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 |
||
29 | function apcal_mini_calendar_show($options) |
||
30 | { |
||
31 | global $xoopsConfig, $xoopsDB, $xoopsUser; |
||
32 | |||
33 | $moduleDirName = empty($options[0]) ? basename(dirname(__DIR__)) : $options[0]; |
||
34 | |||
35 | // caldate ¤äÆüÉÕ¥¸¥ã¥ó¥×¤Î»ØÄ̵꤬¤¯¡ÊÅö·î¤Î¥Ç¥Õ¥©¥ë¥È¥ß¥Ë¥«¥ì¥ó¥À¡¼É½¼¨¡Ë |
||
36 | // ¤«¤Ä¡¢¥æ¡¼¥¶¤ÎTimezone¤¬defaultTZ¤È°ì½ï¡Ê°ìÈÖ¿¤½¤¦¤Ê¥·¥Á¥å¥¨¡¼¥·¥ç¥ó¡Ë¤Î |
||
37 | // ¾ì¹ç¤Ë¤Ï¡¢¥¥ã¥Ã¥·¥å¤ò»È¤¦ |
||
38 | if (empty($_GET['caldate']) && empty($_POST['apcal_jumpcaldate']) |
||
39 | && (!is_object($xoopsUser) |
||
40 | || $xoopsUser->timezone() == $xoopsConfig['default_TZ']) |
||
41 | ) { |
||
42 | $use_cache = true; |
||
43 | $cachefile = XOOPS_CACHE_PATH . "/{$moduleDirName }_minical_cache_{$xoopsConfig['language']}.html"; |
||
44 | // 5 minutes |
||
45 | if (file_exists($cachefile) && filemtime($cachefile) > time() - 300) { |
||
46 | if (false !== $fp = fopen($cachefile, 'r')) { |
||
47 | $block['content'] = ''; |
||
|
|||
48 | while (!feof($fp)) { |
||
49 | $block['content'] .= fgets($fp, 4096); |
||
50 | } |
||
51 | fclose($fp); |
||
52 | |||
53 | return $block; |
||
54 | } |
||
55 | } |
||
56 | } else { |
||
57 | $use_cache = false; |
||
58 | } |
||
59 | |||
60 | // setting physical & virtual paths |
||
61 | $mod_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName"; |
||
62 | $mod_url = XOOPS_URL . "/modules/$moduleDirName"; |
||
63 | |||
64 | // defining class of APCal |
||
65 | if (!class_exists('APCal_xoops')) { |
||
66 | require_once "$mod_path/class/APCal.php"; |
||
67 | require_once "$mod_path/class/APCal_xoops.php"; |
||
68 | } |
||
69 | |||
70 | // creating an instance of APCal |
||
71 | $cal = new APCal_xoops('', $xoopsConfig['language'], true); |
||
72 | |||
73 | // ignoring cid from GET |
||
74 | $cal->now_cid = 0; |
||
75 | |||
76 | // setting properties of APCal |
||
77 | $cal->conn = $GLOBALS['xoopsDB']->conn; |
||
78 | include "$mod_path/include/read_configs.php"; |
||
79 | $cal->base_url = $mod_url; |
||
80 | $cal->base_path = $mod_path; |
||
81 | $cal->images_url = "$mod_url/assets/images/$skin_folder"; |
||
82 | $cal->images_path = "$mod_path/assets/images/$skin_folder"; |
||
83 | |||
84 | switch ($mini_calendar_target) { |
||
85 | case 'MONTHLY': |
||
86 | $get_target = "$mod_url/index.php"; |
||
87 | $query_string = 'smode=Monthly'; |
||
88 | break; |
||
89 | case 'WEEKLY': |
||
90 | $get_target = "$mod_url/index.php"; |
||
91 | $query_string = 'smode=Weekly'; |
||
92 | break; |
||
93 | case 'DAILY': |
||
94 | $get_target = "$mod_url/index.php"; |
||
95 | $query_string = 'smode=Daily'; |
||
96 | break; |
||
97 | case 'LIST': |
||
98 | $get_target = "$mod_url/index.php"; |
||
99 | $query_string = 'smode=List'; |
||
100 | break; |
||
101 | default: |
||
102 | case 'PHP_SELF': |
||
103 | $get_target = ''; |
||
104 | $query_string = ''; |
||
105 | break; |
||
106 | } |
||
107 | |||
108 | $block = array(); |
||
109 | $block['content'] = $cal->get_mini_calendar_html($get_target, $query_string); |
||
110 | |||
111 | // ¥¥ã¥Ã¥·¥å¤Î½ñ¤½Ð¤· |
||
112 | if ($use_cache && $mini_calendar_target !== 'PHP_SELF') { |
||
113 | if (false !== $fp = fopen($cachefile, 'w')) { |
||
114 | fwrite($fp, $block['content']); |
||
115 | fclose($fp); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return $block; |
||
120 | } |
||
121 | |||
131 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.