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

news_top.php ➔ b_news_top_edit()   F

Complexity

Conditions 16
Paths 10752

Size

Total Lines 120
Code Lines 93

Duplication

Lines 5
Ratio 4.17 %

Importance

Changes 0
Metric Value
cc 16
eloc 93
nc 10752
nop 1
dl 5
loc 120
rs 2
c 0
b 0
f 0

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(
176
                        'id'      => $onetopic,
177
                        'title'   => $topicstitles[$onetopic]['title'],
178
                        'picture' => $topicstitles[$onetopic]['picture']
179
                    );
180
                }
181
            }
182
        }
183
        $block['tabs']                 = $tabs;
184
        $block['current_is_spotlight'] = false;
185
        $block['current_tab']          = $currenttab;
186
        $block['use_rating']           = $newsrating;
187
188
        if ($currenttab == 0 && $usespotlight) { // Spotlight or not ?
189
            $block['current_is_spotlight'] = true;
190
            if ($options[5] == 0
191
                && $options[6] == 0
192
            ) { // If the story to use was no selected then we switch to the "recent news" mode.
193
                $options[5] = 1;
194
            }
195
196
            if ($options[5] == 0) { // Use a specific news
197
                if (!isset($permstory)) {
198
                    $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...
199
                } else {
200
                    $tmpstory = $permstory;
201
                }
202 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...
203
                $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...
204
                $stories = NewsStory::getAllPublished(1, 0, $restricted, 0, 1, true, $options[0]);
205
                if (count($stories) > 0) {
206
                    $firststory = $stories[0];
207
                    $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...
208
                } else {
209
                    $block['use_spotlight'] = false;
210
                }
211
            }
212
            $spotlight          = array();
213
            $spotlight['title'] = $tmpstory->title();
214 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...
215
                $spotlight['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(),
216
                                              $myts->displayTarea($options[7], $tmpstory->nohtml));
217
            }
218
            $spotlight['text'] = $tmpstory->hometext();
219
220
            // Added 16 february 2007 *****************************************
221
            $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...
222
            $story_user = new XoopsUser($tmpstory->uid());
223 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...
224
                $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
225
            }
226
            // ****************************************************************
227
            $spotlight['id']     = $tmpstory->storyid();
228
            $spotlight['date']   = formatTimestamp($tmpstory->published(), $dateformat);
229
            $spotlight['hits']   = $tmpstory->counter();
230
            $spotlight['rating'] = number_format($tmpstory->rating(), 2);
231
            $spotlight['votes']  = $tmpstory->votes();
232 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...
233
                $spotlight['read_more'] = true;
234
            } else {
235
                $spotlight['read_more'] = false;
236
            }
237
238
            $spotlight['readmore']        = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(),
239
                                                    _MB_READMORE);
240
            $spotlight['title_with_link'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $tmpstory->storyid(),
241
                                                    $tmpstory->title());
242 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...
243
                $spotlight['number_votes'] = _NW_ONEVOTE;
244
            } else {
245
                $spotlight['number_votes'] = sprintf(_NW_NUMVOTES, $tmpstory->votes());
246
            }
247
248
            $spotlight['votes_with_text'] = sprintf(_NW_NUMVOTES, $tmpstory->votes());
249
            $spotlight['topicid']         = $tmpstory->topicid();
250
            $spotlight['topic_title']     = $tmpstory->topic_title();
251
            // Added, topic's image and description
252
            $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $tmpstory->topic_imgurl();
253
            $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...
254
255
            if ($displayname != 3) {
256
                $spotlight['author']           = sprintf('%s %s', _POSTEDBY, $tmpstory->uname());
257
                $spotlight['author_with_link'] = sprintf("%s <a href='%s'>%s</a>", _POSTEDBY, XOOPS_URL . '/userinfo.php?uid=' . $tmpstory->uid(),
258
                                                         $tmpstory->uname());
259
            } else {
260
                $spotlight['author']           = '';
261
                $spotlight['author_with_link'] = '';
262
            }
263
            $spotlight['author_id'] = $tmpstory->uid();
