Skip to content

Commit c7c83e7

Browse files
committed
[CLEANUP] Don't store array length as a property
Since PHP 5, if not earlier, the length of an array is internally stored by PHP and can be accessed in O(1) time. Maintaining both the array and its length in class properties is error-prone (particularly regarding possible future code changes). When used in a loop, `\count($array)` is more repetitively expensive than `$arrayLength`, but the latter can be set up as a local variable as and when needed. Reference: https://stackoverflow.com/questions/5835241/is-phps-count-function-o1-or-on-for-arrays This change also ensures that the characters are always a (dazzling) array. And adds a type parameter to `ParserState::setCharset()`, which was unavoidably required. Resolves #953.
1 parent d71ff28 commit c7c83e7

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/Parsing/ParserState.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ParserState
3030
/**
3131
* @var array<int, string>
3232
*/
33-
private $characters;
33+
private $characters = [];
3434

3535
/**
3636
* @var int
@@ -44,11 +44,6 @@ class ParserState
4444
*/
4545
private $charset;
4646

47-
/**
48-
* @var int
49-
*/
50-
private $length;
51-
5247
/**
5348
* @var int
5449
*/
@@ -68,15 +63,13 @@ public function __construct($text, Settings $parserSettings, $lineNumber = 1)
6863

6964
/**
7065
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
71-
*
72-
* @param string $charset
7366
*/
74-
public function setCharset($charset): void
67+
public function setCharset(string $charset): void
7568
{
7669
$this->charset = $charset;
7770
$this->characters = $this->strsplit($this->text);
78-
if (\is_array($this->characters)) {
79-
$this->length = \count($this->characters);
71+
if (!\is_array($this->characters)) {
72+
throw new SourceException('Charset `' . $charset . '` cannot be reconciled with the CSS provided.');
8073
}
8174
}
8275

@@ -227,7 +220,7 @@ public function consumeWhiteSpace(): array
227220
try {
228221
$oComment = $this->consumeComment();
229222
} catch (UnexpectedEOFException $e) {
230-
$this->currentPosition = $this->length;
223+
$this->currentPosition = \count($this->characters);
231224
return $comments;
232225
}
233226
} else {
@@ -259,7 +252,7 @@ public function comes($sString, $bCaseInsensitive = false): bool
259252
public function peek($length = 1, $iOffset = 0): string
260253
{
261254
$iOffset += $this->currentPosition;
262-
if ($iOffset >= $this->length) {
255+
if ($iOffset >= \count($this->characters)) {
263256
return '';
264257
}
265258
return $this->substr($iOffset, $length);
@@ -288,7 +281,7 @@ public function consume($mValue = 1): string
288281
$this->currentPosition += $this->strlen($mValue);
289282
return $mValue;
290283
} else {
291-
if ($this->currentPosition + $mValue > $this->length) {
284+
if ($this->currentPosition + $mValue > \count($this->characters)) {
292285
throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber);
293286
}
294287
$result = $this->substr($this->currentPosition, $mValue);
@@ -345,7 +338,7 @@ public function consumeComment()
345338

346339
public function isEnd(): bool
347340
{
348-
return $this->currentPosition >= $this->length;
341+
return $this->currentPosition >= \count($this->characters);
349342
}
350343

351344
/**
@@ -438,10 +431,10 @@ public function strlen($sString): int
438431
private function substr($iStart, $length): string
439432
{
440433
if ($length < 0) {
441-
$length = $this->length - $iStart + $length;
434+
$length = \count($this->characters) - $iStart + $length;
442435
}
443-
if ($iStart + $length > $this->length) {
444-
$length = $this->length - $iStart;
436+
if ($iStart + $length > \count($this->characters)) {
437+
$length = \count($this->characters) - $iStart;
445438
}
446439
$result = '';
447440
while ($length > 0) {

0 commit comments

Comments
 (0)