Skip to content

Commit 949d1df

Browse files
committed
Optimize isString
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 98b704c commit 949d1df

File tree

4 files changed

+9
-21
lines changed

4 files changed

+9
-21
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@
232232
<code><![CDATA[! $flags]]></code>
233233
<code><![CDATA[! $flags]]></code>
234234
<code><![CDATA[! $flags]]></code>
235-
<code><![CDATA[! $flags]]></code>
236235
<code><![CDATA[Context::isComment($token)]]></code>
237236
<code><![CDATA[Context::isComment($token)]]></code>
238237
<code><![CDATA[Context::isComment($token, $end)]]></code>

src/Context.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -469,25 +469,17 @@ public static function isSymbol(string $string): int|null
469469
/**
470470
* Checks if the given character is the beginning of a string.
471471
*
472-
* @param string $string string to be checked
472+
* @param string $character a character to be checked
473473
*
474474
* @return int|null the appropriate flag for the string type
475475
*/
476-
public static function isString(string $string): int|null
476+
public static function isString(string $character): int|null
477477
{
478-
if ($string === '') {
479-
return null;
480-
}
481-
482-
if (str_starts_with($string, '\'')) {
483-
return Token::FLAG_STRING_SINGLE_QUOTES;
484-
}
485-
486-
if (str_starts_with($string, '"')) {
487-
return Token::FLAG_STRING_DOUBLE_QUOTES;
488-
}
489-
490-
return null;
478+
return match ($character) {
479+
'\'' => Token::FLAG_STRING_SINGLE_QUOTES,
480+
'"' => Token::FLAG_STRING_DOUBLE_QUOTES,
481+
default => null,
482+
};
491483
}
492484

493485
/**

src/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ public function parseString(string $quote = ''): Token|null
868868
$token = $this->str[$this->last];
869869
$flags = Context::isString($token);
870870

871-
if (! $flags && $token !== $quote) {
871+
if ($flags === null && $token !== $quote) {
872872
return null;
873873
}
874874

tests/Lexer/IsMethodsTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,8 @@ public function testIsString(): void
107107
$this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'"));
108108
$this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"'));
109109

110-
$this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'foo bar'"));
111-
$this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"foo bar"'));
112-
113110
$this->assertNull(Context::isString(''));
114-
$this->assertNull(Context::isString('foo bar'));
111+
$this->assertNull(Context::isString('f'));
115112
}
116113

117114
public function testIsSymbol(): void

0 commit comments

Comments
 (0)