blocks_letter.php ➔ b_xnewsletter_letter()   C
last analyzed

Complexity

Conditions 12
Paths 160

Size

Total Lines 72

Duplication

Lines 18
Ratio 25 %

Importance

Changes 0
Metric Value
cc 12
nc 160
nop 1
dl 18
loc 72
rs 5.7842
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
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 *  Version : 1 Mon 2012/11/05 14:31:32 :  Exp $
26
 * ****************************************************************************
27
 */
28
29
use XoopsModules\Xnewsletter;
30
31
// defined("XOOPS_ROOT_PATH") || die("XOOPS root path not defined");
32
require_once dirname(__DIR__) . '/include/common.php';
33
34
/**
35
 * @param $options
36
 *
37
 * @return array
38
 */
39
function b_xnewsletter_letter($options)
40
{
41
    global $xoopsUser;
42
    $myts             = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
    /** @var \XoopsGroupPermHandler $grouppermHandler */
44
    $grouppermHandler = xoops_getHandler('groupperm');
45
    /** @var \XoopsMemberHandler $memberHandler */
46
    $memberHandler = xoops_getHandler('member');
47
    $helper        = Xnewsletter\Helper::getInstance();
48
49
    $letter       = [];
50
    $type_block   = $options[0];
51
    $nb_letter    = $options[1];
52
    $length_title = $options[2];
53
54
    array_shift($options);
55
    array_shift($options);
56
    array_shift($options);
57
58
    $letterCriteria = new \CriteriaCompo();
59 View Code Duplication
    switch ($type_block) {
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...
60
        // For the block: letter recents
61
        case 'recent':
62
            $letterCriteria->setSort('letter_created');
63
            $letterCriteria->setOrder('DESC');
64
            break;
65
        // For the block: letter of today
66
        case 'day':
67
            $letterCriteria->add(new \Criteria('letter_created', strtotime(date('Y/m/d')), '>='));
68
            $letterCriteria->add(new \Criteria('letter_created', strtotime(date('Y/m/d')) + 86400, '<='));
69
            $letterCriteria->setSort('letter_created');
70
            $letterCriteria->setOrder('ASC');
71
            break;
72
        // For the block: letter random
73
        case 'random':
74
            $letterCriteria->setSort('RAND()');
75
            break;
76
    }
77
78
    $uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
79
    if (0 == $uid) {
80
        $groups = [XOOPS_GROUP_ANONYMOUS];
81
    } else {
82
        $groups = $memberHandler->getGroupsByUser($uid);
83
    }
84
85
    $letterCriteria->setLimit($nb_letter);
86
    $letterObjs = $helper->getHandler('Letter')->getAll($letterCriteria);
87
    foreach ($letterObjs as $letter_id => $letterObj) {
88
        $letter_cats = [];
0 ignored issues
show
Unused Code introduced by
$letter_cats is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
        $letter_cats = explode('|', $letterObj->getVar('letter_cats'));
90
        $showCat     = false;
0 ignored issues
show
Unused Code introduced by
$showCat is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        foreach ($letter_cats as $cat_id) {
92
            $showCat = $grouppermHandler->checkRight('newsletter_read_cat', $cat_id, $groups, $helper->getModule()->mid());
93
            if (true === $showCat) {
94
                $letter[$letter_id]['letter_id'] = $letterObj->getVar('letter_id');
95
                $letter_title                    = $letterObj->getVar('letter_title');
96
                if ($length_title > 0 && mb_strlen($letter_title) > $length_title) {
97
                    $letter_title = mb_substr($letter_title, 0, $length_title) . '...';
98
                }
99
                $letter[$letter_id]['letter_title'] = $letter_title;
100
                // $letter[$letter_id]["letter_content"] = $letterObj->getVar("letter_content");
101
                // $letter[$letter_id]["letter_cats"] = $letterObj->getVar("letter_cats");
102
                // $letter[$letter_id]["letter_submitter"] = $letterObj->getVar("letter_submitter");
103
                $letter[$letter_id]['letter_created'] = formatTimestamp($letterObj->getVar('letter_created'), 'S');
104
                $letter[$letter_id]['href']           = XOOPS_URL . "/modules/{$helper->getModule()->dirname()}/letter.php?op=show_preview&letter_id={$letterObj->getVar('letter_id')}";
105
            }
106
        }
107
    }
108
109
    return $letter;
110
}
111
112
/**
113
 * @param $options
114
 *
115
 * @return string
116
 */
117
function b_xnewsletter_letter_edit($options)
118
{
119
    $form = '' . _MB_XNEWSLETTER_LETTER_DISPLAY . "\n";
120
    $form .= "<input type=\"hidden\" name=\"options[0]\" value=\"{$options[0]}\">";
121
    $form .= "<input name=\"options[1]\" size=\"5\" maxlength=\"255\" value=\"{$options[1]}\" type=\"text\">";
122
    $form .= '<br>';
123
    $form .= '' . _MB_XNEWSLETTER_LETTER_TITLELENGTH . " : <input name=\"options[2]\" size=\"5\" maxlength=\"255\" value=\"{$options[2]}\" type=\"text\">";
124
    $form .= '<br><br>';
125
    array_shift($options);
126
    array_shift($options);
127
    array_shift($options);
128
    $form .= "<label name='lbl_cattodisplay'>" . _MB_XNEWSLETTER_LETTER_CATTODISPLAY . '</label>';
129
    $form .= '<br><br>';
130
131
    return $form;
132
}
133