Completed
Push — develop ( 406a4f...613d21 )
by Zack
17:44
created

Utils   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 140
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _GET() 0 3 1
A _POST() 0 3 1
A _REQUEST() 0 3 1
A _SERVER() 0 3 1
B get() 0 27 9
A _return() 0 3 1
A strip_excel_formulas() 0 8 2
A gf_query_debug() 0 6 1
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 12 and the first side effect is on line 6.

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 GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * Generic utilities.
11
 */
12
class Utils {
13
	/**
14
	 * Grab a value from the _GET superglobal or default.
15
	 *
16
	 * @param string $name The key name (will be prefixed).
17
	 * @param mixed $default The default value if not found (Default: null)
18
	 *
19
	 * @return mixed The value or $default if not found.
20
	 */
21 38
	public static function _GET( $name, $default = null ) {
0 ignored issues
show
Coding Style introduced by
The function name _GET is in camel caps, but expected _g_e_t instead as per the coding standard.
Loading history...
22 38
		return self::get( $_GET, $name, $default );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
23
	}
24
25
	/**
26
	 * Grab a value from the _POST superglobal or default.
27
	 *
28
	 * @param string $name The key name (will be prefixed).
29
	 * @param mixed $default The default value if not found (Default: null)
30
	 *
31
	 * @return mixed The value or $default if not found.
32
	 */
33 8
	public static function _POST( $name, $default = null ) {
0 ignored issues
show
Coding Style introduced by
The function name _POST is in camel caps, but expected _p_o_s_t instead as per the coding standard.
Loading history...
34 8
		return self::get( $_POST, $name, $default );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
35
	}
36
37
	/**
38
	 * Grab a value from the _REQUEST superglobal or default.
39
	 *
40
	 * @param string $name The key name (will be prefixed).
41
	 * @param mixed $default The default value if not found (Default: null)
42
	 *
43
	 * @return mixed The value or $default if not found.
44
	 */
45 4
	public static function _REQUEST( $name, $default = null ) {
0 ignored issues
show
Coding Style introduced by
The function name _REQUEST is in camel caps, but expected _r_e_q_u_e_s_t instead as per the coding standard.
Loading history...
46 4
		return self::get( $_REQUEST, $name, $default );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
47
	}
48
49
	/**
50
	 * Grab a value from the _SERVER superglobal or default.
51
	 *
52
	 * @param string $name The key name (will be prefixed).
53
	 * @param mixed $default The default value if not found (Default: null)
54
	 *
55
	 * @return mixed The value or $default if not found.
56
	 */
57 18
	public static function _SERVER( $name, $default = null ) {
0 ignored issues
show
Coding Style introduced by
The function name _SERVER is in camel caps, but expected _s_e_r_v_e_r instead as per the coding standard.
Loading history...
58 18
		return self::get( $_SERVER, $name, $default );
59
	}
60
61
	/**
62
	 * Grab a value from an array or an object or default.
63
	 *
64
	 * Supports nested arrays, objects via / key delimiters.
65
	 *
66
	 * @param array|object $array The array (or object)
67
	 * @param string $key The key.
68
	 * @param mixed $default The default value. Default: null
69
	 *
70
	 * @return mixed  The value or $default if not found.
71
	 */
72 120
	public static function get( $array, $key, $default = null ) {
73 120
		if ( ! is_array( $array ) && ! is_object( $array ) ) {
74 26
			return $default;
75
		}
76
77
		/**
78
		 * Try direct key.
79
		 */
80 120
		if ( is_array( $array ) || $array instanceof \ArrayAccess ) {
81 120
			if ( isset( $array[ $key ] ) ) {
82 120
				return $array[ $key ];
83
			}
84 69
		} else if ( is_object( $array ) ) {
85 69
			if ( property_exists( $array, $key ) ) {
86 69
				return $array->$key;
87
			}
88
		}
89
90
		/**
91
		 * Try subkeys after split.
92
		 */
93 109
		if ( count( $parts = explode( '/', $key, 2 ) ) > 1 ) {
94 42
			return self::get( self::get( $array, $parts[0] ), $parts[1], $default );
95
		}
96
97 109
		return $default;
98
	}
99
100
	/**
101
	 * Sanitizes Excel formulas inside CSV output
102
	 *
103
	 * @internal
104
	 * @since 2.1
105
	 *
106
	 * @param string $value The cell value to strip formulas from.
107
	 *
108
	 * @return string The sanitized value.
109
	 */
110 4
	public static function strip_excel_formulas( $value ) {
111
112 4
		if ( strpos( $value, '=' ) === 0 ) {
0 ignored issues
show
introduced by
Found "=== 0". Use Yoda Condition checks, you must
Loading history...
113 3
			$value = "'" . $value;
114
		}
115
116 4
		return $value;
117
	}
118
119
	/**
120
	 * Return a value by call.
121
	 *
122
	 * Use for quick hook callback returns and whatnot.
123
	 *
124
	 * @internal
125
	 * @since 2.1
126
	 *
127
	 * @param mixed $value The value to return from the closure.
128
	 *
129
	 * @return Closure The closure with the $value bound.
130
	 */
131
	public static function _return( $value ) {
132
		return function() use ( $value ) { return $value; };
133
	}
134
135
	/**
136
	 * Output an associative array represenation of the query parameters.
137
	 *
138
	 * @internal
139
	 * @since 2.1
140
	 *
141
	 * @param \GF_Query The query object to dump.
142
	 *
143
	 * @return array An associative array of parameters.
144
	 */
145 29
	public static function gf_query_debug( $query ) {
146 29
		$introspect = $query->_introspect();
147
		return array(
148 29
			'where' => $query->_where_unwrap( $introspect['where'] )
149
		);
150
	}
151
}
152