Completed
Push — master ( eb0f84...204b42 )
by Michael
02:55
created

views.php ➔ userlog_views_edit()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 84
Code Lines 63

Duplication

Lines 6
Ratio 7.14 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 63
c 1
b 0
f 0
nc 5
nop 1
dl 6
loc 84
rs 6.5389

How to fix   Long Method   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 23.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
 *  userlog module
13
 *
14
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
15
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
16
 * @package         userlog blocks
17
 * @since           1
18
 * @author          irmtfan ([email protected])
19
 * @author          The XOOPS Project <www.xoops.org> <www.xoops.ir>
20
 * @version         $Id: views.php 1 2013-02-26 16:25:04Z irmtfan $
21
 */
22
23
defined('XOOPS_ROOT_PATH') or die('Restricted access');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
24
include_once dirname(dirname(__FILE__)) . '/include/common.php';
25
26
if (defined('USERLOG_BLOCK_VIEWS_DEFINED')) return;
27
define('USERLOG_BLOCK_VIEWS_DEFINED',true);
28
xoops_loadLanguage("admin",USERLOG_DIRNAME);
29
30
// options[0] - number of items to show in block. the default is 10
31
// options[1] - items to select in Where claus
32
// options[2] - Time period - default: 1 day
33
// options[3] - Uid in WHERE claus: select some users to only count views by them -1 -> all (by default)
34
// options[4] - Gid in WHERE claus: select some groups to only count views by them 0 -> all (by default)
35
// options[5] - Sort - views, module dirname, module name, module views default: views
36
// options[6] - Order - DESC, ASC default: DESC
37
38
function userlog_views_show($options)
39
{
40
    $loglogObj = UserlogLog::getInstance();
41
    $module=array();
42
    if (!empty($options[1])) {
43
        $options_views = explode(',', $options[1]); // item views in where claus eg: news-storyid, newbb-topic_id, news-storytopic
44 View Code Duplication
        foreach ($options_views as $key=>$item) {
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...
45
            $module_script_item = explode('-', $item); // news:article.php-storyid news:index.php-storytopic => $module["news"]=array("storyid","storytopic");
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
            $module_script = explode(':', $module_script_item[0]); // 	news:article.php => $module_script = array(news,article.php);
47
            if (!isset($module[$module_script[0]])) {
48
                $module[$module_script[0]]["item_name"] = array();
49
                $module[$module_script[0]]["script"] = array_slice($module_script,1);
50
            }
51
            $module[$module_script[0]]["script"] = array_unique(array_merge($module[$module_script[0]]["script"], array_slice($module_script,1)));
52
            $module[$module_script[0]]["item_name"][] = $module_script_item[1];
53
        }
54
    }
55
    $users = ($options[3] != -1) ? explode(",",$options[3]) : array();
56
    $groups = !empty($options[4]) ? explode(",",$options[4]) : array();
57
    
58
    $items = $loglogObj->getViews($options[0] , 0, $options[5], $options[6], $module, $options[2], $users, $groups);
59
    $block = array();
60
    $block["items"] = $items;
61
    $block["sort"] = $options[5];
62
63
    return $block;
64
}
65
66
function userlog_views_edit($options)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
{
68
    // include_once XOOPS_ROOT_PATH . "/class/blockform.php"; //reserve for 2.6
69
    xoops_load('XoopsFormLoader');
70
    // $form = new XoopsBlockForm(); //reserve for 2.6
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
    $form = new XoopsThemeForm(_AM_USERLOG_VIEW,'views','');
72
    
73
    $module_handler =& xoops_gethandler('module');
74
    $criteria = new CriteriaCompo();
75
    $criteria->add(new Criteria('hasnotification', 1));
76
    $criteria->add(new Criteria('isactive', 1));
77
    $modules = $module_handler->getObjects($criteria, true);
78
    $hasviews = array();
79
    foreach ($modules as $module) {
80
        $not_config = $module->getInfo('notification');
81 View Code Duplication
        foreach ($not_config['category'] as $category) {
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...
82
            if (!empty($category['item_name'])) {
83
                $script = is_array($category["subscribe_from"]) ? implode(":", $category["subscribe_from"]) : $category["subscribe_from"];
84
                $hasviews[$module->dirname(). ":" . $script . "-" . $category['item_name']] = $module->dirname()."/" . $script ."?".$category['item_name']."=ITEM_ID";
85
            }
86
        }
87
    }
88
    
89
    $i=0;
90
    // number of items to display element
91
    $numitemsEle = new XoopsFormText(_AM_USERLOG_ITEMS_NUM, "options[{$i}]", 10, 255, intval($options[$i]));
92
93
    $i++;
94
    // views element
95
    $options_views = explode(',', $options[$i]);
96
    $viewsEle = new XoopsFormCheckBox(_AM_USERLOG_ITEMS, "options[{$i}][]", $options_views);
97
    $viewsEle->columns = 3;
98
    $viewsEle->addOptionArray($hasviews);
99
    $check_all = _ALL . ": <input type=\"checkbox\" name=\"item_check\" id=\"item_check\" value=\"1\" onclick=\"xoopsCheckGroup('blockform', 'item_check','options[{$i}][]');\" />"; // blockform is the main form
100
    $viewsEle = new XoopsFormLabel(_AM_USERLOG_ITEMS, $check_all ."<br\>". $viewsEle->render());
101
    $viewsEle->setDescription(_AM_USERLOG_ITEMS_DSC);
102
    
103
    $i++;
104
    $timeEle = new XoopsFormText(_AM_USERLOG_LOG_TIMEGT, "options[{$i}]", 10, 255, $options[$i]);
105
    $timeEle->setDescription(_AM_USERLOG_LOG_TIMEGT_FORM);
106
107
    $i++;
108
    $userRadioEle = new XoopsFormRadio(_AM_USERLOG_UID, "options[{$i}]", $options[$i]);
109
    $userRadioEle->addOption(-1,_ALL);
110
    $userRadioEle->addOption(($options[$i] != -1) ? $options[$i] : 0,_SELECT); // if no user in selection box it select uid=0 anon users
111
    $userRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if user dont select any option it select "all"
112
    $userSelectEle = new XoopsFormSelectUser(_AM_USERLOG_UID, "options[{$i}]", true, explode(',', $options[$i]), 3, true);
113
    $userEle = new XoopsFormLabel(_AM_USERLOG_UID, $userRadioEle->render().$userSelectEle->render());
114
115
    $i++;
116
    $groupRadioEle = new XoopsFormRadio(_AM_USERLOG_GROUPS, "options[{$i}]", !empty($options[$i]));
117
    $groupRadioEle->addOption(0,_ALL);
118
    $groupRadioEle->addOption(!empty($options[$i]) ? $options[$i] : 2,_SELECT); // if no group in selection box it select gid=2 registered users
119
    $groupRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if group dont select any option it select "all"
120
    $groupSelectEle = new XoopsFormSelectGroup(_AM_USERLOG_GROUPS, "options[{$i}]", true, explode(',', $options[$i]), 3, true);
121
    $groupEle = new XoopsFormLabel(_AM_USERLOG_GROUPS, $groupRadioEle->render().$groupSelectEle->render());
122
123
    $i++;
124
    $sortEle = new XoopsFormSelect(_AM_USERLOG_SORT, "options[{$i}]", $options[$i]);
125
    $sortEle->addOptionArray(array(
126
                                "count"=>_AM_USERLOG_VIEW,
127
                                "module"=>_AM_USERLOG_MODULE,
128
                                "module_name"=>_AM_USERLOG_MODULE_NAME,
129
                                "module_count"=>_AM_USERLOG_VIEW_MODULE
130
                                ));
131
    $sortEle->setDescription(_AM_USERLOG_SORT_DSC);
132
    
133
    $i++;
134
    $orderEle = new XoopsFormSelect(_AM_USERLOG_ORDER,"options[{$i}]", $options[$i]);
135
    $orderEle->addOption("DESC", _DESCENDING);
136
    $orderEle->addOption("ASC",  _ASCENDING);
137
    $orderEle->setDescription(_AM_USERLOG_ORDER_DSC);
138
    
139
    // add all elements to form
140
    $form->addElement($numitemsEle);
141
    $form->addElement($viewsEle);
142
    $form->addElement($timeEle);
143
    $form->addElement($userEle);
144
    $form->addElement($groupEle);
145
    $form->addElement($sortEle);
146
    $form->addElement($orderEle);
147
    
148
    return $form->render();
149
}
150