From 85e70b7ab44d863885ffb0b8d37c98f1d79246dd Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Tue, 25 Feb 2025 01:32:46 +0000 Subject: [PATCH] [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. --- config/phpstan-baseline.neon | 6 ------ src/RuleSet/DeclarationBlock.php | 9 ++++----- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 4150a6e8..3f311ad1 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -330,12 +330,6 @@ parameters: count: 1 path: ../src/RuleSet/DeclarationBlock.php - - - message: '#^Loose comparison via "\=\=" is not allowed\.$#' - identifier: equal.notAllowed - count: 1 - path: ../src/RuleSet/DeclarationBlock.php - - message: '#^Parameters should have "string" types as the only types passed to this method$#' identifier: typePerfect.narrowPublicClassMethodParamType diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 5d025898..4dc0ce2d 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -45,18 +45,17 @@ public static function parse(ParserState $parserState, $list = null) $result = new DeclarationBlock($parserState->currentLine()); try { $aSelectorParts = []; - $stringWrapperCharacter = false; do { $aSelectorParts[] = $parserState->consume(1) . $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments); if (\in_array($parserState->peek(), ['\'', '"'], true) && \substr(\end($aSelectorParts), -1) != '\\') { - if ($stringWrapperCharacter === false) { + if (!isset($stringWrapperCharacter)) { $stringWrapperCharacter = $parserState->peek(); - } elseif ($stringWrapperCharacter == $parserState->peek()) { - $stringWrapperCharacter = false; + } elseif ($stringWrapperCharacter === $parserState->peek()) { + unset($stringWrapperCharacter); } } - } while (!\in_array($parserState->peek(), ['{', '}'], true) || $stringWrapperCharacter !== false); + } while (!\in_array($parserState->peek(), ['{', '}'], true) || isset($stringWrapperCharacter)); $result->setSelectors(\implode('', $aSelectorParts), $list); if ($parserState->comes('{')) { $parserState->consume(1);