Failed Conditions
Push — psr2 ( 1cdd00...29fc53 )
by Andreas
06:39 queued 03:24
created

action_plugin_popularity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 4 1
B autosubmit() 0 23 4
A isTooEarlyToSubmit() 0 5 1
1
<?php
2
/**
3
 * Popularity Feedback Plugin
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 */
7
8
class action_plugin_popularity extends Dokuwiki_Action_Plugin
9
{
10
11
    /**
12
     * @var helper_plugin_popularity
13
     */
14
    protected $helper;
15
16
    public function __construct()
17
    {
18
        $this->helper = $this->loadHelper('popularity', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loadHelper('popularity', false) can also be of type object<DokuWiki_PluginInterface>. However, the property $helper is declared as type object<helper_plugin_popularity>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
19
    }
20
21
    /** @inheritdoc */
22
    public function register(Doku_Event_Handler $controller)
23
    {
24
        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', array());
25
    }
26
27
    /**
28
     * Event handler
29
     *
30
     * @param Doku_Event $event
31
     * @param $param
32
     */
33
    public function autosubmit(Doku_Event &$event, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param 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...
34
    {
35
        //Do we have to send the data now
36
        if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) {
37
            return;
38
        }
39
40
        //Actually send it
41
        $status = $this->helper->sendData($this->helper->gatherAsString());
42
43
        if ($status !== '') {
44
            //If an error occured, log it
45
            io_saveFile($this->helper->autosubmitErrorFile, $status);
46
        } else {
47
            //If the data has been sent successfully, previous log of errors are useless
48
            @unlink($this->helper->autosubmitErrorFile);
1 ignored issue
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...
49
            //Update the last time we sent data
50
            touch($this->helper->autosubmitFile);
51
        }
52
53
        $event->stopPropagation();
54
        $event->preventDefault();
55
    }
56
57
    /**
58
     * Check if it's time to send autosubmit data
59
     * (we should have check if autosubmit is enabled first)
60
     */
61
    protected function isTooEarlyToSubmit()
62
    {
63
        $lastSubmit = $this->helper->lastSentTime();
64
        return $lastSubmit + 24*60*60*30 > time();
65
    }
66
}
67