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 Jan 13, 2023
1 parent 61d9122 commit bb1c7d9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function valueToNode(mixed $val, bool $blockMode = false): Node
} elseif ($val instanceof Entity) {
return new Node\EntityNode(
$this->valueToNode($val->value),
$this->arrayToNodes((array) $val->attributes),
$this->arrayToNodes($val->attributes),
);

} elseif (is_object($val) || is_array($val)) {
Expand Down
6 changes: 2 additions & 4 deletions src/Neon/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function tokenize(string $input): TokenStream
{
$input = str_replace("\r", '', $input);
$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 @@ -60,9 +60,7 @@ public function tokenize(string $input): TokenStream
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 === Token::Char) {
$type = $token[0];
Expand Down
2 changes: 1 addition & 1 deletion src/Neon/Node/LiteralNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function toString(): string

} elseif (is_float($this->value)) {
$res = json_encode($this->value);
return strpos($res, '.') === false ? $res . '.0' : $res;
return str_contains($res, '.') ? $res : $res . '.0';

} elseif (is_int($this->value) || is_bool($this->value) || $this->value === null) {
return json_encode($this->value);
Expand Down
8 changes: 2 additions & 6 deletions src/Neon/Node/StringNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ function (array $m): string {
if (isset(self::EscapeSequences[$sq[1]])) {
return self::EscapeSequences[$sq[1]];
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
if (($res = json_decode('"' . $sq . '"')) !== null) {
return $res;
}

throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
} elseif ($sq[1] === 'x' && strlen($sq) === 4) {
trigger_error("Neon: '$sq' is deprecated, use '\\uXXXX' instead.", E_USER_DEPRECATED);
return chr(hexdec(substr($sq, 2)));
Expand All @@ -76,7 +72,7 @@ function (array $m): string {

public function toString(): string
{
if (strpos($this->value, "\n") === false) {
if (!str_contains($this->value, "\n")) {
return "'" . str_replace("'", "''", $this->value) . "'";

} elseif (preg_match('~\n[\t ]+\'{3}~', $this->value)) {
Expand Down

0 comments on commit bb1c7d9

Please sign in to comment.