Skip to content

Commit

Permalink
Merge 62b3228 into fa5d501
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harris committed Mar 20, 2020
2 parents fa5d501 + 62b3228 commit 5aaad48
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build_failure_conditions:
- 'elements.rating(<= F).new.exists'
- 'issues.severity(>= CRITICAL).new.exists'

build:
environment:
php: 7.2.13
44 changes: 18 additions & 26 deletions src/BitMask.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Cruxinator\BitMask;

Expand All @@ -8,46 +10,40 @@

abstract class BitMask extends Enum
{
protected function isFlagSet(int $flag) : bool
protected function isFlag(int $flag): bool
{
return (($this->value & $flag) == $flag);
return $flag === 0 ? $this->value === 0 : (($this->value & $flag) == $flag);
}

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

/**
* @param $name
* @param $arguments
* @throws \ReflectionException
* @return bool|self
*/
public function __call($name, $arguments)
{
$array = static::toArray();
$sub = array_reduce(['is','set'], function ($carry, $item) use ($name) {
return substr($name, 0, strlen($item)) === $item ? strlen($item) : $carry;
}, false);

if ($sub !== false) {
$actualName = substr($name, $sub);
if (isset($array[$actualName]) || array_key_exists($actualName, $array)) {
return $sub === 2 ? $this->isFlagSet($array[$actualName]) : $this->setFlag($array[$actualName], $arguments[0]);
}
$regexBase = '/(is|set)(%s)/m';
$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);
}
throw new BadMethodCallException(sprintf('Method %s not found on Class %s', $name, get_class($this)));
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)
Expand All @@ -58,13 +54,12 @@ public static function isValid($value)
}

