InspectionAgent::nonce()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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
namespace WFV\Agent;
3
defined( 'ABSPATH' ) || die();
4
5
6
/**
7
 *
8
 *
9
 * @since 0.10.0
10
 */
11
class InspectionAgent {
12
13
	/**
14
	 * Action to inspect
15
	 *
16
	 * @since 0.10.0
17
	 * @access private
18
	 * @var string
19
	 */
20
	private $action;
21
22
	/**
23
	 *
24
	 *
25
	 * @since 0.10.0
26
	 *
27
	 * @param string $action
28
	 */
29
	public function __construct( $action ) {
30
		$this->action = $action;
31
	}
32
33
	/**
34
	 * Should we take further action with this $_POST data?
35
	 * Checks if action in post matches $this->action,
36
	 *  if so, verifies the nonce.
37
	 *
38
	 * @since 0.10.0
39
	 *
40
	 * @return bool
41
	 */
42
	public function safe_submit() {
0 ignored issues
show
Coding Style introduced by
safe_submit uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
		if( $this->submit_has_action() ) {
44
			return ( $_POST['action'] === $this->action ) ? $this->nonce() : false;
45
		}
46
		return false;
47
	}
48
49
	/**
50
	 * Does $_POST have an action key?
51
	 *
52
	 * @since 0.10.0
53
	 *
54
	 * @return bool
55
	 */
56
	private function submit_has_action() {
0 ignored issues
show
Coding Style introduced by
submit_has_action uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
		return isset( $_POST['action'] );
58
	}
59
60
	/**
61
	 * Verify nonce.
62
	 *
63
	 * @since 0.10.0
64
	 * @access private
65
	 *
66
	 * @return bool
67
	 */
68
	private function nonce() {
0 ignored issues
show
Coding Style introduced by
nonce uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
69
		$nonce = $_REQUEST[ $this->action.'_token' ];
70
		return ( wp_verify_nonce( $nonce, $this->action ) ) ? true : false;
71
	}
72
73
}
74