From a5223adfa513b5a1bcd80164be5e8dda56a7e793 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 23 Apr 2021 14:34:08 -0400 Subject: [PATCH] Refactor matches to be transpilable --- src/Enum/Highlight.php | 8 ++++++-- src/FileType.php | 4 +++- src/Row.php | 4 +++- src/Terminal.php | 4 +++- src/functions.php | 12 +++++++++--- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Enum/Highlight.php b/src/Enum/Highlight.php index 3bd1c54..341e61e 100644 --- a/src/Enum/Highlight.php +++ b/src/Enum/Highlight.php @@ -33,7 +33,7 @@ class Highlight { */ public static function fromPHPToken(int $token): int { - return match($token) { + $token = match($token) { // Delimiters T_ARRAY, T_CURLY_OPEN, @@ -186,6 +186,8 @@ class Highlight { default => Highlight::NORMAL, }; + + return $token; } /** @@ -197,7 +199,7 @@ class Highlight { */ public static function fromPHPChar(string $char): int { - return match ($char) { + $hl = match ($char) { // Delimiter characters '[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER, @@ -207,5 +209,7 @@ class Highlight { default => Highlight::NORMAL, }; + + return $hl; } } \ No newline at end of file diff --git a/src/FileType.php b/src/FileType.php index 0d6cf50..1c05fc4 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -26,7 +26,7 @@ class FileType { { $ext = strstr(basename($filename), '.'); $ext = ($ext !== FALSE) ? $ext : ''; - return match ($ext) { + $syntax = match ($ext) { '.sh', '.bash' => Syntax::new( 'Shell', [ @@ -127,6 +127,8 @@ class FileType { ), default => Syntax::default(), }; + + return $syntax; } private function __construct(public string $name, public Syntax $syntax) {} diff --git a/src/Row.php b/src/Row.php index 9a7aa77..0ace5c6 100644 --- a/src/Row.php +++ b/src/Row.php @@ -76,13 +76,15 @@ class Row { public function __get(string $name): mixed { - return match ($name) + $prop = match ($name) { 'size' => strlen($this->chars), 'rsize' => strlen($this->render), 'chars' => $this->chars, default => NULL, }; + + return $prop; } /** diff --git a/src/Terminal.php b/src/Terminal.php index b6e85ff..c58a237 100644 --- a/src/Terminal.php +++ b/src/Terminal.php @@ -87,7 +87,7 @@ class Terminal { { $c = Terminal::read(); - return match($c) + $key = match($c) { // Unambiguous mappings RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN, @@ -113,6 +113,8 @@ class Terminal { default => $c, }; + + return $key; } /** diff --git a/src/functions.php b/src/functions.php index bb7e437..820f278 100644 --- a/src/functions.php +++ b/src/functions.php @@ -76,7 +76,7 @@ function is_digit(string $char): bool */ function is_space(string $char): bool { - return match($char) { + $isSpace = match($char) { RawKeyCode::CARRIAGE_RETURN, RawKeyCode::FORM_FEED, RawKeyCode::NEWLINE, @@ -86,6 +86,8 @@ function is_space(string $char): bool default => false, }; + + return $isSpace; } // ---------------------------------------------------------------------------- @@ -158,7 +160,7 @@ function str_has(string $haystack, string $str, ?int $offset = NULL): bool */ function syntax_to_color(int $hl): int { - return match ($hl) + $color = match ($hl) { Highlight::COMMENT => Color::FG_CYAN, Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK, @@ -175,6 +177,8 @@ function syntax_to_color(int $hl): int Highlight::IDENTIFIER => Color::FG_BRIGHT_WHITE, default => Color::FG_WHITE, }; + + return $color; } /** @@ -191,7 +195,7 @@ function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string function error_code_name(int $code): string { - return match ($code) { + $errorName = match ($code) { E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', @@ -208,6 +212,8 @@ function error_code_name(int $code): string E_USER_DEPRECATED => 'User Deprecated', default => 'Unknown', }; + + return $errorName; } function saturating_add(int $a, int $b, int $max): int