Skip to content

Commit 09c8f83

Browse files
authored
[FEATURE] Add internal getters to Settings and use them (#1013)
Also make a type annotation more specific along the way.
1 parent e1f8bd3 commit 09c8f83

File tree

9 files changed

+54
-23
lines changed

9 files changed

+54
-23
lines changed

src/CSSList/CSSList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function parseList(ParserState $parserState, CSSList $list): void
7474
if (\is_string($parserState)) {
7575
$parserState = new ParserState($parserState, Settings::create());
7676
}
77-
$usesLenientParsing = $parserState->getSettings()->lenientParsing;
77+
$usesLenientParsing = $parserState->getSettings()->usesLenientParsing();
7878
$comments = [];
7979
while (!$parserState->isEnd()) {
8080
$comments = \array_merge($comments, $parserState->consumeWhiteSpace());
@@ -138,7 +138,7 @@ private static function parseListItem(ParserState $parserState, CSSList $list)
138138
return $atRule;
139139
} elseif ($parserState->comes('}')) {
140140
if ($isRoot) {
141-
if ($parserState->getSettings()->lenientParsing) {
141+
if ($parserState->getSettings()->usesLenientParsing()) {
142142
return DeclarationBlock::parse($parserState);
143143
} else {
144144
throw new SourceException('Unopened {', $parserState->currentLine());
@@ -215,7 +215,7 @@ private static function parseAtRule(ParserState $parserState)
215215
// Unknown other at rule (font-face or such)
216216
$arguments = \trim($parserState->consumeUntil('{', false, true));
217217
if (\substr_count($arguments, '(') != \substr_count($arguments, ')')) {
218-
if ($parserState->getSettings()->lenientParsing) {
218+
if ($parserState->getSettings()->usesLenientParsing()) {
219219
return null;
220220
} else {
221221
throw new SourceException('Unmatched brace count in media query', $parserState->currentLine());

src/Parsing/ParserState.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct($text, Settings $parserSettings, $lineNumber = 1)
5858
$this->parserSettings = $parserSettings;
5959
$this->text = $text;
6060
$this->lineNumber = $lineNumber;
61-
$this->setCharset($this->parserSettings->defaultCharset);
61+
$this->setCharset($this->parserSettings->getDefaultCharset());
6262
}
6363

6464
/**
@@ -151,7 +151,7 @@ public function parseCharacter($isForIdentifier)
151151
{
152152
if ($this->peek() === '\\') {
153153
if (
154-
$isForIdentifier && $this->parserSettings->lenientParsing
154+
$isForIdentifier && $this->parserSettings->usesLenientParsing()
155155
&& ($this->comes('\\0') || $this->comes('\\9'))
156156
) {
157157
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
@@ -215,7 +215,7 @@ public function consumeWhiteSpace(): array
215215
while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
216216
$this->consume(1);
217217
}
218-
if ($this->parserSettings->lenientParsing) {
218+
if ($this->parserSettings->usesLenientParsing()) {
219219
try {
220220
$comment = $this->consumeComment();
221221
} catch (UnexpectedEOFException $e) {
@@ -416,7 +416,7 @@ public function backtrack($numberOfCharacters): void
416416
*/
417417
public function strlen($string): int
418418
{
419-
if ($this->parserSettings->multibyteSupport) {
419+
if ($this->parserSettings->hasMultibyteSupport()) {
420420
return \mb_strlen($string, $this->charset);
421421
} else {
422422
return \strlen($string);
@@ -449,7 +449,7 @@ private function substr($offset, $length): string
449449
*/
450450
private function strtolower($string): string
451451
{
452-
if ($this->parserSettings->multibyteSupport) {
452+
if ($this->parserSettings->hasMultibyteSupport()) {
453453
return \mb_strtolower($string, $this->charset);
454454
} else {
455455
return \strtolower($string);
@@ -465,7 +465,7 @@ private function strtolower($string): string
465465
*/
466466
private function strsplit($string)
467467
{
468-
if ($this->parserSettings->multibyteSupport) {
468+
if ($this->parserSettings->hasMultibyteSupport()) {
469469
if ($this->streql($this->charset, 'utf-8')) {
470470
$result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
471471
if (!\is_array($result)) {
@@ -498,7 +498,7 @@ private function strsplit($string)
498498
*/
499499
private function strpos($haystack, $needle, $offset)
500500
{
501-
if ($this->parserSettings->multibyteSupport) {
501+
if ($this->parserSettings->hasMultibyteSupport()) {
502502
return \mb_strpos($haystack, $needle, $offset, $this->charset);
503503
} else {
504504
return \strpos($haystack, $needle, $offset);

src/Rule/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function parse(ParserState $parserState): Rule
9191
$parserState->consume(':');
9292
$value = Value::parseValue($parserState, self::listDelimiterForRule($rule->getRule()));
9393
$rule->setValue($value);
94-
if ($parserState->getSettings()->lenientParsing) {
94+
if ($parserState->getSettings()->usesLenientParsing()) {
9595
while ($parserState->comes('\\')) {
9696
$parserState->consume('\\');
9797
$rule->addIeHack($parserState->consume());

src/RuleSet/DeclarationBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function parse(ParserState $parserState, $list = null)
6161
$parserState->consume(1);
6262
}
6363
} catch (UnexpectedTokenException $e) {
64-
if ($parserState->getSettings()->lenientParsing) {
64+
if ($parserState->getSettings()->usesLenientParsing()) {
6565
if (!$parserState->comes('}')) {
6666
$parserState->consumeUntil('}', false, true);
6767
}

src/RuleSet/RuleSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
6464
}
6565
while (!$parserState->comes('}')) {
6666
$rule = null;
67-
if ($parserState->getSettings()->lenientParsing) {
67+
if ($parserState->getSettings()->usesLenientParsing()) {
6868
try {
6969
$rule = Rule::parse($parserState);
7070
} catch (UnexpectedTokenException $e) {

src/Settings.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Settings
2626
/**
2727
* The default charset for the CSS if no `@charset` declaration is found. Defaults to utf-8.
2828
*
29-
* @var string
29+
* @var non-empty-string
3030
*
3131
* @internal since 8.8.0, will be made private in 9.0.0
3232
*/
@@ -62,17 +62,21 @@ public static function create(): self
6262
public function withMultibyteSupport(bool $multibyteSupport = true): self
6363
{
6464
$this->multibyteSupport = $multibyteSupport;
65+
6566
return $this;
6667
}
6768

6869
/**
6970
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
7071
*
72+
* @param non-empty-string $defaultCharset
73+
*
7174
* @return $this fluent interface
7275
*/
7376
public function withDefaultCharset(string $defaultCharset): self
7477
{
7578
$this->defaultCharset = $defaultCharset;
79+
7680
return $this;
7781
}
7882

@@ -84,6 +88,7 @@ public function withDefaultCharset(string $defaultCharset): self
8488
public function withLenientParsing(bool $usesLenientParsing = true): self
8589
{
8690
$this->lenientParsing = $usesLenientParsing;
91+
8792
return $this;
8893
}
8994

@@ -96,4 +101,30 @@ public function beStrict(): self
96101
{
97102
return $this->withLenientParsing(false);
98103
}
104+
105+
/**
106+
* @internal
107+
*/
108+
public function hasMultibyteSupport(): bool
109+
{
110+
return $this->multibyteSupport;
111+
}
112+
113+
/**
114+
* @return non-empty-string
115+
*
116+
* @internal
117+
*/
118+
public function getDefaultCharset(): string
119+
{
120+
return $this->defaultCharset;
121+
}
122+
123+
/**
124+
* @internal
125+
*/
126+
public function usesLenientParsing(): bool
127+
{
128+
return $this->lenientParsing;
129+
}
99130
}

src/Value/LineName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function parse(ParserState $parserState): LineName
3232
$parserState->consumeWhiteSpace();
3333
$aNames = [];
3434
do {
35-
if ($parserState->getSettings()->lenientParsing) {
35+
if ($parserState->getSettings()->usesLenientParsing()) {
3636
try {
3737
$aNames[] = $parserState->parseIdentifier();
3838
} catch (UnexpectedTokenException $e) {

src/Value/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static function parsePrimitiveValue(ParserState $parserState)
166166
$oValue = Color::parse($parserState);
167167
} elseif ($parserState->comes("'") || $parserState->comes('"')) {
168168
$oValue = CSSString::parse($parserState);
169-
} elseif ($parserState->comes('progid:') && $parserState->getSettings()->lenientParsing) {
169+
} elseif ($parserState->comes('progid:') && $parserState->getSettings()->usesLenientParsing()) {
170170
$oValue = self::parseMicrosoftFilter($parserState);
171171
} elseif ($parserState->comes('[')) {
172172
$oValue = LineName::parse($parserState);

tests/Unit/SettingsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function createReturnsANewInstanceForEachCall(): void
4848
*/
4949
public function multibyteSupportByDefaultStateOfMbStringExtension(): void
5050
{
51-
self::assertSame(\extension_loaded('mbstring'), $this->subject->multibyteSupport);
51+
self::assertSame(\extension_loaded('mbstring'), $this->subject->hasMultibyteSupport());
5252
}
5353

5454
/**
@@ -78,15 +78,15 @@ public function withMultibyteSupportSetsMultibyteSupport(bool $value): void
7878
{
7979
$this->subject->withMultibyteSupport($value);
8080

81-
self::assertSame($value, $this->subject->multibyteSupport);
81+
self::assertSame($value, $this->subject->hasMultibyteSupport());
8282
}
8383

8484
/**
8585
* @test
8686
*/
8787
public function defaultCharsetByDefaultIsUtf8(): void
8888
{
89-
self::assertSame('utf-8', $this->subject->defaultCharset);
89+
self::assertSame('utf-8', $this->subject->getDefaultCharset());
9090
}
9191

9292
/**
@@ -105,15 +105,15 @@ public function withDefaultCharsetSetsDefaultCharset(): void
105105
$charset = 'ISO-8859-1';
106106
$this->subject->withDefaultCharset($charset);
107107

108-
self::assertSame($charset, $this->subject->defaultCharset);
108+
self::assertSame($charset, $this->subject->getDefaultCharset());
109109
}
110110

111111
/**
112112
* @test
113113
*/
114114
public function lenientParsingByDefaultIsTrue(): void
115115
{
116-
self::assertTrue($this->subject->lenientParsing);
116+
self::assertTrue($this->subject->usesLenientParsing());
117117
}
118118

119119
/**
@@ -132,7 +132,7 @@ public function withLenientParsingSetsLenientParsing(bool $value): void
132132
{
133133
$this->subject->withLenientParsing($value);
134134

135-
self::assertSame($value, $this->subject->lenientParsing);
135+
self::assertSame($value, $this->subject->usesLenientParsing());
136136
}
137137

138138
/**
@@ -150,6 +150,6 @@ public function beStrictSetsLenientParsingToFalse(): void
150150
{
151151
$this->subject->beStrict();
152152

153-
self::assertFalse($this->subject->lenientParsing);
153+
self::assertFalse($this->subject->usesLenientParsing());
154154
}
155155
}

0 commit comments

Comments
 (0)