264
265
            // Create the summary table under the spotlight text
266 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...
267
                $stories = NewsStory::getAllPublished($options[1], 0, $restricted, 0, 1, true, $options[0]);
268
            } else { // Use some topics
269
                $topics  = array_slice($options, 14);
270
                $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...
271
            }
272
            if (count($stories) > 0) {
273
                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...
274
                    $news  = array();
275
                    $title = $story->title();
276 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...
277
                        $title = xoops_substr($title, 0, $options[2] + 3);
278
                    }
279
                    $news['title']       = $title;
280
                    $news['id']          = $story->storyid();
281
                    $news['date']        = formatTimestamp($story->published(), $dateformat);
282
                    $news['hits']        = $story->counter();
283
                    $news['rating']      = number_format($story->rating(), 2);
284
                    $news['votes']       = $story->votes();
285
                    $news['topicid']     = $story->topicid();
286
                    $news['topic_title'] = $story->topic_title();
287
                    $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
288
                    $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
289
                    $news['pictureinfo'] = $story->pictureinfo();
290 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...
291
                        $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
292
                    } else {
293
                        $news['author'] = '';
294
                    }
295 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...
296
                        $html           = $story->nohtml() == 1 ? 0 : 1;
297
                        $news['teaser'] = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
298
                    } else {
299
                        $news['teaser'] = '';
300
                    }
301
                    if ($infotips > 0) {
302
                        $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
303
                    } else {
304
                        $news['infotips'] = '';
305
                    }
306
307
                    $news['title_with_link'] = sprintf("<a href='%s'%s>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(),
308
                                                       $news['infotips'], $title);
309
                    $spotlight['news'][]     = $news;
310
                }
311
            }
312
313
            $block['spotlight'] = $spotlight;
314
        } else {
315
            if ($tabscount > 0) {
316
                $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...
317
                $thetopic = $currenttab;
318
                $stories  = NewsStory::getAllPublished($options[1], 0, $restricted, $thetopic, 1, true, $options[0]);
319
320
                $topic->getTopic($thetopic);
321
                // Added, topic's image and description
322
                $block['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $topic->topic_imgurl();
323
                $block['topic_description'] = $topic->topic_description();
324
325
                $smallheader   = array();
326
                $stats         = $topic->getTopicMiniStats($thetopic);
327
                $smallheader[] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/index.php?storytopic=' . $thetopic, _MB_READMORE);
328
                $smallheader[] = sprintf('%u %s', $stats['count'], _NW_ARTICLES);
329
                $smallheader[] = sprintf('%u %s', $stats['reads'], _READS);
330
                if (count($stories) > 0) {
331
                    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...
332
                        $news  = array();
333
                        $title = $story->title();
334
                        if (strlen($title) > $options[2]) {
335
                            $title = news_truncate_tagsafe($title, $options[2] + 3);
336
                        }
337 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...
338
                            $news['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(),
339
                                                     $myts->displayTarea($options[7], $story->nohtml));
340
                        }
341 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...
342
                            $html         = $story->nohtml() == 1 ? 0 : 1;
343
                            $news['text'] = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
344
                        } else {
345
                            $news['text'] = '';
346
                        }
347
348 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...
349
                            $news['number_votes'] = _NW_ONEVOTE;
350
                        } else {
351
                            $news['number_votes'] = sprintf(_NW_NUMVOTES, $story->votes());
352
                        }
353
                        if ($infotips > 0) {
354
                            $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
355
                        } else {
356
                            $news['infotips'] = '';
357
                        }
358
                        $news['title']       = sprintf("<a href='%s' %s>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(),
359
                                                       $news['infotips'], $title);
360
                        $news['id']          = $story->storyid();
361
                        $news['date']        = formatTimestamp($story->published(), $dateformat);
362
                        $news['hits']        = $story->counter();
363
                        $news['rating']      = number_format($story->rating(), 2);
364
                        $news['votes']       = $story->votes();
365
                        $news['topicid']     = $story->topicid();
366
                        $news['topic_title'] = $story->topic_title();
367
                        $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
368
                        $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
369
                        $news['pictureinfo'] = $story->pictureinfo();
370
371 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...
372
                            $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
373
                        } else {
374
                            $news['author'] = '';
375
                        }
