forked from simpletest/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompatibility.php
More file actions
87 lines (82 loc) · 2.32 KB
/
compatibility.php
File metadata and controls
87 lines (82 loc) · 2.32 KB
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
<?php
/**
* Static methods for compatibility between different PHP versions.
*/
class SimpleTestCompatibility
{
/**
* Recursive type test.
*
* @param mixed $first Test subject.
* @param mixed $second Comparison object.
*
* @return bool True if same type.
*/
public static function isIdentical($first, $second)
{
if (gettype($first) != gettype($second)) {
return false;
}
if (is_object($first) && is_object($second)) {
if (get_class($first) != get_class($second)) {
return false;
}
return self::isArrayOfIdenticalTypes(
(array) $first,
(array) $second);
}
if (is_array($first) && is_array($second)) {
return self::isArrayOfIdenticalTypes($first, $second);
}
if ($first !== $second) {
return false;
}
return true;
}
/**
* Recursive type test for each element of an array.
*
* @param mixed $first Test subject.
* @param mixed $second Comparison object.
*
* @return bool True if identical.
*/
protected static function isArrayOfIdenticalTypes($first, $second)
{
if (array_keys($first) != array_keys($second)) {
return false;
}
foreach (array_keys($first) as $key) {
$is_identical = self::isIdentical(
$first[$key],
$second[$key]);
if (! $is_identical) {
return false;
}
}
return true;
}
/**
* Test for two variables being aliases.
*
* @param mixed $first Test subject.
* @param mixed $second Comparison object.
*
* @return bool True if same.
*/
public static function isReference(&$first, &$second)
{
if($first !== $second){
return false;
}
$temp_first = $first;
// modify $first
$first = ($first === true) ? false : true;
// after modifying $first, $second will not be equal to $first,
// unless $second and $first points to the same variable.
$is_ref = ($first === $second);
// unmodify $first
$first = $temp_first;
return $is_ref;
}
}