b_xfguestbook_show()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 40
nc 8
nop 1
dl 0
loc 59
rs 8.0355
c 1
b 0
f 0

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
2
// $Id: block/xfguestbook_new.php,v 1.11 2004/12/02 C. Félix AKA the Cat
3
//  ------------------------------------------------------------------------ //
4
//             XF Guestbook                                                  //
5
// ------------------------------------------------------------------------- //
6
//  This program is free software; you can redistribute it and/or modify     //
7
//  it under the terms of the GNU General Public License as published by     //
8
//  the Free Software Foundation; either version 2 of the License, or        //
9
//  (at your option) any later version.                                      //
10
//                                                                           //
11
//  You may not change or alter any portion of this comment or credits       //
12
//  of supporting developers from this source code or any supporting         //
13
//  source code which is considered copyrighted (c) material of the          //
14
//  original comment or credit authors.                                      //
15
//                                                                           //
16
//  This program is distributed in the hope that it will be useful,          //
17
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
18
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
19
//  GNU General Public License for more details.                             //
20
//                                                                           //
21
//  You should have received a copy of the GNU General Public License        //
22
//  along with this program; if not, write to the Free Software              //
23
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
24
//  ------------------------------------------------------------------------ //
25
use XoopsModules\Xfguestbook\Helper;
26
27
/**
28
 * @param $options
29
 * @return array
30
 */
31
function b_xfguestbook_show($options)
32
{
33
    global $xoopsModule, $xoopsModuleConfig, $xoopsDB;
34
    /** @var Helper $helper */
35
    $helper = Helper::getInstance();
36
    if (empty($xoopsModule) || 'xfguestbook' !== $xoopsModule->getVar('dirname')) {
37
        /** @var \XoopsModuleHandler $moduleHandler */
38
        $moduleHandler = xoops_getHandler('module');
39
        $module        = $moduleHandler->getByDirname('xfguestbook');
40
        /** @var \XoopsConfigHandler $configHandler */
41
        $configHandler = xoops_getHandler('config');
42
        $config        = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
43
    } else {
44
        $module = $xoopsModule;
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
45
        $config = $xoopsModuleConfig;
46
    }
47
48
    $block = [];
49
    if (0 != $options[1]) {
50
        $block['full_view'] = true;
51
    } else {
52
        $block['full_view'] = false;
53
    }
54
55
    /** @var \XoopsModules\Xfguestbook\MessageHandler $messageHandler */
56
    $messageHandler = $helper->getHandler('Message');
57
    $criteria       = new \Criteria('moderate', '0', '=');
58
    $criteria->setSort('post_time');
59
    $criteria->setOrder('DESC');
60
    $criteria->setLimit($options[0]);
61
    $nbmsg = $messageHandler->countMsg($criteria);
62
63
    $a_item = [];
64
65
    if ($nbmsg > 0) {
66
        $msg = $messageHandler->getObjects($criteria);
67
        $ts  = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $ts is dead and can be removed.
Loading history...
68
69
        /** @var \XoopsModules\Xfguestbook\Message $onemsg */
70
        foreach ($msg as $onemsg) {
71
            $msg_id          = $onemsg->getVar('msg_id');
72
            $a_item['id']    = $msg_id;
73
            $a_item['title'] = $onemsg->getVar('title');
74
            if (!XOOPS_USE_MULTIBYTES) {
75
                $length = mb_strlen($onemsg->getVar('title'));
76
                if ($length >= $options[1]) {
77
                    $a_item['title'] = mb_substr($a_item['title'], 0, $options[1] - $length) . '...';
78
                }
79
            }
80
            $a_item['name']   = $onemsg->getVar('uname');
81
            $a_item['date']   = formatTimestamp($onemsg->getVar('post_time'), 's');
82
            $block['items'][] = $a_item;
83
            unset($a_item);
84
        }
85
    } else {
86
        $block['nbmsg'] = 0;
87
    }
88
89
    return $block;
90
}
91
92
/**
93
 * @param $options
94
 * @return string
95
 */
96
function b_xfguestbook_edit($options)
97
{
98
    $form = '' . MB_XFGUESTBOOK_DISP . '&nbsp;';
99
    $form .= '<input type="text" name="options[]" value="' . $options[0] . '">&nbsp;' . MB_XFGUESTBOOK_NBMSG . '';
100
    $form .= '&nbsp;<br>' . MB_XFGUESTBOOK_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . '\'>&nbsp;' . MB_XFGUESTBOOK_LENGTH . '';
101
102
    return $form;
103
}
104