376
                        $news['title_with_link'] = sprintf("<a href='%s'%s>%s</a>",
377
                                                           XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(), $news['infotips'],
378
                                                           $title);
379
                        $block['news'][]         = $news;
380
                    }
381
                    $block['smallheader'] = $smallheader;
382
                }
383
            }
384
        }
385
        $block['lang_on']    = _ON; // on
386
        $block['lang_reads'] = _READS; // reads
387
        // Default values
388
        $block['color1'] = $defcolors[$tabskin][0];
389
        $block['color2'] = $defcolors[$tabskin][1];
390
        $block['color3'] = $defcolors[$tabskin][2];
391
        $block['color4'] = $defcolors[$tabskin][3];
392
        $block['color5'] = $defcolors[$tabskin][4];
393
394
        if (xoops_trim($options[9]) !== '') {
395
            $block['color1'] = $options[9];
396
        }
397
        if (xoops_trim($options[10]) !== '') {
398
            $block['color2'] = $options[10];
399
        }
400
        if (xoops_trim($options[11]) !== '') {
401
            $block['color3'] = $options[11];
402
        }
403
        if (xoops_trim($options[12]) !== '') {
404
            $block['color4'] = $options[12];
405
        }
406
        if (xoops_trim($options[13]) !== '') {
407
            $block['color5'] = $options[13];
408
        }
409
    } else { // ************************ Classical view **************************************************************************************************************
410
        $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...
411 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...
412
            $stories = NewsStory::getAllPublished($options[1], 0, $restricted, 0, 1, true, $options[0]);
413
        } else {
414
            $topics  = array_slice($options, 14);
415
            $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...
416
        }
417
418
        if (!count($stories)) {
419
            return '';
420
        }
421
        $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...
422
423
        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...
424
            $news  = array();
425
            $title = $story->title();
426 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...
427
                $title = xoops_substr($title, 0, $options[2] + 3);
428
            }
429
430
            //if spotlight is enabled and this is either the first article or the selected one
431
            if (($options[5] == 0) && ($options[4] == 1)
432
                && (($options[6] > 0 && $options[6] == $story->storyid())
433
                    || ($options[6] == 0 && $key == 0))
434
            ) {
435
                $spotlight = array();
436
                $visible   = true;
437
                if ($restricted) {
438
                    $permittedtopics = news_MygetItemIds();
439
                    if (!in_array($story->topicid(), $permittedtopics)) {
440
                        $visible = false;
441
                    }
442
                }
443
444
                if ($visible) {
445
                    $spotlight['title'] = $title;
446 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...
447
                        $spotlight['image'] = sprintf("<a href='%s'>%s</a>", XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid(),
448
                                                      $myts->displayTarea($options[7], $story->nohtml));
449
                    }
450
                    // Added 16 february 2007 *****************************************
451
                    $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...
452
                    $story_user = new XoopsUser($story->uid());
453 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...
454
                        $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
455
                    }
456
                    // ****************************************************************
457
                    $spotlight['text']        = $story->hometext();
458
                    $spotlight['id']          = $story->storyid();
459
                    $spotlight['date']        = formatTimestamp($story->published(), $dateformat);
460
                    $spotlight['hits']        = $story->counter();
461
                    $spotlight['rating']      = $story->rating();
462
                    $spotlight['votes']       = $story->votes();
463
                    $spotlight['topicid']     = $story->topicid();
464
                    $spotlight['topic_title'] = $story->topic_title();
465
                    $spotlight['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
466
                    // Added, topic's image and description
467
                    $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $story->topic_imgurl();
468
                    $spotlight['topic_description'] = $myts->displayTarea($story->topic_description, 1);
469 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...
470
                        $spotlight['read_more'] = true;
471
                    } else {
472
                        $spotlight['read_more'] = false;
473
                    }
474
475 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...
476
                        $spotlight['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
477
                    } else {
478
                        $spotlight['author'] = '';
479
                    }
