Page MenuHomePhabricator

D9935.id23864.diff
No OneTemporary

D9935.id23864.diff

diff --git a/src/utils/__tests__/PhutilUtilsTestCase.php b/src/utils/__tests__/PhutilUtilsTestCase.php
--- a/src/utils/__tests__/PhutilUtilsTestCase.php
+++ b/src/utils/__tests__/PhutilUtilsTestCase.php
@@ -566,10 +566,14 @@
}
public function testVarExport() {
+ $this->assertEqual('null', phutil_var_export(null));
+ $this->assertEqual('true', phutil_var_export(true));
+ $this->assertEqual('false', phutil_var_export(false));
+
+ // Arrays
$this->assertEqual(
'array()',
phutil_var_export(array()));
-
$this->assertEqual(
implode("\n", array(
'array(',
@@ -579,17 +583,22 @@
')',
)),
phutil_var_export(array(1, 2, 3)));
-
$this->assertEqual(
implode("\n", array(
'array(',
- " 0 => 'foo',",
- " 'bar' => array(",
- " 'baz' => stdClass::__set_state(array()),",
- ' ),',
+ " 'foo' => 'bar',",
+ " 'bar' => 'baz',",
')',
)),
- phutil_var_export(array('foo', 'bar' => array('baz' => new stdClass()))));
+ phutil_var_export(array('foo' => 'bar', 'bar' => 'baz')));
+
+ // Objects
+ $this->assertEqual(
+ 'stdClass::__set_state(array())',
+ phutil_var_export(new stdClass()));
+ $this->assertEqual(
+ 'PhutilTestPhobject::__set_state(array())',
+ phutil_var_export(new PhutilTestPhobject()));
}
}
diff --git a/src/utils/utils.php b/src/utils/utils.php
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -1080,12 +1080,44 @@
* @return string
*/
function phutil_var_export($var) {
- $regex = array(
- "/=>\s*\n\s+/" => '=> ',
- "/array\s*\(\n\s*\)/" => 'array()',
- '/array\s+\(/' => 'array(',
- );
+ // `var_export(null, true)` returns `"NULL"` (in uppercase).
+ if ($var === null) {
+ return 'null';
+ }
+
+ if (is_array($var)) {
+ if (count($var) === 0) {
+ return 'array()';
+ }
+
+ $output = array();
+ $output[] = 'array(';
+
+ static $indent = 0;
+
+ foreach ($var as $key => $value) {
+ $indent = $indent + 2;
+
+ $output[] = implode('', array(
+ str_repeat(' ', $indent),
+ var_export($key, true),
+ ' => ',
+ phutil_var_export($value),
+ ',',
+ ));
+
+ $indent = $indent - 2;
+ }
+
+ $output[] = str_repeat(' ', $indent).')';
+
+ return implode("\n", $output);
+ }
+
+ if (is_object($var)) {
+ $var = var_export($var, true);
+ return preg_replace("/array\(\n\)/", 'array()', $var);
+ }
- $var = var_export($var, true);
- return preg_replace(array_keys($regex), array_values($regex), $var);
+ return var_export($var, true);
}

File Metadata

Mime Type
text/plain
Expires
Thu, Oct 24, 9:34 PM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6747281
Default Alt Text
D9935.id23864.diff (2 KB)

Event Timeline