Spike PHPCoverage Details: compatibility.php

Line #FrequencySource Line
1 <?php
2     /**
3      *  base include file for SimpleTest
4      *  @package    SimpleTest
5      *  @version    $Id: compatibility.php 1672 2008-03-02 04:47:34Z edwardzyang $
6      */
7     
8     /**
9      *  Static methods for compatibility between different
10      *  PHP versions.
11      *  @package    SimpleTest
12      */
13     class SimpleTestCompatibility {
14         
15         /**
16          *    Creates a copy whether in PHP5 or PHP4.
17          *    @param object $object     Thing to copy.
18          *    @return object            A copy.
19          *    @access public
20          *    @static
21          */
22         function copy($object) {
23             if (version_compare(phpversion(), '5') >= 0) {
24                 eval('$copy = clone $object;');
25                 return $copy;
26             }
27             return $object;
28         }
29         
30         /**
31          *    Identity test. Drops back to equality + types for PHP5
32          *    objects as the === operator counts as the
33          *    stronger reference constraint.
34          *    @param mixed $first    Test subject.
35          *    @param mixed $second   Comparison object.
36          *    @return boolean        True if identical.
37          *    @access public
38          *    @static
39          */
40         function isIdentical($first, $second) {
41             if ($first != $second) {
42                 return false;
43             }
44             if (version_compare(phpversion(), '5') >= 0) {
45                 return SimpleTestCompatibility::_isIdenticalType($first, $second);
46             }
47             return ($first === $second);
48         }
49         
50         /**
51          *    Recursive type test.
52          *    @param mixed $first    Test subject.
53          *    @param mixed $second   Comparison object.
54          *    @return boolean        True if same type.
55          *    @access private
56          *    @static
57          */
58         function _isIdenticalType($first, $second) {
59             if (gettype($first) != gettype($second)) {
60                 return false;
61             }
62             if (is_object($first) && is_object($second)) {
63                 if (get_class($first) != get_class($second)) {
64                     return false;
65                 }
66                 return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
67                         get_object_vars($first),
68                         get_object_vars($second));
69             }
70             if (is_array($first) && is_array($second)) {
71                 return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
72             }
73             if ($first !== $second) {
74                 return false;
75             }
76             return true;
77         }
78         
79         /**
80          *    Recursive type test for each element of an array.
81          *    @param mixed $first    Test subject.
82          *    @param mixed $second   Comparison object.
83          *    @return boolean        True if identical.
84          *    @access private
85          *    @static
86          */
87         function _isArrayOfIdenticalTypes($first, $second) {
88             if (array_keys($first) != array_keys($second)) {
89                 return false;
90             }
91             foreach (array_keys($first) as $key) {
92                 $is_identical = SimpleTestCompatibility::_isIdenticalType(
93                         $first[$key],
94                         $second[$key]);
95                 if (! $is_identical) {
96                     return false;
97                 }
98             }
99             return true;
100         }
101         
102         /**
103          *    Test for two variables being aliases.
104          *    @param mixed $first    Test subject.
105          *    @param mixed $second   Comparison object.
106          *    @return boolean        True if same.
107          *    @access public
108          *    @static
109          */
110         function isReference(&$first, &$second) {
111             if (version_compare(phpversion(), '5', '>=') && is_object($first)) {
112                 return ($first === $second);
113             }
114             if (is_object($first) && is_object($second)) {
115                 $id = uniqid("test");
116                 $first->$id = true;
117                 $is_ref = isset($second->$id);
118                 unset($first->$id);
119                 return $is_ref;
120             }
121             $temp = $first;
122             $first = uniqid("test");
123             $is_ref = ($first === $second);
124             $first = $temp;
125             return $is_ref;
126         }
127         
128         /**
129          *    Test to see if an object is a member of a
130          *    class hiearchy.
131          *    @param object $object    Object to test.
132          *    @param string $class     Root name of hiearchy.
133          *    @return boolean         True if class in hiearchy.
134          *    @access public
135          *    @static
136          */
137         function isA($object, $class) {
1381            if (version_compare(phpversion(), '5') >= 0) {
1391                if (! class_exists($class, false)) {
1401                    if (function_exists('interface_exists')) {
1411                        if (! interface_exists($class, false))  {
1421                            return false;
143                         }
144                     }
145                 }
146                 eval("\$is_a = \$object instanceof $class;");
147                 return $is_a;
148             }
149             if (function_exists('is_a')) {
150                 return is_a($object, $class);
151             }
152             return ((strtolower($class) == get_class($object))
153                     or (is_subclass_of($object, $class)));
154         }
155         
156         /**
157          *    Sets a socket timeout for each chunk.
158          *    @param resource $handle    Socket handle.
159          *    @param integer $timeout    Limit in seconds.
160          *    @access public
161          *    @static
162          */
163         function setTimeout($handle, $timeout) {
164             if (function_exists('stream_set_timeout')) {
165                 stream_set_timeout($handle, $timeout, 0);
166             } elseif (function_exists('socket_set_timeout')) {
167                 socket_set_timeout($handle, $timeout, 0);
168             } elseif (function_exists('set_socket_timeout')) {
169                 set_socket_timeout($handle, $timeout, 0);
170             }
171         }
172     }
173 ?