Surnames::grabEntries()   B
last analyzed

Complexity

Conditions 9
Paths 23

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 39
c 1
b 0
f 0
nc 23
nop 1
dl 0
loc 52
rs 7.7404

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
3
declare(strict_types=1);
4
5
namespace XoopsModules\Rssfit\Plugins;
6
7
/*
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
/**
18
 * @copyright    XOOPS Project (https://xoops.org)
19
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package      RSSFit - Extendable XML news feed generator
21
 * @author       NS Tai (aka tuff) <http://www.brandycoke.com>
22
 * @author       XOOPS Development Team
23
 */
24
25
/**
26
 * About this RSSFit plug-in
27
 * Author: Richard Griffith <[email protected]>
28
 * Requirements (or Tested with):
29
 *  Module: Surnames https://github.com/geekwright/surnames
30
 *  Version: 1.0
31
 *  RSSFit verision: 1.3
32
 *  XOOPS version: 2.5.9
33
 */
34
35
use XoopsModules\Rssfit\{
36
    AbstractPlugin
37
};
38
39
//use XoopsModules\Surnames\Helper as PluginHelper;
40
41
if (!\defined('RSSFIT_ROOT_PATH')) {
42
    exit();
43
}
44
45
/**
46
 * Class Surnames
47
 * @package XoopsModules\Rssfit\Plugins
48
 */
49
final class Surnames extends AbstractPlugin
50
{
51
    public function __construct() {
52
        $this->dirname = 'surnames';
53
    }
54
55
    public function loadModule(): ?\XoopsModule
56
    {
57
        $mod = $GLOBALS['module_handler']->getByDirname($this->dirname);
58
        if (!$mod || !$mod->getVar('isactive')) {
59
            return null;
60
        }
61
62
        if (!$mod->getVar('isactive')) {
63
            return null;
64
        }
65
        $this->modname = $mod->getVar('name');
66
        $this->module  = $mod;   // optional, remove this line if there is nothing to do with module info when grabbing entries
67
68
        return $mod;
69
    }
70
71
    public function myGetUnameFromId(int $uid): string
72
    {
73
        static $thisUser = false;
74
        static $lastUid = false;
75
        static $lastName = '';
76
77
        if ($lastUid == $uid) {
78
            return $lastName;
79
        }
80
81
        if (!\is_object($thisUser)) {
82
            /** @var \XoopsMemberHandler $memberHandler */
83
            $memberHandler = \xoops_getHandler('member');
84
            $thisUser      = $memberHandler->getUser($uid);
85
        }
86
        $name = \htmlspecialchars($thisUser->getVar('name'), \ENT_QUOTES | \ENT_HTML5);
87
        if ('' === $name) {
88
            $name = \htmlspecialchars($thisUser->getVar('uname'), \ENT_QUOTES | \ENT_HTML5);
89
        }
90
        $lastUid  = $uid;
91
        $lastName = $name;
92
93
        return $name;
94
    }
95
96
    public function grabEntries(\XoopsMySQLDatabase $xoopsDB): ?array
97
    {
98
        $ret  = null;
99
100
        $i        = -1;
101
        $lasttime = false;
102
        $lastuser = false;
103
        $limit    = 10 * $this->grab;
104
105
        $sql    = "SELECT uid, id, surname, notes, DATE_FORMAT(changed_ts,'%Y-%m-%d') as changedate FROM " . $xoopsDB->prefix('surnames');
106
        $sql    .= ' WHERE approved=1 ORDER BY changedate DESC, uid ';
107
        $result = $xoopsDB->query($sql, $limit, 0);
108
        if ($result instanceof \mysqli_result) {
109
            $ret = [];
110
            while (false !== ($row = $xoopsDB->fetchArray($result))) {
111
                $changedate = \strtotime($row['changedate']);
112
                $uid        = $row['uid'];
113
                if ($lasttime == $changedate && $lastuser == $uid) {
114
                    $link    = XOOPS_URL . '/modules/surnames/view.php?id=' . $row['id'];
115
                    $surname = $row['surname'];
116
                    $desc    .= "<a href=\"$link\">$surname</a><br>";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $desc does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
                } else {
118
                    if ($i >= 0) {
119
                        $ret[$i]['description'] = $desc;
120
                    }
121
                    ++$i;
122
                    $lasttime = $changedate;
123
                    $lastuser = $uid;
124
                    if ($i <= $this->grab) {
125
                        $desc                 = '';
126
                        $name                 = $this->myGetUnameFromId($row['uid']);
127
                        $ret[$i]['title']     = $this->modname . ': by ' . $name;
128
                        $ret[$i]['link']      = XOOPS_URL . '/modules/surnames/list.php?uid=' . $row['uid'];
129
                        $ret[$i]['timestamp'] = $changedate;
130
131
                        $link                = XOOPS_URL . '/modules/surnames/view.php?id=' . $row['id'];
132
                        $ret[$i]['guid']     = $link;
133
                        $ret[$i]['category'] = $this->modname;
134
135
                        $surname = $row['surname'];
136
                        $desc    .= "<a href=\"$link\">$surname</a><br>";
137
                    }
138
                }
139
                if ($i > $this->grab) {
140
                    break;
141
                }
142
            }
143
            if ($i < $this->grab) {
144
                $ret[$i]['description'] = $desc;
145
            }
146
        }
147
        return $ret;
148
    }
149
}
150