Skip to content

Commit 85e70b7

Browse files
committed
[CLEANUP] Use null for unset value in DeclarationBlock::parse()
The variable is either a string or it isn't. The best practice for when it isn't is for it to be `null` or `unset()`. Using `false` for when it isn't can lead to type-safety issues. Reference: https://vulke.medium.com/when-should-variables-be-null-false-undefined-or-an-empty-string-in-php-4ebd73c7a954 Also use `===` in a comparison in the affected code. Resolves #975. Part of #976.
1 parent afdd72f commit 85e70b7

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

config/phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,6 @@ parameters:
330330
count: 1
331331
path: ../src/RuleSet/DeclarationBlock.php
332332

333-
-
334-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
335-
identifier: equal.notAllowed
336-
count: 1
337-
path: ../src/RuleSet/DeclarationBlock.php
338-
339333
-
340334
message: '#^Parameters should have "string" types as the only types passed to this method$#'
341335
identifier: typePerfect.narrowPublicClassMethodParamType

src/RuleSet/DeclarationBlock.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ public static function parse(ParserState $parserState, $list = null)
4545
$result = new DeclarationBlock($parserState->currentLine());
4646
try {
4747
$aSelectorParts = [];
48-
$stringWrapperCharacter = false;
4948
do {
5049
$aSelectorParts[] = $parserState->consume(1)
5150
. $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments);
5251
if (\in_array($parserState->peek(), ['\'', '"'], true) && \substr(\end($aSelectorParts), -1) != '\\') {
53-
if ($stringWrapperCharacter === false) {
52+
if (!isset($stringWrapperCharacter)) {
5453
$stringWrapperCharacter = $parserState->peek();
55-
} elseif ($stringWrapperCharacter == $parserState->peek()) {
56-
$stringWrapperCharacter = false;
54+
} elseif ($stringWrapperCharacter === $parserState->peek()) {
55+
unset($stringWrapperCharacter);
5756
}
5857
}
59-
} while (!\in_array($parserState->peek(), ['{', '}'], true) || $stringWrapperCharacter !== false);
58+
} while (!\in_array($parserState->peek(), ['{', '}'], true) || isset($stringWrapperCharacter));
6059
$result->setSelectors(\implode('', $aSelectorParts), $list);
6160
if ($parserState->comes('{')) {
6261
$parserState->consume(1);

0 commit comments

Comments
 (0)