Completed
Push — next ( 5f2bc0...cef70f )
by Thomas
25s queued 12s
created

getNew   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 361
Duplicated Lines 57.89 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 209
loc 361
rs 8.48
c 0
b 0
f 0
wmc 49
lcom 2
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get_userCountry() 0 4 1
A set_userCountry() 0 4 1
A __construct() 0 4 1
A rsForSmarty() 0 14 4
A feedForSmarty() 0 14 4
A cacheRs() 47 47 3
A eventRs() 45 45 3
A ratingDays() 0 14 2
A ratingRs() 51 51 3
B blogFeed() 22 22 9
B forumFeed() 21 21 9
B wikiFeed() 23 23 9

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like getNew often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use getNew, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/***************************************************************************
4
 * for license information see LICENSE.md
5
 *  summarize methods to get new events, caches, ratings, etc.
6
 ***************************************************************************/
7
class getNew
8
{
9
    private $userCountry;
10
11
    /**
12
     * @return mixed
13
     */
14
    public function get_userCountry()
15
    {
16
        return $this->userCountry;
17
    }
18
19
    /**
20
     * @param $userCountry
21
     */
22
    public function set_userCountry($userCountry): void
23
    {
24
        $this->userCountry = $userCountry;
25
    }
26
27
    /**
28
     * constructor
29
     * creates the object
30
     *
31
     * @param string $userCountry country of the loggedin user as parameter for the sql statements
32
     */
33
    public function __construct($userCountry)
34
    {
35
        $this->set_userCountry($userCountry);
36
    }
37
38
    /**
39
     * rsForSmarty creates the result from database to use with smarty assign-rs method
40
     * based on $this->type
41
     *
42
     * @param string $type type of the "new"-information, i.e. cache, event, rating, etc
43
     * @param array $args numeric array containing the parameter for "sql_slave"
44
     * @return object mysql result used by smarty assign_rs
45
     */
46
    public function rsForSmarty($type, $args = null)
47
    {
48
        switch ($type) {
49
            case 'cache':
50
                return $this->cacheRs($args);
51
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
52
            case 'event':
53
                return $this->eventRs($args);
54
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
55
            case 'rating':
56
                return $this->ratingRs($args);
57
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
58
        }
59
    }
60
61
    /**
62
     * feedForSmarty creates a HTML string to use with smarty assign method
63
     * based on $this->type by using RSSParser class
64
     *
65
     * @param string $type type of the "new"-information, i.e. cache, event, rating, etc
66
     * @param int $items number of feeditems to parse from feed (RSSParser)
67
     * @param string $url url of the feed to parse (RSSParser)
68
     * @param int $timeout maximum seconds to wait for the requested page
69
     * @param null|mixed $includeText
70
     * @return string HTML string used for smarty assign method
71
     * @internal param bool $includetext ???following??? add table-tag?
72
     */
73
    public function feedForSmarty($type, $items = null, $url = null, $timeout = null, $includeText = null)
74
    {
75
        switch ($type) {
76
            case 'forum':
77
                return $this->forumFeed($items, $url, $timeout, $includeText);
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
79
            case 'blog':
80
                return $this->blogFeed($items, $url, $timeout, $includeText);
81
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
82
            case 'wiki':
83
                return $this->wikiFeed($items, $url, $timeout, $includeText);
84
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

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

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

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

Loading history...
85
        }
86
    }
87
88
    /**
89
     * cacheRs executes the database statements for type "cache"
90
     *
91
     * @param array $args numeric array containing the parameter for "sql_slave"
92
     * @return object mysql result used by smarty assign_rs
93
     */
94 View Code Duplication
    private function cacheRs($args = null)
0 ignored issues
show
Duplication introduced by
This method 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...
95
    {
96
        global $opt;
97
98
        if ($args === null || !is_array($args)) {
99
            $args = [
100
                $this->get_userCountry(),
101
                $opt['template']['locale'],
102
                10,
103
            ];
104
        }
105
106
        return sql_slave(
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
107
            "SELECT `user`.`user_id` `user_id`,
108
                `user`.`username` `username`,
109
                `caches`.`cache_id` `cache_id`,
110
                `caches`.`name` `name`,
111
                `caches`.`date_created` `date_created`,
112
                `caches`.`type`,
113
                `caches`.`longitude` `longitude`,
114
                `caches`.`latitude` `latitude`,
115
                IFNULL(`sys_trans_text`.`text`,`countries`.`en`) AS `adm1`,
116
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm2`,'') `adm2`,
117
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm3`,'') `adm3`,
118
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm4`,'') `adm4`,
119
                `ca`.`attrib_id` IS NOT NULL AS `oconly`
120
            FROM `caches`
121
            INNER JOIN `user`
122
              ON `user`.`user_id`=`caches`.`user_id`
123
            LEFT JOIN `cache_location`
124
              ON `caches`.`cache_id`=`cache_location`.`cache_id`
125
            LEFT JOIN `countries`
126
              ON `countries`.`short`=`caches`.`country`
127
            LEFT JOIN `sys_trans_text`
128
              ON `sys_trans_text`.`trans_id`=`countries`.`trans_id`
129
              AND `sys_trans_text`.`lang`='&2'
130
            LEFT JOIN `caches_attributes` `ca`
131
              ON `ca`.`cache_id`=`caches`.`cache_id`
132
              AND `ca`.`attrib_id`=6
133
            WHERE `caches`.`country`='&1'
134
              AND `caches`.`type` != 6
135
              AND `caches`.`status` = 1
136
            ORDER BY `caches`.`date_created` DESC
137
            LIMIT 0, &3",
138
            $args
139
        );
140
    }
141
142
    /**
143
     * eventRs executes the database statements for type "event"
144
     *
145
     * @param array $args numeric array containing the parameter for "sql_slave"
146
     * @return object mysql result used by smarty assign_rs
147
     */
148 View Code Duplication
    private function eventRs($args = null)
0 ignored issues
show
Duplication introduced by
This method 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...
149
    {
150
        global $opt;
151
152
        if ($args === null || !is_array($args)) {
153
            $args = [
154
                $this->get_userCountry(),
155
                $opt['template']['locale'],
156
                10,
157
            ];
158
        }
159
160
        return sql_slave(
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
161
            "SELECT `user`.`user_id` `user_id`,
162
                `user`.`username` `username`,
163
                `caches`.`cache_id` `cache_id`,
164
                `caches`.`name` `name`,
165
                `caches`.`date_hidden`,
166
                IFNULL(`sys_trans_text`.`text`,`countries`.`en`) AS `adm1`,
167
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm2`,'') `adm2`,
168
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm3`,'') `adm3`,
169
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm4`,'') `adm4`,
170
                `ca`.`attrib_id` IS NOT NULL AS `oconly`
