Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native property types in other classes #531

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/TokensList.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@
{
/**
* The count of tokens.
*
* @var int
*/
public $count = 0;
public int $count = 0;

/**
* The index of the next token to be returned.
*
* @var int
*/
public $idx = 0;
public int $idx = 0;

/** @param Token[] $tokens The array of tokens. */
public function __construct(public array $tokens = [])
Expand Down Expand Up @@ -204,7 +200,7 @@
*/
public function offsetGet(mixed $offset): Token|null
{
return $offset < $this->count ? $this->tokens[$offset] : null;

Check warning on line 203 in src/TokensList.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ */ public function offsetGet(mixed $offset) : Token|null { - return $offset < $this->count ? $this->tokens[$offset] : null; + return $offset <= $this->count ? $this->tokens[$offset] : null; } /** * Checks if an offset was previously set.
}

/**
Expand All @@ -227,7 +223,7 @@
unset($this->tokens[$offset]);
--$this->count;
for ($i = $offset; $i < $this->count; ++$i) {
$this->tokens[$i] = $this->tokens[$i + 1];

Check warning on line 226 in src/TokensList.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "Plus": --- Original +++ New @@ @@ unset($this->tokens[$offset]); --$this->count; for ($i = $offset; $i < $this->count; ++$i) { - $this->tokens[$i] = $this->tokens[$i + 1]; + $this->tokens[$i] = $this->tokens[$i - 1]; } unset($this->tokens[$this->count]); } }
}

unset($this->tokens[$this->count]);
Expand Down
20 changes: 5 additions & 15 deletions src/UtfString.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,41 @@
{
/**
* The raw, multi-byte string.
*
* @var string
*/
public $str = '';
public string $str = '';

/**
* The index of current byte.
*
* For ASCII strings, the byte index is equal to the character index.
*
* @var int
*/
public $byteIdx = 0;
public int $byteIdx = 0;

/**
* The index of current character.
*
* For non-ASCII strings, some characters occupy more than one byte and
* the character index will have a lower value than the byte index.
*
* @var int
*/
public $charIdx = 0;
public int $charIdx = 0;

/**
* The length of the string (in bytes).
*
* @var int
*/
public $byteLen = 0;
public int $byteLen = 0;

/**
* The length of the string (in characters).
*
* @var int
*/
public $charLen = 0;
public int $charLen = 0;

/** @param string $str the string */
public function __construct(string $str)
{
$this->str = $str;
$this->byteLen = mb_strlen($str, '8bit');

Check warning on line 68 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ public function __construct(string $str) { $this->str = $str; - $this->byteLen = mb_strlen($str, '8bit'); + $this->byteLen = strlen($str); if (!mb_check_encoding($str, 'UTF-8')) { $this->charLen = 0; } else {
if (! mb_check_encoding($str, 'UTF-8')) {
$this->charLen = 0;

Check warning on line 70 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ $this->str = $str; $this->byteLen = mb_strlen($str, '8bit'); if (!mb_check_encoding($str, 'UTF-8')) { - $this->charLen = 0; + $this->charLen = -1; } else { $this->charLen = mb_strlen($str, 'UTF-8'); }
} else {
$this->charLen = mb_strlen($str, 'UTF-8');
}
Expand All @@ -90,7 +80,7 @@
*/
public function offsetExists(mixed $offset): bool
{
return ($offset >= 0) && ($offset < $this->charLen);

Check warning on line 83 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "GreaterThanOrEqualTo": --- Original +++ New @@ @@ */ public function offsetExists(mixed $offset) : bool { - return $offset >= 0 && $offset < $this->charLen; + return $offset > 0 && $offset < $this->charLen; } /** * Gets the character at given offset.
}

/**
Expand All @@ -112,18 +102,18 @@

$delta = $offset - $this->charIdx;

if ($delta > 0) {

Check warning on line 105 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ return null; } $delta = $offset - $this->charIdx; - if ($delta > 0) { + if ($delta >= 0) { // Fast forwarding. $this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4 * $delta), 0, $delta)); $this->charIdx += $delta;
// Fast forwarding.
$this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4 * $delta), 0, $delta));

Check warning on line 107 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ $delta = $offset - $this->charIdx; if ($delta > 0) { // Fast forwarding. - $this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4 * $delta), 0, $delta)); + $this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 5 * $delta), 0, $delta)); $this->charIdx += $delta; } elseif ($delta < 0) { // Rewinding.
$this->charIdx += $delta;
} elseif ($delta < 0) {

Check warning on line 109 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ // Fast forwarding. $this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4 * $delta), 0, $delta)); $this->charIdx += $delta; - } elseif ($delta < 0) { + } elseif ($delta <= 0) { // Rewinding. while ($delta++ < 0) { // We rewind byte by byte and only count characters that are not continuation bytes,
// Rewinding.
while ($delta++ < 0) {
// We rewind byte by byte and only count characters that are not continuation bytes,
// i.e. ASCII characters and first octets of multibyte characters
do {
$byte = ord($this->str[--$this->byteIdx]);
} while (($byte >= 128) && ($byte < 192));

Check warning on line 116 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "GreaterThanOrEqualTo": --- Original +++ New @@ @@ // i.e. ASCII characters and first octets of multibyte characters do { $byte = ord($this->str[--$this->byteIdx]); - } while ($byte >= 128 && $byte < 192); + } while ($byte > 128 && $byte < 192); --$this->charIdx; } }

Check warning on line 116 in src/UtfString.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ // i.e. ASCII characters and first octets of multibyte characters do { $byte = ord($this->str[--$this->byteIdx]); - } while ($byte >= 128 && $byte < 192); + } while ($byte >= 128 && $byte <= 192); --$this->charIdx; } }

--$this->charIdx;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Lexer/TokensListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TokensListTest extends TestCase
*
* @var Token[]
*/
public $tokens;
public array $tokens;

/**
* Test setup.
Expand Down
3 changes: 1 addition & 2 deletions tests/benchmarks/UtfStringBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class UtfStringBench
{
/** @var string */
private $testContents;
private string $testContents;

/**
* @BeforeMethods("setUp")
Expand Down
Loading