Completed
Push — master ( 70d8b1...741c06 )
by Michael
02:57
created

news_randomnews.php ➔ b_news_randomnews_edit()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 40
nc 32
nop 1
dl 0
loc 54
rs 7.8331
c 0
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
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 36 and the first side effect is on line 29.

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
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <http://xoops.org/>                             //
7
// ------------------------------------------------------------------------- //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
28
29
include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
30
31
/**
32
 * @param $options
33
 *
34
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

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...
35
 */
36
function b_news_randomnews_show($options)
37
{
38
    include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php';
39
    $myts          = MyTextSanitizer::getInstance();
40
    $block         = array();
41
    $block['sort'] = $options[0];
42
43
    $tmpstory   = new NewsStory;
44
    $restricted = news_getmoduleoption('restrictindex');
45
    $dateformat = news_getmoduleoption('dateformat');
46
    $infotips   = news_getmoduleoption('infotips');
47
    if ($dateformat == '') {
48
        $dateformat = 's';
49
    }
50
    if ($options[4] == 0) {
51
        $stories = $tmpstory->getRandomNews($options[1], 0, $restricted, 0, 1, $options[0]);
52
    } else {
53
        $topics  = array_slice($options, 4);
54
        $stories = $tmpstory->getRandomNews($options[1], 0, $restricted, $topics, 1, $options[0]);
0 ignored issues
show
Documentation introduced by
$topics is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
    }
56
    unset($tmpstory);
57
    if (count($stories) == 0) {
58
        return '';
59
    }
60
    foreach ($stories as $story) {
0 ignored issues
show
Bug introduced by
The expression $stories of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
61
        $news  = array();
62
        $title = $story->title();
63 View Code Duplication
        if (strlen($title) > $options[2]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $title = xoops_substr($title, 0, $options[2] + 3);
65
        }
66
        $news['title']       = $title;
67
        $news['id']          = $story->storyid();
68
        $news['date']        = formatTimestamp($story->published(), $dateformat);
69
        $news['hits']        = $story->counter();
70
        $news['rating']      = $story->rating();
71
        $news['votes']       = $story->votes();
72
        $news['author']      = sprintf('%s %s', _POSTEDBY, $story->uname());
73
        $news['topic_title'] = $story->topic_title();
74
        $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
75
        $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
76
        $news['pictureinfo'] = $story->pictureinfo();
77
78
        if ($options[3] > 0) {
79
            $html             = $story->nohtml() == 1 ? 0 : 1;
80
            $news['teaser']   = news_truncate_tagsafe($myts->displayTarea($story->hometext, $html), $options[3] + 3);
81
            $news['infotips'] = ' title="' . $story->title() . '"';
82 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $news['teaser'] = '';
84
            if ($infotips > 0) {
85
                $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
86
            } else {
87
                $news['infotips'] = ' title="' . $story->title() . '"';
88
            }
89
        }
90
        $block['stories'][] = $news;
91
    }
92
    $block['lang_read_more'] = _MB_READMORE;
93
94
    return $block;
95
}
96
97
/**
98
 * @param $options
99
 *
100
 * @return string
101
 */
102
function b_news_randomnews_edit($options)
103
{
104
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
105
    $form = _MB_NEWS_ORDER . "&nbsp;<select name='options[]'>";
106
    $form .= "<option value='published'";
107
    if ($options[0] === 'published') {
108
        $form .= ' selected';
109
    }
110
    $form .= '>' . _MB_NEWS_DATE . "</option>\n";
111
112
    $form .= "<option value='counter'";
113
    if ($options[0] === 'counter') {
114
        $form .= ' selected';
115
    }
116
    $form .= '>' . _MB_NEWS_HITS . '</option>';
117
118
    $form .= "<option value='rating'";
119
    if ($options[0] === 'rating') {
120
        $form .= ' selected';
121
    }
122
    $form .= '>' . _MB_NEWS_RATE . '</option>';
123
124
    $form .= "</select>\n";
125
    $form .= '&nbsp;' . _MB_NEWS_DISP . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . "'/>&nbsp;" . _MB_NEWS_ARTCLS;
126
    $form .= '&nbsp;<br><br>'
127
             . _MB_NEWS_CHARS
128
             . "&nbsp;<input type='text' name='options[]' value='"
129
             . $options[2]
130
             . "'/>&nbsp;"
131
             . _MB_NEWS_LENGTH
132
             . '<br><br>';
133
134
    $form .= _MB_NEWS_TEASER . " <input type='text' name='options[]' value='" . $options[3] . "' />" . _MB_NEWS_LENGTH;
135
    $form .= '<br><br>' . _MB_SPOTLIGHT_TOPIC . "<br><select id='options[4]' name='options[]' multiple='multiple'>";
136
137
    include_once XOOPS_ROOT_PATH . '/modules/news/class/xoopsstory.php';
138
    $xt                    = new MyXoopsTopic($xoopsDB->prefix('news_topics'));
139
    $alltopics             = $xt->getTopicsList();
140
    $alltopics[0]['title'] = _MB_SPOTLIGHT_ALL_TOPICS;
141
    ksort($alltopics);
142
    $size = count($options);
143
    foreach ($alltopics as $topicid => $topic) {
144
        $sel = '';
145
        for ($i = 4; $i < $size; ++$i) {
146
            if ($options[$i] == $topicid) {
147
                $sel = ' selected';
148
            }
149
        }
150
        $form .= "<option value='$topicid'$sel>" . $topic['title'] . '</option>';
151
    }
152
    $form .= '</select><br>';
153
154
    return $form;
155
}
156
157
/**
158
 * @param $options
159
 */
160 View Code Duplication
function b_news_randomnews_onthefly($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
{
162
    $options = explode('|', $options);
163
    $block   = &b_news_randomnews_show($options);
164
165
    $tpl = new XoopsTpl();
166
    $tpl->assign('block', $block);
167
    $tpl->display('db:news_block_moderate.tpl');
168
}
169