171
            FROM `caches`
172
            INNER JOIN `user`
173
              ON `user`.`user_id`=`caches`.`user_id`
174
            LEFT JOIN `cache_location`
175
              ON `caches`.`cache_id`=`cache_location`.`cache_id`
176
            LEFT JOIN `countries`
177
              ON `countries`.`short`=`caches`.`country`
178
            LEFT JOIN `sys_trans_text`
179
              ON `sys_trans_text`.`trans_id`=`countries`.`trans_id`
180
              AND `sys_trans_text`.`lang`='&2'
181
            LEFT JOIN `caches_attributes` `ca`
182
              ON `ca`.`cache_id`=`caches`.`cache_id`
183
              AND `ca`.`attrib_id`=6
184
            WHERE `caches`.`country`='&1'
185
              AND `caches`.`date_hidden` >= curdate()
186
              AND `caches`.`type` = 6
187
              AND `caches`.`status`=1
188
            ORDER BY `date_hidden` ASC
189
            LIMIT 0, &3",
190
            $args
191
        );
192
    }
193
194
    /**
195
     * ratingDays returns the number of days used for top rating calculation
196
     *
197
     * @return int
198
     */
199
    public function ratingDays()
200
    {
201
        global $opt;
202
203
        // Calculate days depending on country selection.
204
        // Todo: make default country configurable and use this also for
205
        // "except of [Germany]" new caches and logs lists
206
207
        if ($this->get_userCountry() === 'DE') {
208
            return $opt['logic']['rating']['topdays_mainCountry'];
209
        }
210
211
        return $opt['logic']['rating']['topdays_otherCountry'];
212
    }
213
214
    /**
215
     * ratingRs executes the database statements for type "rating"
216
     *
217
     * @param array $args numeric array containing the parameter for "sql_slave"
218
     * @return object mysql result used by smarty assign_rs
219
     */
220 View Code Duplication
    private function ratingRs($args = null)
