Completed
Push — sidebaracl ( 7a112d...7c3e4a )
by Andreas
04:38
created

action_plugin_popularity::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Popularity Feedback Plugin
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 */
7
8
require_once(DOKU_PLUGIN.'action.php');
9
require_once(DOKU_PLUGIN.'popularity/admin.php');
10
11
class action_plugin_popularity extends Dokuwiki_Action_Plugin {
12
13
    /**
14
     * @var helper_plugin_popularity
15
     */
16
    var $helper;
17
18
    function __construct(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
        $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_Plugin>. 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...
20
    }
21
22
    /**
23
     * Register its handlers with the dokuwiki's event controller
24
     */
25
    function register(Doku_Event_Handler $controller) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER',  $this, '_autosubmit', array());
27
    }
28
29
    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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
        //Do we have to send the data now
31
        if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){
32
            return;
33
        }
34
35
        //Actually send it
36
        $status = $this->helper->sendData( $this->helper->gatherAsString() );
37
38
        if ( $status !== '' ){
39
            //If an error occured, log it
40
            io_saveFile( $this->helper->autosubmitErrorFile, $status );
0 ignored issues
show
Bug introduced by
The property autosubmitErrorFile cannot be accessed from this context as it is declared private in class helper_plugin_popularity.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
41
        } else {
42
            //If the data has been sent successfully, previous log of errors are useless
43
            @unlink($this->helper->autosubmitErrorFile);
1 ignored issue
show
Bug introduced by
The property autosubmitErrorFile cannot be accessed from this context as it is declared private in class helper_plugin_popularity.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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...
44
            //Update the last time we sent data
45
            touch ( $this->helper->autosubmitFile );
0 ignored issues
show
Bug introduced by
The property autosubmitFile cannot be accessed from this context as it is declared private in class helper_plugin_popularity.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
46
        }
47
48
        $event->stopPropagation();
49
        $event->preventDefault();
50
    }
51
52
    /**
53
     * Check if it's time to send autosubmit data
54
     * (we should have check if autosubmit is enabled first)
55
     */
56
    function _isTooEarlyToSubmit(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
        $lastSubmit = $this->helper->lastSentTime();
58
        return $lastSubmit + 24*60*60*30 > time();
59
    }
60
}
61