Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/EnumMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ public function __construct(string $enumeration, iterable $map = null)
}
}

/**
* Add virtual private property "__pairs" with a list of key-value-pairs
* to the result of var_dump.
*
* This helps debugging as internally the map is using the ordinal number.
*
* @return array<string, mixed>
*/
public function __debugInfo(): array {
$dbg = (array)$this;
$dbg["\0" . self::class . "\0__pairs"] = array_map(function ($k, $v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be static::class instead because the class is not final?

Copy link
Owner Author

@marc-mabe marc-mabe Apr 5, 2020

Choose a reason for hiding this comment

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

No, because this is debug information only and it should not make the impression to access this.
Adding it as private property of the base class shows it's impossible to access (at least without reflection/closure).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I understand, thx for the clarification :)

return [$k, $v];
}, $this->getKeys(), $this->getValues());
return $dbg;
}

/* write access (mutable) */

/**
Expand Down
15 changes: 15 additions & 0 deletions src/EnumSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ public function __construct(string $enumeration, iterable $enumerators = null)
}
}

/**
* Add virtual private property "__enumerators" with a list of enumerator values set
* to the result of var_dump.
*
* This helps debugging as internally the enumerators of this EnumSet gets stored
* as either integer or binary bit-array.
*
* @return array<string, mixed>
*/
public function __debugInfo() {
$dbg = (array)$this;
$dbg["\0" . self::class . "\0__enumerators"] = $this->getValues();
return $dbg;
}

/**
* Get the classname of the enumeration
* @return string
Expand Down
29 changes: 29 additions & 0 deletions tests/MabeEnumTest/EnumMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MabeEnumTest\TestAsset\Enum32;
use MabeEnumTest\TestAsset\EnumBasic;
use MabeEnumTest\TestAsset\EnumInheritance;
use MabeEnumTest\TestAsset\EnumMapExt;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

Expand Down Expand Up @@ -488,6 +489,34 @@ public function testIsEmpty()
$this->assertTrue($map2->isEmpty());
}

public function testDebugInfo()
{
$map = new EnumMapExt(EnumBasic::class);
foreach (EnumBasic::getEnumerators() as $i => $enumerator) {
$map->add($enumerator, $i);
}

$dbg = $map->__debugInfo();

$privateEnumMapPrefix = "\0" . EnumMap::class . "\0";
$privateEnumMapExtPrefix = "\0" . EnumMapExt::class . "\0";
$protectedEnumMapExtPrefix = "\0*\0";
$publicEnumMapExtPrefix = '';

// assert real properties still exists
$this->assertArrayHasKey("{$privateEnumMapPrefix}enumeration", $dbg);
$this->assertArrayHasKey("{$privateEnumMapPrefix}map", $dbg);
$this->assertArrayHasKey("{$privateEnumMapExtPrefix}priv", $dbg);
$this->assertArrayHasKey("{$protectedEnumMapExtPrefix}prot", $dbg);
$this->assertArrayHasKey("{$publicEnumMapExtPrefix}pub", $dbg);

// assert virtual private property __pairs
$this->assertArrayHasKey("{$privateEnumMapPrefix}__pairs", $dbg);
$this->assertSame(array_map(function ($k, $v) {
return [$k, $v];
}, $map->getKeys(), $map->getValues()), $dbg["{$privateEnumMapPrefix}__pairs"]);
}

/* deprecated */

public function testContains()
Expand Down
23 changes: 23 additions & 0 deletions tests/MabeEnumTest/EnumSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MabeEnumTest\TestAsset\Enum64;
use MabeEnumTest\TestAsset\Enum65;
use MabeEnumTest\TestAsset\Enum66;
use MabeEnumTest\TestAsset\EnumSetExt;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -956,6 +957,28 @@ public function testSetSymDiffThrowsInvalidArgumentException()
$set1->setSymDiff($set2);
}

public function testDebugInfo()
{
$set = new EnumSetExt(EnumBasic::class, EnumBasic::getEnumerators());
$dbg = $set->__debugInfo();

$privateEnumSetPrefix = "\0" . EnumSet::class . "\0";
$privateEnumSetExtPrefix = "\0" . EnumSetExt::class . "\0";
$protectedEnumSetExtPrefix = "\0*\0";
$publicEnumSetExtPrefix = '';

// assert real properties still exists
$this->assertArrayHasKey("{$privateEnumSetPrefix}enumeration", $dbg);
$this->assertArrayHasKey("{$privateEnumSetPrefix}bitset", $dbg);
$this->assertArrayHasKey("{$privateEnumSetExtPrefix}priv", $dbg);
$this->assertArrayHasKey("{$protectedEnumSetExtPrefix}prot", $dbg);
$this->assertArrayHasKey("{$publicEnumSetExtPrefix}pub", $dbg);

// assert virtual private property __enumerators
$this->assertArrayHasKey("{$privateEnumSetPrefix}__enumerators", $dbg);
$this->assertSame(EnumBasic::getValues(), $dbg["{$privateEnumSetPrefix}__enumerators"]);
}

/* deprecated */

/** @deprecated */
Expand Down
17 changes: 17 additions & 0 deletions tests/MabeEnumTest/TestAsset/EnumMapExt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MabeEnumTest\TestAsset;

use MabeEnum\EnumMap;

/**
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2020 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
class EnumMapExt extends EnumMap
{
private $priv = 'private';
protected $prot = 'protected';
public $pub = 'public';
}
17 changes: 17 additions & 0 deletions tests/MabeEnumTest/TestAsset/EnumSetExt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MabeEnumTest\TestAsset;

use MabeEnum\EnumSet;

/**
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2020 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
class EnumSetExt extends EnumSet
{
private $priv = 'private';
protected $prot = 'protected';
public $pub = 'public';
}