Completed
Push — master ( 923121...f83415 )
by Michael
02:52
created

adsligh_rsslib.php ➔ RSS_Display()   C

Complexity

Conditions 10
Paths 136

Size

Total Lines 48
Code Lines 31

Duplication

Lines 12
Ratio 25 %

Importance

Changes 0
Metric Value
cc 10
eloc 31
nc 136
nop 3
dl 12
loc 48
rs 5.0666
c 0
b 0
f 0

How to fix   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 31 and the first side effect is on line 23.

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
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
23
$RSS_Content = array();
24
25
/**
26
 * @param $item
27
 * @param $type
28
 *
29
 * @return array
30
 */
31
function RSS_Tags($item, $type)
32
{
33
    $y     = array();
34
    $tnl   = $item->getElementsByTagName('title');
35
    $tnl   = $tnl->item(0);
36
    $title = $tnl->firstChild->textContent;
37
38
    $tnl  = $item->getElementsByTagName('link');
39
    $tnl  = $tnl->item(0);
40
    $link = $tnl->firstChild->textContent;
41
42
    $tnl  = $item->getElementsByTagName('pubDate');
43
    $tnl  = $tnl->item(0);
44
    $date = $tnl->firstChild->textContent;
45
46
    $tnl         = $item->getElementsByTagName('description');
47
    $tnl         = $tnl->item(0);
48
    $description = $tnl->firstChild->textContent;
49
50
    $y['title']       = $title;
51
    $y['link']        = $link;
52
    $y['date']        = $date;
53
    $y['description'] = $description;
54
    $y['type']        = $type;
55
56
    return $y;
57
}
58
59
/**
60
 * @param $channel
61
 */
62
function RSS_Channel($channel)
63
{
64
    global $RSS_Content;
1 ignored issue
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...
65
66
    $items = $channel->getElementsByTagName('item');
67
68
    // Processing channel
69
70
    $y = RSS_Tags($channel, 0);        // get description of channel, type 0
71
    array_push($RSS_Content, $y);
72
73
    // Processing articles
74
75
    foreach ($items as $item) {
76
        $y = RSS_Tags($item, 1);    // get description of article, type 1
77
        array_push($RSS_Content, $y);
78
    }
79
}
80
81
/**
82
 * @param $url
83
 */
84
function RSS_Retrieve($url)
85
{
86
    global $RSS_Content;
1 ignored issue
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...
87
88
    $doc = new DOMDocument();
89
    $doc->load($url);
90
91
    $channels = $doc->getElementsByTagName('channel');
92
93
    $RSS_Content = array();
94
95
    foreach ($channels as $channel) {
96
        RSS_Channel($channel);
97
    }
98
}
99
100
/**
101
 * @param $url
102
 */
103
function RSS_RetrieveLinks($url)
104
{
105
    global $RSS_Content;
1 ignored issue
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...
106
107
    $doc = new DOMDocument();
108
    $doc->load($url);
109
110
    $channels = $doc->getElementsByTagName('channel');
111
112
    $RSS_Content = array();
113
114
    foreach ($channels as $channel) {
115
        $items = $channel->getElementsByTagName('item');
116
        foreach ($items as $item) {
117
            $y = RSS_Tags($item, 1);    // get description of article, type 1
118
            array_push($RSS_Content, $y);
119
        }
120
    }
121
}
122
123
/**
124
 * @param     $url
125
 * @param int $size
126
 *
127
 * @return string
128
 */
129
function RSS_Links($url, $size = 15)
130
{
131
    global $RSS_Content;
1 ignored issue
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...
132
133
    $page = '<ul>';
134
135
    RSS_RetrieveLinks($url);
136
    if ($size > 0) {
137
        $recents = array_slice($RSS_Content, 0, $size + 1);
138
    }
139
140
    foreach ($recents as $article) {
0 ignored issues
show
Bug introduced by
The variable $recents 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...
141
        $type = $article['type'];
142
        if ($type == 0) {
143
            continue;
144
        }
145
        $title = $article['title'];
146
        $link  = $article['link'];
147
        $page .= "<li><a href=\"$link\">$title</a></li>\n";
148
    }
149
150
    $page .= "</ul>\n";
151
152
    return $page;
153
}
154
155
/**
156
 * @param     $url
157
 * @param int $size
158
 * @param int $site
159
 *
160
 * @return string
161
 */
162
function RSS_Display($url, $size = 15, $site = 0)
163
{
164
    global $RSS_Content;
1 ignored issue
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...
165
166
    $opened = false;
167
    $page   = '';
168
    $site   = ((int)$site == 0) ? 1 : 0;
169
170
    RSS_Retrieve($url);
171
    if ($size > 0) {
172
        $recents = array_slice($RSS_Content, $site, $size + 1 - $site);
173
    }
174
175
    foreach ($recents as $article) {
0 ignored issues
show
Bug introduced by
The variable $recents 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...
176
        $type = $article['type'];
177 View Code Duplication
        if ($type == 0) {
1 ignored issue
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...
178
            if ($opened == 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...
179
                $page .= '</ul>';
180
                $opened = false;
181
            }
182
            $page .= '<b>';
183
        } else {
184
            if ($opened == false) {
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...
185
                $page .= '<ul>';
186
                $opened = true;
187
            }
188
        }
189
        $title = $article['title'];
190
        $link  = $article['link'];
191
        $page .= "<tr class=\"even\"><td width=\"300\"><img src=\"../assets/images/admin/info_button.png\" border=0 /> <a href=\"$link\">$title</a><br>";
192
193
        $description = $article['description'];
194
        if ($description != false) {
195
            $page .= "$description<br><br></td></tr>";
196
        }
197
        $page .= '';
198
199
        if ($type == 0) {
200
            $page .= '</b>';
201
        }
202
    }
203
204
    if ($opened == 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...
205
        $page .= '</ul>';
206
    }
207
208
    return $page . '';
209
}
210
211
/**
212
 * @param     $url
213
 * @param int $size
214
 * @param int $site
215
 * @param int $withdate
216
 *
217
 * @return string
218
 */
219
function RSS_DisplayForum($url, $size = 15, $site = 0, $withdate = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $withdate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
220
{
221
    global $RSS_Content;
1 ignored issue
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...
222
223
    $opened = false;
224
    $page   = '';
225
    $site   = ((int)$site == 0) ? 1 : 0;
226
227
    RSS_Retrieve($url);
228
    if ($size > 0) {
229
        $recents = array_slice($RSS_Content, $site, $size + 1 - $site);
230
    }
231
232
    foreach ($recents as $article) {
0 ignored issues
show
Bug introduced by
The variable $recents 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...
233
        $type = $article['type'];
234 View Code Duplication
        if ($type == 0) {
1 ignored issue
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...
235
            if ($opened == 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...
236
                $page .= '</ul>';
237
                $opened = false;
238
            }
239
            $page .= '<b>';
240
        } else {
241
            if ($opened == false) {
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...
242
                $page .= '<ul>';
243
                $opened = true;
244
            }
245
        }
246
247
        $title = $article['title'];
248
        $link  = $article['link'];
249
250
        $page .= "<img src=\"../assets/images/admin/comment.png\" border=0 />&nbsp;&nbsp;&nbsp;<a href=\"$link\">$title</a><br><br>";
251
252
        if ($type == 0) {
253
            $page .= '</b>';
254
        }
255
    }
256
257
    if ($opened == 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...
258
        $page .= '</ul>';
259
    }
260
261
    return $page . '';
262
}
263