diff --git a/CHANGELOG.md b/CHANGELOG.md index e3aa7db1..3ef217d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ Please also have a look at our ### Fixed -- Use typesafe versions of PHP functions (#1368) +- Use typesafe versions of PHP functions (#1368, #1370) ### Documentation diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 74ad0efe..cc69ed97 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -7,6 +7,10 @@ use Sabberworm\CSS\Comment\Comment; use Sabberworm\CSS\Settings; +use function Safe\iconv; +use function Safe\preg_match; +use function Safe\preg_split; + /** * @internal since 8.7.0 */ @@ -63,8 +67,6 @@ public function __construct(string $text, Settings $parserSettings, int $lineNum /** * Sets the charset to be used if the CSS does not contain an `@charset` declaration. - * - * @throws SourceException if the charset is UTF-8 and the content has invalid byte sequences */ public function setCharset(string $charset): void { @@ -122,7 +124,7 @@ public function parseIdentifier(bool $ignoreCase = true): string } $character = null; while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) { - if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character)) { + if (preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character) !== 0) { $result .= $character; } else { $result .= '\\' . $character; @@ -146,13 +148,13 @@ public function parseCharacter(bool $isForIdentifier): ?string if ($this->comes('\\n') || $this->comes('\\r')) { return ''; } - if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) { + if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) { return $this->consume(1); } $hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6); if ($this->strlen($hexCodePoint) < 6) { // Consume whitespace after incomplete unicode escape - if (\preg_match('/\\s/isSu', $this->peek())) { + if (preg_match('/\\s/isSu', $this->peek()) !== 0) { if ($this->comes('\\r\\n')) { $this->consume(2); } else { @@ -166,7 +168,7 @@ public function parseCharacter(bool $isForIdentifier): ?string $utf32EncodedCharacter .= \chr($codePoint & 0xff); $codePoint = $codePoint >> 8; } - return \iconv('utf-32le', $this->charset, $utf32EncodedCharacter); + return iconv('utf-32le', $this->charset, $utf32EncodedCharacter); } if ($isForIdentifier) { $peek = \ord($this->peek()); @@ -198,7 +200,7 @@ public function consumeWhiteSpace(): array { $comments = []; do { - while (\preg_match('/\\s/isSu', $this->peek()) === 1) { + while (preg_match('/\\s/isSu', $this->peek()) === 1) { $this->consume(1); } if ($this->parserSettings->usesLenientParsing()) { @@ -291,7 +293,7 @@ public function consumeExpression(string $expression, ?int $maximumLength = null { $matches = null; $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft(); - if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) { + if (preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) { throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber); } @@ -437,17 +439,12 @@ private function strtolower(string $string): string /** * @return list - * - * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences */ private function strsplit(string $string): array { if ($this->parserSettings->hasMultibyteSupport()) { if ($this->streql($this->charset, 'utf-8')) { - $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); - if (!\is_array($result)) { - throw new SourceException('`preg_split` failed with error ' . \preg_last_error()); - } + $result = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); } else { $length = \mb_strlen($string, $this->charset); $result = [];