Skip to content

Commit

Permalink
used PHP 8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 12, 2021
1 parent 615d283 commit aa03eb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function encode($var, int $flags = 0): string
foreach ($var as $k => $v) {
$v = $this->encode($v, self::BLOCK);
$s .= ($isList ? '-' : $this->encode($k) . ':')
. (strpos($v, "\n") === false
? ' ' . $v . "\n"
: "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n"));
. (str_contains($v, "\n")
? "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n")
: ' ' . $v . "\n");
}
return $s;

Expand All @@ -77,7 +77,7 @@ public function encode($var, int $flags = 0): string
if ($res === false) {
throw new Exception('Invalid UTF-8 sequence: ' . $var);
}
if (strpos($var, "\n") !== false) {
if (str_contains($var, "\n")) {
$res = preg_replace_callback(
'#[^\\\\]|\\\\(.)#s',
fn($m) => ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0],
Expand All @@ -89,7 +89,7 @@ public function encode($var, int $flags = 0): string

} elseif (is_float($var)) {
$var = json_encode($var);
return strpos($var, '.') === false ? $var . '.0' : $var;
return str_contains($var, '.') ? $var : $var . '.0';

} else {
return json_encode($var);
Expand Down
6 changes: 2 additions & 4 deletions src/Neon/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
public static function tokenize(string $input): array
{
$pattern = '~(' . implode(')|(', self::PATTERNS) . ')~Amixu';
$res = preg_match_all($pattern, $input, $tokens, PREG_SET_ORDER);
$res = preg_match_all($pattern, $input, $tokens, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
if ($res === false) {
throw new Exception('Invalid UTF-8 sequence.');
}
Expand All @@ -76,9 +76,7 @@ public static function tokenize(string $input): array
foreach ($tokens as &$token) {
$type = null;
for ($i = 1; $i <= count($types); $i++) {
if (!isset($token[$i])) {
break;
} elseif ($token[$i] !== '') {
if (isset($token[$i])) {
$type = $types[$i - 1];
if ($type === self::T_CHAR) {
$type = $token[0];
Expand Down

0 comments on commit aa03eb9

Please sign in to comment.