Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumper: object hash #277

Merged
merged 1 commit into from Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Framework/Dumper.php
Expand Up @@ -102,7 +102,7 @@ private static function objectToLine($object)
$line .= '(' . $object->format('Y-m-d H:i:s O') . ')';
}

return $line . '(#' . substr(md5(spl_object_hash($object)), 0, 4) . ')';
return $line . '(' . self::hash($object) . ')';
}


Expand All @@ -117,6 +117,17 @@ public static function toPhp($var)
}


/**
* Returns object's stripped hash.
* @param object
* @return string
*/
private static function hash($object)
{
return '#' . substr(md5(spl_object_hash($object)), 0, 4);
}


/**
* @return string
*/
Expand Down Expand Up @@ -216,9 +227,10 @@ private static function _toPhp(&$var, & $list = array(), $level = 0, & $line = 1
}
$out .= $space;
}
$hash = self::hash($var);
return $class === 'stdClass'
? "(object) array($out)"
: "$class::__set_state(array($out))";
? "(object) /* $hash */ array($out)"
: "$class::__set_state(/* $hash */ array($out))";

} elseif (is_resource($var)) {
return '/* resource ' . get_resource_type($var) . ' */';
Expand Down
6 changes: 3 additions & 3 deletions tests/Framework/Dumper.toPhp.phpt
Expand Up @@ -38,12 +38,12 @@ Assert::match('array(
)', Dumper::toPhp(array(1, 'hello', "\r" => array(), array(1, 2), array(1 => 1, 2, 3, 4, 5, 6, 7))));

Assert::match('/* resource stream */', Dumper::toPhp(fopen(__FILE__, 'r')));
Assert::match('(object) array()', Dumper::toPhp((object) NULL));
Assert::match("(object) array(
Assert::match('(object) /* #%a% */ array()', Dumper::toPhp((object) NULL));
Assert::match("(object) /* #%a% */ array(
'a' => 'b',
)", Dumper::toPhp((object) array('a' => 'b')));

Assert::match("Test::__set_state(array(
Assert::match("Test::__set_state(/* #%a% */ array(
'x' => array(10, NULL),
'y' => 'hello',
'z' => 30.0,
Expand Down
8 changes: 4 additions & 4 deletions tests/Framework/Dumper.toPhp.recursion.phpt
Expand Up @@ -18,7 +18,7 @@ Assert::match('array(

$obj = (object) array('x' => 1, 'y' => 2);
$obj->z = & $obj;
Assert::match("(object) array(
Assert::match("(object) /* #%a% */ array(
'x' => 1,
'y' => 2,
'z' => /* stdClass dumped on line 1 */,
Expand All @@ -39,12 +39,12 @@ Assert::match("array(
3,
array(1, 2, 3, /* Nesting level too deep or recursive dependency */),
),
(object) array(),
(object) array(
(object) /* #%a% */ array(),
(object) /* #%a% */ array(
'x' => 1,
'y' => 2,
'z' => /* stdClass dumped on line 9 */,
),
(object) array(),
(object) /* #%a% */ array(),
/* stdClass dumped on line 9 */,
)", Dumper::toPhp($var));