apcal_mini_calendar.php ➔ apcal_mini_calendar_show()   F
last analyzed

Complexity

Conditions 19
Paths 184

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 184
nop 1
dl 0
loc 92
rs 3.8166
c 0
b 0
f 0

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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright   {@link http://xoops.org/ XOOPS Project}
14
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team,
18
 * @author       Antiques Promotion (http://www.antiquespromotion.ca)
19
 * @author       GIJ=CHECKMATE (PEAK Corp. http://www.peak.ne.jp/)
20
 */
21
22
if (!defined('APCAL_BLOCK_MINI_CALENDAR_INCLUDED')) {
23
    define('APCAL_BLOCK_MINI_CALENDAR_INCLUDED', 1);
24
25
    /**
26
     * @param $options
27
     * @return array
28
     */
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'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$block was never initialized. Although not strictly required by PHP, it is generally a good practice to add $block = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
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";
0 ignored issues
show
Bug introduced by
The variable $skin_folder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
        $cal->images_path = "$mod_path/assets/images/$skin_folder";
83
84
        switch ($mini_calendar_target) {
0 ignored issues
show
Bug introduced by
The variable $mini_calendar_target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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':
0 ignored issues
show
Unused Code introduced by
case 'PHP_SELF': $ge...string = ''; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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')) {
0 ignored issues
show
Bug introduced by
The variable $cachefile does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
114
                fwrite($fp, $block['content']);
115
                fclose($fp);
116
            }
117
        }
118
119
        return $block;
120
    }
121
122
    /**
123
     * @param $options
124
     * @return string
125
     */
126
    function apcal_mini_calendar_edit($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        return '';
129
    }
130
}
131