PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPUnit/Util/Type.php

https://github.com/inmarelibero/Legacy-Code
PHP | 192 lines | 114 code | 23 blank | 55 comment | 26 complexity | b1f73c282fcc1add021ae0187754949b MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Util
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.0.0
  44. */
  45. /**
  46. * Utility class for textual type (and value) representation.
  47. *
  48. * @package PHPUnit
  49. * @subpackage Util
  50. * @author Sebastian Bergmann <sebastian@phpunit.de>
  51. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  53. * @version Release: @package_version@
  54. * @link http://www.phpunit.de/
  55. * @since Class available since Release 3.0.0
  56. */
  57. class PHPUnit_Util_Type
  58. {
  59. public static function isType($type)
  60. {
  61. return in_array(
  62. $type,
  63. array(
  64. 'numeric',
  65. 'integer',
  66. 'int',
  67. 'float',
  68. 'string',
  69. 'boolean',
  70. 'bool',
  71. 'null',
  72. 'array',
  73. 'object',
  74. 'resource',
  75. 'scalar'
  76. )
  77. );
  78. }
  79. public static function shortenedExport($value)
  80. {
  81. if (is_string($value)) {
  82. return self::shortenedString($value);
  83. }
  84. elseif (is_array($value)) {
  85. if (count($value) == 0) {
  86. return 'array()';
  87. }
  88. $a1 = array_slice($value, 0, 1, TRUE);
  89. $k1 = key($a1);
  90. $v1 = $a1[$k1];
  91. if (is_string($v1)) {
  92. $v1 = self::shortenedString($v1);
  93. }
  94. elseif (is_array($v1)) {
  95. $v1 = 'array(...)';
  96. } else {
  97. $v1 = self::toString($v1);
  98. }
  99. $a2 = FALSE;
  100. if (count($value) > 1) {
  101. $a2 = array_slice($value, -1, 1, TRUE);
  102. $k2 = key($a2);
  103. $v2 = $a2[$k2];
  104. if (is_string($v2)) {
  105. $v2 = self::shortenedString($v2);
  106. }
  107. elseif (is_array($v2)) {
  108. $v2 = 'array(...)';
  109. } else {
  110. $v2 = self::toString($v2);
  111. }
  112. }
  113. $text = 'array( ' . self::toString($k1) . ' => ' . $v1;
  114. if ($a2 !== FALSE) {
  115. $text .= ', ..., ' . self::toString($k2) . ' => ' . $v2 . ' )';
  116. } else {
  117. $text .= ' )';
  118. }
  119. return $text;
  120. }
  121. elseif (is_object($value)) {
  122. return get_class($value) . '(...)';
  123. }
  124. return self::toString($value);
  125. }
  126. public static function shortenedString($string)
  127. {
  128. $string = preg_replace('#\n|\r\n|\r#', ' ', $string);
  129. if (strlen($string) > 14) {
  130. return PHPUnit_Util_Type::toString(
  131. substr($string, 0, 7) . '...' . substr($string, -7)
  132. );
  133. } else {
  134. return PHPUnit_Util_Type::toString($string);
  135. }
  136. }
  137. public static function toString($value, $short = FALSE)
  138. {
  139. if (is_array($value) || is_object($value)) {
  140. if (!$short) {
  141. return "\n" . print_r($value, TRUE);
  142. } else {
  143. if (is_array($value)) {
  144. return '<array>';
  145. } else {
  146. return '<' . get_class($value) . '>';
  147. }
  148. }
  149. }
  150. if (is_string($value) && strpos($value, "\n") !== FALSE) {
  151. return '<text>';
  152. }
  153. if (!is_null($value)) {
  154. $type = gettype($value) . ':';
  155. } else {
  156. $type = '';
  157. $value = 'null';
  158. }
  159. if (is_bool($value)) {
  160. if ($value === TRUE) {
  161. $value = 'true';
  162. }
  163. else if ($value === FALSE) {
  164. $value = 'false';
  165. }
  166. }
  167. return '<' . $type . $value . '>';
  168. }
  169. }