Completed
Pull Request — master (#320)
by
unknown
07:52
created

phpbbtopics.class.php ➔ sort_compare_updated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
1 ignored issue
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 12 and the first side effect is on line 10.

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
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 *
7
 *  Publish new geocaches that are marked for timed publish
8
 ***************************************************************************/
9
10
checkJob(new phpbbtopics());
11
12
class phpbbtopics
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
13
{
14
    public $name = 'phpbbtopics';
15
    public $interval = 600;
16
    public $topiclist = [];
17
18
    public function run()
19
    {
20
        global $opt;
21
22
        foreach ($opt['cron']['phpbbtopics']['forumids'] as $id) {
23
            $url = $opt['cron']['phpbbtopics']['url'];
24
            $url = str_replace('{id}', $id, $url);
25
26
            if ($this->processUrl($url) == 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...
27
                return;
28
            }
29
        }
30
31
        usort($this->topiclist, 'sort_compare_updated');
32
        $this->topiclist = array_slice($this->topiclist, 0, $opt['cron']['phpbbtopics']['count']);
33
34
        $f = fopen(__DIR__ . '/../../cache2/phpbb.inc.php', 'w');
35
        fwrite($f, '<?php' . "\n");
36
        fwrite(
37
            $f,
38
            '$phpbb_topics = unserialize("' . str_replace('"', '\\"', serialize($this->topiclist)) . '");' . "\n"
39
        );
40
        fwrite($f, '?>');
41
        fclose($f);
42
    }
43
44
    public function processUrl($url)
45
    {
46
        global $opt;
47
48
        $maxsize = 100 * 1024; // max. 100kB
49
50
        $content = @read_file($url, $maxsize);
0 ignored issues
show
Unused Code introduced by
The call to read_file() has too many arguments starting with $maxsize.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
51
        if ($content === false) {
52
            return false;
53
        }
54
55
        $xml = @simplexml_load_string($content);
56
        if ($xml === false) {
57
            return false;
58
        }
59
60
        if (!isset($xml->entry)) {
61
            return false;
62
        }
63
64
        foreach ($xml->entry as $item) {
65
            $posting = [];
66
67
            $sTitle = $item->title;
68
            if (mb_strpos($sTitle, '•') !== false) {
69
                $sTitle = mb_trim(mb_substr($sTitle, mb_strpos($sTitle, '•') + 1));
70
            }
71
            $sTitle = strip_tags($sTitle);
72
            $sTitle = htmlspecialchars_decode($sTitle);
73
            $sTitle = preg_replace('/\\&.*\\;/U', '', $sTitle);
74
75
            $nTopicId = crc32($item->id); // workaround ...
76
            $tUpdated = strtotime($item->updated);
77
78
            $sUsername = (string)$item->author->name;
79
            $sUsername = htmlspecialchars_decode($sUsername);
80
            $sUsername = preg_replace('/\\&.*\\;/U', '', $sUsername);
81
82
            foreach ($item->link->Attributes() as $key => $value) {
83
                if ($key == 'href') {
84
                    $sLink = (string)$value;
85
                }
86
            }
87
            $posting['id'] = $nTopicId;
88
            $posting['title'] = $sTitle;
89
            $posting['updated'] = $tUpdated;
90
            $posting['username'] = $sUsername;
91
            $posting['link'] = $sLink;
0 ignored issues
show
Bug introduced by
The variable $sLink 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...
92
93
            if ($nTopicId != 0) {
94
                if (isset($this->topiclist[$nTopicId]) && $posting['updated'] > $this->topiclist[$nTopicId]['updated']) {
95
                    $this->topiclist[$nTopicId] = $posting;
96
                } else {
97
                    if (!isset($this->topiclist[$nTopicId])) {
98
                        $this->topiclist[$nTopicId] = $posting;
99
                    }
100
                }
101
            }
102
        }
103
104
        return true;
105
    }
106
}
107
108
function sort_compare_updated($a, $b)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
109
{
110
    return ($b['updated'] - $a['updated']);
111
}
112