Skip to content

[CLEANUP] Clean up ParserState a bit #1173

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

Merged
merged 1 commit into from
Mar 16, 2025
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
12 changes: 0 additions & 12 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ parameters:
count: 1
path: ../src/CSSList/Document.php

-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
identifier: equal.notAllowed
count: 1
path: ../src/Parsing/ParserState.php

-
message: '#^PHPDoc tag @return with type array\<int, Sabberworm\\CSS\\Comment\\Comment\>\|void is not subtype of native type array\.$#'
identifier: return.phpDocType
count: 1
path: ../src/Parsing/ParserState.php

-
message: '#^Cannot call method render\(\) on string\.$#'
identifier: method.nonObject
Expand Down
104 changes: 52 additions & 52 deletions src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function parseIdentifier(bool $ignoreCase = true): string
if ($ignoreCase) {
$result = $this->strtolower($result);
}

return $result;
}

Expand Down Expand Up @@ -181,11 +182,12 @@ public function parseCharacter(bool $isForIdentifier): ?string
} else {
return $this->consume(1);
}

return null;
}

/**
* @return array<int, Comment>|void
* @return list<Comment>
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
Expand All @@ -202,24 +204,27 @@ public function consumeWhiteSpace(): array
$comment = $this->consumeComment();
} catch (UnexpectedEOFException $e) {
$this->currentPosition = \count($this->characters);
return $comments;
break;
}
} else {
$comment = $this->consumeComment();
}
if ($comment !== false) {
if ($comment instanceof Comment) {
$comments[] = $comment;
}
} while ($comment !== false);
} while ($comment instanceof Comment);

return $comments;
}

/**
* @param non-empty-string $string
*/
public function comes(string $string, bool $caseInsensitive = false): bool
{
$peek = $this->peek(\strlen($string));
return ($peek == '')
? false
: $this->streql($peek, $string, $caseInsensitive);

return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
}

/**
Expand All @@ -232,6 +237,7 @@ public function peek(int $length = 1, int $offset = 0): string
if ($offset >= \count($this->characters)) {
return '';
}

return $this->substr($offset, $length);
}

Expand All @@ -254,19 +260,22 @@ public function consume($value = 1): string
$this->lineNumber
);
}

$this->lineNumber += $numberOfLines;
$this->currentPosition += $this->strlen($value);
return $value;
$result = $value;
} else {
if ($this->currentPosition + $value > \count($this->characters)) {
throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
}

$result = $this->substr($this->currentPosition, $value);
$numberOfLines = \substr_count($result, "\n");
$this->lineNumber += $numberOfLines;
$this->currentPosition += $value;
return $result;
}

return $result;
}

/**
Expand All @@ -279,21 +288,23 @@ public function consume($value = 1): string
public function consumeExpression(string $expression, ?int $maximumLength = null): string
{
$matches = null;
$input = $maximumLength !== null ? $this->peek($maximumLength) : $this->inputLeft();
if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) === 1) {
return $this->consume($matches[0][0]);
$input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
}
throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);

return $this->consume($matches[0][0]);
}

/**
* @return Comment|false
*/
public function consumeComment()
{
$comment = false;
$lineNumber = $this->lineNumber;
$comment = null;

if ($this->comes('/*')) {
$lineNumber = $this->lineNumber;
$this->consume(1);
$comment = '';
while (($char = $this->consume(1)) !== '') {
Expand All @@ -305,12 +316,8 @@ public function consumeComment()
}
}

if ($comment !== false) {
// We skip the * which was included in the comment.
return new Comment(\substr($comment, 1), $lineNumber);
}

return $comment;
// We skip the * which was included in the comment.
return \is_string($comment) ? new Comment(\substr($comment, 1), $lineNumber) : false;
}

public function isEnd(): bool
Expand All @@ -319,7 +326,7 @@ public function isEnd(): bool
}

/**
* @param array<array-key, string>|string $stopCharacters
* @param list<string>|string $stopCharacters
* @param array<int, Comment> $comments
*
* @throws UnexpectedEOFException
Expand All @@ -346,7 +353,8 @@ public function consumeUntil(
return $consumedCharacters;
}
$consumedCharacters .= $character;
if ($comment = $this->consumeComment()) {
$comment = $this->consumeComment();
if ($comment instanceof Comment) {
$comments[] = $comment;
}
}
Expand All @@ -371,11 +379,9 @@ private function inputLeft(): string

public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
{
if ($caseInsensitive) {
return $this->strtolower($string1) === $this->strtolower($string2);
} else {
return $string1 === $string2;
}
return $caseInsensitive
? ($this->strtolower($string1) === $this->strtolower($string2))
: ($string1 === $string2);
}

/**
Expand All @@ -391,11 +397,9 @@ public function backtrack(int $numberOfCharacters): void
*/
public function strlen(string $string): int
{
if ($this->parserSettings->hasMultibyteSupport()) {
return \mb_strlen($string, $this->charset);
} else {
return \strlen($string);
}
return $this->parserSettings->hasMultibyteSupport()
? \mb_strlen($string, $this->charset)
: \strlen($string);
}

/**
Expand All @@ -415,20 +419,22 @@ private function substr(int $offset, int $length): string
$offset++;
$length--;
}

return $result;
}

/**
* @return ($string is non-empty-string ? non-empty-string : string)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know this was possible.

*/
private function strtolower(string $string): string
{
if ($this->parserSettings->hasMultibyteSupport()) {
return \mb_strtolower($string, $this->charset);
} else {
return \strtolower($string);
}
return $this->parserSettings->hasMultibyteSupport()
? \mb_strtolower($string, $this->charset)
: \strtolower($string);
}

/**
* @return array<int, string>
* @return list<string>
*
* @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
*/
Expand All @@ -440,33 +446,27 @@ private function strsplit(string $string): array
if (!\is_array($result)) {
throw new SourceException('`preg_split` failed with error ' . \preg_last_error());
}
return $result;
} else {
$length = \mb_strlen($string, $this->charset);
$result = [];
for ($i = 0; $i < $length; ++$i) {
$result[] = \mb_substr($string, $i, 1, $this->charset);
}
return $result;
}
} else {
if ($string === '') {
return [];
} else {
return \str_split($string);
}
$result = ($string !== '') ? \str_split($string) : [];
}

return $result;
}

/**
* @return int|false
* @return int<0, max>|false
*/
private function strpos(string $haystack, string $needle, int $offset)
{
if ($this->parserSettings->hasMultibyteSupport()) {
return \mb_strpos($haystack, $needle, $offset, $this->charset);
} else {
return \strpos($haystack, $needle, $offset);
}
return $this->parserSettings->hasMultibyteSupport()
? \mb_strpos($haystack, $needle, $offset, $this->charset)
: \strpos($haystack, $needle, $offset);
}
}