Skip to content

Commit

Permalink
Rename add/remove to with/without
Browse files Browse the repository at this point in the history
in order to reflect better singleton & immutability
  • Loading branch information
ogizanagi committed Nov 13, 2016
1 parent 062e204 commit 1ea250e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,16 @@ Get instances using bitwise operations and manipulate them:
```php
<?php
$permissions = Permissions::get(Permissions::EXECUTE | Permissions::WRITE | Permissions::READ);
$permissions = $permissions->removeFlags(Permissions::EXECUTE); // Returns an instance without "execute" flag
$permissions = $permissions->withoutFlags(Permissions::EXECUTE); // Returns an instance without "execute" flag
$permissions->getValue(); // Returns 6 (int)
$permissions->getFlags(); // Returns [2, 4] (=> [Permissions::EXECUTE, Permissions::WRITE])

$permissions = $permissions->removeFlags(Permissions::READ | Permissions::WRITE); // Returns an instance without "read" and "write" flags
$permissions = $permissions->withoutFlags(Permissions::READ | Permissions::WRITE); // Returns an instance without "read" and "write" flags
$permissions->getValue(); // Returns Permissions::NONE (0). Note: NONE is defined in parent class, FlaggedEnum.
$permissions->getFlags(); // Returns an empty array

$permissions = Permissions::get(Permissions::NONE); // Returns an empty bitmask instance
$permissions = $permissions->addFlags(Permissions::READ | Permissions::EXECUTE); // Returns an instance with "read" and "execute" permissions
$permissions = $permissions->withFlags(Permissions::READ | Permissions::EXECUTE); // Returns an instance with "read" and "execute" permissions
$permissions->hasFlag(Permissions::READ); // True
$permissions->hasFlag(Permissions::READ | Permissions::EXECUTE); // True
$permissions->hasFlag(Permissions::WRITE); // False
Expand Down Expand Up @@ -616,5 +616,5 @@ Method | Static | Returns | Description
`getReadable(string $separator = '; ')` | <kbd>No</kbd> | <kbd>string</kbd> | Same as before, but with a delimiter option (see above).
`getFlags()` | <kbd>No</kbd> | <kbd>int[]</kbd> | Returns an array of bit flags set in the current enumeration instance.
`hasFlag(int $bitFlag)` | <kbd>No</kbd> | <kbd>bool</kbd> | True if the current instance has the given bit flag(s).
`addFlags(int $flags)` | <kbd>No</kbd> | <kbd>static</kbd> | Returns a new instance of the enumeration with additional flag(s).
`removeFlags(int $flags)` | <kbd>No</kbd> | <kbd>static</kbd> | Returns a new instance of the enumeration without given flag(s).
`withFlags(int $flags)` | <kbd>No</kbd> | <kbd>static</kbd> | Computes a new value with given flags, and returns the corresponding instance.
`withoutFlags(int $flags)` | <kbd>No</kbd> | <kbd>static</kbd> | Computes a new value without given flags, and returns the corresponding instance.
8 changes: 4 additions & 4 deletions src/FlaggedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ public function hasFlag(int $bitFlag): bool
}

/**
* Adds a bitmask to the value of this instance.
* Computes a new value with given flags, and returns the corresponding instance.
*
* @param int $flags The bit flag or bit flags
*
* @throws InvalidValueException When $flags is not acceptable for this enumeration type
*
* @return static The enum instance for computed value
*/
public function addFlags(int $flags): self
public function withFlags(int $flags): self
{
if (!static::accepts($flags)) {
throw new InvalidValueException($flags, static::class);
Expand All @@ -173,15 +173,15 @@ public function addFlags(int $flags): self
}

/**
* Removes a bitmask from the value of this instance.
* Computes a new value without given flags, and returns the corresponding instance.
*
* @param int $flags The bit flag or bit flags
*
* @throws InvalidValueException When $flags is not acceptable for this enumeration type
*
* @return static The enum instance for computed value
*/
public function removeFlags(int $flags): self
public function withoutFlags(int $flags): self
{
if (!static::accepts($flags)) {
throw new InvalidValueException($flags, static::class);
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/FlaggedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testSameEnumValueActsAsSingleton()
$all = Permissions::get(Permissions::ALL);
$this->assertTrue($all === Permissions::get(Permissions::READ | Permissions::WRITE | Permissions::EXECUTE));
$this->assertTrue(
$all->removeFlags(Permissions::READ) === Permissions::get(
$all->withoutFlags(Permissions::READ) === Permissions::get(
Permissions::WRITE | Permissions::EXECUTE
)
);
Expand Down Expand Up @@ -130,10 +130,10 @@ public function testReadableSeparatorCanBeChanged()
$this->assertSame('Execute | Write', $instance->getReadable(' | '));
}

public function testAddFlags()
public function testWithFlags()
{
$original = Permissions::get(Permissions::READ);
$result = $original->addFlags(Permissions::WRITE | Permissions::EXECUTE);
$result = $original->withFlags(Permissions::WRITE | Permissions::EXECUTE);

$this->assertNotSame($original, $result);
$this->assertTrue($result->hasFlag(Permissions::EXECUTE));
Expand All @@ -145,27 +145,27 @@ public function testAddFlags()
* @expectedException \Elao\Enum\Exception\InvalidValueException
* @expectedExceptionMessage 8 is not an acceptable value
*/
public function testThrowExceptionWhenInvalidFlagsAdded()
public function testThrowExceptionWhenWithInvalidFlags()
{
$value = Permissions::get(Permissions::READ);
$value->addFlags(Permissions::ALL + 1);
$value->withFlags(Permissions::ALL + 1);
}

public function testRemoveFlags()
public function testWithoutFlags()
{
$original = Permissions::get(Permissions::ALL);
$result = $original->removeFlags(Permissions::READ | Permissions::WRITE);
$result = $original->withoutFlags(Permissions::READ | Permissions::WRITE);

$this->assertNotSame($original, $result);
$this->assertTrue($result->hasFlag(Permissions::EXECUTE));
$this->assertFalse($result->hasFlag(Permissions::READ));
$this->assertFalse($result->hasFlag(Permissions::WRITE));
}

public function testRemoveAllFlags()
public function testWithoutAnyFlag()
{
$original = Permissions::get(Permissions::ALL);
$result = $original->removeFlags(Permissions::ALL);
$result = $original->withoutFlags(Permissions::ALL);
$this->assertCount(0, $result->getFlags());
$this->assertSame(Permissions::NONE, $result->getValue());
}
Expand All @@ -177,7 +177,7 @@ public function testRemoveAllFlags()
public function testThrowExceptionWhenInvalidFlagsRemoved()
{
$value = Permissions::get(Permissions::ALL);
$value->removeFlags(99);
$value->withoutFlags(99);
}

public function testInstances()
Expand Down

0 comments on commit 1ea250e

Please sign in to comment.