VartypePHP7   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_tests() 0 3 1
A do_modulus() 0 15 3
A do_intdiv() 0 14 3
A handle_exception() 0 3 1
1
<?php
2
/**
3
 * PHP 7+ tests.
4
 *
5
 * @package PHPCheatsheets
6
 *
7
 * Select PHPCS exclusions: this file is only included when on PHP 7+.
8
 * @phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.intdivFound
9
 * @phpcs:disable PHPCompatibility.Classes.NewClasses.errorFound
10
 */
11
12
// Prevent direct calls to this file.
13
if ( ! defined( 'APP_DIR' ) ) {
14
	header( 'Status: 403 Forbidden' );
15
	header( 'HTTP/1.1 403 Forbidden' );
16
	exit();
17
}
18
19
/**
20
 * Overload some tests when using PHP7.
21
 *
22
 * These tests are added in the relevant child class of the Vartype class.
23
 */
24
class VartypePHP7 {
0 ignored issues
show
Coding Style introduced by
VartypePHP7 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...
25
26
	/**
27
	 * The PHP7 specific tests.
28
	 *
29
	 * @var array $tests  Multi-dimensional array.
30
	 */
31
	public static $tests = array(
32
33
		/*
34
		 * Functions where errors have been turned into exceptions.
35
		 *
36
		 * @see class.vartype-arithmetic.php
37
		 */
38
		'modulus'        => array(
39
			'function'      => 'VartypePHP7::do_modulus( $a, $b );',
40
		),
41
		'intdiv'         => array(
42
			'function'      => 'VartypePHP7::do_intdiv( $a, $b );',
43
		),
44
	);
45
46
47
	/**
48
	 * Helper method to retrieve the static variable.
49
	 * Needed to prevent parse error in PHP4.. *sigh*.
50
	 *
51
	 * @return array
52
	 */
53
	public static function get_tests() {
54
		return self::$tests;
55
	}
56
57
58
	/**
59
	 * PHP7 compatible version of % arithmetics.
60
	 *
61
	 * @param mixed $var1
62
	 * @param mixed $var2
63
	 */
64
	public static function do_modulus( $var1, $var2 ) {
65
		try {
66
			$result = ( $var1 % $var2 );
67
			if ( is_bool( $result ) ) {
68
				pr_bool( $result );
69
			}
70
			else {
71
				pr_var( $result, '', true, true );
72
			}
73
		}
74
		catch ( Error $e ) {
75
			$message = '<span class="error">(Catchable) Fatal error</span>: ' . $e->getMessage();
76
			self::handle_exception( $message );
77
		}
78
	}
79
80
81
	/**
82
	 * Test intdiv.
83
	 *
84
	 * @param mixed $var1
85
	 * @param mixed $var2
86
	 */
87
	public static function do_intdiv( $var1, $var2 ) {
88
		if ( function_exists( 'intdiv' ) ) {
89
			try {
90
				pr_var( intdiv( $var1, $var2 ), '', true, true );
91
			}
92
			catch ( Error $e ) {
93
				$message = '<span class="error">(Catchable) Fatal error</span>: ' . $e->getMessage();
94
				self::handle_exception( $message );
95
			}
96
		}
97
		else {
98
			print 'E: not available (PHP 7.0.0+)';
99
		}
100
	}
101
102
103
	/**
104
	 * Helper function to handle exceptions from overloaded functions.
105
	 *
106
	 * @internal Exception handling is currently the same as for the PHP5 specific code, but added as separate
107
	 * method to allow for future adjustment.
108
	 *
109
	 * @param string $message The error message.
110
	 */
111
	public static function handle_exception( $message ) {
112
		VartypePHP5::handle_exception( $message );
113
	}
114
}
115