Skip to content

Commit 981464c

Browse files
authored
[CLEANUP] Don't store array length as a property (#954)
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 Resolves #953.
1 parent 5af88d7 commit 981464c

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/Parsing/ParserState.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
@@ -75,7 +70,6 @@ public function setCharset(string $charset): void
7570
{
7671
$this->charset = $charset;
7772
$this->characters = $this->strsplit($this->text);
78-
$this->length = \count($this->characters);
7973
}
8074

8175
/**
@@ -225,7 +219,7 @@ public function consumeWhiteSpace(): array
225219
try {
226220
$comment = $this->consumeComment();
227221
} catch (UnexpectedEOFException $e) {
228-
$this->currentPosition = $this->length;
222+
$this->currentPosition = \count($this->characters);
229223
return $comments;
230224
}
231225
} else {
@@ -257,7 +251,7 @@ public function comes($sString, $bCaseInsensitive = false): bool
257251
public function peek($length = 1, $offset = 0): string
258252
{
259253
$offset += $this->currentPosition;
260-
if ($offset >= $this->length) {
254+
if ($offset >= \count($this->characters)) {
261255
return '';
262256
}
263257
return $this->substr($offset, $length);
@@ -286,7 +280,7 @@ public function consume($mValue = 1): string
286280
$this->currentPosition += $this->strlen($mValue);
287281
return $mValue;
288282
} else {
289-
if ($this->currentPosition + $mValue > $this->length) {
283+
if ($this->currentPosition + $mValue > \count($this->characters)) {
290284
throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber);
291285
}
292286
$result = $this->substr($this->currentPosition, $mValue);
@@ -343,7 +337,7 @@ public function consumeComment()
343337

344338
public function isEnd(): bool
345339
{
346-
return $this->currentPosition >= $this->length;
340+
return $this->currentPosition >= \count($this->characters);
347341
}
348342

349343
/**
@@ -436,10 +430,10 @@ public function strlen($sString): int
436430
private function substr($iStart, $length): string
437431
{
438432
if ($length < 0) {
439-
$length = $this->length - $iStart + $length;
433+
$length = \count($this->characters) - $iStart + $length;
440434
}
441-
if ($iStart + $length > $this->length) {
442-
$length = $this->length - $iStart;
435+
if ($iStart + $length > \count($this->characters)) {
436+
$length = \count($this->characters) - $iStart;
443437
}
444438
$result = '';
445439
while ($length > 0) {

0 commit comments

Comments
 (0)