VartypePHP5   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 307
rs 8.8798
c 0
b 0
f 0
wmc 44
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_tests() 0 3 1
A generate_value() 0 12 4
C compare_strings() 0 22 12
A handle_exception() 0 4 1
A filter_combined() 0 14 4
A do_filter_var() 0 18 6
B do_filter_var_array() 0 23 7
B print_typed_result() 0 23 9

How to fix   Complexity   

Complex Class

Complex classes like VartypePHP5 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use VartypePHP5, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * PHP 5+ tests.
4
 *
5
 * @package PHPCheatsheets
6
 *
7
 * Select PHPCS exclusions: this file is only included when on PHP 5+.
8
 * @phpcs:disable PHPCompatibility.Classes.NewClasses.exceptionFound
9
 *
10
 * Select PHPCS exclusions: the calls to these functions are wrapped in a function_exists() check.
11
 * @phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.filter_varFound
12
 * @phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.filter_var_arrayFound
13
 */
14
15
// Prevent direct calls to this file.
16
if ( ! defined( 'APP_DIR' ) ) {
17
	header( 'Status: 403 Forbidden' );
18
	header( 'HTTP/1.1 403 Forbidden' );
19
	exit();
20
}
21
22
/**
23
 * Overload some tests when using PHP5.
24
 *
25
 * These tests are added in the relevant child class of the Vartype class.
26
 */
27
class VartypePHP5 {
0 ignored issues
show
Coding Style introduced by
VartypePHP5 does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
28
29
	/**
30
	 * The PHP5 specific tests which will overrule the PHP4 compatible tests.
31
	 *
32
	 * @var array $tests  Multi-dimensional array.
33
	 */
34
	public static $tests = array(
35
36
		/*
37
		 * String comparison functions.
38
		 *
39
		 * @see class.vartype-compare.php
40
		 */
41
		'strcmp'        => array(
42
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "strcmp" );',
43
		),
44
		'strcasecmp'    => array(
45
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "strcasecmp" );',
46
		),
47
		'strnatcmp'     => array(
48
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "strnatcmp" );',
49
		),
50
		'strnatcasecmp' => array(
51
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "strnatcasecmp" );',
52
		),
53
		'strcoll'       => array(
54
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "strcoll" );',
55
		),
56
		'similar_text'  => array(
57
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "similar_text" );',
58
		),
59
		'levenshtein'   => array(
60
			'function'      => 'VartypePHP5::compare_strings( $a, $b, "levenshtein" );',
61
		),
62
63
64
65
		/*
66
		 * Loose type juggling.
67
		 *
68
		 * @see class.vartype-test.php
69
		 */
