Passed
Push — master ( 37a2f2...290aa0 )
by Michael
02:35
created

b_xfguestbook_show()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 55
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 55
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
26
/**
27
 * @param $options
28
 * @return array
29
 */
30
function b_xfguestbook_show($options)
31
{
32
    global $xoopsModule, $xoopsModuleConfig, $xoopsDB;
33
    /** @var \XoopsModules\Xfguestbook\Helper $helper */
34
    $helper = \XoopsModules\Xfguestbook\Helper::getInstance();
35
    if (empty($xoopsModule) || 'xfguestbook' !== $xoopsModule->getVar('dirname')) {
36
        /** @var XoopsModuleHandler $moduleHandler */
37
        $moduleHandler = xoops_getHandler('module');
38
        $module        = $moduleHandler->getByDirname('xfguestbook');
39
        $configHandler = xoops_getHandler('config');
40
        $config        = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        /** @scrutinizer ignore-call */ 
41
        $config        = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
Loading history...
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
41
    } else {
42
        $module = $xoopsModule;
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
43
        $config = $xoopsModuleConfig;
44
    }
45
46
    $block = [];
47
    if (0 != $options[1]) {
48
        $block['full_view'] = true;
49
    } else {
50
        $block['full_view'] = false;
51
    }
52
53
    $msg_hnd  = $helper->getHandler('Message');
54
    $criteria = new \Criteria('moderate', '0', '=');
55
    $criteria->setSort('post_time');
56
    $criteria->setOrder('DESC');
57
    $criteria->setLimit($options[0]);
58
    $nbmsg = $msg_hnd->countMsg($criteria);
0 ignored issues
show
Bug introduced by
The method countMsg() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
    /** @scrutinizer ignore-call */ 
59
    $nbmsg = $msg_hnd->countMsg($criteria);
Loading history...
59
60
    $a_item = [];
61
62
    if ($nbmsg > 0) {
63
        $msg = $msg_hnd->getObjects($criteria);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $msg = $msg_hnd->getObjects($criteria);
Loading history...
64
        $ts  = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $ts is dead and can be removed.
Loading history...
65
        foreach ($msg as $onemsg) {
66
            $msg_id          = $onemsg->getVar('msg_id');
67
            $a_item['id']    = $msg_id;
68
            $a_item['title'] = $onemsg->getVar('title');
69
            if (!XOOPS_USE_MULTIBYTES) {
70
                $length = strlen($onemsg->getVar('title'));
71
                if ($length >= $options[1]) {
72
                    $a_item['title'] = substr($a_item['title'], 0, $options[1] - $length) . '...';
73
                }
74
            }
75
            $a_item['name']   = $onemsg->getVar('uname');
76
            $a_item['date']   = formatTimestamp($onemsg->getVar('post_time'), 's');
77
            $block['items'][] = $a_item;
78
            unset($a_item);
79
        }
80
    } else {
81
        $block['nbmsg'] = 0;
82
    }
83
84
    return $block;
85
}
86
87
/**
88
 * @param $options
89
 * @return string
90
 */
91
function b_xfguestbook_edit($options)
92
{
93
    $form = '' . MB_XFGUESTBOOK_DISP . '&nbsp;';
94
    $form .= '<input type="text" name="options[]" value="' . $options[0] . '">&nbsp;' . MB_XFGUESTBOOK_NBMSG . '';
95
    $form .= '&nbsp;<br>' . MB_XFGUESTBOOK_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . '\'>&nbsp;' . MB_XFGUESTBOOK_LENGTH . '';
96
97
    return $form;
98
}
99