480
                }
481
                $block['spotlight'] = $spotlight;
482
            } else {
483
                $news['title']       = $title;
484
                $news['id']          = $story->storyid();
485
                $news['date']        = formatTimestamp($story->published(), $dateformat);
486
                $news['hits']        = $story->counter();
487
                $news['rating']      = $story->rating();
488
                $news['votes']       = $story->votes();
489
                $news['topicid']     = $story->topicid();
490
                $news['topic_title'] = $story->topic_title();
491
                $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
492
                $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
493
                $news['pictureinfo'] = $story->pictureinfo();
494
495 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...
496
                    $news['author'] = sprintf('%s %s', _POSTEDBY, $story->uname());
497
                } else {
498
                    $news['author'] = '';
499
                }
500
                if ($options[3] > 0) {
501
                    $html             = $story->nohtml() == 1 ? 0 : 1;
502
                    $news['teaser']   = news_truncate_tagsafe($myts->displayTarea($story->hometext(), $html), $options[3] + 3);
503
                    $news['infotips'] = '';
504
                } else {
505
                    $news['teaser'] = '';
506
                    if ($infotips > 0) {
507
                        $news['infotips'] = ' title="' . news_make_infotips($story->hometext()) . '"';
508
                    } else {
509
                        $news['infotips'] = '';
510
                    }
511
                }
512
                $block['stories'][] = $news;
513
            }
514
        }
515
516
        // If spotlight article was not in the fetched stories
517
        if (!isset($spotlight) && $options[4]) {
518
            $block['use_spotlight'] = true;
519
            $visible                = true;
520
            if ($options[5] == 0 && $restricted) { // Use a specific news and we are in restricted mode
521
                $permittedtopics = news_MygetItemIds();
522
                $permstory       = new NewsStory($options[6]);
523
                if (!in_array($permstory->topicid(), $permittedtopics)) {
524
                    $visible = false;
525
                }
526
                unset($permstory);
527
            }
528
529
            if ($options[5] == 0) { // Use a specific news
530
                if ($visible) {
531
                    $spotlightArticle = new NewsStory($options[6]);
532
                } else {
533
                    $block['use_spotlight'] = false;
534
                }
535 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...
536
                $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...
537
                $stories = NewsStory::getAllPublished(1, 0, $restricted, 0, 1, true, $options[0]);
538
                if (count($stories) > 0) {
539
                    $firststory       = $stories[0];
540
                    $spotlightArticle = new NewsStory($firststory->storyid());
541
                } else {
542
                    $block['use_spotlight'] = false;
543
                }
544
            }
545
            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...
546
                $spotlight          = array();
547
                $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...
548 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...
549
                    $spotlight['image'] = sprintf("<a href='%s'>%s</a>",
550
                                                  XOOPS_URL . '/modules/news/article.php?storyid=' . $spotlightArticle->storyid(),
551
                                                  $myts->displayTarea($options[7], $spotlightArticle->nohtml));
552
                }
553
                // Added 16 february 2007 *****************************************
554
                $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...
555
                $story_user = new XoopsUser($spotlightArticle->uid());
556 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...
557
                    $spotlight['avatar'] = XOOPS_UPLOAD_URL . '/' . $story_user->getVar('user_avatar');
558
                }
559
                // ****************************************************************
560
                $spotlight['topicid']     = $spotlightArticle->topicid();
561
                $spotlight['topic_title'] = $spotlightArticle->topic_title();
562
                $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...
563
                $spotlight['text']        = $spotlightArticle->hometext();
564
                $spotlight['id']          = $spotlightArticle->storyid();
565
                $spotlight['date']        = formatTimestamp($spotlightArticle->published(), $dateformat);
566
                $spotlight['hits']        = $spotlightArticle->counter();
567
                $spotlight['rating']      = $spotlightArticle->rating();
568
                $spotlight['votes']       = $spotlightArticle->votes();
569
                // Added, topic's image and description
570
                $spotlight['topic_image']       = XOOPS_URL . '/modules/news/assets/images/topics/' . $spotlightArticle->topic_imgurl();
