PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/simpletest/compatibility.php

https://github.com/chregu/fluxcms
PHP | 181 lines | 99 code | 9 blank | 73 comment | 27 complexity | 34e1321d593fa87e0b549b6c91f54122 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @version $Id: compatibility.php,v 1.4 2005/09/10 22:35:31 tswicegood Exp $
  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 ($first != $second) {
  39. return false;
  40. }
  41. if (version_compare(phpversion(), '5') >= 0) {
  42. return SimpleTestCompatibility::_isIdenticalType($first, $second);
  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. return true;
  70. }
  71. /**
  72. * Recursive type test for each element of an array.
  73. * @param mixed $first Test subject.
  74. * @param mixed $second Comparison object.
  75. * @return boolean True if identical.
  76. * @access private
  77. * @static
  78. */
  79. function _isArrayOfIdenticalTypes($first, $second) {
  80. if (array_keys($first) != array_keys($second)) {
  81. return false;
  82. }
  83. foreach (array_keys($first) as $key) {
  84. $is_identical = SimpleTestCompatibility::_isIdenticalType(
  85. $first[$key],
  86. $second[$key]);
  87. if (! $is_identical) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Test for two variables being aliases.
  95. * @param mixed $first Test subject.
  96. * @param mixed $second Comparison object.
  97. * @return boolean True if same.
  98. * @access public
  99. * @static
  100. */
  101. function isReference(&$first, &$second) {
  102. if (version_compare(phpversion(), '5', '>=')
  103. && is_object($first)) {
  104. return ($first === $second);
  105. }
  106. if (is_object($first) && is_object($second)) {
  107. $id = uniqid("test");
  108. $first->$id = true;
  109. $is_ref = isset($second->$id);
  110. unset($first->$id);
  111. return $is_ref;
  112. }
  113. $temp = $first;
  114. $first = uniqid("test");
  115. $is_ref = ($first === $second);
  116. $first = $temp;
  117. return $is_ref;
  118. }
  119. /**
  120. * Test to see if an object is a member of a
  121. * class hiearchy.
  122. * @param object $object Object to test.
  123. * @param string $class Root name of hiearchy.
  124. * @return boolean True if class in hiearchy.
  125. * @access public
  126. * @static
  127. */
  128. function isA($object, $class) {
  129. if (version_compare(phpversion(), '5') >= 0) {
  130. if (! class_exists($class, false) &&
  131. (function_exists('interface_exists') && !interface_exists($class, false)) ) {
  132. return false;
  133. }
  134. eval("\$is_a = \$object instanceof $class;");
  135. return $is_a;
  136. }
  137. if (function_exists('is_a')) {
  138. return is_a($object, $class);
  139. }
  140. return ((strtolower($class) == get_class($object))
  141. or (is_subclass_of($object, $class)));
  142. }
  143. /**
  144. * Sets a socket timeout for each chunk.
  145. * @param resource $handle Socket handle.
  146. * @param integer $timeout Limit in seconds.
  147. * @access public
  148. * @static
  149. */
  150. function setTimeout($handle, $timeout) {
  151. if (function_exists('stream_set_timeout')) {
  152. stream_set_timeout($handle, $timeout, 0);
  153. } elseif (function_exists('socket_set_timeout')) {
  154. socket_set_timeout($handle, $timeout, 0);
  155. } elseif (function_exists('set_socket_timeout')) {
  156. set_socket_timeout($handle, $timeout, 0);
  157. }
  158. }
  159. /**
  160. * Gets the current stack trace topmost first.
  161. * @return array List of stack frames.
  162. * @access public
  163. * @static
  164. */
  165. function getStackTrace() {
  166. if (function_exists('debug_backtrace')) {
  167. return array_reverse(debug_backtrace());
  168. }
  169. return array();
  170. }
  171. }
  172. ?>