Skip to content

Commit 5c048f2

Browse files
authored
Merge pull request #141 from marc-mabe/debugInfo
Added __debugInfo with human readable properties to EnumSet & EnumMap
2 parents 93b7bbb + bbb308a commit 5c048f2

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

src/EnumMap.php

+16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ public function __construct(string $enumeration, iterable $map = null)
5454
}
5555
}
5656

57+
/**
58+
* Add virtual private property "__pairs" with a list of key-value-pairs
59+
* to the result of var_dump.
60+
*
61+
* This helps debugging as internally the map is using the ordinal number.
62+
*
63+
* @return array<string, mixed>
64+
*/
65+
public function __debugInfo(): array {
66+
$dbg = (array)$this;
67+
$dbg["\0" . self::class . "\0__pairs"] = array_map(function ($k, $v) {
68+
return [$k, $v];
69+
}, $this->getKeys(), $this->getValues());
70+
return $dbg;
71+
}
72+
5773
/* write access (mutable) */
5874

5975
/**

src/EnumSet.php

+15
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ public function __construct(string $enumeration, iterable $enumerators = null)
106106
}
107107
}
108108

109+
/**
110+
* Add virtual private property "__enumerators" with a list of enumerator values set
111+
* to the result of var_dump.
112+
*
113+
* This helps debugging as internally the enumerators of this EnumSet gets stored
114+
* as either integer or binary bit-array.
115+
*
116+
* @return array<string, mixed>
117+
*/
118+
public function __debugInfo() {
119+
$dbg = (array)$this;
120+
$dbg["\0" . self::class . "\0__enumerators"] = $this->getValues();
121+
return $dbg;
122+
}
123+
109124
/**
110125
* Get the classname of the enumeration
111126
* @return string

tests/MabeEnumTest/EnumMapTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MabeEnumTest\TestAsset\Enum32;
88
use MabeEnumTest\TestAsset\EnumBasic;
99
use MabeEnumTest\TestAsset\EnumInheritance;
10+
use MabeEnumTest\TestAsset\EnumMapExt;
1011
use PHPUnit\Framework\TestCase;
1112
use UnexpectedValueException;
1213

@@ -488,6 +489,34 @@ public function testIsEmpty()
488489
$this->assertTrue($map2->isEmpty());
489490
}
490491

492+
public function testDebugInfo()
493+
{
494+
$map = new EnumMapExt(EnumBasic::class);
495+
foreach (EnumBasic::getEnumerators() as $i => $enumerator) {
496+
$map->add($enumerator, $i);
497+
}
498+
499+
$dbg = $map->__debugInfo();
500+
501+
$privateEnumMapPrefix = "\0" . EnumMap::class . "\0";
502+
$privateEnumMapExtPrefix = "\0" . EnumMapExt::class . "\0";
503+
$protectedEnumMapExtPrefix = "\0*\0";
504+
$publicEnumMapExtPrefix = '';
505+
506+
// assert real properties still exists
507+
$this->assertArrayHasKey("{$privateEnumMapPrefix}enumeration", $dbg);
508+
$this->assertArrayHasKey("{$privateEnumMapPrefix}map", $dbg);
509+
$this->assertArrayHasKey("{$privateEnumMapExtPrefix}priv", $dbg);
510+
$this->assertArrayHasKey("{$protectedEnumMapExtPrefix}prot", $dbg);
511+
$this->assertArrayHasKey("{$publicEnumMapExtPrefix}pub", $dbg);
512+
513+
// assert virtual private property __pairs
514+
$this->assertArrayHasKey("{$privateEnumMapPrefix}__pairs", $dbg);
515+
$this->assertSame(array_map(function ($k, $v) {
516+
return [$k, $v];
517+
}, $map->getKeys(), $map->getValues()), $dbg["{$privateEnumMapPrefix}__pairs"]);
518+
}
519+
491520
/* deprecated */
492521

493522
public function testContains()

tests/MabeEnumTest/EnumSetTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MabeEnumTest\TestAsset\Enum64;
1313
use MabeEnumTest\TestAsset\Enum65;
1414
use MabeEnumTest\TestAsset\Enum66;
15+
use MabeEnumTest\TestAsset\EnumSetExt;
1516
use PHPUnit\Framework\TestCase;
1617

1718
/**
@@ -956,6 +957,28 @@ public function testSetSymDiffThrowsInvalidArgumentException()
956957
$set1->setSymDiff($set2);
957958
}
958959

960+
public function testDebugInfo()
961+
{
962+
$set = new EnumSetExt(EnumBasic::class, EnumBasic::getEnumerators());
963+
$dbg = $set->__debugInfo();
964+
965+
$privateEnumSetPrefix = "\0" . EnumSet::class . "\0";
966+
$privateEnumSetExtPrefix = "\0" . EnumSetExt::class . "\0";
967+
$protectedEnumSetExtPrefix = "\0*\0";
968+
$publicEnumSetExtPrefix = '';
969+
970+
// assert real properties still exists
971+
$this->assertArrayHasKey("{$privateEnumSetPrefix}enumeration", $dbg);
972+
$this->assertArrayHasKey("{$privateEnumSetPrefix}bitset", $dbg);
973+
$this->assertArrayHasKey("{$privateEnumSetExtPrefix}priv", $dbg);
974+
$this->assertArrayHasKey("{$protectedEnumSetExtPrefix}prot", $dbg);
975+
$this->assertArrayHasKey("{$publicEnumSetExtPrefix}pub", $dbg);
976+
977+
// assert virtual private property __enumerators
978+
$this->assertArrayHasKey("{$privateEnumSetPrefix}__enumerators", $dbg);
979+
$this->assertSame(EnumBasic::getValues(), $dbg["{$privateEnumSetPrefix}__enumerators"]);
980+
}
981+
959982
/* deprecated */
960983

961984
/** @deprecated */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\EnumMap;
6+
7+
/**
8+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
9+
* @copyright Copyright (c) 2020 Marc Bennewitz
10+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
11+
*/
12+
class EnumMapExt extends EnumMap
13+
{
14+
private $priv = 'private';
15+
protected $prot = 'protected';
16+
public $pub = 'public';
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\EnumSet;
6+
7+
/**
8+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
9+
* @copyright Copyright (c) 2020 Marc Bennewitz
10+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
11+
*/
12+
class EnumSetExt extends EnumSet
13+
{
14+
private $priv = 'private';
15+
protected $prot = 'protected';
16+
public $pub = 'public';
17+
}

0 commit comments

Comments
 (0)