Skip to content

Commit 0e979c8

Browse files
committed
Optimize isSymbol
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 949d1df commit 0e979c8

File tree

4 files changed

+11
-24
lines changed

4 files changed

+11
-24
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@
229229
<code><![CDATA[$keywordToken->value]]></code>
230230
</PropertyTypeCoercion>
231231
<RiskyTruthyFalsyComparison>
232-
<code><![CDATA[! $flags]]></code>
233232
<code><![CDATA[! $flags]]></code>
234233
<code><![CDATA[! $flags]]></code>
235234
<code><![CDATA[Context::isComment($token)]]></code>

src/Context.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -445,25 +445,14 @@ public static function isNumber(string $string): bool
445445
*
446446
* @return int|null the appropriate flag for the symbol type
447447
*/
448-
public static function isSymbol(string $string): int|null
448+
public static function isSymbol(string $character): int|null
449449
{
450-
if ($string === '') {
451-
return null;
452-
}
453-
454-
if (str_starts_with($string, '@')) {
455-
return Token::FLAG_SYMBOL_VARIABLE;
456-
}
457-
458-
if (str_starts_with($string, '`')) {
459-
return Token::FLAG_SYMBOL_BACKTICK;
460-
}
461-
462-
if (str_starts_with($string, ':') || str_starts_with($string, '?')) {
463-
return Token::FLAG_SYMBOL_PARAMETER;
464-
}
465-
466-
return null;
450+
return match ($character) {
451+
'@' => Token::FLAG_SYMBOL_VARIABLE,
452+
'`' => Token::FLAG_SYMBOL_BACKTICK,
453+
':', '?' => Token::FLAG_SYMBOL_PARAMETER,
454+
default => null,
455+
};
467456
}
468457

469458
/**

src/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ public function parseSymbol(): Token|null
918918
$token = $this->str[$this->last];
919919
$flags = Context::isSymbol($token);
920920

921-
if (! $flags) {
921+
if ($flags === null) {
922922
return null;
923923
}
924924

tests/Lexer/IsMethodsTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ public function testIsSymbol(): void
115115
{
116116
$this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@'));
117117
$this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`'));
118-
119-
$this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id'));
120-
$this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`'));
118+
$this->assertEquals(Token::FLAG_SYMBOL_PARAMETER, Context::isSymbol(':'));
119+
$this->assertEquals(Token::FLAG_SYMBOL_PARAMETER, Context::isSymbol('?'));
121120

122121
$this->assertNull(Context::isSymbol(''));
123-
$this->assertNull(Context::isSymbol('id'));
122+
$this->assertNull(Context::isSymbol('i'));
124123
}
125124

126125
public function testisSeparator(): void

0 commit comments

Comments
 (0)