Completed
Push — master ( 6773cf...cb49b4 )
by Maciej
15s
created

InputCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_array() 0 3 2
A neat_array() 0 6 1
A populate() 0 9 3
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 13 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\Collection;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Abstraction\Collectable;
6
use WFV\Agent\InspectionAgent;
7
8
/**
9
 *
10
 *
11
 * @since 0.10.0
12
 */
13
class InputCollection extends Collectable {
14
15
	/**
16
	 *
17
	 *
18
	 * @since 0.11.0
19
	 * @access private
20
	 * @var InspectionAgent
21
	 */
22
	private $guard;
23
24
	/**
25
	 * __construct
26
	 *
27
	 * @since 0.10.0
28
	 *
29
	 * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
	 * @param bool $trim
31
	 */
32
	public function __construct( InspectionAgent $guard, $trim = true ) {
33
		$this->guard = $guard;
34
		$this->populate( $trim );
35
	}
36
37
	/**
38
	 *
39
	 *
40
	 * @since 0.11.0
41
	 *
42
	 * @param bool $neat With or without token and action attributes.
43
	 * @return array
44
	 */
45
	public function get_array( $neat = true ) {
46
		return ( $neat ) ? $this->neat_array() : $this->data;
47
	}
48
49
	/**
50
	 * Returns input array with out token and action attributes
51
	 *
52
	 * @since 0.11.0
53
	 * @access protected
54
	 *
55
	 * @return array
56
	 */
57
	protected function neat_array() {
58
		$input = $this->data;
59
		unset( $input[ $input['action'] .'_token'] );
60
		unset( $input['action'] );
61
		return $input;
62
	}
63
64
	/**
65
	 *
66
	 *
67
	 * @since 0.11.0
68
	 * @access protected
69
	 *
70
	 */
71
	protected function populate( $trim ) {
0 ignored issues
show
Coding Style introduced by
populate 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...
72
		if( $this->guard->safe_submit() ) {
73
			$data = $this->transform_array_leafs( $_POST, 'stripslashes' );
74
75
			$this->data = ( $trim )
76
				? $this->transform_array_leafs( $data, 'trim' )
77
				: $data;
78
		}
79
	}
80
}
81