PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/web/lib/simpletestlib/compatibility.php

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