Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13996486
D9935.id23864.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D9935.id23864.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D9935: Refactor `phutil_var_export`
Attached
Detach File
Event Timeline
Log In to Comment