PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPUnit/Util/Type.php

https://github.com/p/phpunit-bundle
PHP | 303 lines | 138 code | 38 blank | 127 comment | 20 complexity | 99fec231817d0b0e1e57fb963be91826 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2013, 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 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause 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 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  53. * @link http://www.phpunit.de/
  54. * @since Class available since Release 3.0.0
  55. */
  56. class PHPUnit_Util_Type
  57. {
  58. public static function isType($type)
  59. {
  60. return in_array(
  61. $type,
  62. array(
  63. 'numeric',
  64. 'integer',
  65. 'int',
  66. 'float',
  67. 'string',
  68. 'boolean',
  69. 'bool',
  70. 'null',
  71. 'array',
  72. 'object',
  73. 'resource',
  74. 'scalar'
  75. )
  76. );
  77. }
  78. /**
  79. * Exports a value into a string
  80. *
  81. * The output of this method is similar to the output of print_r(), but
  82. * improved in various aspects:
  83. *
  84. * - NULL is rendered as "null" (instead of "")
  85. * - TRUE is rendered as "true" (instead of "1")
  86. * - FALSE is rendered as "false" (instead of "")
  87. * - Strings are always quoted with single quotes
  88. * - Carriage returns and newlines are normalized to \n
  89. * - Recursion and repeated rendering is treated properly
  90. *
  91. * @param mixed $value The value to export
  92. * @param integer $indentation The indentation level of the 2nd+ line
  93. * @return string
  94. * @since Method available since Release 3.6.0
  95. */
  96. public static function export($value, $indentation = 0)
  97. {
  98. return self::recursiveExport($value, $indentation);
  99. }
  100. /**
  101. * Recursive implementation of export
  102. *
  103. * @param mixed $value The value to export
  104. * @param integer $indentation The indentation level of the 2nd+ line
  105. * @param array $processedObjects Contains all objects that were already
  106. * rendered
  107. * @return string
  108. * @since Method available since Release 3.6.0
  109. * @see PHPUnit_Util_Type::export
  110. */
  111. protected static function recursiveExport($value, $indentation, &$processedObjects = array())
  112. {
  113. if ($value === NULL) {
  114. return 'null';
  115. }
  116. if ($value === TRUE) {
  117. return 'true';
  118. }
  119. if ($value === FALSE) {
  120. return 'false';
  121. }
  122. if (is_string($value)) {
  123. // Match for most non printable chars somewhat taking multibyte chars into account
  124. if (preg_match('/[^\x09-\x0d\x20-\xff]/', $value)) {
  125. return 'Binary String: 0x' . bin2hex($value);
  126. }
  127. return "'" .
  128. str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) .
  129. "'";
  130. }
  131. $origValue = $value;
  132. if (is_object($value)) {
  133. if (in_array($value, $processedObjects, TRUE)) {
  134. return sprintf(
  135. '%s Object (*RECURSION*)',
  136. get_class($value)
  137. );
  138. }
  139. $processedObjects[] = $value;
  140. // Convert object to array
  141. $value = self::toArray($value);
  142. }
  143. if (is_array($value)) {
  144. $whitespace = str_repeat(' ', $indentation);
  145. // There seems to be no other way to check arrays for recursion
  146. // http://www.php.net/manual/en/language.types.array.php#73936
  147. preg_match_all('/\n \[(\w+)\] => Array\s+\*RECURSION\*/', print_r($value, TRUE), $matches);
  148. $recursiveKeys = array_unique($matches[1]);
  149. // Convert to valid array keys
  150. // Numeric integer strings are automatically converted to integers
  151. // by PHP
  152. foreach ($recursiveKeys as $key => $recursiveKey) {
  153. if ((string)(integer)$recursiveKey === $recursiveKey) {
  154. $recursiveKeys[$key] = (integer)$recursiveKey;
  155. }
  156. }
  157. $content = '';
  158. foreach ($value as $key => $val) {
  159. if (in_array($key, $recursiveKeys, TRUE)) {
  160. $val = 'Array (*RECURSION*)';
  161. }
  162. else {
  163. $val = self::recursiveExport($val, $indentation+1, $processedObjects);
  164. }
  165. $content .= $whitespace . ' ' . self::export($key) . ' => ' . $val . "\n";
  166. }
  167. if (strlen($content) > 0) {
  168. $content = "\n" . $content . $whitespace;
  169. }
  170. return sprintf(
  171. "%s (%s)",
  172. is_object($origValue) ? get_class($origValue) . ' Object' : 'Array',
  173. $content
  174. );
  175. }
  176. if (is_double($value) && (double)(integer)$value === $value) {
  177. return $value . '.0';
  178. }
  179. return (string)$value;
  180. }
  181. /**
  182. * Exports a value into a single-line string
  183. *
  184. * The output of this method is similar to the output of
  185. * PHPUnit_Util_Type::export. This method guarantees thought that the
  186. * result contains now newlines.
  187. *
  188. * Newlines are replaced by the visible string '\n'. Contents of arrays
  189. * and objects (if any) are replaced by '...'.
  190. *
  191. * @param mixed $value The value to export
  192. * @param integer $indentation The indentation level of the 2nd+ line
  193. * @return string
  194. * @see PHPUnit_Util_Type::export
  195. */
  196. public static function shortenedExport($value)
  197. {
  198. if (is_string($value)) {
  199. return self::shortenedString($value);
  200. }
  201. if (is_object($value)) {
  202. return sprintf(
  203. '%s Object (%s)',
  204. get_class($value),
  205. count(self::toArray($value)) > 0 ? '...' : ''
  206. );
  207. }
  208. if (is_array($value)) {
  209. return sprintf(
  210. 'Array (%s)',
  211. count($value) > 0 ? '...' : ''
  212. );
  213. }
  214. return self::export($value);
  215. }
  216. /**
  217. * Shortens a string and converts all new lines to '\n'
  218. *
  219. * @param string $string The string to shorten
  220. * @param integer $max The maximum length for the string
  221. * @return string
  222. */
  223. public static function shortenedString($string, $maxLength = 40)
  224. {
  225. $string = self::export($string);
  226. if (strlen($string) > $maxLength) {
  227. $string = substr($string, 0, $maxLength - 10) . '...' . substr($string, -7);
  228. }
  229. return str_replace("\n", '\n', $string);
  230. }
  231. /**
  232. * Converts an object to an array containing all of its private, protected
  233. * and public properties.
  234. *
  235. * @param object $object
  236. * @return array
  237. * @since Method available since Release 3.6.0
  238. */
  239. public static function toArray($object)
  240. {
  241. $array = array();
  242. foreach ((array)$object as $key => $value) {
  243. // properties are transformed to keys in the following way:
  244. // private $property => "\0Classname\0property"
  245. // protected $property => "\0*\0property"
  246. // public $property => "property"
  247. if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
  248. $key = $matches[1];
  249. }
  250. $array[$key] = $value;
  251. }
  252. // Some internal classes like SplObjectStorage don't work with the
  253. // above (fast) mechanism nor with reflection
  254. // Format the output similarly to print_r() in this case
  255. if ($object instanceof SplObjectStorage) {
  256. foreach ($object as $key => $value) {
  257. $array[spl_object_hash($value)] = array(
  258. 'obj' => $value,
  259. 'inf' => $object->getInfo(),
  260. );
  261. }
  262. }
  263. return $array;
  264. }
  265. }