571
                $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...
572 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...
573
                    $spotlight['author'] = sprintf('%s %s', _POSTEDBY, $spotlightArticle->uname());
574
                } else {
575
                    $spotlight['author'] = '';
576
                }
577 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...
578
                    $spotlight['read_more'] = true;
579
                } else {
580
                    $spotlight['read_more'] = false;
581
                }
582
                $block['spotlight'] = $spotlight;
583
            }
584
        }
585
    }
586
    if (isset($permstory)) {
587
        unset($permstory);
588
    }
589
    $block['lang_read_more']      = $myts->htmlSpecialChars(_MB_READMORE); // Read More...
590
    $block['lang_orderby']        = $myts->htmlSpecialChars(_MB_NEWS_ORDER); // "Order By"
591
    $block['lang_orderby_date']   = $myts->htmlSpecialChars(_MB_NEWS_DATE); // Published date
592
    $block['lang_orderby_hits']   = $myts->htmlSpecialChars(_MB_NEWS_HITS); // Number of Hits
593
    $block['lang_orderby_rating'] = $myts->htmlSpecialChars(_MB_NEWS_RATE); // Rating
594
    $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...
595
596
    return $block;
597
}
598
599
/**
600
 * Function used to edit the block
601
 * @param $options
602
 * @return string
603
 */
604
function b_news_top_edit($options)
605
{
606
    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...
607
    $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...
608
    $form     = _MB_NEWS_ORDER . "&nbsp;<select name='options[]'>";
609
    $form .= "<option value='published'";
610
    if ($options[0] === 'published') {
611
        $form .= ' selected';
612
    }
613
    $form .= '>' . _MB_NEWS_DATE . "</option>\n";
614
615
    $form .= "<option value='counter'";
616
    if ($options[0] === 'counter') {
617
        $form .= ' selected';
618
    }
619
    $form .= '>' . _MB_NEWS_HITS . '</option>';
620
    $form .= "<option value='rating'";
621
    if ($options[0] === 'rating') {
622
        $form .= ' selected';
623
    }
624
    $form .= '>' . _MB_NEWS_RATE . '</option>';
625
    $form .= "</select>\n";
626
627
    $form .= '&nbsp;' . _MB_NEWS_DISP . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . "'/>&nbsp;" . _MB_NEWS_ARTCLS;
628
    $form .= '&nbsp;<br><br>'
629
             . _MB_NEWS_CHARS
630
             . "&nbsp;<input type='text' name='options[]' value='"
631
             . $options[2]
632
             . "'/>&nbsp;"
633
             . _MB_NEWS_LENGTH
634
             . '<br><br>';
635
636
    $form .= _MB_NEWS_TEASER . " <input type='text' name='options[]' value='" . $options[3] . "' />" . _MB_NEWS_LENGTH;
637
    $form .= '<br><br>';
638
639
    $form .= _MB_NEWS_SPOTLIGHT . " <input type='radio' name='options[]' value='1'";
640
    if ($options[4] == 1) {
641
        $form .= ' checked';
642
    }
643
    $form .= ' />' . _YES;
644
    $form .= "<input type='radio' name='options[]' value='0'";
645
    if ($options[4] == 0) {
646
        $form .= ' checked';
647
    }
648
    $form .= ' />' . _NO . '<br><br>';
649
650
    $form .= _MB_NEWS_WHAT_PUBLISH . " <select name='options[]'><option value='1'";
651
    if ($options[5] == 1) {
652
        $form .= ' selected';
653
    }
654
    $form .= ' />' . _MB_NEWS_RECENT_NEWS;
655
    $form .= "</option><option value='0'";
656
    if ($options[5] == 0) {
657
        $form .= ' selected';
658
    }
659
    $form .= ' />' . _MB_NEWS_RECENT_SPECIFIC . '</option></select>';
660
661
    $form .= '<br><br>' . _MB_NEWS_SPOTLIGHT_ARTICLE . '<br>';
662
    $articles = NewsStory::getAllPublished(200, 0, false, 0, 0, false); // I have limited the listbox to the last 200 articles
663
    $form .= "<select name ='options[]'>";
664
    $form .= "<option value='0'>" . _MB_NEWS_FIRST . '</option>';
665
    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...
666
        $sel = '';
667
        if ($options[6] == $storyid) {
668
            $sel = ' selected';
669
        }
670
        $form .= "<option value='$storyid'$sel>" . $storytitle . '</option>';
671
    }
