Completed
Push — master ( c622f7...33a06a )
by Richard
09:52 queued 02:32
created

SystemUsermenuPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 3
loc 62
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A usermenu() 3 55 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
14
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author          trabis <[email protected]>
16
 */
17
class SystemUsermenuPlugin implements UsermenuPluginInterface
18
{
19
20
    /**
21
     * @return array
22
     */
23
    public function usermenu()
24
    {
25
        $xoops = \Xoops::getInstance();
26
        $ret = array();
27
        // View Account
28
        $ret[] = [
29
            'name' => XoopsLocale::VIEW_ACCOUNT,
30
            'link' => $xoops->url('userinfo.php?uid=' . $xoops->user->getVar('uid')),
31
            'icon' => 'glyphicon-user',
32
        ];
33
34
        // Edit Account
35
        $ret[] = [
36
            'name' => XoopsLocale::EDIT_ACCOUNT,
37
            'link' => $xoops->url('edituser.php'),
38
            'icon' => 'glyphicon-pencil',
39
        ];
40
41
        // Administration Menu
42
        if ($xoops->isAdmin()) {
43
            $ret[] = [
44
                'name' => SystemLocale::ADMINISTRATION_MENU,
45
                'link' => $xoops->url('admin.php'),
46
                'icon' => 'glyphicon-wrench',
47
            ];
48
        }
49
50
        // Inbox
51
        if (!$xoops->isActiveModule('pm')) {
52
            $criteria = new CriteriaCompo(new Criteria('read_msg', 0));
53
            $criteria->add(new Criteria('to_userid', $xoops->user->getVar('uid')));
54
            $pm_handler = $xoops->getHandlerPrivateMessage();
55
            $xoops->events()->triggerEvent('system.blocks.system_blocks.usershow', array(&$pm_handler));
56
57
            $name = XoopsLocale::INBOX;
58 View Code Duplication
            if ($pm_count = $pm_handler->getCount($criteria)) {
0 ignored issues
show
Bug introduced by
The method getCount does only exist in XoopsPersistableObjectHandler, but not in XoopsObjectHandler.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
                $name = XoopsLocale::INBOX . ' <span class="badge">' . $pm_count . '</span>';
60
            }
61
62
            $ret[] = [
63
                'name' => $name,
64
                'link' => $xoops->url('viewpmsg.php'),
65
                'icon' => 'glyphicon-envelope',
66
            ];
67
        }
68
69
        // Logout
70
        $ret[] = [
71
            'name' => XoopsLocale::A_LOGOUT,
72
            'link' => $xoops->url('user.php?op=logout'),
73
            'icon' => 'glyphicon-log-out',
74
        ];
75
76
        return $ret;
77
    }
78
}