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