70
		'juggle_int'    => array(
71
			'function'      => '
72
				try {
73
					if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) {
74
						$x = $x + 0;
75
						if ( is_int( $x ) ) {
76
							pr_int( $x );
77
						}
78
						else if ( is_float( $x ) ) {
79
							pr_flt( $x );
80
						}
81
						else {
82
							pr_var( $x, \'\', true, true );
83
						}
84
					}
85
					else {
86
						trigger_error( \'Unsupported operand types\', E_USER_ERROR );
87
					}
88
				}
89
				catch ( Exception $e ) {
90
					$message = $e->getMessage();
91
					$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
92
					if ( $key === false ) {
93
						$GLOBALS[\'encountered_errors\'][] = $message;
94
						$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
95
					}
96
					echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\';
97
				}
98
			',
99
		),
100
		'juggle_flt'    => array(
101
			'function'      => '
102
				try {
103
					if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) {
104
						$r = $x + 0.0;
105
						if ( is_float( $r ) ) {
106
							pr_flt( $r );
107
						}
108
						else if ( is_int( $r ) ) {
109
							pr_int( $r );
110
						}
111
						else {
112
							pr_var( $r, \'\', true, true );
113
						}
114
					}
115
					else {
116
						trigger_error( \'Unsupported operand types\', E_USER_ERROR );
117
					}
118
				}
119
				catch ( Exception $e ) {
120
					$message = $e->getMessage();
121
					$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
122
					if ( $key === false ) {
123
						$GLOBALS[\'encountered_errors\'][] = $message;
124
						$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
125
					}
126
					echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\';
127
				}
128
			',
129
		),
130
131
132
		/*
133
		 * Some object related functions.
134
		 *
135
		 * @see class.vartype-test.php
136
		 */
137
		'instanceof'    => array(
138
			'function'      => '$c = \'TestObject\'; $r = ( $x instanceof $c ); if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); }',
139
		),
140
	);
141
142
143
	/**
144
	 * Helper method to retrieve the static variable.
145
	 * Needed to prevent parse error in PHP4.. *sigh*.
146
	 *
147
	 * @return array
148
	 */
149
	public static function get_tests() {
150
		return self::$tests;
151
	}
152
153
154
	/**
155
	 * Ensure we clone an object before using it to avoid contamination by results of previous actions.
156
	 *
157
	 * @param mixed $value
158
	 *
159
	 * @return mixed
160
	 */
161
	public static function generate_value( $value ) {
162
163
		if ( is_object( $value ) ) {
164
			$value = clone $value;
165
		}
166
167
		if ( is_array( $value ) || is_object( $value ) ) {
168
			reset( $value );
169
		}
170
171
		return $value;
172
	}
173
174
175
	/**
176
	 * Smarter way to compare strings in PHP5.
177
	 *
178
	 * @param mixed  $var1
179
	 * @param mixed  $var2
180
	 * @param string $function
181
	 */
182
	public static function compare_strings( $var1, $var2, $function ) {
183
184
		if ( ( PHP_VERSION_ID >= 50000 && $function === 'levenshtein' ) && ( ( gettype( $var1 ) === 'array' || gettype( $var1 ) === 'resource' ) || ( gettype( $var2 ) === 'array' || gettype( $var2 ) === 'resource' ) ) ) {
185
			try {
186
				pc_compare_strings( $var1, $var2, $function );
187
			}
188
			catch ( Exception $e ) {
189
				self::handle_exception( $e->getMessage() );
190
			}
191
		}
192
		else if ( PHP_VERSION_ID >= 50200 && ( gettype( $var1 ) === 'object' || gettype( $var2 ) === 'object' ) ) {
193
			try {
194
				pc_compare_strings( $var1, $var2, $function );
195
			}
196
			catch ( Exception $e ) {
197
				self::handle_exception( $e->getMessage() );
198
			}
199
		}
200
		else {
201
			pc_compare_strings( $var1, $var2, $function );
202
		}
203
	}
204
205
206
	/**
207
	 * Helper function to handle exceptions from the string compare function.
208
	 *
209
	 * @param string $message The error message.
210
	 */
211
	public static function handle_exception( $message ) {
212
		$key = get_error_key( $message );
213
		echo '<span class="error">(Catchable) Fatal error <a href="#', $GLOBALS['test'], '-errors">#', ( $key + 1 ), '</a></span>';
214
	}
215
216
217
	/**
218
	 * Run tests using the filter extension.
219
	 *
220
	 * @param mixed       $value    Value to test.
221
	 * @param string|null $expected Expected variable type of the output of the test.
222
	 * @param int         $filter   The Filter to apply.
223
	 * @param mixed|null  $flags    Which filter flags to apply.
224
	 * @param mixed|null  $options  Which options to apply.
225
	 */
226
	public static function filter_combined( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
227
228
		if ( function_exists( 'filter_var' ) && function_exists( 'filter_var_array' ) ) {
229
			if ( ! is_array( $value ) ) {
230
				self::do_filter_var( $value, $expected, $filter, $flags, $options );
231
			}
232
			else {
233
				self::do_filter_var_array( $value, $filter, $flags, $options );
234
			}
235
		}
236
		else {
237
			echo 'E: not available (PHP 5.2.0+)';
238
		}
239
	}
240
241
242
	/**
243
	 * Helper function: Run tests using the `filter_var()` function.
244
	 *
245
	 * @param mixed       $value    Value to test.
246
	 * @param string|null $expected Expected variable type of the output of the test.
247
	 * @param int         $filter   The Filter to apply.
248
	 * @param mixed|null  $flags    Which filter flags to apply.
249
	 * @param mixed|null  $options  Which options to apply.
250
	 */
251
	public static function do_filter_var( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
252
		$opt = array();
253
		if ( isset( $flags ) ) {
254
			$opt['flags'] = $flags;
255
		}
256
		if ( isset( $options ) && ( is_array( $options ) && $options !== array() ) ) {
257
			$opt['options'] = $options;
258
		}
259
260
		if ( $opt !== array() ) {
261
			$result = filter_var( $value, $filter, $opt );
262
		}
263
		else {
264
			$result = filter_var( $value, $filter );
265
		}
266
267
		self::print_typed_result( $result, $expected );
268
	}
269
270
271
	/**
272
	 * Helper function: Run tests using the `filter_var_array()` function.
273
	 *
274
	 * @param mixed      $value   Value to test.
275
	 * @param int        $filter  The Filter to apply.
276
	 * @param mixed|null $flags   Which filter flags to apply.
277
	 * @param mixed|null $options Which options to apply.
278
	 */
279
	public static function do_filter_var_array( $value, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
280
		if ( ! isset( $flags ) && ! isset( $options ) ) {
281
			pr_var( filter_var_array( $value, $filter ), '', true, true );
282
		}
283
		else {
284
			$input      = array(
285
				'x' => $value,
286
			);
287
			$filter_def = array(
288
				'x' => array(
289
					'filter' => $filter,
290
				),
291
			);
292
			if ( isset( $flags ) ) {
293
				$filter_def['x']['flags'] = ( FILTER_REQUIRE_ARRAY | $flags );
294
			}
295
			if ( isset( $options ) && ( is_array( $options ) && $options !== array() ) ) {
296
				$filter_def['x']['options'] = $options;
297
			}
298
			$output = filter_var_array( $input, $filter_def );
299
			pr_var( $output['x'], '', true, true );
300
		}
301
	}
302
303
304
	/**
305
	 * Helper function to print the filter result.
306
	 *
307
	 * @param mixed       $result
308
	 * @param string|null $expected Expected variable type of the output of the test.
309
	 */
310
	public static function print_typed_result( $result, $expected = null ) {
311
		switch ( true ) {
312
			case ( $expected === 'bool' && is_bool( $result ) === true ):
313
				pr_bool( $result );
314
				break;
315
316
			case ( $expected === 'int' && is_int( $result ) === true ):
317
				pr_int( $result );
318
				break;
319
320
			case ( $expected === 'float' && is_float( $result ) === true ):
321
				pr_flt( $result );
322
				break;
323
324
			case ( $expected === 'string' && is_string( $result ) === true ):
325
				pr_str( $result );
326
				break;
327
328
			default:
329
				pr_var( $result, '', true, true );
330
				break;
331
		}
332
	}
333
}
334