GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Updater   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 12 5
A getNewDomainList() 0 16 2
A getBlocklistFilename() 0 4 1
A doUpdate() 0 9 2
A isWritable() 0 4 1
A isOutdated() 0 4 1
1
<?php
2
3
namespace Nabble\SemaltBlocker;
4
5
/**
6
 * The `update` method is called from the Blocker class every week to grab latest domain list from GitHub.
7
 */
8
class Updater
9
{
10
    public static $ttl = 604800; // = 60 * 60 * 24 * 7 = 7 days
11
    public static $updateUrl = 'https://raw.githubusercontent.com/nabble/semalt-blocker/master/domains/blocked';
12
13
    private static $blocklist = './../../domains/blocked';
14
15
    //////////////////////////////////////////
16
    // PUBLIC API                           //
17
    //////////////////////////////////////////
18
19
    /**
20
     * Try to update the blocked domains list.
21
     *
22
     * @param bool $force
23
     */
24
    public static function update($force = false)
25
    {
26
        if (!defined('SEMALT_UNIT_TESTING') && !self::isWritable()) {
27
            return;
28
        }
29
30
        if (!$force && !self::isOutdated()) {
31
            return;
32
        }
33
34
        self::doUpdate();
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public static function getNewDomainList()
41
    {
42
        if (function_exists('curl_init')) {
43
            $curl = curl_init();
44
            curl_setopt_array($curl, [
45
                CURLOPT_RETURNTRANSFER => 1,
46
                CURLOPT_URL            => self::$updateUrl,
47
            ]);
48
            $domains = curl_exec($curl);
49
            curl_close($curl);
50
        } else {
51
            $domains = @file_get_contents(self::$updateUrl);
52
        }
53
54
        return $domains;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public static function getBlocklistFilename()
61
    {
62
        return __DIR__ . DIRECTORY_SEPARATOR . static::$blocklist;
0 ignored issues
show
Bug introduced by
Since $blocklist is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blocklist to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
63
    }
64
65
    //////////////////////////////////////////
66
    // PRIVATE FUNCTIONS                    //
67
    //////////////////////////////////////////
68
69
    private static function doUpdate()
70
    {
71
        $domains = self::getNewDomainList();
72
73
        // Don't panic if updating the file throws an error of some kind
74
        if (trim($domains) !== '') {
75
            @file_put_contents(self::getBlocklistFilename(), $domains);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
        }
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    private static function isWritable()
83
    {
84
        return is_writable(self::getBlocklistFilename());
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    private static function isOutdated()
91
    {
92
        return filemtime(self::getBlocklistFilename()) < (time() - self::$ttl);
93
    }
94
}
95