Skip to content

Commit

Permalink
Merge b38399a into e6783bd
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harris committed Sep 19, 2023
2 parents e6783bd + b38399a commit efc07ab
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 72 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ jobs:

- name: Run unit tests
shell: bash
run: vendor/bin/phpunit
run: vendor/bin/phpunit --coverage-clover clover.xml
- name: Coveralls Parallel
uses: coverallsapp/github-action@v2
with:
flag-name: run-${{ join(matrix.*, '-') }}
parallel: true

# This is a meta job to avoid to have to constantly change the protection rules
# whenever we touch the matrix.
Expand All @@ -78,7 +83,10 @@ jobs:
- name: Successful run
if: ${{ !(contains(needs.*.result, 'failure')) }}
run: exit 0

- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
- name: Failing run
if: ${{ contains(needs.*.result, 'failure') }}
run: exit 1
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
}
},
"require": {
"php": ">=7.1 | >=8.0",
"php": ">=8.1",
"myclabs/php-enum": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 51 additions & 54 deletions src/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,43 @@

use BadMethodCallException;
use MyCLabs\Enum\Enum;
use ReflectionException;
use UnexpectedValueException;

abstract class BitMask extends Enum
{
// Because 0 is the identity element when ORing a bitmask, we have to special-case it when ANDing bitmasks
// (akin to ordinary division by 0, the addition identity element on integers/rationals/reals)

protected function isFlag(int $flag): bool
protected static function assertValidValueReturningKey($value): string
{
return 0 === $flag ? 0 === $this->value : (($this->value & $flag) == $flag);
}
if (!static::isValid($value)) {
throw new UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
}

protected function isComponentOfFlag(int $flag): bool
{
return 0 === $flag ? $flag === $this->value : (($this->value & $flag) == $this->value);
return implode('|', self::getKeyArray($value));
}

protected function setFlag(int $flag, bool $value)
/**
* @param $value
*
* @return bool
*/
public static function isValid($value): bool
{
if ($value) {
$this->value = 0 === $flag ? 0 : $this->value | $flag;
} else {
$this->value = 0 === $flag ? $this->value : $this->value & ~$flag;
}
$min = min(static::toArray());
$max = max(static::toArray()) * 2 - 1;

return $this;
return $value >= $min && $value <= $max;
}

/**
* @param $name
* @param $arguments
*
* @throws \ReflectionException
*
* @return bool|self
* @throws ReflectionException
*
*/
public function __call($name, $arguments)
{
Expand All @@ -49,39 +51,22 @@ public function __call($name, $arguments)
$regexFull = sprintf($regexBase, implode('$|', array_keys($array)));
preg_match($regexFull, $name, $match);
if (count($match) > 0 && $match[0] === $name) {
return $this->{$match[1].'Flag'}($array[$match[2]], $arguments[0] ?? true);
return $this->{$match[1] . 'Flag'}($array[$match[2]], $arguments[0] ?? true);
}

throw new BadMethodCallException(sprintf('Enum %s not found on %s', $name, get_class($this)));
}

/**
* @param $value
*
* @throws \ReflectionException
*
* @return bool
*/
public static function isValid($value): bool
{
$min = min(static::toArray());
$max = max(static::toArray()) * 2 - 1;

return $value >= $min && $value <= $max;
}

/**
* @throws \ReflectionException
*
* @return array
*/
public static function toArray(): array
{
$firstTime = !isset(static::$cache[static::class]);
$array = array_filter(parent::toArray(), function ($value): bool {
$array = array_filter(parent::toArray(), static function ($value): bool {
return is_scalar($value);
});
$firstTime && array_walk($array, function ($item): void {
$firstTime && array_walk($array, static function ($item): void {
if (!is_int($item)) {
throw new UnexpectedValueException(
sprintf('All defined Const on Enum %s should be integers', static::class)
Expand All @@ -95,51 +80,42 @@ public static function toArray(): array
/**
* @return string
*/
public function getKey()
public function getKey(): string
{
return implode('|', $this->getKeyArray($this->value));
return implode('|', self::getKeyArray($this->value));
}

/**
* @param mixed $value
*
* @return array
*/
public static function getKeyArray($value)
public static function getKeyArray(mixed $value): array
{
$f = array_filter(static::toArray(), function ($key) use (&$value) {
$f = array_filter(static::toArray(), static function ($key) use (&$value) {
return $value & $key;
});

return array_keys($f);
}

protected static function assertValidValueReturningKey($value): string
{
if (!static::isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum ".static::class);
}

return implode('|', self::getKeyArray($value));
}

/**
* @throws \ReflectionException
*
* @return string
* @throws ReflectionException
*
*/
public function __toString(): string
{
$name = $this->getName();
$array = static::toArray();
$ret = '';
foreach ($array as $key => $value) {
$ret .= "'".$key."' => ".($this->{'is'.$key}() ? 'TRUE' : 'FALSE').PHP_EOL;
$ret .= "'" . $key . "' => " . ($this->{'is' . $key}() ? 'TRUE' : 'FALSE') . PHP_EOL;
}

return $name.'['.PHP_EOL.
$ret.
']'.PHP_EOL;
return $name . '[' . PHP_EOL .
$ret .
']' . PHP_EOL;
}

public function getName(): string
Expand All @@ -148,4 +124,25 @@ public function getName(): string

return array_pop($path);
}

protected function isFlag(int $flag): bool
{
return 0 === $flag ? 0 === $this->value : (($this->value & $flag) === $flag);
}

protected function isComponentOfFlag(int $flag): bool
{
return 0 === $flag ? $flag === $this->value : (($this->value & $flag) === $this->value);
}

protected function setFlag(int $flag, bool $value): static
{
if ($value) {
$this->value = 0 === $flag ? 0 : $this->value | $flag;
} else {
$this->value = 0 === $flag ? $this->value : $this->value & ~$flag;
}

return $this;
}
}

0 comments on commit efc07ab

Please sign in to comment.