Skip to content

Commit

Permalink
#32
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavche committed Jun 13, 2024
1 parent 477c340 commit ba02ed0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 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
7 changes: 1 addition & 6 deletions src/EnumBitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ public function set(UnitEnum ...$bits): void
public function remove(UnitEnum ...$bits): void
{
$this->has(...$bits);
foreach ($bits as $bit) {
if (!$this->bitmask->has(...$this->enumToInt($bit))) {
continue;
}
$this->bitmask->remove(...$this->enumToInt($bit));
}
$this->bitmask->remove(...$this->enumToInt(...$bits));
}

/** @throws UnknownEnumException|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());
}
}

0 comments on commit ba02ed0

Please sign in to comment.