chess_notify_item_info()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 29
nc 5
nop 2
dl 0
loc 54
rs 9.1448
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
3
// $Id$
4
//  ------------------------------------------------------------------------ //
5
//                XOOPS - PHP Content Management System                      //
6
//                    Copyright (c) 2000 XOOPS.org                           //
7
//                       <https://xoops.org>                             //
8
//  ------------------------------------------------------------------------ //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
29
/**
30
 * Required file for supporting notification feature
31
 *
32
 * @package    chess
33
 * @subpackage notification
34
 */
35
36
/**#@+
37
 */
38
// Needed since module language constants are used here.
39
// The language file gets automatically included within this module,
40
// but not when viewing the notifications page via notifications.php.
41
global $xoopsConfig;
42
if (file_exists(XOOPS_ROOT_PATH . "/modules/chess/language/{$xoopsConfig['language']}/main.php")) {
43
    require_once XOOPS_ROOT_PATH . "/modules/chess/language/{$xoopsConfig['language']}/main.php";
44
} else {
45
    require_once XOOPS_ROOT_PATH . '/modules/chess/language/english/main.php';
46
}
47
/**#@-*/
48
49
/**
50
 * Get name and URL of notification item.
51
 *
52
 * @param string $category Notification category
53
 * @param int    $item_id  ID of item for which notification is being made
54
 * @return array  Array containing two elements:
55
 *                         - Name of item
56
 *                         - URL of item
57
 */
58
function chess_notify_item_info($category, $item_id)
59
{
60
    if ('global' == $category) {
61
        $item['name'] = 'Chess';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.
Loading history...
62
63
        $item['url'] = XOOPS_URL . '/modules/chess/';
64
65
        return $item;
66
    } elseif ('game' == $category) {
67
        global $xoopsDB;
68
69
        $table = $xoopsDB->prefix('chess_games');
70
71
        $result = $xoopsDB->query(
72
            trim(
73
                "
74
            SELECT white_uid, black_uid, UNIX_TIMESTAMP(start_date) AS start_date
75
            FROM   $table
76
            WHERE  game_id = '$item_id'
77
        "
78
            )
79
        );
80
81
        $gamedata = $xoopsDB->fetchArray($result);
82
83
        $xoopsDB->freeRecordSet($result);
84
85
        if (false !== $gamedata) {
86
            // get mapping of user IDs to usernames
87
88
            $criteria = new \Criteria('uid', "({$gamedata['white_uid']}, {$gamedata['black_uid']})", 'IN');
89
90
            $memberHandler = xoops_getHandler('member');
91
92
            $usernames = $memberHandler->getUserList($criteria);
0 ignored issues
show
Bug introduced by
The method getUserList() 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

92
            /** @scrutinizer ignore-call */ 
93
            $usernames = $memberHandler->getUserList($criteria);
Loading history...
93
94
            $username_white = $usernames[$gamedata['white_uid']] ?? _MD_CHESS_NA;
95
96
            $username_black = $usernames[$gamedata['black_uid']] ?? _MD_CHESS_NA;
97
98
            $date = $gamedata['start_date'] ? date('Y.m.d', $gamedata['start_date']) : _MD_CHESS_NA;
99
        } else {
100
            $username_white = _MD_CHESS_NA;
101
102
            $username_black = _MD_CHESS_NA;
103
104
            $date = _MD_CHESS_NA;
105
        }
106
107
        $item['name'] = "$username_white " . _MD_CHESS_LABEL_VS . " $username_black ($date)";
108
109
        $item['url'] = XOOPS_URL . '/modules/chess/game.php?game_id=' . $item_id;
110
111
        return $item;
112
    }
113
}
114