672
    $form .= '</select><br><br>';
673
674
    $form .= _MB_NEWS_IMAGE . "&nbsp;<input type='text' id='spotlightimage' name='options[]' value='" . $options[7] . "' size='50'/>";
675
    $form .= "&nbsp;<img align='middle' onmouseover='style.cursor=\"hand\"' onclick='javascript:openWithSelfMain(\""
676
             . XOOPS_URL
677
             . "/imagemanager.php?target=spotlightimage\",\"imgmanager\",400,430);' src='"
678
             . XOOPS_URL
679
             . "/images/image.gif' alt='image' title='image' />";
680
    $form .= '<br><br>' . _MB_NEWS_DISP . "&nbsp;<select name='options[]'><option value='1' ";
681
    if ($options[8] == 1) {
682
        $form .= 'selected';
683
    }
684
    $form .= '>' . _MB_NEWS_VIEW_TYPE1 . "</option><option value='2' ";
685
    if ($options[8] == 2) {
686
        $form .= 'selected';
687
    }
688
    $form .= '>' . _MB_NEWS_VIEW_TYPE2 . '</option></select><br><br>';
689
690
    $form .= "<table border=0>\n";
691
    $form .= "<tr><td colspan='2' align='center'><u>" . _MB_NEWS_DEFAULT_COLORS . '</u></td></tr>';
692
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR1 . "</td><td><input type='text' name='options[]' value='" . $options[9] . "' size=7></td></tr>";
693
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR2 . "</td><td><input type='text' name='options[]' value='" . $options[10] . "' size=7></td></tr>";
694
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR3 . "</td><td><input type='text' name='options[]' value='" . $options[11] . "' size=7></td></tr>";
695
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR4 . "</td><td><input type='text' name='options[]' value='" . $options[12] . "' size=7></td></tr>";
696
    $form .= '<tr><td>' . _MB_NEWS_TAB_COLOR5 . "</td><td><input type='text' name='options[]' value='" . $options[13] . "' size=7></td></tr>";
697
    $form .= "</table>\n";
698
699
    $form .= '<br><br>' . _MB_SPOTLIGHT_TOPIC . "<br><select name='options[]' multiple='multiple'>";
700
    include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newstopic.php';
701
    $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...
702
    include_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstree.php';
703
    $xt         = new MyXoopsTree($xoopsDB->prefix('news_topics'), 'topic_id', 'topic_pid');
704
    $topics_arr = $xt->getChildTreeArray(0, 'topic_title');
705
    $size       = count($options);
706
    foreach ($topics_arr as $onetopic) {
707
        $sel = '';
708 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...
709
            $onetopic['prefix'] = str_replace('.', '-', $onetopic['prefix']) . '&nbsp;';
710
        } else {
711
            $onetopic['prefix'] = str_replace('.', '', $onetopic['prefix']);
712
        }
713
        for ($i = 14; $i < $size; ++$i) {
714
            if ($options[$i] == $onetopic['topic_id']) {
715
                $sel = ' selected';
716
            }
717
        }
718
        $form .= "<option value='" . $onetopic['topic_id'] . "'$sel>" . $onetopic['prefix'] . $onetopic['topic_title'] . '</option>';
719
    }
720
    $form .= '</select><br>';
721
722
    return $form;
723
}
724
725
/**
726
 * @param $options
727
 */
728 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...
729
{
730
    $options = explode('|', $options);
731
    $block   = &b_news_top_show($options);
732
733
    $tpl = new XoopsTpl();
734
    $tpl->assign('block', $block);
735
    $tpl->display('db:news_block_top.tpl');
736
}
737