Page MenuHomePhabricator

D9935.diff
No OneTemporary

D9935.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,17 @@
}
public function testVarExport() {
+ // Constants
+ $this->assertEqual('null', phutil_var_export(null));
+ $this->assertEqual('true', phutil_var_export(true));
+ $this->assertEqual('false', phutil_var_export(false));
+ $this->assertEqual("'quack'", phutil_var_export('quack'));
+ $this->assertEqual('1234567', phutil_var_export(1234567));
+
+ // Arrays
$this->assertEqual(
'array()',
phutil_var_export(array()));
-
$this->assertEqual(
implode("\n", array(
'array(',
@@ -579,17 +586,34 @@
')',
)),
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', 'bar' => 'baz')));
+ $this->assertEqual(
+ implode("\n", array(
+ 'array(',
+ " 'foo' => array(",
+ " 'bar' => array(",
+ " 'baz' => array(),",
+ ' ),',
' ),',
')',
)),
- phutil_var_export(array('foo', 'bar' => array('baz' => new stdClass()))));
+ phutil_var_export(
+ array('foo' => array('bar' => array('baz' => array())))));
+
+ // Objects
+ $this->assertEqual(
+ "stdClass::__set_state(array(\n))",
+ phutil_var_export(new stdClass()));
+ $this->assertEqual(
+ "PhutilTestPhobject::__set_state(array(\n))",
+ 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
@@ -1076,16 +1076,43 @@
/**
* Returns a parsable string representation of a variable.
*
+ * This function is intended to behave similarly to PHP's `var_export` function,
+ * but the output is intended to follow our style conventions.
+ *
* @param wild The variable you want to export.
* @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';
+ }
+
+ // PHP's `var_export` doesn't format arrays very nicely. In particular:
+ //
+ // - An empty array is split over two lines (`"array (\n)"`).
+ // - A space separates "array" and the first opening brace.
+ // - Non-associative arrays are returned as associative arrays with an
+ // integer key.
+ //
+ if (is_array($var)) {
+ if (count($var) === 0) {
+ return 'array()';
+ }
+
+ $output = array();
+ $output[] = 'array(';
+
+ foreach ($var as $key => $value) {
+ // Adjust the indentation of the value.
+ $value = str_replace("\n", "\n ", phutil_var_export($value));
+ $output[] = ' '.var_export($key, true).' => '.$value.',';
+ }
+
+ $output[] = ')';
+ return implode("\n", $output);
+ }
- $var = var_export($var, true);
- return preg_replace(array_keys($regex), array_values($regex), $var);
+ // Let PHP handle everything else.
+ return var_export($var, true);
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 19, 9:42 PM (14 h, 52 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6725595
Default Alt Text
D9935.diff (3 KB)

Event Timeline