Skip to content

Commit ff247ef

Browse files
oliverkleeJakeQZ
andauthored
[TASK] Use native type declarations in ParserState (#1136)
Part of #811 Co-authored-by: JakeQZ <[email protected]>
1 parent db8cad1 commit ff247ef

File tree

3 files changed

+30
-80
lines changed

3 files changed

+30
-80
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Please also have a look at our
3232
- Make all non-private properties `@internal` (#886)
3333
- Use more native type declarations and strict mode
3434
(#641, #772, #774, #778, #804, #841, #873, #875, #891, #922, #923, #933, #958,
35-
#964, #967, #1000, #1044, #1134, #1137, #1139)
35+
#964, #967, #1000, #1044, #1134, #1136, #1137, #1139)
3636
- Add visibility to all class/interface constants (#469)
3737

3838
### Deprecated

config/phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,6 @@ parameters:
114114
count: 1
115115
path: ../src/Parsing/ParserState.php
116116

117-
-
118-
message: '#^Parameters should have "bool" types as the only types passed to this method$#'
119-
identifier: typePerfect.narrowPublicClassMethodParamType
120-
count: 2
121-
path: ../src/Parsing/ParserState.php
122-
123-
-
124-
message: '#^Parameters should have "int" types as the only types passed to this method$#'
125-
identifier: typePerfect.narrowPublicClassMethodParamType
126-
count: 2
127-
path: ../src/Parsing/ParserState.php
128-
129-
-
130-
message: '#^Parameters should have "string\|int\|null" types as the only types passed to this method$#'
131-
identifier: typePerfect.narrowPublicClassMethodParamType
132-
count: 1
133-
path: ../src/Parsing/ParserState.php
134-
135117
-
136118
message: '#^Cannot call method render\(\) on string\.$#'
137119
identifier: method.nonObject

src/Parsing/ParserState.php

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ParserState
3333
private $characters;
3434

3535
/**
36-
* @var int
36+
* @var int<0, max>
3737
*/
3838
private $currentPosition = 0;
3939

@@ -53,7 +53,7 @@ class ParserState
5353
* @param string $text the complete CSS as text (i.e., usually the contents of a CSS file)
5454
* @param int<1, max> $lineNumber
5555
*/
56-
public function __construct($text, Settings $parserSettings, int $lineNumber = 1)
56+
public function __construct(string $text, Settings $parserSettings, int $lineNumber = 1)
5757
{
5858
$this->parserSettings = $parserSettings;
5959
$this->text = $text;
@@ -75,23 +75,20 @@ public function setCharset(string $charset): void
7575
/**
7676
* @return int<1, max>
7777
*/
78-
public function currentLine()
78+
public function currentLine(): int
7979
{
8080
return $this->lineNumber;
8181
}
8282

8383
/**
84-
* @return int
84+
* @return int<0, max>
8585
*/
86-
public function currentColumn()
86+
public function currentColumn(): int
8787
{
8888
return $this->currentPosition;
8989
}
9090

91-
/**
92-
* @return Settings
93-
*/
94-
public function getSettings()
91+
public function getSettings(): Settings
9592
{
9693
return $this->parserSettings;
9794
}
@@ -102,21 +99,17 @@ public function anchor(): Anchor
10299
}
103100

104101
/**
105-
* @param int $position
102+
* @param int<0, max> $position
106103
*/
107-
public function setPosition($position): void
104+
public function setPosition(int $position): void
108105
{
109106
$this->currentPosition = $position;
110107
}
111108

112109
/**
113-
* @param bool $ignoreCase
114-
*
115-
* @return string
116-
*
117110
* @throws UnexpectedTokenException
118111
*/
119-
public function parseIdentifier($ignoreCase = true)
112+
public function parseIdentifier(bool $ignoreCase = true): string
120113
{
121114
if ($this->isEnd()) {
122115
throw new UnexpectedEOFException('', '', 'identifier', $this->lineNumber);
@@ -140,14 +133,10 @@ public function parseIdentifier($ignoreCase = true)
140133
}
141134

142135
/**
143-
* @param bool $isForIdentifier
144-
*
145-
* @return string|null
146-
*
147136
* @throws UnexpectedEOFException
148137
* @throws UnexpectedTokenException
149138
*/
150-
public function parseCharacter($isForIdentifier)
139+
public function parseCharacter(bool $isForIdentifier): ?string
151140
{
152141
if ($this->peek() === '\\') {
153142
$this->consume('\\');
@@ -225,11 +214,7 @@ public function consumeWhiteSpace(): array
225214
return $comments;
226215
}
227216

228-
/**
229-
* @param string $string
230-
* @param bool $caseInsensitive
231-
*/
232-
public function comes($string, $caseInsensitive = false): bool
217+
public function comes(string $string, bool $caseInsensitive = false): bool
233218
{
234219
$peek = $this->peek(\strlen($string));
235220
return ($peek == '')
@@ -238,10 +223,10 @@ public function comes($string, $caseInsensitive = false): bool
238223
}
239224

240225
/**
241-
* @param int $length
242-
* @param int $offset
226+
* @param int<1, max> $length
227+
* @param int<0, max> $offset
243228
*/
244-
public function peek($length = 1, $offset = 0): string
229+
public function peek(int $length = 1, int $offset = 0): string
245230
{
246231
$offset += $this->currentPosition;
247232
if ($offset >= \count($this->characters)) {
@@ -251,7 +236,7 @@ public function peek($length = 1, $offset = 0): string
251236
}
252237

253238
/**
254-
* @param int $value
239+
* @param string|int<1, max> $value
255240
*
256241
* @throws UnexpectedEOFException
257242
* @throws UnexpectedTokenException
@@ -286,12 +271,12 @@ public function consume($value = 1): string
286271

287272
/**
288273
* @param string $expression
289-
* @param int|null $maximumLength
274+
* @param int<1, max>|null $maximumLength
290275
*
291276
* @throws UnexpectedEOFException
292277
* @throws UnexpectedTokenException
293278
*/
294-
public function consumeExpression($expression, $maximumLength = null): string
279+
public function consumeExpression(string $expression, ?int $maximumLength = null): string
295280
{
296281
$matches = null;
297282
$input = $maximumLength !== null ? $this->peek($maximumLength) : $this->inputLeft();
@@ -335,17 +320,15 @@ public function isEnd(): bool
335320

336321
/**
337322
* @param array<array-key, string>|string $stopCharacters
338-
* @param bool $includeEnd
339-
* @param bool $consumeEnd
340323
* @param array<int, Comment> $comments
341324
*
342325
* @throws UnexpectedEOFException
343326
* @throws UnexpectedTokenException
344327
*/
345328
public function consumeUntil(
346329
$stopCharacters,
347-
$includeEnd = false,
348-
$consumeEnd = false,
330+
bool $includeEnd = false,
331+
bool $consumeEnd = false,
349332
array &$comments = []
350333
): string {
351334
$stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
@@ -386,12 +369,7 @@ private function inputLeft(): string
386369
return $this->substr($this->currentPosition, -1);
387370
}
388371

389-
/**
390-
* @param string $string1
391-
* @param string $string2
392-
* @param bool $caseInsensitive
393-
*/
394-
public function streql($string1, $string2, $caseInsensitive = true): bool
372+
public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
395373
{
396374
if ($caseInsensitive) {
397375
return $this->strtolower($string1) === $this->strtolower($string2);
@@ -401,17 +379,17 @@ public function streql($string1, $string2, $caseInsensitive = true): bool
401379
}
402380

403381
/**
404-
* @param int $numberOfCharacters
382+
* @param int<1, max> $numberOfCharacters
405383
*/
406-
public function backtrack($numberOfCharacters): void
384+
public function backtrack(int $numberOfCharacters): void
407385
{
408386
$this->currentPosition -= $numberOfCharacters;
409387
}
410388

411389
/**
412-
* @param string $string
390+
* @return int<0, max>
413391
*/
414-
public function strlen($string): int
392+
public function strlen(string $string): int
415393
{
416394
if ($this->parserSettings->hasMultibyteSupport()) {
417395
return \mb_strlen($string, $this->charset);
@@ -421,10 +399,9 @@ public function strlen($string): int
421399
}
422400

423401
/**
424-
* @param int $offset
425-
* @param int $length
402+
* @param int<0, max> $offset
426403
*/
427-
private function substr($offset, $length): string
404+
private function substr(int $offset, int $length): string
428405
{
429406
if ($length < 0) {
430407
$length = \count($this->characters) - $offset + $length;
@@ -441,10 +418,7 @@ private function substr($offset, $length): string
441418
return $result;
442419
}
443420

444-
/**
445-
* @param string $string
446-
*/
447-
private function strtolower($string): string
421+
private function strtolower(string $string): string
448422
{
449423
if ($this->parserSettings->hasMultibyteSupport()) {
450424
return \mb_strtolower($string, $this->charset);
@@ -454,13 +428,11 @@ private function strtolower($string): string
454428
}
455429

456430
/**
457-
* @param string $string
458-
*
459431
* @return array<int, string>
460432
*
461433
* @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
462434
*/
463-
private function strsplit($string)
435+
private function strsplit(string $string): array
464436
{
465437
if ($this->parserSettings->hasMultibyteSupport()) {
466438
if ($this->streql($this->charset, 'utf-8')) {
@@ -487,13 +459,9 @@ private function strsplit($string)
487459
}
488460

489461
/**
490-
* @param string $haystack
491-
* @param string $needle
492-
* @param int $offset
493-
*
494462
* @return int|false
495463
*/
496-
private function strpos($haystack, $needle, $offset)
464+
private function strpos(string $haystack, string $needle, int $offset)
497465
{
498466
if ($this->parserSettings->hasMultibyteSupport()) {
499467
return \mb_strpos($haystack, $needle, $offset, $this->charset);

0 commit comments

Comments
 (0)