/**
* @throws \ReflectionException
* @return array
*/
public static function toArray()
{
$array = parent::toArray();
//TODO: check that the array is defined correctly.
//TODO: check that the array is defined correctly. basically check everything in the array is a int.
/*$array = array_filter($array, function ($temp) {
$raw = log($temp, 2);
return is_int($temp) && 0.01 > abs($raw - round($raw));
Expand All @@ -74,14 +69,12 @@ public static function toArray()
}

/**
* @throws \ReflectionException
* @return array|mixed
*/
public function getKey()
{
$value = $this->value;
$f = array_filter(static::toArray(), function (
/* @noinspection PhpUnusedParameterInspection needed for function def */ $key) use (&$value) {
$f = array_filter(static::toArray(), function () use (&$value) {
$isSet = $value & 1;
$value = $value >> 1;
return $isSet;
Expand All @@ -91,20 +84,19 @@ public function getKey()
}

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

public function getName()
Expand Down
32 changes: 27 additions & 5 deletions tests/BitMaskFixture.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
<?php

declare(strict_types=1);


namespace Cruxinator\BitMask\Tests;

use Cruxinator\BitMask\BitMask;

/**
* @method self ONE()
* @method bool isONE()
* @method self setONE(bool $onOrOff = true)
* @method self TWO()
* @method bool isTWO()
* @method self setTWO(bool $onOrOff = true)
* @method self FOUR()
* @method bool isFOUR()
* @method self setFOUR(bool $onOrOff = true)
* @method self EIGHT()
* @method bool isEIGHT()
* @method self setEIGHT(bool $onOrOff = true)
* @method self SIXTEEN()
* @method bool isSIXTEEN()
* @method self setSIXTEEN(bool $onOrOff = true)
* @method self THIRTYTWO()
* @method bool isTHIRTYTWO()
* @method self setTHIRTYTWO(bool $onOrOff = true)
*/
class BitMaskFixture extends BitMask
{
const ONE = 1;
const TWO = 2;
const FOUR = 4;
const EIGHT = 8;
const SIXTEEN = 16;
const ONE = 1;
const TWO = 2;
const FOUR = 4;
const EIGHT = 8;
const SIXTEEN = 16;
const THIRTYTWO = 32;
}
14 changes: 14 additions & 0 deletions tests/BitMaskZeroFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Cruxinator\BitMask\Tests;

/**
* @method self ZERO()
* @method bool isZERO()
* @method self setZERO(bool $onOrOff = true)
*/
class BitMaskZeroFixture extends BitMaskFixture
{
const ZERO = 0;
}
31 changes: 25 additions & 6 deletions tests/BitmaskTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Cruxinator\BitMask\Tests;

class BitmaskTest extends \PHPUnit\Framework\TestCase
Expand All @@ -16,7 +18,7 @@ public function testGetValue()
$value = new BitMaskFixture(BitMaskFixture::THIRTYTWO);
$this->assertEquals(BitMaskFixture::THIRTYTWO, $value->getValue());
}

public function testFoo()
{
$Val =new BitMaskFixture(BitMaskFixture::ONE | BitMaskFixture::TWO);
Expand All @@ -33,7 +35,8 @@ public function testBadCall()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage(
'Method startTheDance not found on Class Cruxinator\BitMask\Tests\BitMaskFixture');
'Enum startTheDance not found on Cruxinator\BitMask\Tests\BitMaskFixture'
);
$foo = new BitMaskFixture(BitMaskFixture::FOUR);

$foo->startTheDance();
Expand All @@ -44,27 +47,43 @@ public function testGetName()
$foo = new BitMaskFixture(BitMaskFixture::ONE | BitMaskFixture::TWO);

$expected = 'BitMask';
$actual = $foo->getName();
$actual = $foo->getName();
$this->assertEquals($expected, $actual);
}

public function testToString()
{
$foo = new BitMaskFixture(BitMaskFixture::ONE | BitMaskFixture::TWO);

$expected = 'BitMask['.PHP_EOL.'\'ONE\' => TRUE'.PHP_EOL.'\'TWO\' => TRUE'.PHP_EOL.'\'FOUR\' => FALSE'.PHP_EOL;
$expected .= '\'EIGHT\' => FALSE'.PHP_EOL.'\'SIXTEEN\' => FALSE'.PHP_EOL.'\'THIRTYTWO\' => FALSE'.PHP_EOL.']';
$expected = 'BitMask[' . PHP_EOL . '\'ONE\' => TRUE' . PHP_EOL . '\'TWO\' => TRUE' . PHP_EOL . '\'FOUR\' => FALSE' . PHP_EOL;
$expected .= '\'EIGHT\' => FALSE' . PHP_EOL . '\'SIXTEEN\' => FALSE' . PHP_EOL . '\'THIRTYTWO\' => FALSE' . PHP_EOL . ']';
$expected .= PHP_EOL;
$actual = $foo->__toString();
$this->assertEquals($expected, $actual);
}

public function testGetKey()
{
$foo = new BitMaskFixture(BitMaskFixture::ONE | BitMaskFixture::THIRTYTWO);
$foo = new BitMaskFixture(BitMaskFixture::ONE | BitMaskFixture::THIRTYTWO);
$expected = ['ONE', 'THIRTYTWO'];

$actual = $foo->getKey();
$this->assertEquals($expected, $actual);
}

public function testZero()
{
$foo = BitMaskZeroFixture::ONE();
$this->assertTrue($foo->isONE());
$this->assertFalse($foo->isZERO());
$foo->setONE(false);
$this->assertFalse($foo->isONE());
$this->assertTrue($foo->isZERO());
$foo->setTHIRTYTWO(true);
$this->assertTrue($foo->isTHIRTYTWO());
$this->assertFalse($foo->isZERO());
$foo->setZERO();
$this->assertTrue($foo->isZERO());
$this->assertFalse($foo->isTHIRTYTWO());
}
}
4 changes: 3 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require_once(dirname(__DIR__). '/vendor/autoload.php');

declare(strict_types=1);
require_once(dirname(__DIR__) . '/vendor/autoload.php');

\SebastianBergmann\Comparator\Factory::getInstance()->register(new \MyCLabs\Enum\PHPUnit\Comparator());

0 comments on commit 5aaad48

Please sign in to comment.