NewbbUserlogPlugin   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C item() 0 38 7
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
 *  userlog module
13
 *
14
 * @copyright       XOOPS Project (https://xoops.org)
15
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
16
 * @package         userlog class plugin
17
 * @since           1.16
18
 * @author          irmtfan ([email protected])
19
 * @author          XOOPS Project <www.xoops.org> <www.xoops.ir>
20
 */
21
22
use Xmf\Request;
23
24
defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
25
26
/**
27
 * Class NewbbUserlogPlugin
28
 */
29
class NewbbUserlogPlugin extends Userlog_Module_Plugin_Abstract implements UserlogPluginInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     * @param string $subscribe_from Name of the script
33
     *
34
     * 'name' => 'thread';
35
     * 'title' => _MI_NEWBB_THREAD_NOTIFY;
36
     * 'description' => _MI_NEWBB_THREAD_NOTIFYDSC;
37
     * 'subscribe_from' => 'viewtopic.php';
38
     * 'item_name' => 'topic_id';
39
     * 'allow_bookmark' => 1;
40
     *
41
     * publisher:
42
     * 'name' = 'category_item';
43
     * 'title' = _MI_PUBLISHER_CATEGORY_ITEM_NOTIFY;
44
     * 'description' = _MI_PUBLISHER_CATEGORY_ITEM_NOTIFY_DSC;
45
     * 'subscribe_from' = array('index.php', 'category.php', 'item.php');
46
     * 'item_name' = 'categoryid';
47
     * 'allow_bookmark' = 1;
48
     *
49
     * empty($subscribe_from):
50
     * @return array $script_arr["item_name"] name of the item = array("subscribe_from1", "subscribe_from2") Name of the script
51
     *
52
     * !empty($subscribe_from):
53
     * @return false|array $item["item_name"] name of the item, $item["item_id"] id of the item
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string[]>|array|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
54
     */
55
    public function item($subscribe_from)
56
    {
57
        if (empty($subscribe_from)) {
58
            $script_arr             = [];
59
            $script_arr['topic_id'] = ['viewtopic.php'];
60
            $script_arr['forum']    = ['viewforum.php'];
61
62
            return $script_arr;
63
        }
64
65
        switch ($subscribe_from) {
66
            case 'viewtopic.php':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
67
68
                /** @var \NewbbTopicHandler $topicHandler */
69
                $topicHandler = xoops_getModuleHandler('topic', 'newbb');
70
                $post_id      = Request::getInt('post_id',  0);
71
                $move         = Request::getString('move', '', 'GET') ;
72
                $topic_id     = Request::getInt('topic_id',  0);
73
                if (!empty($post_id)) {
74
                    $topic_obj = $topicHandler->getByPost($post_id);
75
                    $topic_id  = $topic_obj->getVar('topic_id');
76
                } elseif (!empty($move)) {
77
                    $forum_id  = Request::getInt('forum_id',  0);
78
                    $topic_obj = $topicHandler->getByMove($topic_id, ('prev' === $move) ? -1 : 1, $forum_id);
79
                    $topic_id  = $topic_obj->getVar('topic_id');
80
                }
81
82
                return ['item_name' => 'topic_id', 'item_id' => $topic_id];
83
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84
            case 'viewforum.php':
85
                $forum_id = Request::getInt('forum', 0);
86
87
                return ['item_name' => 'forum', 'item_id' => $forum_id];
88
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
        }
90
91
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface UserlogPluginInterface::item of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
92
    }
93
}
94