From 11932a471b4ef10faf76b6fcfe30a35f946eb7ab Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 13 Sep 2020 21:44:30 +0200 Subject: Use short array syntax We can use it now that we switched to PHP 7.3 --- .php_cs.dist | 1 + example/index.php | 24 ++++---- src/ste/Calc.php | 30 +++++----- src/ste/Parser.php | 28 ++++----- src/ste/STECore.php | 6 +- src/ste/STEStandardLibrary.php | 2 +- src/ste/Scope.php | 8 +-- src/ste/TagNode.php | 4 +- src/ste/Transcompiler.php | 68 +++++++++++----------- src/ste/VariableNode.php | 2 +- tests/functional/test_array/Test.php | 12 ++-- tests/functional/test_foreach/Test.php | 10 ++-- .../test_short_for_additional_attribs/Test.php | 10 ++-- .../test_static_numeric_array_access/Test.php | 4 +- tests/functional/test_tagname/Test.php | 4 +- .../test_trailing_closing_array_bracket/Test.php | 4 +- 16 files changed, 109 insertions(+), 108 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 314dfd7..9607d0e 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -7,6 +7,7 @@ return PhpCsFixer\Config::create() ->setRules([ '@PSR1' => true, '@PSR2' => true, + 'array_syntax' => ['syntax' => 'short'], ]) ->setFinder($finder) ; diff --git a/example/index.php b/example/index.php index caf57eb..0213c34 100644 --- a/example/index.php +++ b/example/index.php @@ -45,22 +45,22 @@ $ste->register_tag( ); # assign some data -$ste->vars["users"] = array( - array("name" => "Foo", "username" => "foo", "online" => true), - array("name" => "Bar", "username" => "bar", "online" => false), - array("name" => "Baz", "username" => "baz", "online" => true) -); +$ste->vars["users"] = [ + ["name" => "Foo", "username" => "foo", "online" => true], + ["name" => "Bar", "username" => "bar", "online" => false], + ["name" => "Baz", "username" => "baz", "online" => true] +]; $ste->vars["title"] = "cool"; -$ste->vars["articles"] = array( - array("author" => "foo", "title" => "cool article", "timestamp" => 1316553353, "excerpt" => "bla", "full" => "blablabla"), - array("author" => "bar", "title" => "awesome", "timestamp" => 1316552000, "excerpt" => "...", "full" => ".........."), - array("author" => "baz", "title" => " 1316551000, "excerpt" => "...", "full" => ".........."), - array("author" => "baz", "title" => "whatever...", "timestamp" => 1316550000, "excerpt" => "...", "full" => "..........") -); +$ste->vars["articles"] = [ + ["author" => "foo", "title" => "cool article", "timestamp" => 1316553353, "excerpt" => "bla", "full" => "blablabla"], + ["author" => "bar", "title" => "awesome", "timestamp" => 1316552000, "excerpt" => "...", "full" => ".........."], + ["author" => "baz", "title" => " 1316551000, "excerpt" => "...", "full" => ".........."], + ["author" => "baz", "title" => "whatever...", "timestamp" => 1316550000, "excerpt" => "...", "full" => ".........."] +]; $ste->vars["foo"] = "baz"; $ste->vars["bar"] = "lol"; -$ste->vars["baz"] = array("lol" => "cool"); +$ste->vars["baz"] = ["lol" => "cool"]; # Execute the template and output the result echo $ste->exectemplate("articles.html"); diff --git a/src/ste/Calc.php b/src/ste/Calc.php index 2f2d09d..9030c08 100644 --- a/src/ste/Calc.php +++ b/src/ste/Calc.php @@ -22,27 +22,27 @@ class Calc */ private static function shunting_yard($infix_math) { - $operators = array( - "+" => array("l", 2), - "-" => array("l", 2), - "*" => array("l", 3), - "/" => array("l", 3), - "^" => array("r", 4), - "_" => array("r", 5), - "(" => array("", 0), - ")" => array("", 0) - ); + $operators = [ + "+" => ["l", 2], + "-" => ["l", 2], + "*" => ["l", 3], + "/" => ["l", 3], + "^" => ["r", 4], + "_" => ["r", 5], + "(" => ["", 0], + ")" => ["", 0] + ]; preg_match_all("/\s*(?:(?:[+\\-\\*\\/\\^\\(\\)])|(\\d*[\\.]?\\d*))\\s*/s", $infix_math, $tokens, PREG_PATTERN_ORDER); $tokens_raw = array_filter(array_map('trim', $tokens[0]), function ($x) { return ($x === "0") || (!empty($x)); }); - $output_queue = array(); - $op_stack = array(); + $output_queue = []; + $op_stack = []; $lastpriority = null; /* Make - unary, if neccessary */ - $tokens = array(); + $tokens = []; foreach ($tokens_raw as $token) { $priority = isset($operators[$token]) ? $operators[$token][1] : -1; if (($token == "-") && (($lastpriority === null) || ($lastpriority >= 0))) { @@ -114,7 +114,7 @@ class Calc */ private static function pop2(&$array) { - $rv = array(array_pop($array), array_pop($array)); + $rv = [array_pop($array), array_pop($array)]; if (array_search(null, $rv, true) !== false) { throw new RuntimeError("Not enough numbers on stack. Invalid formula."); } @@ -128,7 +128,7 @@ class Calc */ private static function calc_rpn($rpn) { - $stack = array(); + $stack = []; foreach ($rpn as $token) { switch ($token) { case "+": diff --git a/src/ste/Parser.php b/src/ste/Parser.php index e786396..399ed75 100644 --- a/src/ste/Parser.php +++ b/src/ste/Parser.php @@ -114,7 +114,7 @@ class Parser $this->off = $minoff + (($which === null) ? 0 : mb_strlen((string) $needles[$which])); - return array($which, $minoff, mb_substr($this->text, $oldoff, $minoff - $oldoff), $oldoff); + return [$which, $minoff, mb_substr($this->text, $oldoff, $minoff - $oldoff), $oldoff]; } /** @@ -128,11 +128,11 @@ class Parser $off = $this->search_off($needle); if ($off === false) { $this->off = $this->len; - return array(false, mb_substr($this->text, $oldoff), $oldoff); + return [false, mb_substr($this->text, $oldoff), $oldoff]; } $this->off = $off + mb_strlen($needle); - return array($off, mb_substr($this->text, $oldoff, $off - $oldoff), $oldoff); + return [$off, mb_substr($this->text, $oldoff, $off - $oldoff), $oldoff]; } /** @@ -200,7 +200,7 @@ class Parser */ private static function combine_consecutive_text(array $ast) { - $out = array(); + $out = []; /** @var TextNode|null $last_text */ $last_text = null; @@ -244,9 +244,9 @@ class Parser foreach ($ast as $node) { if ($node instanceof TagNode) { $node->sub = self::tidyup_ast($node->sub); - $node->params = array_map(array(__CLASS__, 'tidyup_ast'), $node->params); + $node->params = array_map([__CLASS__, 'tidyup_ast'], $node->params); } elseif ($node instanceof VariableNode) { - $node->arrayfields = array_map(array(__CLASS__, 'tidyup_ast'), $node->arrayfields); + $node->arrayfields = array_map([__CLASS__, 'tidyup_ast'], $node->arrayfields); } } @@ -268,16 +268,16 @@ class Parser */ private function parse_text($escapes, $flags, $breakon = null, $separator = null, $nullaction = null, $opentag = null, $openedat = -1) { - $elems = array(); - $astlist = array(); + $elems = []; + $astlist = []; - $needles = array( + $needles = [ "commentopen" => "", "rawopen" => "", "escape" => '\\', "varcurlyopen" => '${', "var" => '$', - ); + ]; if ($flags & self::PARSE_TAG) { $needles["tagopen"] = 'parse_var($off, true); @@ -463,7 +463,7 @@ class Parser { $tplname = $this->name; - $arrayfields = array(); + $arrayfields = []; while ($this->peek() == "[") { $this->next(); @@ -498,8 +498,8 @@ class Parser $this->skip_ws(); $tag = new TagNode($this->name, $openedat); $name = $tag->name = $this->get_name(); - $tag->params = array(); - $tag->sub = array(); + $tag->params = []; + $tag->sub = []; for (;;) { $this->skip_ws(); diff --git a/src/ste/STECore.php b/src/ste/STECore.php index f00a4f4..9274587 100644 --- a/src/ste/STECore.php +++ b/src/ste/STECore.php @@ -63,10 +63,10 @@ class STECore $this->storage_access = $storage_access; $this->cur_tpl_dir = "/"; STEStandardLibrary::_register_lib($this); - $this->blockorder = array(); - $this->blocks = array(); + $this->blockorder = []; + $this->blocks = []; - $this->vars = array(); + $this->vars = []; $this->scope = new Scope(); $this->scope->vars =& $this->vars; } diff --git a/src/ste/STEStandardLibrary.php b/src/ste/STEStandardLibrary.php index fcd1cd2..7191718 100644 --- a/src/ste/STEStandardLibrary.php +++ b/src/ste/STEStandardLibrary.php @@ -8,7 +8,7 @@ class STEStandardLibrary { foreach (get_class_methods(__CLASS__) as $method) { if ($method[0] != "_") { - $ste->register_tag($method, array(__CLASS__, $method)); + $ste->register_tag($method, [__CLASS__, $method]); } } } diff --git a/src/ste/Scope.php b/src/ste/Scope.php index 15d7d6c..ea71634 100644 --- a/src/ste/Scope.php +++ b/src/ste/Scope.php @@ -8,7 +8,7 @@ class Scope implements \ArrayAccess private $parent = null; /** @var array */ - public $vars = array(); + public $vars = []; /** * @param string $name @@ -18,7 +18,7 @@ class Scope implements \ArrayAccess private static function parse_name($name) { $remain = $name; - $fields = array(); + $fields = []; while ($remain !== "") { $br_open = strpos($remain, '['); @@ -96,7 +96,7 @@ class Scope implements \ArrayAccess $ref = &$this->get_topvar_reference($first, $localonly); } catch (VarNotInScope $e) { if ($create_if_not_exist) { - $this->vars[$first] = (count($fields) > 0) ? array() : ""; + $this->vars[$first] = (count($fields) > 0) ? [] : ""; $ref = &$this->vars[$first]; } else { return $nullref; @@ -116,7 +116,7 @@ class Scope implements \ArrayAccess } if ($i < count($fields) - 1) { - $ref[$field] = array(); + $ref[$field] = []; } else { $ref[$field] = ""; } diff --git a/src/ste/TagNode.php b/src/ste/TagNode.php index 18567b2..cef4220 100644 --- a/src/ste/TagNode.php +++ b/src/ste/TagNode.php @@ -8,10 +8,10 @@ class TagNode extends ASTNode public $name; /** @var ASTNode[][] */ - public $params = array(); + public $params = []; /** @var ASTNode[] */ - public $sub = array(); + public $sub = []; /** * @param string $tpl diff --git a/src/ste/Transcompiler.php b/src/ste/Transcompiler.php index 1c65185..629a023 100644 --- a/src/ste/Transcompiler.php +++ b/src/ste/Transcompiler.php @@ -26,25 +26,25 @@ class Transcompiler } /** @var callable[] builtins */ - self::$builtins = array( - "if" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_if")), - "cmp" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_cmp")), - "not" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_not")), - "even" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_even")), - "for" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_for")), - "foreach" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_foreach")), - "infloop" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_infloop")), - "break" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_break")), - "continue" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_continue")), - "block" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_block")), - "load" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_load")), - "mktag" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_mktag")), - "tagcontent" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_tagcontent")), - "set" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_set")), - "setlocal" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_setlocal")), - "get" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_get")), - "calc" => self::mark_builtin_callable(array("\\kch42\\ste\\Transcompiler", "builtin_calc")) - ); + self::$builtins = [ + "if" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_if"]), + "cmp" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_cmp"]), + "not" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_not"]), + "even" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_even"]), + "for" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_for"]), + "foreach" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_foreach"]), + "infloop" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_infloop"]), + "break" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_break"]), + "continue" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_continue"]), + "block" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_block"]), + "load" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_load"]), + "mktag" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_mktag"]), + "tagcontent" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_tagcontent"]), + "set" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_set"]), + "setlocal" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_setlocal"]), + "get" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_get"]), + "calc" => self::mark_builtin_callable(["\\kch42\\ste\\Transcompiler", "builtin_calc"]) + ]; } /** @@ -55,7 +55,7 @@ class Transcompiler private static function builtin_if($ast) { $output = ""; - $condition = array(); + $condition = []; $then = null; $else = null; @@ -93,14 +93,14 @@ class Transcompiler */ private static function builtin_cmp($ast) { - $operators = array( - array('eq', '=='), - array('neq', '!='), - array('lt', '<'), - array('lte', '<='), - array('gt', '>'), - array('gte', '>=') - ); + $operators = [ + ['eq', '=='], + ['neq', '!='], + ['lt', '<'], + ['lte', '<='], + ['gt', '>'], + ['gte', '>='] + ]; $code = ""; @@ -298,8 +298,8 @@ class Transcompiler $loopbody .= "\$${loopname}_counter++;\n\$ste->set_var_by_name(\$${loopname}_countervar, \$${loopname}_counter);\n"; } - $loop = array(); - $else = array(); + $loop = []; + $else = []; foreach ($ast->sub as $node) { if (($node instanceof TagNode) && ($node->name == "else")) { $else = array_merge($else, $node->sub); @@ -541,7 +541,7 @@ class Transcompiler { /* The real self::transcompile function, does not add boilerplate code. */ $code = ""; - $text_and_var_buffer = array(); + $text_and_var_buffer = []; foreach ($ast as $node) { if ($node instanceof TextNode) { @@ -551,7 +551,7 @@ class Transcompiler } elseif ($node instanceof TagNode) { if (!empty($text_and_var_buffer)) { $code .= "\$outputstack[\$outputstack_i] .= " . implode(" . ", $text_and_var_buffer) . ";\n"; - $text_and_var_buffer = array(); + $text_and_var_buffer = []; } if (isset(self::$builtins[$node->name])) { $code .= call_user_func(self::$builtins[$node->name], $node); @@ -572,7 +572,7 @@ class Transcompiler } if ($avoid_outputstack && ($code == "")) { - return array(implode(" . ", $text_and_var_buffer), ""); + return [implode(" . ", $text_and_var_buffer), ""]; } if (!empty($text_and_var_buffer)) { @@ -583,7 +583,7 @@ class Transcompiler $tmpvar = self::tempvar("tmp"); $code = "\$outputstack[] = '';\n\$outputstack_i++;" . $code; $code .= "\$$tmpvar = array_pop(\$outputstack);\n\$outputstack_i--;\n"; - return array("\$$tmpvar", $code); + return ["\$$tmpvar", $code]; } return $code; diff --git a/src/ste/VariableNode.php b/src/ste/VariableNode.php index 9752d28..fbe1efc 100644 --- a/src/ste/VariableNode.php +++ b/src/ste/VariableNode.php @@ -8,7 +8,7 @@ class VariableNode extends ASTNode public $name; /** @var ASTNode[][] */ - public $arrayfields = array(); + public $arrayfields = []; /** * @return string diff --git a/tests/functional/test_array/Test.php b/tests/functional/test_array/Test.php index 58f0cb8..85d0217 100644 --- a/tests/functional/test_array/Test.php +++ b/tests/functional/test_array/Test.php @@ -14,14 +14,14 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $ste->vars["foo"] = array( - "a" => array( + $ste->vars["foo"] = [ + "a" => [ "blabla" => "OK" - ), + ], "b" => "bla" - ); - $ste->vars["bar"] = array( + ]; + $ste->vars["bar"] = [ "baz" => "a" - ); + ]; } } diff --git a/tests/functional/test_foreach/Test.php b/tests/functional/test_foreach/Test.php index 3f8cc73..2fc3d91 100644 --- a/tests/functional/test_foreach/Test.php +++ b/tests/functional/test_foreach/Test.php @@ -14,10 +14,10 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $ste->vars["foo"] = array( - "a" => array("a" => 100, "b" => 200), - "b" => array("a" => 1, "b" => 2), - "c" => array("a" => 42, "b" => 1337) - ); + $ste->vars["foo"] = [ + "a" => ["a" => 100, "b" => 200], + "b" => ["a" => 1, "b" => 2], + "c" => ["a" => 42, "b" => 1337] + ]; } } diff --git a/tests/functional/test_short_for_additional_attribs/Test.php b/tests/functional/test_short_for_additional_attribs/Test.php index 8099869..e0e0934 100644 --- a/tests/functional/test_short_for_additional_attribs/Test.php +++ b/tests/functional/test_short_for_additional_attribs/Test.php @@ -14,10 +14,10 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $ste->set_var_by_name("data", array( - array('content' => 'foo', 'foo' => true), - array('content' => 'bar', 'foo' => false), - array('content' => 'baz', 'foo' => false), - )); + $ste->set_var_by_name("data", [ + ['content' => 'foo', 'foo' => true], + ['content' => 'bar', 'foo' => false], + ['content' => 'baz', 'foo' => false], + ]); } } diff --git a/tests/functional/test_static_numeric_array_access/Test.php b/tests/functional/test_static_numeric_array_access/Test.php index 0c6a853..150b74a 100644 --- a/tests/functional/test_static_numeric_array_access/Test.php +++ b/tests/functional/test_static_numeric_array_access/Test.php @@ -14,10 +14,10 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $ste->set_var_by_name("foo", array( + $ste->set_var_by_name("foo", [ "foo", "bar", "baz", - )); + ]); } } diff --git a/tests/functional/test_tagname/Test.php b/tests/functional/test_tagname/Test.php index 8e3d8f6..38f26c2 100644 --- a/tests/functional/test_tagname/Test.php +++ b/tests/functional/test_tagname/Test.php @@ -14,12 +14,12 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $names = array( + $names = [ "foo", "ab_cd", "foo123baz", "x0123", - ); + ]; foreach ($names as $name) { $ste->register_tag( diff --git a/tests/functional/test_trailing_closing_array_bracket/Test.php b/tests/functional/test_trailing_closing_array_bracket/Test.php index 9e6ff2c..a24f93c 100644 --- a/tests/functional/test_trailing_closing_array_bracket/Test.php +++ b/tests/functional/test_trailing_closing_array_bracket/Test.php @@ -14,10 +14,10 @@ class Test extends BaseTest protected function setUpSte(STECore $ste): void { - $ste->set_var_by_name("foo", array( + $ste->set_var_by_name("foo", [ "foo", "bar", "baz", - )); + ]); } } -- cgit v1.2.3-54-g00ecf