Skip to content

Commit

Permalink
fix all tests and deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
apemsel committed Mar 29, 2024
1 parent f13e67a commit f38a1e4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

interface Attribute extends \ArrayAccess, \Countable
{
public function setRange($from, $to, $state = true);
public function toString($true = "1", $false = "0");
public function search($offset = 0, $returnLength = false, $state = true, $strict = true);
public function setRange(int $from, int $to, bool $state = true): void;
public function toString(string $true = "1", string $false = "0"): string;
public function search(int $offset = 0, bool $returnLength = false, bool $state = true, bool $strict = true);
}
5 changes: 1 addition & 4 deletions src/AttributedString.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AttributedString implements \Countable, \ArrayAccess
* @param string|AttributedString $string Either a simple string or another AttributedString to init the AttributedString
* @param string $attributeClass Class to use for attributes
*/
public function __construct(string $string, string $attributeClass = "apemsel\AttributedString\BooleanArray") {
public function __construct(string|AttributedString $string, string $attributeClass = "apemsel\AttributedString\BooleanArray") {
if (is_string($string)) {
$this->string = $string;
$this->length = mb_strlen($string, "utf-8");
Expand All @@ -31,9 +31,6 @@ public function __construct(string $string, string $attributeClass = "apemsel\At
$this->length = $string->length;
$this->byteToChar = $string->byteToChar;
}
else {
throw new \InvalidArgumentException();
}

$this->attributeClass = $attributeClass;
}
Expand Down
44 changes: 22 additions & 22 deletions src/Bitmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class Bitmap implements Attribute
{
protected $bitmap;
protected $length;

/**
* @param int length of bitmask
*/
public function __construct($length) {
$this->length = $length;
$this->bitmap = str_repeat(chr(0), ceil($this->length / 8));
}

/**
* Returns the bitmask as a visual string
*
Expand All @@ -29,39 +29,39 @@ public function __construct($length) {
public function __toString() {
return $this->toString();
}

/**
* Returns the bitmask as a visual string with custom chars for 0s and 1s
*
* @param string $true representation of 1s
* @param string $true representation of 0s
* @return string bitmask as visual string of the given representations
*/
public function toString($true = "1", $false = "0") {
public function toString(string $true = "1", string $false = "0"): string {
$string = str_repeat($false, $this->length);
for ($offset = 0; $offset < $this->length; $offset++) {
if (ord($this->bitmap[(int) ($offset / 8)]) & (1 << $offset % 8)) {
$string[$offset] = $true;
}
}

return $string;
}

/**
* Set given range to a state
*
* @param int $from start offset
* @param int $to end offset
* @param bool $state set state to true (default) or false
*/
public function setRange($from, $to, $state = true) {
public function setRange(int $from, int $to, bool $state = true): void {
// Set attribute state for given range
for($i = $from; $i <= $to; $i++) {
$this->offsetSet($i, $state);
}
}

/**
* Search inside bitmap for ranges with the given state
*
Expand All @@ -77,51 +77,51 @@ public function search($offset = 0, $returnLength = false, $state = true, $stric
if ($returnLength) {
$length = $this->search($i, false, !$state, $strict);
$length = $length ? $length - $i : $this->length - $i;

return [$i, $length];
} else {
return $i;
}
}
}

return false;
}

// ArrayAccess interface

/**
* Check if the given offset exists in the bitmap
*
* @param int $offset offset
* @return bool does the offset exist
*/
public function offsetExists($offset) {
public function offsetExists(mixed $offset): bool {
return is_int($offset) && $offset >= 0 && $offset < $this->length;
}

/**
* Get bit at given offset
*
* @param int $offset offset
* @return bool bit at given offset
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): bool
{
if ($this->offsetExists($offset)) {
return (bool) (ord($this->bitmap[(int) ($offset / 8)]) & (1 << $offset % 8));
} else {
throw new \OutOfRangeException();
}
}

/**
* Set bit at given offset
*
* @param int $offset offset
* @param bool $value bit at given offset
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
if ($this->offsetExists($offset)) {
$index = (int) ($offset / 8);
Expand All @@ -134,24 +134,24 @@ public function offsetSet($offset, $value)
throw new \OutOfRangeException();
}
}

/**
* Unset bit at given offset - not implemented
*
* @throws RuntimeException always
*/
public function offsetUnset($offset) {
public function offsetUnset(mixed $offset): void {
throw new \RuntimeException("Bitmap does not support offsetUnset");
}

// Countable interface

/**
* Return bitmap length
*
* @return int bitmap length
*/
public function count() {
public function count(): int {
return $this->length;
}
}
56 changes: 28 additions & 28 deletions src/BooleanArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,49 @@ class BooleanArray implements MutableAttribute
{
protected $attribute;
protected $length;

/**
* @param int length of array
*/
public function __construct($length) {
public function __construct(int $length) {
$this->length = $length;
$this->attribute = array_fill(0, $length, false);
}

/**
* Returns the array as a visual string
*
* @return string array as visual string of 0s and 1s
*/
public function __toString() {
public function __toString(): string {
return $this->toString();
}

/**
* Returns the array as a visual string with custom chars for 0s and 1s
*
* @param string $true representation of 1s
* @param string $true representation of 0s
* @return string array as visual string of the given representations
*/
public function toString($true = "1", $false = "0") {
public function toString(string $true = "1", string $false = "0"): string {
return implode("", array_map(function($v) use ($true, $false) {
return $v ? $true : $false;
}, $this->attribute));
}

/**
* Set given range to a state
*
* @param int $from start offset
* @param int $to end offset
* @param bool $state set state to true (default) or false
*/
public function setRange($from, $to, $state = true) {
public function setRange(int $from, int $to, bool $state = true): void {
// Set attribute state for given range
$this->attribute = array_replace($this->attribute, array_fill($from, $to-$from+1, $state));
}

/**
* Search inside bitmap for ranges with the given state
*
Expand All @@ -69,14 +69,14 @@ public function search($offset = 0, $returnLength = false, $state = true, $stric
if ($offset) {
$a = array_slice($a, $offset, NULL, true);
}

$pos = array_search($state, $a, $strict);

if ($returnLength) {
if (false === $pos) {
return false;
}

$a = array_slice($a, $pos - $offset);
$length = array_search(!$state, $a, $strict);
$length = $length ? $length : $this->length - $pos;
Expand All @@ -85,9 +85,9 @@ public function search($offset = 0, $returnLength = false, $state = true, $stric
return $pos;
}
}

// MutableAttribute interface

/**
* Insert a piece into the array at given offset with a given state and length
*
Expand All @@ -99,7 +99,7 @@ public function insert($offset, $length, $state) {
$this->length += $length;
array_splice($this->attribute, $offset, 0, array_fill(0, $length, $state));
}

/**
* Delete a piece of the array at given offset with a given length
*
Expand All @@ -110,58 +110,58 @@ public function delete($offset, $length) {
$this->length -= $length;
array_splice($this->attribute, $offset, $length);
}

// ArrayAccess interface

/**
* Check if the given offset exists in the array
*
* @param int $offset offset
* @return bool does the offset exist
*/
public function offsetExists($offset) {
public function offsetExists(mixed $offset): bool {
return $offset < $this->length;
}

/**
* Get bool at given offset
*
* @param int $offset offset
* @return bool bit at given offset
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): bool
{
return $this->attribute[$offset];
}

/**
* Set bool at given offset
*
* @param int $offset offset
* @param bool $value bit at given offset
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
$this->attribute[$offset] = $value;
$this->attribute[$offset] = (bool) $value;
}

/**
* Unset bit at given offset
*
* @param int $offset offset
*/
public function offsetUnset($offset) {
public function offsetUnset(mixed $offset): void {
unset($this->attribute[$offset]);
}

// Countable interface

/**
* Return array length
*
* @return int array length
*/
public function count() {
public function count(): int {
return $this->length;
}
}
5 changes: 0 additions & 5 deletions test/unit/AttributedStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ public function testConstructAndToString() {

$as2 = new AttributedString($as);
$this->assertEquals("fóò", $as2);
$this->expectException('InvalidArgumentException');
$as = new AttributedString(1);

}

public function testBasicAttributes() {
Expand Down Expand Up @@ -113,8 +110,6 @@ public function testSearchAttribute() {
$this->assertEquals(4, $as->searchAttribute("bold"));
$this->assertEquals([4, 3], $as->searchAttribute("bold", 0, true));
$this->assertEquals([0, 4], $as->searchAttribute("bold", 0, true, false), "search for false state of attribute");
$this->assertEquals([0, 4], $as->searchAttribute("bold", 0, true, 0, false), "search for 0 state of attribute, non-strict");
$this->assertEquals(false, $as->searchAttribute("bold", 0, true, 0, true), "search for 0 state of attribute, strict");
$this->assertEquals(false, $as->searchAttribute("underlined"));
$this->assertEquals(false, $as->searchAttribute("bold", 7));
$this->assertEquals(false, $as->searchAttribute("underlined", 0, true));
Expand Down

0 comments on commit f38a1e4

Please sign in to comment.