Skip to content

Commit

Permalink
Dumper: added support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 24, 2021
1 parent 6115fb4 commit 46e804a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$parameters->set('skip', [
'tmp/*',
'fixtures*/*',
'tests/Tracy/Dumper.toText().specials.enum.phpt', // enum

PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer::class => [
'src/Tracy/Debugger/Debugger.php',
Expand Down
1 change: 1 addition & 0 deletions src/Tracy/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Dumper
/** @var array */
public static $objectExporters = [
\Closure::class => [Exposer::class, 'exposeClosure'],
\UnitEnum::class => [Exposer::class, 'exposeEnum'],
\ArrayObject::class => [Exposer::class, 'exposeArrayObject'],
\SplFileInfo::class => [Exposer::class, 'exposeSplFileInfo'],
\SplObjectStorage::class => [Exposer::class, 'exposeSplObjectStorage'],
Expand Down
10 changes: 10 additions & 0 deletions src/Tracy/Dumper/Exposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ public static function exposeClosure(\Closure $obj, Value $value, Describer $des
}


public static function exposeEnum(\UnitEnum $enum, Value $value, Describer $describer): void
{
$value->value = get_class($enum) . '::' . $enum->name;
if ($enum instanceof \BackedEnum) {
$describer->addPropertyTo($value, 'value', $enum->value);
$value->collapsed = true;
}
}


public static function exposeArrayObject(\ArrayObject $obj, Value $value, Describer $describer): void
{
$flags = $obj->getFlags();
Expand Down
35 changes: 35 additions & 0 deletions tests/Tracy/Dumper.toText().specials.enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Test: Tracy\Dumper::toText() enums
* @phpVersion 8.1
*/

declare(strict_types=1);

use Tester\Assert;
use Tracy\Dumper;

require __DIR__ . '/../bootstrap.php';


enum Suit {
case Clubs;
case Diamonds;
case Hearts;
case Spades;
}

enum Methods: string {
case GET = 'get';
case POST = 'post';
}

Assert::match(<<<'XX'
array (3)
0 => Suit::Clubs #5
1 => Methods::GET #6
| value: 'get'
2 => Methods::GET #6 see above
XX
, Dumper::toText([Suit::Clubs, Methods::GET, Methods::GET]));

0 comments on commit 46e804a

Please sign in to comment.