Skip to content

Commit 8bdf056

Browse files
committed
Leverage the match expression
1 parent 6e66c5b commit 8bdf056

File tree

3 files changed

+42
-79
lines changed

3 files changed

+42
-79
lines changed

Command/LintCommand.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,12 @@ private function validate(string $content, int $flags, string $file = null)
154154

155155
private function display(SymfonyStyle $io, array $files): int
156156
{
157-
switch ($this->format) {
158-
case 'txt':
159-
return $this->displayTxt($io, $files);
160-
case 'json':
161-
return $this->displayJson($io, $files);
162-
case 'github':
163-
return $this->displayTxt($io, $files, true);
164-
default:
165-
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
166-
}
157+
return match ($this->format) {
158+
'txt' => $this->displayTxt($io, $files),
159+
'json' => $this->displayJson($io, $files),
160+
'github' => $this->displayTxt($io, $files, true),
161+
default => throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format)),
162+
};
167163
}
168164

169165
private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false): int

Parser.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,25 +1043,14 @@ private function isStringUnIndentedCollectionItem(): bool
10431043
public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
10441044
{
10451045
if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
1046-
switch (preg_last_error()) {
1047-
case \PREG_INTERNAL_ERROR:
1048-
$error = 'Internal PCRE error.';
1049-
break;
1050-
case \PREG_BACKTRACK_LIMIT_ERROR:
1051-
$error = 'pcre.backtrack_limit reached.';
1052-
break;
1053-
case \PREG_RECURSION_LIMIT_ERROR:
1054-
$error = 'pcre.recursion_limit reached.';
1055-
break;
1056-
case \PREG_BAD_UTF8_ERROR:
1057-
$error = 'Malformed UTF-8 data.';
1058-
break;
1059-
case \PREG_BAD_UTF8_OFFSET_ERROR:
1060-
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
1061-
break;
1062-
default:
1063-
$error = 'Error.';
1064-
}
1046+
$error = match (preg_last_error()) {
1047+
\PREG_INTERNAL_ERROR => 'Internal PCRE error.',
1048+
\PREG_BACKTRACK_LIMIT_ERROR => 'pcre.backtrack_limit reached.',
1049+
\PREG_RECURSION_LIMIT_ERROR => 'pcre.recursion_limit reached.',
1050+
\PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data.',
1051+
\PREG_BAD_UTF8_OFFSET_ERROR => 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.',
1052+
default => 'Error.',
1053+
};
10651054

10661055
throw new ParseException($error);
10671056
}

Unescaper.php

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,56 +60,34 @@ public function unescapeDoubleQuotedString(string $value): string
6060
*/
6161
private function unescapeCharacter(string $value): string
6262
{
63-
switch ($value[1]) {
64-
case '0':
65-
return "\x0";
66-
case 'a':
67-
return "\x7";
68-
case 'b':
69-
return "\x8";
70-
case 't':
71-
return "\t";
72-
case "\t":
73-
return "\t";
74-
case 'n':
75-
return "\n";
76-
case 'v':
77-
return "\xB";
78-
case 'f':
79-
return "\xC";
80-
case 'r':
81-
return "\r";
82-
case 'e':
83-
return "\x1B";
84-
case ' ':
85-
return ' ';
86-
case '"':
87-
return '"';
88-
case '/':
89-
return '/';
90-
case '\\':
91-
return '\\';
92-
case 'N':
93-
// U+0085 NEXT LINE
94-
return "\xC2\x85";
95-
case '_':
96-
// U+00A0 NO-BREAK SPACE
97-
return "\xC2\xA0";
98-
case 'L':
99-
// U+2028 LINE SEPARATOR
100-
return "\xE2\x80\xA8";
101-
case 'P':
102-
// U+2029 PARAGRAPH SEPARATOR
103-
return "\xE2\x80\xA9";
104-
case 'x':
105-
return self::utf8chr(hexdec(substr($value, 2, 2)));
106-
case 'u':
107-
return self::utf8chr(hexdec(substr($value, 2, 4)));
108-
case 'U':
109-
return self::utf8chr(hexdec(substr($value, 2, 8)));
110-
default:
111-
throw new ParseException(sprintf('Found unknown escape character "%s".', $value));
112-
}
63+
return match ($value[1]) {
64+
'0' => "\x0",
65+
'a' => "\x7",
66+
'b' => "\x8",
67+
't' => "\t",
68+
"\t" => "\t",
69+
'n' => "\n",
70+
'v' => "\xB",
71+
'f' => "\xC",
72+
'r' => "\r",
73+
'e' => "\x1B",
74+
' ' => ' ',
75+
'"' => '"',
76+
'/' => '/',
77+
'\\' => '\\',
78+
// U+0085 NEXT LINE
79+
'N' => "\xC2\x85",
80+
// U+00A0 NO-BREAK SPACE
81+
'_' => "\xC2\xA0",
82+
// U+2028 LINE SEPARATOR
83+
'L' => "\xE2\x80\xA8",
84+
// U+2029 PARAGRAPH SEPARATOR
85+
'P' => "\xE2\x80\xA9",
86+
'x' => self::utf8chr(hexdec(substr($value, 2, 2))),
87+
'u' => self::utf8chr(hexdec(substr($value, 2, 4))),
88+
'U' => self::utf8chr(hexdec(substr($value, 2, 8))),
89+
default => throw new ParseException(sprintf('Found unknown escape character "%s".', $value)),
90+
};
11391
}
11492

11593
/**

0 commit comments

Comments
 (0)