xvardump.php ➔ object_info()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 4
dl 0
loc 27
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * File: xvardump.php
4
 *
5
 * Group of functions for variable visualisation purposes.
6
 *
7
 * The main function pr_var() can be used as a color-coded alternative for var_dump().
8
 *
9
 * A couple of 'shortcut' functions are provided for known variable types.
10
 * Only use those if you know what you are receiving and don't need variable typing info.
11
 *
12
 * @package xvardump
13
 * @author  Juliette Reinders Folmer, {@link https://www.adviesenzo.nl/ Advies en zo} -
14
 *  <[email protected]>
15
 *
16
 * @version   1.7
17
 * @since     2009-09-30 // Last changed: by Juliette Reinders Folmer
18
 * @copyright Advies en zo, Meedenken en -doen �2005-2009
19
 * @license   https://www.opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
20
 * @license   http://opensource.org/licenses/academic Academic Free License Version 1.2
21
 * @example   example/example.php
22
 */
23
24
// Prevent direct calls to this file.
25
if ( ! defined( 'APP_DIR' ) ) {
26
	header( 'Status: 403 Forbidden' );
27
	header( 'HTTP/1.1 403 Forbidden' );
28
	exit();
29
}
30
31
32
define( 'XVARDUMP_SPACE_LONG', '&nbsp;&nbsp;&nbsp;&nbsp;' );
33
define( 'XVARDUMP_SPACE_SHORT', '&nbsp;&nbsp;' );
34
35
define( 'XVARDUMP_CLASS_STRING', 'vt-string' );
36
define( 'XVARDUMP_CLASS_INT', 'vt-int' );
37
define( 'XVARDUMP_CLASS_INT_0', 'vt-int-0' );
38
define( 'XVARDUMP_CLASS_FLOAT', 'vt-float' );
39
define( 'XVARDUMP_CLASS_BOOL', 'vt-bool' );
40
define( 'XVARDUMP_CLASS_B_TRUE', 'vt-b-true' );
41
define( 'XVARDUMP_CLASS_B_FALSE', 'vt-b-false' );
42
define( 'XVARDUMP_CLASS_RESOURCE', 'vt-resource' );
43
define( 'XVARDUMP_CLASS_NULL', 'vt-null' );
44
45
46
/**
47
 * Retrieve the debug info and value of a variable into a string.
48
 *
49
 * @param mixed  $var
50
 * @param string $title
51
 * @param bool   $escape
52
 * @param bool   $short
53
 * @param string $space
54
 *
55
 * @uses pr_var()
56
 * @see pr_var()
57
 *
58
 * @return string  debug info on the variable $var
59
 */
60
function get_var( $var, $title = '', $escape = true, $short = false, $space = '' ) {
61
	ob_start();
62
	pr_var( $var, $title, $escape, $short, $space );
63
	$debug_info = ob_get_clean();
64
	return $debug_info;
65
}
66
67
68
/**
69
 * Prints color-coded debug info and value of a variable to the screen.
70
 *
71
 * If you like, you can customize the color-coding used by changing the
72
 * values of the associated CONSTANTS at the top of this file.
73
 *
74
 * @param mixed  $var      The variable to print the debug info for.
75
 * @param string $title    Optional. If set, prefaces the debug info with
76
 *                         a header containing this title.
77
 *                         Useful if you want to print several states of the
78
 *                         same variable and you want to keep track of which state
79
 *                         you are at.
80
 * @param bool   $escape   Optional. Whether or not to escape html entities in
81
 *                         the $var.
82
 *                         Useful if the $var contains html and you want to see
83
 *                         the source value.
84
 *                         Defaults to true.
85
 * @param bool   $short    Optional. Whether to limit the debug info to color coding.
86
 *                         Defaults to false.
87
 * @param string $space    Optional. Internal variable needed to create the proper
88
 *                         spacing for display of arrays and objects.
89
 * @param bool   $in_array Optional. Internal pointer for whether or not to use
90
 *                         breaks when using short annotation which would give issues
91
 *                         when displaying arrays.
92
 *
93
 * @uses object_info()
94
 */
95
function pr_var( $var, $title = '', $escape = true, $short = false, $space = '', $in_array = false ) {
96
97
	if ( is_string( $title ) && $title !== '' ) {
98
		echo '<h4 style="clear: both;">', ( ( $escape === true ) ? htmlentities( $title, ENT_QUOTES ) : $title ), "</h4>\n";
99
	}
100
101
	if ( is_array( $var ) ) {
102
		if ( $var !== array() ) {
103
			echo 'Array: ', $space, "(<br />\n";
104
			$spacing = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
105
			foreach ( $var as $key => $value ) {
106
				echo $spacing, '[', ( ( $escape === true ) ? htmlentities( $key, ENT_QUOTES ) : $key );
107
				if ( $short !== true ) {
108
					echo ' ';
109
					switch ( true ) {
110
						case ( is_string( $key ) ):
111
							echo '<b class="', XVARDUMP_CLASS_STRING, '"><i>(string)</i></b>';
112
							break;
113
114
						case ( is_int( $key ) ):
115
							echo '<b class="', XVARDUMP_CLASS_INT, '"><i>(int)</i></b>';
116
							break;
117
118
						case ( is_float( $key ) ):
119
							echo '<b class="', XVARDUMP_CLASS_FLOAT, '"><i>(float)</i></b>';
120
							break;
121
122
						default:
123
							echo '(unknown)';
124
							break;
125
					}
126
				}
127
				echo '] => ';
128
				pr_var( $value, '', $escape, $short, $spacing, true );
129
			}
130
			echo $space, ")<br />\n\n";
131
		}
132
		else {
133
			echo 'array()<br />';
134
		}
135
	}
136
	else if ( is_string( $var ) ) {
137
		echo '<span class="', XVARDUMP_CLASS_STRING, '">';
138
		if ( $short !== true ) {
139
			echo '<b><i>string[', strlen( $var ), ']</i></b> : ';
140
		}
141
		echo '&lsquo;',
142
			( ( $escape === true ) ? str_replace( '  ', ' &nbsp;', htmlentities( $var, ENT_QUOTES, 'UTF-8' ) ) : str_replace( '  ', ' &nbsp;', $var ) ),
143
			"&rsquo;</span><br />\n";
144
	}
145
	else if ( is_bool( $var ) ) {
146
		if ( $short !== true ) {
147
			echo '<span class="', XVARDUMP_CLASS_BOOL, '"><b><i>bool</i></b> : ', $var, ' ( = ';
148
		}
149
		else {
150
			echo '<b class="', XVARDUMP_CLASS_BOOL, '"><i>b</i></b> ';
151
		}
152 View Code Duplication
		if ( $var === false ) {
153
			echo '<i class="', XVARDUMP_CLASS_B_FALSE, '">false</i>';
154
		}
155
		else if ( $var === true ) {
156
			echo '<i class="', XVARDUMP_CLASS_B_TRUE, '">true</i>';
157
		}
158
		else {
159
			echo '<i>undetermined</i>';
160
		}
161
		if ( $short !== true ) {
162
			echo ' )</span>';
163
		}
164
		echo "<br />\n";
165
	}
166
	else if ( is_int( $var ) ) {
167
		echo '<span class="', XVARDUMP_CLASS_INT, '">';
168
		if ( $short !== true ) {
169
			echo '<b><i>int</i></b> : ';
170
		}
171
		if ( $var === 0 ) {
172
			echo '<span class="', XVARDUMP_CLASS_INT_0, '">', $var, '</span>';
173
		}
174
		else {
175
			echo $var;
176
		}
177
		echo "</span><br />\n";
178
	}
179
	else if ( is_float( $var ) ) {
180
		echo '<span class="', XVARDUMP_CLASS_FLOAT, '">';
181
		if ( $short !== true ) {
182
			echo '<b><i>float</i></b> : ';
183
		}
184
		echo $var, "</span><br />\n";
185
	}
186
	else if ( is_null( $var ) ) {
187
		echo '<span class="', XVARDUMP_CLASS_NULL, '">';
188
		if ( $short !== true ) {
189
			echo '<b><i>';
190
		}
191
		echo 'null';
192
		if ( $short !== true ) {
193
			echo '</i></b> : ', $var, ' ( = <i>NULL</i> )', "</span><br />\n";
194
		}
195
		else if ( $in_array === true ) {
196
			echo "</span><br />\n";
197
		}
198
		else {
199
			echo "</span>\n";
200
		}
201
	}
202
	else if ( is_resource( $var ) ) {
203
		echo '<span class="', XVARDUMP_CLASS_RESOURCE, '">';
204
		if ( $short !== true ) {
205
			echo '<b><i>resource</i></b> : ';
206
		}
207
		echo $var;
208
		if ( $short !== true ) {
209
			echo ' ( = <i>RESOURCE</i> )';
210
		}
211
		echo "</span><br />\n";
212
	}
213
	else if ( is_object( $var ) ) {
214
		echo "Object: \n", $space, "(<br />\n";
215
		$spacing = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
216
		object_info( $var, $escape, $short, $spacing );
217
		echo $space, ")<br />\n\n";
218
	}
219
	else {
220
		echo 'I haven&#39;t got a clue what this is: ', gettype( $var ), "<br />\n";
221
	}
222
}
223
224
225
/**
226
 * Internal function to print debug info on an object.
227
 *
228
 * @param object $obj    Object to print debug info on.
229
 * @param bool   $escape Whether or not to escape html entities in
230
 *                       the $var. {@see pr_var()}.
231
 * @param bool   $short  Whether to limit the debug info to color coding. {@see pr_var()}.
232
 * @param string $space  Internal variable needed to create the proper spacing
233
 *                       for display of arrays and objects. {@see pr_var()}.
234
 *
235
 * @internal
236
 * @uses pr_var()
237
 */
238
function object_info( $obj, $escape, $short, $space ) {
239
	echo $space, '<b><i>Class</i></b>: ', get_class( $obj ), " (<br />\n";
240
	$spacing    = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
241
	$properties = get_object_vars( $obj );
242
	if ( is_array( $properties ) && $properties !== array() ) {
243
		foreach ( $properties as $var => $val ) {
244
			if ( is_array( $val ) ) {
245
				echo $spacing, '<b><i>property</i></b>: ', $var, "<b><i> (array)</i></b>\n";
246
				pr_var( $val, '', $escape, $short, $spacing );
247
			}
248
			else {
249
				echo $spacing, '<b><i>property</i></b>: ', $var, ' = ';
250
				pr_var( $val, '', $escape, $short, $spacing );
251
			}
252
		}
253
	}
254
	unset( $properties, $var, $val );
255
256
	$methods = get_class_methods( $obj );
257
	if ( is_array( $methods ) && $methods !== array() ) {
258
		foreach ( $methods as $method ) {
259
			echo $spacing, '<b><i>method</i></b>: ', $method, "<br />\n";
260
		}
261
	}
262
	unset( $methods, $method );
263
	echo $space, ")<br />\n\n";
264
}
265
266
267
/**
268
 * Function to dump all defined variables.
269
 *
270
 * @uses pr_var()
271
 */
272
function dump_all() {
273
	$var = get_defined_vars();
274
	pr_var( $var, 'Dump of all defined variables' );
275
}
276
277
278
/**
279
 * Alias for pr_str().
280
 *
281
 * @param string $var
282
 */
283
function pr_string( $var ) {
284
	pr_str( $var );
285
}
286
287
288
/**
289
 * Alias for pr_bool().
290
 *
291
 * @param bool $var
292
 */
293
function pr_boolean( $var ) {
294
	pr_bool( $var );
295
}
296
297
298
/**
299
 * Alias for pr_int().
300
 *
301
 * @param int $var
302
 */
303
function pr_integer( $var ) {
304
	pr_int( $var );
305
}
306
307
308
/**
309
 * Alias for pr_flt().
310
 *
311
 * @param float $var
312
 */
313
function pr_float( $var ) {
314
	pr_flt( $var );
315
}
316
317
318
/**
319
 * Shortcut function to print a string variable.
320
 *
321
 * @param string $var
322
 */
323
function pr_str( $var ) {
324
	if ( is_string( $var ) ) {
325
		echo '<span class="', XVARDUMP_CLASS_STRING, '">&lsquo;', str_replace( '  ', ' &nbsp;', $var ), "&rsquo;</span>\n";
326
	}
327
	else {
328
		echo 'E: not a string';
329
	}
330
}
331
332
333
/**
334
 * Shortcut function to print a boolean variable.
335
 *
336
 * @param bool $var
337
 */
338
function pr_bool( $var ) {
339
	if ( is_bool( $var ) ) {
340 View Code Duplication
		if ( $var === false ) {
341
			echo '<span class="', XVARDUMP_CLASS_B_FALSE, '">false', "</span>\n";
342
		}
343
		else if ( $var === true ) {
344
			echo '<span class="', XVARDUMP_CLASS_B_TRUE, '">true', "</span>\n";
345
		}
346
		else {
347
			echo 'E: boolean value undetermined';
348
		}
349
	}
350
	else {
351
		echo 'E: not boolean';
352
	}
353
}
354
355
356
/**
357
 * Shortcut function to print an integer variable.
358
 *
359
 * Will print 0 value in red, other values in blue.
360
 *
361
 * @param int $var
362
 */
363
function pr_int( $var ) {
364
	if ( is_int( $var ) ) {
365
		if ( $var === 0 ) {
366
			echo '<span class="', XVARDUMP_CLASS_INT_0, '">', $var, "</span>\n";
367
		}
368
		else {
369
			echo '<span class="', XVARDUMP_CLASS_INT, '">', $var, "</span>\n";
370
		}
371
	}
372
	else {
373
		echo 'E: not an integer';
374
	}
375
}
376
377
378
/**
379
 * Shortcut function to print a float variable.
380
 *
381
 * @param float $var
382
 */
383
function pr_flt( $var ) {
384
	if ( is_float( $var ) ) {
385
		echo '<span class="', XVARDUMP_CLASS_FLOAT, '">', $var, "</span>\n";
386
	}
387
	else {
388
		echo 'E: not a float';
389
	}
390
}
391