Completed
Push — master ( d576ea...caa4fc )
by Michael
06:09 queued 03:25
created

news_top.php ➔ b_news_top_show()   F

Complexity

Conditions 102
Paths > 20000

Size

Total Lines 533
Code Lines 398

Duplication

Lines 127
Ratio 23.83 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 102
eloc 398
c 2
b 0
f 0
nc 2273901648
nop 1
dl 127
loc 533
rs 2

How to fix   Long Method    Complexity   

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 45 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
include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newstopic.php';
31
32
/**
33
 * Notes about the spotlight :
34
 * If you have restricted topics on index page (in fact if the program must completly respect the permissions) and if
35
 * the news you have selected to be viewed in the spotlight can't be viewed by someone then the spotlight is not visible !
36
 * This is available in the classical and in the tabbed view.
37
 * But if you have uncheck the option "Restrict topics on index page", then the news will be visible but users without
38
 * permissions will be rejected when they will try to read news content.
39
 *
40
 * Also, if you have selected a tabbed view and wanted to use the Spotlight but did not choosed a story, then the block
41
 * will switch to the "most recent news" mode (the visible news will be searched according to the permissions)
42
 * @param $options
43
 * @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...
44
 */
45
function b_news_top_show($options)
0 ignored issues
show
Coding Style introduced by
b_news_top_show uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
b_news_top_show uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
b_news_top_show uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
{
47
    global $xoopsConfig;
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...
48
    include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php';
49
    $myts        = MyTextSanitizer::getInstance();
50
    $block       = array();
51
    $displayname = news_getmoduleoption('displayname');
52
    $tabskin     = news_getmoduleoption('tabskin');
53
54 View Code Duplication
    if (file_exists(XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php')) {
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...
55
        include_once XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php';
56
    } else {
57
        include_once XOOPS_ROOT_PATH . '/modules/news/language/english/main.php';
58
    }
59
60
    $block['displayview'] = $options[8];
61
    $block['tabskin']     = $tabskin;
62
    $block['imagesurl']   = XOOPS_URL . '/modules/news/assets/images/';
63
64
    $restricted = news_getmoduleoption('restrictindex');
65
    $dateformat = news_getmoduleoption('dateformat');
66
    $infotips   = news_getmoduleoption('infotips');
67
    $newsrating = news_getmoduleoption('ratenews');
68
    if ($dateformat == '') {
69
        $dateformat = 's';
70
    }
71
72
    $perm_verified = false;
73
    $news_visible  = true;
74
    // Is the spotlight visible ?
75
    if ($options[4] == 1 && $restricted && $options[5] == 0) {
76
        $perm_verified   = true;
77
        $permittedtopics = news_MygetItemIds();
78
        $permstory       = new NewsStory($options[6]);
79
        if (!in_array($permstory->topicid(), $permittedtopics)) {
80
            $usespotlight = false;
0 ignored issues
show
Unused Code introduced by
$usespotlight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
            $news_visible = false;
82
            $topicstitles = array();
0 ignored issues
show
Unused Code introduced by
$topicstitles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
        }
84
        $options[4] == 0;
85
    }
86
    // Try to see what tabs are visibles (if we are in restricted view of course)
87
    if ($options[8] == 2 && $restricted && $options[14] != 0) {
88
        $topics2         = array();
89
        $permittedtopics = news_MygetItemIds();
90
        $topics          = array_slice($options, 14);
91
        foreach ($topics as $onetopic) {
92
            if (in_array($onetopic, $permittedtopics)) {
93
                $topics2[] = $onetopic;
94
            }
95
        }
96
        $before  = array_slice($options, 0, 14);
97
        $options = array_merge($before, $topics2);
98
    }
99
100
    if ($options[8] == 2) { // Tabbed view ********************************************************************************************
101
        $defcolors[1] = array('#F90', '#FFFFFF', '#F90', '#C60', '#999'); // Bar Style
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defcolors was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defcolors = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
102
        $defcolors[2] = array('#F90', '#FFFFFF', '#F90', '#AAA', '#666'); // Beveled
103
        $defcolors[3] = array('#F90', '#FFFFFF', '', '#789', '#789'); // Classic
104
        $defcolors[4] = array('#F90', '#FFFFFF', '', '', ''); // Folders
105
        $defcolors[5] = array('#F90', '#FFFFFF', '#CCC', 'inherit', '#999'); // MacOs
106
        $defcolors[6] = array('#F90', '#FFFFFF', '#FFF', '#DDD', '#999'); // Plain
107
        $defcolors[7] = array('#F90', '#FFFFFF', '', '', ''); // Rounded
108
        $defcolors[8] = array('#F90', '#FFFFFF', '#F90', '#930', '#C60'); // ZDnet
109
110
        $myurl = $_SERVER['PHP_SELF'];
111
        if (substr($myurl, strlen($myurl) - 1, 1) === '/') {
112
            $myurl .= 'index.php';
113
        }
114
        $myurl .= '?';
115
116
        foreach ($_GET as $key => $value) {
117
            if ($key !== 'NewsTab') {
118
                $myurl .= $key . '=' . $value . '&';
119
            }
120
        }
121
        $block['url'] = $myurl;
122
123
        $tabscount    = 0;
124
        $usespotlight = false;
125
126
        if (isset($_GET['NewsTab'])) {
127
            $_SESSION['NewsTab'] = (int)$_GET['NewsTab'];
128
            $currenttab          = (int)$_GET['NewsTab'];
129
        } elseif (isset($_SESSION['NewsTab'])) {
130
            $currenttab = (int)$_SESSION['NewsTab'];
131
        } else {
132
            $currenttab = 0;
133
        }
134
135
        $tmpstory     = new NewsStory();
136
        $topic        = new NewsTopic();
137
        $topicstitles = array();
138
        if ($options[4] == 1) { // Spotlight enabled
139
            $topicstitles[0] = _MB_NEWS_SPOTLIGHT_TITLE;
140
            ++$tabscount;
141
            $usespotlight = true;
142
        }
143
144
        if ($options[5] == 0 && $restricted) { // Use a specific news and we are in restricted mode
145
            if (!$perm_verified) {
146
                $permittedtopics = news_MygetItemIds();
147
                $permstory       = new NewsStory($options[6]);
148
                if (!in_array($permstory->topicid(), $permittedtopics)) {
149
                    $usespotlight = false;
150
                    $topicstitles = array();
151
                }
152
                //unset($permstory);
153
            } else {
154
                if (!$news_visible) {
155
                    $usespotlight = false;
156
                    $topicstitles = array();
157
                }
158
            }
159
        }
160
161
        $block['use_spotlight'] = $usespotlight;
162
163
        if (isset($options[14]) && $options[14] != 0) { // Topic to use
164
            $topics = array_slice($options, 14);
165
            $tabscount += count($topics);
166
            $topicstitles = $topic->getTopicTitleFromId($topics, $topicstitles);
167
        }
168
        $tabs = array();
169
        if ($usespotlight) {
170
            $tabs[] = array('id' => 0, 'title' => _MB_NEWS_SPOTLIGHT_TITLE);
171
        }
172
        if (count($topics) > 0) {
173
            foreach ($topics as $onetopic) {
0 ignored issues
show
Bug introduced by
The variable $topics does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
174
                if (isset($topicstitles[$onetopic])) {
175
                    $tabs[] = array('id' => $onetopic, 'title' => $topicstitles[$onetopic]['title'], 'picture' => $topicstitles[$onetopic]['picture']);
176
                }
177
            }
178
        }
179
        $block['tabs']                 = $tabs;
180
        $block['current_is_spotlight'] = false;
181
        $block['current_tab']          = $currenttab;
182
        $block['use_rating']           = $newsrating;
183
184
        if ($currenttab == 0 && $usespotlight) { // Spotlight or not ?
185
            $block['current_is_spotlight'] = true;
186
            if ($options[5] == 0 && $options[6] == 0) { // If the story to use was no selected then we switch to the "recent news" mode.
187
                $options[5] = 1;
188
            }
189
190
            if ($options[5] == 0) { // Use a specific news
191
                if (!isset($permstory)) {
192
                    $tmpstory->NewsStory($options[6]);
0 ignored issues
show
Bug introduced by
The method NewsStory() does not seem to exist on object<NewsStory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
193
                } else {
194
                    $tmpstory = $permstory;
195
                }
196 View Code Duplication
            } else { // Use the most recent news
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...
197
                $stories = array();
0 ignored issues
show
Unused Code introduced by
$stories is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
                $stories = NewsStory::getAllPublished(1, 0, $restricted, 0, 1, true, $options[0]);
199
                if (count($stories) > 0) {
200
                    $firststory = $stories[0];
201
                    $tmpstory->NewsStory($firststory->storyid());
0 ignored issues
show
Bug introduced by
The method NewsStory() does not seem to exist on object<NewsStory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
                } else {
203
                    $block['use_spotlight'] = false;
204
                }
205
            }
206
            $spotlight          = array();
207
            $spotlight['title'] = $tmpstory->title();
208 View Code Duplication
            if ($options[7] !== '') {
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...
209
                $spotlight['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(), $myts->displayTarea($options[7], $tmpstory->nohtml));
210
            }
211
            $spotlight['text'] = $tmpstory->hometext();
212
213
            // Added 16 february 2007 *****************************************
214
            $story_user = null;
0 ignored issues
show
Unused Code introduced by
$story_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
215
            $story_user = new XoopsUser($tmpstory->uid());
216 View Code Duplication
            if (is_object($story_user)) {
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...
217
                $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
218
            }
219
            // ****************************************************************
220
            $spotlight['id']     = $tmpstory->storyid();
221
            $spotlight['date']   = formatTimestamp($tmpstory->published(), $dateformat);
222
            $spotlight['hits']   = $tmpstory->counter();
223
            $spotlight['rating'] = number_format($tmpstory->rating(), 2);
224
            $spotlight['votes']  = $tmpstory->votes();
225 View Code Duplication
            if (strlen(xoops_trim($tmpstory->bodytext())) > 0) {
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...
226
                $spotlight['read_more'] = true;
227
            } else {
228
                $spotlight['read_more'] = false;
229
            }
230
231
            $spotlight['readmore']        = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(), _MB_READMORE);
232
            $spotlight['title_with_link'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(), $tmpstory->title());
233 View Code Duplication
            if ($tmpstory->votes() == 1) {
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...
234
                $spotlight['number_votes'] = _NW_ONEVOTE;
235
            } else {
236
                $spotlight['number_votes'] = sprintf(_NW_NUMVOTES, $tmpstory->votes());
237
            }
238
239
            $spotlight['votes_with_text'] = sprintf(_NW_NUMVOTES, $tmpstory->votes());
240
            $spotlight['topicid']         = $tmpstory->topicid();
241
            $spotlight['topic_title']     = $tmpstory->topic_title();
242
            // Added, topic's image and description
243
            $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $tmpstory->topic_imgurl();
244
            $spotlight['topic_description'] = $myts->displayTarea($tmpstory->topic_description, 1);
0 ignored issues
show
Bug introduced by
The property topic_description does not seem to exist. Did you mean description?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
245
246
            if ($displayname != 3) {
247
                $spotlight['author']           = sprintf('%s %s', _POSTEDBY, $tmpstory->uname());
248
                $spotlight['author_with_link'] = sprintf("%s <a href='%s'>%s</a>", _POSTEDBY, XOOPS_URL . '/userinfo.php?uid=' . $tmpstory->uid(), $tmpstory->uname());
249
            } else {
250
                $spotlight['author']           = '';
251
                $spotlight['author_with_link'] = '';
252
            }
253
            $spotlight['author_id'] = $tmpstory->uid();
254
255
            // Create the summary table under the spotlight text
256 View Code Duplication
            if (isset($options[14]) && $options[14] == 0) { // Use all topics
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...
257
                $stories = NewsStory::getAllPublished($options[1], 0, $restricted, 0, 1, true, $options[0]);
258
            } else { // Use some topics
259
                $topics  = array_slice($options, 14);
260
                $stories = NewsStory::getAllPublished($options[1], 0, $restricted, $topics, 1, true, $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...
261
            }
262
            if (count($stories) > 0) {
263
                foreach ($stories as $key => $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...
264
                    $news  = array();
265
                    $title = $story->title();
266 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...
267
                        $title = xoops_substr($title, 0, $options[2] + 3);
268
                    }
269
                    $news['title']       = $title;
270
                    $news['id']          = $story->storyid();
271
                    $news['date']        = formatTimestamp($story->published(), $dateformat);
272
                    $news['hits']        = $story->counter();
273
                    $news['rating']      = number_format($story->rating(), 2);
274
                    $news['votes']       = $story->votes();
275
                    $news['topicid']     = $story->topicid();
276
                    $news['topic_title'] = $story->topic_title();
277
                    $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
278
                    $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
279
                    $news['pictureinfo'] = $story->pictureinfo();
280 View Code Duplication
                    if ($displayname != 3) {
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...
281
                        $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
282
                    } else {
283
                        $news['author'] = '';
284
                    }
285 View Code Duplication
                    if ($options[3] > 0) {
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...
286
                        $html           = $story->nohtml() == 1 ? 0 : 1;
287
                        $news['teaser'] = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
288
                    } else {
289
                        $news['teaser'] = '';
290
                    }
291
                    if ($infotips > 0) {
292
                        $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
293
                    } else {
294
                        $news['infotips'] = '';
295
                    }
296
297
                    $news['title_with_link'] = sprintf("<a href='%s'%s>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $news['infotips'], $title);
298
                    $spotlight['news'][]     = $news;
299
                }
300
            }
301
302
            $block['spotlight'] = $spotlight;
303
        } else {
304
            if ($tabscount > 0) {
305
                $topics   = array_slice($options, 14);
0 ignored issues
show
Unused Code introduced by
$topics is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
306
                $thetopic = $currenttab;
307
                $stories  = NewsStory::getAllPublished($options[1], 0, $restricted, $thetopic, 1, true, $options[0]);
308
309
                $topic->getTopic($thetopic);
310
                // Added, topic's image and description
311
                $block['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $topic->topic_imgurl();
312
                $block['topic_description'] = $topic->topic_description();
313
314
                $smallheader   = array();
315
                $stats         = $topic->getTopicMiniStats($thetopic);
316
                $smallheader[] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/index.php?storytopic=' . $thetopic, _MB_READMORE);
317
                $smallheader[] = sprintf('%u %s', $stats['count'], _NW_ARTICLES);
318
                $smallheader[] = sprintf('%u %s', $stats['reads'], _READS);
319
                if (count($stories) > 0) {
320
                    foreach ($stories as $key => $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...
321
                        $news  = array();
322
                        $title = $story->title();
323
                        if (strlen($title) > $options[2]) {
324
                            $title = news_truncate_tagsafe($title, $options[2] + 3);
325
                        }
326 View Code Duplication
                        if ($options[7] !== '') {
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...
327
                            $news['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $myts->displayTarea($options[7], $story->nohtml));
328
                        }
329 View Code Duplication
                        if ($options[3] > 0) {
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...
330
                            $html         = $story->nohtml() == 1 ? 0 : 1;
331
                            $news['text'] = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
332
                        } else {
333
                            $news['text'] = '';
334
                        }
335
336 View Code Duplication
                        if ($story->votes() == 1) {
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...
337
                            $news['number_votes'] = _NW_ONEVOTE;
338
                        } else {
339
                            $news['number_votes'] = sprintf(_NW_NUMVOTES, $story->votes());
340
                        }
341
                        if ($infotips > 0) {
342
                            $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
343
                        } else {
344
                            $news['infotips'] = '';
345
                        }
346
                        $news['title']       = sprintf("<a href='%s' %s>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $news['infotips'], $title);
347
                        $news['id']          = $story->storyid();
348
                        $news['date']        = formatTimestamp($story->published(), $dateformat);
349
                        $news['hits']        = $story->counter();
350
                        $news['rating']      = number_format($story->rating(), 2);
351
                        $news['votes']       = $story->votes();
352
                        $news['topicid']     = $story->topicid();
353
                        $news['topic_title'] = $story->topic_title();
354
                        $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
355
                        $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
356
                        $news['pictureinfo'] = $story->pictureinfo();
357
358 View Code Duplication
                        if ($displayname != 3) {
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...
359
                            $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
360
                        } else {
361
                            $news['author'] = '';
362
                        }
363
                        $news['title_with_link'] = sprintf("<a href='%s'%s>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $news['infotips'], $title);
364
                        $block['news'][]         = $news;
365
                    }
366
                    $block['smallheader'] = $smallheader;
367
                }
368
            }
369
        }
370
        $block['lang_on']    = _ON; // on
371
        $block['lang_reads'] = _READS; // reads
372
        // Default values
373
        $block['color1'] = $defcolors[$tabskin][0];
374
        $block['color2'] = $defcolors[$tabskin][1];
375
        $block['color3'] = $defcolors[$tabskin][2];
376
        $block['color4'] = $defcolors[$tabskin][3];
377
        $block['color5'] = $defcolors[$tabskin][4];
378
379
        if (xoops_trim($options[9]) !== '') {
380
            $block['color1'] = $options[9];
381
        }
382
        if (xoops_trim($options[10]) !== '') {
383
            $block['color2'] = $options[10];
384
        }
385
        if (xoops_trim($options[11]) !== '') {
386
            $block['color3'] = $options[11];
387
        }
388
        if (xoops_trim($options[12]) !== '') {
389
            $block['color4'] = $options[12];
390
        }
391
        if (xoops_trim($options[13]) !== '') {
392
            $block['color5'] = $options[13];
393
        }
394
    } else { // ************************ Classical view **************************************************************************************************************
395
        $tmpstory = new NewsStory;
0 ignored issues
show
Unused Code introduced by
$tmpstory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
396 View Code Duplication
        if (isset($options[14]) && $options[14] == 0) {
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...
397
            $stories = NewsStory::getAllPublished($options[1], 0, $restricted, 0, 1, true, $options[0]);
398
        } else {
399
            $topics  = array_slice($options, 14);
400
            $stories = NewsStory::getAllPublished($options[1], 0, $restricted, $topics, 1, true, $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...
401
        }
402
403
        if (!count($stories)) {
404
            return '';
405
        }
406
        $topic = new NewsTopic();
0 ignored issues
show
Unused Code introduced by
$topic is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
407
408
        foreach ($stories as $key => $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...
409
            $news  = array();
410
            $title = $story->title();
411 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...
412
                $title = xoops_substr($title, 0, $options[2] + 3);
413
            }
414
415
            //if spotlight is enabled and this is either the first article or the selected one
416
            if (($options[5] == 0) && ($options[4] == 1) && (($options[6] > 0 && $options[6] == $story->storyid()) || ($options[6] == 0 && $key == 0))) {
417
                $spotlight = array();
418
                $visible   = true;
419
                if ($restricted) {
420
                    $permittedtopics = news_MygetItemIds();
421
                    if (!in_array($story->topicid(), $permittedtopics)) {
422
                        $visible = false;
423
                    }
424
                }
425
426
                if ($visible) {
427
                    $spotlight['title'] = $title;
428 View Code Duplication
                    if ($options[7] !== '') {
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...
429
                        $spotlight['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $myts->displayTarea($options[7], $story->nohtml));
430
                    }
431
                    // Added 16 february 2007 *****************************************
432
                    $story_user = null;
0 ignored issues
show
Unused Code introduced by
$story_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
433
                    $story_user = new XoopsUser($story->uid());
434 View Code Duplication
                    if (is_object($story_user)) {
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...
435
                        $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
436
                    }
437
                    // ****************************************************************
438
                    $spotlight['text']        = $story->hometext();
439
                    $spotlight['id']          = $story->storyid();
440
                    $spotlight['date']        = formatTimestamp($story->published(), $dateformat);
441
                    $spotlight['hits']        = $story->counter();
442
                    $spotlight['rating']      = $story->rating();
443
                    $spotlight['votes']       = $story->votes();
444
                    $spotlight['topicid']     = $story->topicid();
445
                    $spotlight['topic_title'] = $story->topic_title();
446
                    $spotlight['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
447
                    // Added, topic's image and description
448
                    $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $story->topic_imgurl();
449
                    $spotlight['topic_description'] = $myts->displayTarea($story->topic_description, 1);
450 View Code Duplication
                    if (strlen(xoops_trim($story->bodytext())) > 0) {
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...
451
                        $spotlight['read_more'] = true;
452
                    } else {
453
                        $spotlight['read_more'] = false;
454
                    }
455
456 View Code Duplication
                    if ($displayname != 3) {
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...
457
                        $spotlight['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
458
                    } else {
459
                        $spotlight['author'] = '';
460
                    }
461
                }
462
                $block['spotlight'] = $spotlight;
463
            } else {
464
                $news['title']       = $title;
465
                $news['id']          = $story->storyid();
466
                $news['date']        = formatTimestamp($story->published(), $dateformat);
467
                $news['hits']        = $story->counter();
468
                $news['rating']      = $story->rating();
469
                $news['votes']       = $story->votes();
470
                $news['topicid']     = $story->topicid();
471
                $news['topic_title'] = $story->topic_title();
472
                $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
473
                $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
474
                $news['pictureinfo'] = $story->pictureinfo();
475
476 View Code Duplication
                if ($displayname != 3) {
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...
477
                    $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
478
                } else {
479
                    $news['author'] = '';
480
                }
481
                if ($options[3] > 0) {
482
                    $html             = $story->nohtml() == 1 ? 0 : 1;
483
                    $news['teaser']   = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
484
                    $news['infotips'] = '';
485
                } else {
486
                    $news['teaser'] = '';
487
                    if ($infotips > 0) {
488
                        $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
489
                    } else {
490
                        $news['infotips'] = '';
491
                    }
492
                }
493
                $block['stories'][] = $news;
494
            }
495
        }
496
497
        // If spotlight article was not in the fetched stories
498
        if (!isset($spotlight) && $options[4]) {
499
            $block['use_spotlight'] = true;
500
            $visible                = true;
501
            if ($options[5] == 0 && $restricted) { // Use a specific news and we are in restricted mode
502
                $permittedtopics = news_MygetItemIds();
503
                $permstory       = new NewsStory($options[6]);
504
                if (!in_array($permstory->topicid(), $permittedtopics)) {
505
                    $visible = false;
506
                }
507
                unset($permstory);
508
            }
509
510
            if ($options[5] == 0) { // Use a specific news
511
                if ($visible) {
512
                    $spotlightArticle = new NewsStory($options[6]);
513
                } else {
514
                    $block['use_spotlight'] = false;
515
                }
516 View Code Duplication
            } else { // Use the most recent news
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...
517
                $stories = array();
0 ignored issues
show
Unused Code introduced by
$stories is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
518
                $stories = NewsStory::getAllPublished(1, 0, $restricted, 0, 1, true, $options[0]);
519
                if (count($stories) > 0) {
520
                    $firststory       = $stories[0];
521
                    $spotlightArticle = new NewsStory($firststory->storyid());
522
                } else {
523
                    $block['use_spotlight'] = false;
524
                }
525
            }
526
            if ($block['use_spotlight'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
527
                $spotlight          = array();
528
                $spotlight['title'] = xoops_substr($spotlightArticle->title(), 0, $options[2] - 1);
0 ignored issues
show
Bug introduced by
The variable $spotlightArticle does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
529 View Code Duplication
                if ($options[7] !== '') {
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...
530
                    $spotlight['image'] =
531
                        sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $spotlightArticle->storyid(), $myts->displayTarea($options[7], $spotlightArticle->nohtml));
532
                }
533
                // Added 16 february 2007 *****************************************
534
                $story_user = null;
0 ignored issues
show
Unused Code introduced by
$story_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
535
                $story_user = new XoopsUser($spotlightArticle->uid());
536 View Code Duplication
                if (is_object($story_user)) {
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...
537
                    $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
538
                }
539
                // ****************************************************************
540
                $spotlight['topicid']     = $spotlightArticle->topicid();
541
                $spotlight['topic_title'] = $spotlightArticle->topic_title();
542
                $spotlight['topic_color'] = '#' . $myts->displayTarea($spotlightArticle->topic_color);
0 ignored issues
show
Bug introduced by
The property topic_color does not seem to exist in NewsStory.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
543
                $spotlight['text']        = $spotlightArticle->hometext();
544
                $spotlight['id']          = $spotlightArticle->storyid();
545
                $spotlight['date']        = formatTimestamp($spotlightArticle->published(), $dateformat);
546
                $spotlight['hits']        = $spotlightArticle->counter();
547
                $spotlight['rating']      = $spotlightArticle->rating();
548
                $spotlight['votes']       = $spotlightArticle->votes();
549
                // Added, topic's image and description
550
                $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $spotlightArticle->topic_imgurl();
551
                $spotlight['topic_description'] = $myts->displayTarea($spotlightArticle->topic_description, 1);
0 ignored issues
show
Bug introduced by
The property topic_description does not seem to exist. Did you mean description?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
552 View Code Duplication
                if ($displayname != 3) {
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...
553
                    $spotlight['author'] = sprintf('%s %s', _POSTEDBY, $spotlightArticle->uname());
554
                } else {
555
                    $spotlight['author'] = '';
556
                }
557 View Code Duplication
                if (strlen(xoops_trim($spotlightArticle->bodytext())) > 0) {
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...
558
                    $spotlight['read_more'] = true;
559
                } else {
560
                    $spotlight['read_more'] = false;
561
                }
562
                $block['spotlight'] = $spotlight;
563
            }
564
        }
565
    }
566
    if (isset($permstory)) {
567
        unset($permstory);
568
    }
569
    $block['lang_read_more']      = $myts->htmlSpecialChars(_MB_READMORE); // Read More...
570
    $block['lang_orderby']        = $myts->htmlSpecialChars(_MB_NEWS_ORDER); // "Order By"
571
    $block['lang_orderby_date']   = $myts->htmlSpecialChars(_MB_NEWS_DATE); // Published date
572
    $block['lang_orderby_hits']   = $myts->htmlSpecialChars(_MB_NEWS_HITS); // Number of Hits
573
    $block['lang_orderby_rating'] = $myts->htmlSpecialChars(_MB_NEWS_RATE); // Rating
574
    $block['sort']                = $options[0]; // "published" or "counter" or "rating"
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
575
576
    return $block;
577
}
578
579
/**
580
 * Function used to edit the block
581
 * @param $options
582
 * @return string
583
 */
584
function b_news_top_edit($options)
585
{
586
    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...
587
    $tmpstory = new NewsStory;
0 ignored issues
show
Unused Code introduced by
$tmpstory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
588
    $form     = _MB_NEWS_ORDER . "&nbsp;<select name='options[]'>";
589
    $form .= "<option value='published'";
590
    if ($options[0] === 'published') {
591
        $form .= " selected='selected'";
592
    }
593
    $form .= '>' . _MB_NEWS_DATE . "</option>\n";
594
595
    $form .= "<option value='counter'";
596
    if ($options[0] === 'counter') {
597
        $form .= " selected='selected'";
598
    }
599
    $form .= '>' . _MB_NEWS_HITS . '</option>';
600
    $form .= "<option value='rating'";
601
    if ($options[0] === 'rating') {
602
        $form .= " selected='selected'";
603
    }
604
    $form .= '>' . _MB_NEWS_RATE . '</option>';
605
    $form .= "</select>\n";
606
607
    $form .= '&nbsp;' . _MB_NEWS_DISP . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . "'/>&nbsp;" . _MB_NEWS_ARTCLS;
608
    $form .= '&nbsp;<br><br>' . _MB_NEWS_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'/>&nbsp;" . _MB_NEWS_LENGTH . '<br><br>';
609
610
    $form .= _MB_NEWS_TEASER . " <input type='text' name='options[]' value='" . $options[3] . "' />" . _MB_NEWS_LENGTH;
611
    $form .= '<br><br>';
612
613
    $form .= _MB_NEWS_SPOTLIGHT . " <input type='radio' name='options[]' value='1'";
614
    if ($options[4] == 1) {
615
        $form .= " checked='checked'";
616
    }
617
    $form .= ' />' . _YES;
618
    $form .= "<input type='radio' name='options[]' value='0'";
619
    if ($options[4] == 0) {
620
        $form .= " checked='checked'";
621
    }
622
    $form .= ' />' . _NO . '<br><br>';
623
624
    $form .= _MB_NEWS_WHAT_PUBLISH . " <select name='options[]'><option value='1'";
625
    if ($options[5] == 1) {
626
        $form .= ' selected';
627
    }
628
    $form .= ' />' . _MB_NEWS_RECENT_NEWS;
629
    $form .= "</option><option value='0'";
630
    if ($options[5] == 0) {
631
        $form .= ' selected';
632
    }
633
    $form .= ' />' . _MB_NEWS_RECENT_SPECIFIC . '</option></select>';
634
635
    $form .= '<br><br>' . _MB_NEWS_SPOTLIGHT_ARTICLE . '<br>';
636
    $articles = NewsStory::getAllPublished(200, 0, false, 0, 0, false); // I have limited the listbox to the last 200 articles
637
    $form .= "<select name ='options[]'>";
638
    $form .= "<option value='0'>" . _MB_NEWS_FIRST . '</option>';
639
    foreach ($articles as $storyid => $storytitle) {
0 ignored issues
show
Bug introduced by
The expression $articles 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...
640
        $sel = '';
641
        if ($options[6] == $storyid) {
642
            $sel = " selected='selected'";
643
        }
644
        $form .= "<option value='$storyid'$sel>" . $storytitle . '</option>';
645
    }
646
    $form .= '</select><br><br>';
647
648
    $form .= _MB_NEWS_IMAGE . "&nbsp;<input type='text' id='spotlightimage' name='options[]' value='" . $options[7] . "' size='50'/>";
649
    $form .= "&nbsp;<img align='middle' onmouseover='style.cursor=\"hand\"' onclick='javascript:openWithSelfMain(\""
650
             . XOOPS_URL
651
             . "/imagemanager.php?target=spotlightimage\",\"imgmanager\",400,430);' src='"
652
             . XOOPS_URL
653
             . "/images/image.gif' alt='image' title='image' />";
654
    $form .= '<br><br>' . _MB_NEWS_DISP . "&nbsp;<select name='options[]'><option value='1' ";
655
    if ($options[8] == 1) {
656
        $form .= 'selected';
657
    }
658
    $form .= '>' . _MB_NEWS_VIEW_TYPE1 . "</option><option value='2' ";
659
    if ($options[8] == 2) {
660
        $form .= 'selected';
661
    }
662
    $form .= '>' . _MB_NEWS_VIEW_TYPE2 . '</option></select><br><br>';
663
664
    $form .= "<table border=0>\n";
665
    $form .= "<tr><td colspan='2' align='center'><u>" . _MB_NEWS_DEFAULT_COLORS . '</u></td></tr>';
666
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR1 . "</td><td><input type='text' name='options[]' value='" . $options[9] . "' size=7></td></tr>";
667
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR2 . "</td><td><input type='text' name='options[]' value='" . $options[10] . "' size=7></td></tr>";
668
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR3 . "</td><td><input type='text' name='options[]' value='" . $options[11] . "' size=7></td></tr>";
669
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR4 . "</td><td><input type='text' name='options[]' value='" . $options[12] . "' size=7></td></tr>";
670
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR5 . "</td><td><input type='text' name='options[]' value='" . $options[13] . "' size=7></td></tr>";
671
    $form .= "</table>\n";
672
673
    $form .= '<br><br>' . _MB_SPOTLIGHT_TOPIC . "<br><select name='options[]' multiple='multiple'>";
674
    include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newstopic.php';
675
    $topics_arr = array();
0 ignored issues
show
Unused Code introduced by
$topics_arr is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
676
    include_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstree.php';
677
    $xt         = new MyXoopsTree($xoopsDB->prefix('news_topics'), 'topic_id', 'topic_pid');
678
    $topics_arr = $xt->getChildTreeArray(0, 'topic_title');
679
    $size       = count($options);
680
    foreach ($topics_arr as $onetopic) {
681
        $sel = '';
682 View Code Duplication
        if ($onetopic['topic_pid'] != 0) {
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...
683
            $onetopic['prefix'] = str_replace('.', '-', $onetopic['prefix']) . '&nbsp;';
684
        } else {
685
            $onetopic['prefix'] = str_replace('.', '', $onetopic['prefix']);
686
        }
687
        for ($i = 14; $i < $size; ++$i) {
688
            if ($options[$i] == $onetopic['topic_id']) {
689
                $sel = " selected='selected'";
690
            }
691
        }
692
        $form .= "<option value='" . $onetopic['topic_id'] . "'$sel>" . $onetopic['prefix'] . $onetopic['topic_title'] . '</option>';
693
    }
694
    $form .= '</select><br>';
695
696
    return $form;
697
}
698
699
/**
700
 * @param $options
701
 */
702 View Code Duplication
function b_news_top_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...
703
{
704
    $options = explode('|', $options);
705
    $block   = &b_news_top_show($options);
706
707
    $tpl = new XoopsTpl();
708
    $tpl->assign('block', $block);
709
    $tpl->display('db:news_block_top.tpl');
710
}
711