Completed
Pull Request — master (#1446)
by Zack
22:43 queued 19:57
created

Utils::gf_query_debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
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 63
	public static function _GET( $name, $default = null ) {
22 63
		return self::get( $_GET, $name, $default );
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 24
	public static function _POST( $name, $default = null ) {
34 24
		return self::get( $_POST, $name, $default );
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 6
	public static function _REQUEST( $name, $default = null ) {
46 6
		return self::get( $_REQUEST, $name, $default );
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 43
	public static function _SERVER( $name, $default = null ) {
58 43
		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 205
	public static function get( $array, $key, $default = null ) {
73 205
		if ( ! is_array( $array ) && ! is_object( $array ) ) {
74 60
			return $default;
75
		}
76
77
		/**
78
		 * Try direct key.
79
		 */
80 205
		if ( is_array( $array ) || $array instanceof \ArrayAccess ) {
81 205
			if ( isset( $array[ $key ] ) ) {
82 205
				return $array[ $key ];
83
			}
84 103
		} else if ( is_object( $array ) ) {
85 103
			if ( property_exists( $array, $key ) ) {
86 103
				return $array->$key;
87
			}
88
		}
89
90
		/**
91
		 * Try subkeys after split.
92
		 */
93 195
		if ( count( $parts = explode( '/', $key, 2 ) ) > 1 ) {
94 84
			return self::get( self::get( $array, $parts[0] ), $parts[1], $default );
95
		}
96
97 195
		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 12
	public static function strip_excel_formulas( $value ) {
111
112 12
		if ( strpos( $value, '=' ) === 0 ) {
113 3
			$value = "'" . $value;
114
		}
115
116 12
		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 72
	public static function gf_query_debug( $query ) {
146 72
		$introspect = $query->_introspect();
147
		return array(
148 72
			'where' => $query->_where_unwrap( $introspect['where'] )
149
		);
150
	}
151
152
	/**
153
	 * Strips aliases in columns
154
	 *
155
	 * @see https://github.com/gravityview/GravityView/issues/1308#issuecomment-617075190
156
	 *
157
	 * @internal
158
	 *
159
	 * @since 2.8.1
160
	 *
161
	 * @param \GF_Query_Condition $condition The condition to strip column aliases from.
162
	 *
163
	 * @return \GF_Query_Condition
164
	 */
165
	public static function gf_query_strip_condition_column_aliases( $condition ) {
166
		if ( $condition->expressions ) {
167
			$conditions = array();
168
			foreach ( $condition->expressions as $expression ) {
169
				$conditions[] = self::gf_query_strip_condition_column_aliases( $expression );
170
			}
171
			return call_user_func_array(
172
				array( '\GF_Query_Condition', $condition->operator == 'AND' ? '_and' : '_or' ),
173
				$conditions
174
			);
175
		} else {
176
			if ( $condition->left instanceof \GF_Query_Column ) {
0 ignored issues
show
Bug introduced by
The class GF_Query_Column does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
177
				return new \GF_Query_Condition(
178
					new \GF_Query_Column( $condition->left->field_id ),
179
					$condition->operator,
180
					$condition->right
181
				);
182
			}
183
		}
184
185
		return $condition;
186
	}
187
}
188