Skip to content

Commit

Permalink
Allow to remove an enum bit (case) twice (#32)
Browse files Browse the repository at this point in the history
* Allow to remove an enum bit (case) twice

* Fixed BitMask remove

---------

Co-authored-by: yaroslavche <[email protected]>
  • Loading branch information
gisostallenberg and yaroslavche authored Jun 13, 2024
1 parent 3072607 commit bcc6eab
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public function set(int ...$bits): void
/** @throws NotSingleBitException */
public function remove(int ...$bits): void
{
array_walk($bits, fn(int $bit) => $this->checkBit($bit));
array_walk($bits, fn(int $bit) => $this->mask ^= $bit);
// $this->mask &= ~$bit;
foreach ($bits as $bit) {
if ($this->has($bit)) {
$this->mask ^= $bit;
// $this->mask &= ~$bit;
}
}
}

/** @throws NotSingleBitException */
Expand Down
10 changes: 10 additions & 0 deletions tests/BitMaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,14 @@ public function testToString(): void
$bitmask->set(8);
assertSame('15', (string) $bitmask);
}

public function testRemoveTwice(): void
{
$bitmask = new BitMask(self::READ | self::WRITE | self::EXECUTE);
$bitmask->remove(self::READ);
assertFalse($bitmask->has(self::READ));
$bitmask->remove(self::READ);
assertFalse($bitmask->has(self::READ));
assertSame(self::WRITE | self::EXECUTE, $bitmask->get());
}
}
34 changes: 34 additions & 0 deletions tests/EnumBitMaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ public function testSetUnset(): void
$this->expectException(UnknownEnumException::class);
$enumBitmask->remove(Unknown::Case);
}
public function testSetTwice(): void
{
$enumBitmask = new EnumBitMask(Permissions::class, 3);
assertTrue($enumBitmask->has(Permissions::Create));
$enumBitmask->set(Permissions::Create);
assertSame(3, $enumBitmask->get());
assertTrue($enumBitmask->has(Permissions::Create));
$enumBitmask->set(Permissions::Create, Permissions::Read);
assertTrue($enumBitmask->has(Permissions::Create));
assertTrue($enumBitmask->has(Permissions::Read));
assertSame(3, $enumBitmask->get());
$enumBitmask->set(Permissions::Update);
assertTrue($enumBitmask->has(Permissions::Update));
assertSame(7, $enumBitmask->get());
}

public function testRemoveTwice(): void
{
$enumBitmask = new EnumBitMask(Permissions::class, 3);
assertTrue($enumBitmask->has(Permissions::Create));
$enumBitmask->remove(Permissions::Create);
assertSame(2, $enumBitmask->get());
$enumBitmask->remove(Permissions::Create);
assertSame(2, $enumBitmask->get());
$enumBitmask->remove(Permissions::Create, Permissions::Read);
assertSame(0, $enumBitmask->get());

$enumBitmask->set(...Permissions::cases());
foreach (Permissions::cases() as $case) {
$enumBitmask->remove($case);
$enumBitmask->remove($case);
}
assertSame(0, $enumBitmask->get());
}

public function testBackedEnum(): void
{
Expand Down

0 comments on commit bcc6eab

Please sign in to comment.