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

Add var type #76

Merged
merged 12 commits into from
Oct 28, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.3.1 under development
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.0.0 since response format changed.


- Enh #76: Add a variable type to output when it can't be exported more (@xepozz)
- Enh #75: Add method `asPrimitives` that exports a variable like destructed json (@xepozz)
- Enh #74: Add support for integer property names (@xepozz)

Expand Down
19 changes: 18 additions & 1 deletion src/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@
*/
final class VarDumper
{
public const VAR_TYPE_PROPERTY = '$__type__$';
public const OBJECT_ID_PROPERTY = '$__id__$';
public const OBJECT_CLASS_PROPERTY = '$__class__$';
public const DEPTH_LIMIT_EXCEEDED_PROPERTY = '$__depth_limit_exceeded__$';

public const VAR_TYPE_ARRAY = 'array';
public const VAR_TYPE_OBJECT = 'object';
public const VAR_TYPE_RESOURCE = 'resource';

/**
* @var mixed Variable to dump.
*/
Expand Down Expand Up @@ -353,10 +359,20 @@ private function exportPrimitives(mixed $var, int $depth, int $level): mixed
switch (gettype($var)) {
case 'resource':
case 'resource (closed)':
return '{resource}';
/** @var resource $var */
$id = get_resource_id($var);
$type = get_resource_type($var);

return [
self::VAR_TYPE_PROPERTY => self::VAR_TYPE_RESOURCE,
'id' => $id,
'type' => $type,
'closed' => !is_resource($var),
];
case 'array':
if ($depth <= $level) {
return [
self::VAR_TYPE_PROPERTY => self::VAR_TYPE_ARRAY,
self::DEPTH_LIMIT_EXCEEDED_PROPERTY => true,
];
}
Expand All @@ -372,6 +388,7 @@ private function exportPrimitives(mixed $var, int $depth, int $level): mixed
$objectId = $this->getObjectId($var);
if ($depth <= $level) {
return [
self::VAR_TYPE_PROPERTY => self::VAR_TYPE_OBJECT,
self::OBJECT_ID_PROPERTY => $objectId,
self::OBJECT_CLASS_PROPERTY => $objectClass,
self::DEPTH_LIMIT_EXCEEDED_PROPERTY => true,
Expand Down
62 changes: 56 additions & 6 deletions tests/VarDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,13 @@ public function asJsonDataProvider(): array
$objectWithPrivatePropertiesId = spl_object_id($objectWithPrivateProperties);
$objectWithPrivatePropertiesClass = str_replace('\\', '\\\\', PrivateProperties::class);

$openedResource = fopen('php://input', 'rb');
$openedResourceId = get_resource_id($openedResource);

$closedResource = fopen('php://input', 'rb');
$closedResourceId = get_resource_id($closedResource);
fclose($closedResource);

return [
'custom debug info' => [
$dummyDebugInfo,
Expand Down Expand Up @@ -912,9 +919,27 @@ static function () {
true,
'true',
],
'resource' => [
fopen('php://input', 'rb'),
'"{resource}"',
'opened resource' => [
$openedResource,
<<<JSON
{
"\$__type__\$": "resource",
"id": {$openedResourceId},
"type": "stream",
"closed": false
}
JSON,
],
'closed resource' => [
$closedResource,
<<<JSON
{
"\$__type__\$": "resource",
"id": {$closedResourceId},
"type": "Unknown",
"closed": true
}
JSON,
],
'empty array' => [
[],
Expand Down Expand Up @@ -1023,6 +1048,7 @@ static function () {
"\$__id__\$": "{$nestedObjectId}",
"\$__class__\$": "stdClass",
"nested": {
"\$__type__\$": "object",
"\$__id__\$": "{$nestedObjectId}",
"\$__class__\$": "stdClass",
"\$__depth_limit_exceeded__\$": true
Expand All @@ -1047,6 +1073,7 @@ static function () {
[
[
{
"\$__type__\$": "array",
"\$__depth_limit_exceeded__\$": true
}
]
Expand Down Expand Up @@ -1101,6 +1128,13 @@ public function asPrimitivesDataProvider(): array
$objectWithPrivatePropertiesId = spl_object_id($objectWithPrivateProperties);
$objectWithPrivatePropertiesClass = PrivateProperties::class;

$openedResource = fopen('php://input', 'rb');
$openedResourceId = get_resource_id($openedResource);

$closedResource = fopen('php://input', 'rb');
$closedResourceId = get_resource_id($closedResource);
fclose($closedResource);

return [
'custom debug info' => [
$dummyDebugInfo,
Expand Down Expand Up @@ -1186,9 +1220,23 @@ static function () {
true,
true,
],
'resource' => [
fopen('php://input', 'rb'),
'{resource}',
'opened resource' => [
$openedResource,
[
'$__type__$' => 'resource',
'id' => $openedResourceId,
'type' => 'stream',
'closed' => false,
],
],
'closed resource' => [
$closedResource,
[
'$__type__$' => 'resource',
'id' => $closedResourceId,
'type' => 'Unknown',
'closed' => true,
],
],
'empty array' => [
[],
Expand Down Expand Up @@ -1285,6 +1333,7 @@ static function () {
'nested' => [
'$__id__$' => "$nestedObjectId",
'$__class__$' => stdClass::class,
'$__type__$' => 'object',
'$__depth_limit_exceeded__$' => true,
],
],
Expand All @@ -1305,6 +1354,7 @@ static function () {
[
[
[
'$__type__$' => 'array',
'$__depth_limit_exceeded__$' => true,
],
],
Expand Down