0 ignored issues
show
Duplication introduced by
This method 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...
221
    {
222
        global $opt;
223
224
        // check $args and set defaults
225
        if ($args === null || !is_array($args)) {
226
            $args = [
227
                $this->get_userCountry(),
228
                $opt['template']['locale'],
229
                10,
230
                $this->ratingDays(),
231
            ];
232
        }
233
234
        // execute sql
235
        // 2012-08-24 following
236
        //   optimized by adding rating_date field to cache_rating, so we don't need the log table.
237
        return sql_slave(
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
238
            "SELECT COUNT(`cache_rating`.`user_id`) AS `cRatings`,
239
                MAX(`cache_rating`.`rating_date`) AS `dLastLog`,
240
                `user`.`user_id` AS `user_id`,
241
                `user`.`username` AS `username`,
242
                `caches`.`cache_id` AS `cache_id`,
243
                `caches`.`name` AS `name`,
244
                `caches`.`type`,
245
                IFNULL(`sys_trans_text`.`text`,`countries`.`en`) AS `adm1`,
246
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm2`,'') `adm2`,
247
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm3`,'') `adm3`,
248
                IF(`caches`.`country`=`cache_location`.`code1`,`cache_location`.`adm4`,'') `adm4`,
249
                `ca`.`attrib_id` IS NOT NULL AS `oconly`
250
            FROM `cache_rating`
251
                INNER JOIN `caches` ON `caches`.`cache_id`=`cache_rating`.`cache_id`
252
                INNER JOIN `user` ON `user`.`user_id`=`caches`.`user_id`
253
                LEFT JOIN `cache_location` ON `cache_rating`.`cache_id`=`cache_location`.`cache_id`
254
                LEFT JOIN `countries` ON `countries`.`short`=`caches`.`country`
255
                LEFT JOIN `sys_trans_text`
256
                  ON `sys_trans_text`.`trans_id`=`countries`.`trans_id`
257
                  AND `sys_trans_text`.`lang`='&2'
258
                LEFT JOIN `caches_attributes` `ca` ON `ca`.`cache_id`=`caches`.`cache_id` AND `ca`.`attrib_id`=6
259
            WHERE `caches`.`country`='&1' AND
260
                `cache_rating`.`rating_date`>DATE_SUB(NOW(), INTERVAL &4 DAY) AND
261
                `caches`.`type`!=6 AND
262
                `caches`.`status`=1
263
            GROUP BY `cache_rating`.`cache_id`
264
            ORDER BY `cRatings` DESC,
265
                `dLastLog` DESC,
266
                `cache_id` DESC
267
            LIMIT 0, &3",
268
            $args
269
        );
270
    }
271
272
    /**
273
     * blogFeed executes the RSSParser for type "blog"
274
     *
275
     * @param int $items number of feeditems to parse from feed (RSSParser)
276
     * @param string $url url of the feed to parse (RSSParser)
277
     * @param int $timeout maximum seconds to wait for the requested page
278
     * @param bool $includeText ???following??? add table-tag?
279
     * @return string HTML string used for smarty assign method
280
     */
281 View Code Duplication
    private function blogFeed($items = null, $url = null, $timeout = null, $includeText = null)
0 ignored issues
show
Duplication introduced by
This method 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...
282
    {
283
        global $opt;
284
285
        if (!is_numeric($items) || $items === null) {
286
            $items = $opt['news']['count'];
287
        }
288
289
        if ($url === null || !is_string($url)) {
290
            $url = $opt['news']['include'];
291
        }
292
293
        if ($timeout === null || !is_numeric($timeout)) {
294
            $timeout = $opt['news']['timeout'];
295
        }
296
297
        if ($includeText === null || !is_bool($includeText)) {
298
            $includeText = false;
299
        }
300
301
        return RSSParser::parse($items, $url, $timeout, $includeText);
302
    }
303
304
    /**
305
     * forumFeed executes the RSSParser for type "forum"
306
     *
307
     * @param int $items number of feeditems to parse from feed (RSSParser)
308
     * @param string $url url of the feed to parse (RSSParser)
309
     * @param int $timeout maximum seconds to wait for the requested page
310
     * @param bool $includeText ???following??? add table-tag?
311
     * @return string HTML string used for smarty assign method
312
     */
313 View Code Duplication
    private function forumFeed($items = null, $url = null, $timeout = null, $includeText = null)
0 ignored issues
show
Duplication introduced by
This method 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...
314
    {
315
        global $opt;
316
317
        if (!is_numeric($items) || $items === null) {
318
            $items = $opt['forum']['count'];
319
        }
320
321
        if ($url === null || !is_string($url)) {
322
            $url = $opt['forum']['url'];
323
        }
324
        if ($timeout === null || !is_numeric($timeout)) {
325
            $timeout = $opt['forum']['timeout'];
326
        }
327
328
        if ($includeText === null || !is_bool($includeText)) {
329
            $includeText = false;
330
        }
331
332
        return RSSParser::parse($items, $url, $timeout, $includeText);
333
    }
334
335
    /**
336
     * wikiFeed executes the RSSParser for type "wiki"
337
     *
338
     * @param int $items number of feeditems to parse from feed (RSSParser)
339
     * @param string $url url of the feed to parse (RSSParser)
340
     * @param bool $includeText ???following??? add table-tag?
341
     * @param int $timeout maximum seconds to wait for the requested page
342
     * @return string HTML string used for smarty assign method
343
     */
344 View Code Duplication
    private function wikiFeed($items = null, $url = null, $timeout = null, $includeText = null)
0 ignored issues
show
Duplication introduced by
This method 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...
345
    {
346
        global $opt;
347
348
        if (!is_numeric($items) || $items === null) {
349
            $items = $opt['wikinews']['count'];
350
        }
351
352
        if (!is_string($url) || $url === null) {
353
            $url = $opt['wikinews']['url'];
354
        }
355
356
        if ($timeout === null || !is_numeric($timeout)) {
357
            $timeout = $opt['wikinews']['timeout'];
358
        }
359
360
        if ($includeText === null || !is_bool($includeText)) {
361
            $includeText = false;
362
        }
363
364
        // execute RSSParser
365
        return RSSParser::parse($items, $url, $timeout, $includeText);
366
    }
367
}
368