diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2020-09-13 22:25:15 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2020-09-13 22:25:15 +0200 |
commit | 76a1e915e0eea54b9b9a01d921efa6f9edffb3a8 (patch) | |
tree | 2d3140702e971cbe492561fea0f2d4c512366c07 /src/ste | |
parent | 2bbb1646f56fe84f38ed2a0cba03a386acc6160e (diff) | |
download | ste-76a1e915e0eea54b9b9a01d921efa6f9edffb3a8.tar.gz ste-76a1e915e0eea54b9b9a01d921efa6f9edffb3a8.tar.bz2 ste-76a1e915e0eea54b9b9a01d921efa6f9edffb3a8.zip |
Code cleanup
- Add parameter and return types
- Add missing @throws tags
- Add visibilities to consts
Diffstat (limited to 'src/ste')
-rw-r--r-- | src/ste/ASTNode.php | 2 | ||||
-rw-r--r-- | src/ste/Calc.php | 8 | ||||
-rw-r--r-- | src/ste/FilesystemStorageAccess.php | 6 | ||||
-rw-r--r-- | src/ste/Misc.php | 2 | ||||
-rw-r--r-- | src/ste/ParseCompileError.php | 2 | ||||
-rw-r--r-- | src/ste/Parser.php | 42 | ||||
-rw-r--r-- | src/ste/STECore.php | 27 | ||||
-rw-r--r-- | src/ste/STEStandardLibrary.php | 24 | ||||
-rw-r--r-- | src/ste/Scope.php | 14 | ||||
-rw-r--r-- | src/ste/StorageAccess.php | 8 | ||||
-rw-r--r-- | src/ste/TagNode.php | 2 | ||||
-rw-r--r-- | src/ste/TextNode.php | 2 | ||||
-rw-r--r-- | src/ste/Transcompiler.php | 46 | ||||
-rw-r--r-- | src/ste/VariableNode.php | 2 |
14 files changed, 94 insertions, 93 deletions
diff --git a/src/ste/ASTNode.php b/src/ste/ASTNode.php index 3e74635..4eb0348 100644 --- a/src/ste/ASTNode.php +++ b/src/ste/ASTNode.php @@ -14,7 +14,7 @@ abstract class ASTNode * @param string $tpl * @param int $off */ - public function __construct($tpl, $off) + public function __construct(string $tpl, int $off) { $this->tpl = $tpl; $this->offset = $off; diff --git a/src/ste/Calc.php b/src/ste/Calc.php index 9030c08..660943d 100644 --- a/src/ste/Calc.php +++ b/src/ste/Calc.php @@ -20,7 +20,7 @@ class Calc * @return array * @throws RuntimeError */ - private static function shunting_yard($infix_math) + private static function shunting_yard(string $infix_math): array { $operators = [ "+" => ["l", 2], @@ -112,7 +112,7 @@ class Calc * @return array * @throws RuntimeError */ - private static function pop2(&$array) + private static function pop2(array &$array): array { $rv = [array_pop($array), array_pop($array)]; if (array_search(null, $rv, true) !== false) { @@ -126,7 +126,7 @@ class Calc * @return int|float * @throws RuntimeError */ - private static function calc_rpn($rpn) + private static function calc_rpn(array $rpn) { $stack = []; foreach ($rpn as $token) { @@ -174,7 +174,7 @@ class Calc * @return float|int * @throws RuntimeError */ - public static function calc($expr) + public static function calc(string $expr) { return self::calc_rpn(self::shunting_yard($expr)); } diff --git a/src/ste/FilesystemStorageAccess.php b/src/ste/FilesystemStorageAccess.php index cb2fe7f..ead2757 100644 --- a/src/ste/FilesystemStorageAccess.php +++ b/src/ste/FilesystemStorageAccess.php @@ -17,13 +17,13 @@ class FilesystemStorageAccess implements StorageAccess * @param string $src - The directory with the sources (Writing permissions are not mandatory, because STE does not save template sources). * @param string $transc - The directory with the compiled templates (the PHP instance / the HTTP Server needs writing permissions to this directory). */ - public function __construct($src, $transc) + public function __construct(string $src, string $transc) { $this->sourcedir = $src; $this->transcompileddir = $transc; } - public function load($tpl, &$mode) + public function load(string $tpl, string &$mode) { $src_fn = $this->sourcedir . "/" . $tpl; $transc_fn = $this->transcompileddir . "/" . $tpl . ".php"; @@ -64,7 +64,7 @@ class FilesystemStorageAccess implements StorageAccess } } - public function save($tpl, $data, $mode) + public function save(string $tpl, string $data, $mode): void { $fn = (($mode == StorageAccess::MODE_SOURCE) ? $this->sourcedir : $this->transcompileddir) . "/" . $tpl . (($mode == StorageAccess::MODE_TRANSCOMPILED) ? ".php" : ""); @mkdir(dirname($fn), 0777, true); diff --git a/src/ste/Misc.php b/src/ste/Misc.php index 330b3d2..7118b28 100644 --- a/src/ste/Misc.php +++ b/src/ste/Misc.php @@ -8,7 +8,7 @@ class Misc * @param string $text * @return string */ - public static function escape_text($text) + public static function escape_text(string $text): string { return addcslashes($text, "\r\n\t\$\0..\x1f\\\"\x7f..\xff"); } diff --git a/src/ste/ParseCompileError.php b/src/ste/ParseCompileError.php index 0d5a240..bc0c252 100644 --- a/src/ste/ParseCompileError.php +++ b/src/ste/ParseCompileError.php @@ -24,7 +24,7 @@ class ParseCompileError extends \Exception * Update the message to include a human readable offset. * @param string $code */ - public function rewrite($code) + public function rewrite(string $code): void { $line = substr_count(str_replace("\r\n", "\n", substr($code, 0, $this->off)), "\n") + 1; $this->message = "{$this->msg} (Template {$this->tpl}, Line $line)"; diff --git a/src/ste/Parser.php b/src/ste/Parser.php index 399ed75..a588203 100644 --- a/src/ste/Parser.php +++ b/src/ste/Parser.php @@ -22,16 +22,16 @@ class Parser /** @var int */ private $len; - const PARSE_SHORT = 1; - const PARSE_TAG = 2; + private const PARSE_SHORT = 1; + private const PARSE_TAG = 2; - const ESCAPES_DEFAULT = '$?~{}|\\'; + private const ESCAPES_DEFAULT = '$?~{}|\\'; /** * @param string $text * @param string $name */ - private function __construct($text, $name) + private function __construct(string $text, string $name) { $this->text = $text; $this->name = $name; @@ -43,7 +43,7 @@ class Parser * @param int $n * @return string */ - private function peek($n = 1) + private function peek(int $n = 1): string { if ($n <= 0) { throw new \InvalidArgumentException("\$n must be > 0"); @@ -55,7 +55,7 @@ class Parser * @param int $n * @return string */ - private function next($n = 1) + private function next(int $n = 1): string { $c = $this->peek($n); $this->off = min($this->off + $n, $this->len); @@ -65,7 +65,7 @@ class Parser /** * @return bool */ - private function eof() + private function eof(): bool { return ($this->off == $this->len); } @@ -73,7 +73,7 @@ class Parser /** * @param int $n */ - private function back($n = 1) + private function back(int $n = 1): void { if ($n <= 0) { throw new \InvalidArgumentException("\$n must be > 0"); @@ -85,7 +85,7 @@ class Parser * @param string $needle * @return false|int */ - private function search_off($needle) + private function search_off(string $needle) { return mb_strpos($this->text, $needle, $this->off); } @@ -94,7 +94,7 @@ class Parser * @param string[] $needles * @return array 4-tuple of found key, offset, text preceding offset, old offset */ - private function search_multi($needles) + private function search_multi(array $needles): array { $oldoff = $this->off; @@ -121,7 +121,7 @@ class Parser * @param string $needle * @return array 3-tuple of offset (or false), text preceding offset, old offset */ - private function search($needle) + private function search(string $needle): array { $oldoff = $this->off; @@ -139,7 +139,7 @@ class Parser * @param callable $cb * @return string */ - private function take_while(callable $cb) + private function take_while(callable $cb): string { $s = ""; while (($c = $this->next()) !== "") { @@ -152,7 +152,7 @@ class Parser return $s; } - private function skip_ws() + private function skip_ws(): void { $this->take_while("ctype_space"); } @@ -161,7 +161,7 @@ class Parser * @return string * @throws ParseCompileError */ - private function get_name() + private function get_name(): string { $off = $this->off; $name = $this->take_while(function ($c) { @@ -184,7 +184,7 @@ class Parser * @return ASTNode[] * @throws ParseCompileError */ - public static function parse($text, $name) + public static function parse(string $text, string $name): array { $obj = new self($text, $name); $res = $obj->parse_text( @@ -198,7 +198,7 @@ class Parser * @param ASTNode[] $ast * @return ASTNode[] */ - private static function combine_consecutive_text(array $ast) + private static function combine_consecutive_text(array $ast): array { $out = []; @@ -239,7 +239,7 @@ class Parser * @param ASTNode[] $ast * @return ASTNode[] */ - private static function tidyup_ast(array $ast) + private static function tidyup_ast(array $ast): array { foreach ($ast as $node) { if ($node instanceof TagNode) { @@ -266,7 +266,7 @@ class Parser * @return ASTNode[][] * @throws ParseCompileError */ - private function parse_text($escapes, $flags, $breakon = null, $separator = null, $nullaction = null, $opentag = null, $openedat = -1) + private function parse_text(string $escapes, int $flags, $breakon = null, $separator = null, $nullaction = null, $opentag = null, $openedat = -1): array { $elems = []; $astlist = []; @@ -418,7 +418,7 @@ class Parser * @return ASTNode[][] * @throws ParseCompileError */ - private function parse_short($shortname, $openedat) + private function parse_short(string $shortname, int $openedat): array { $tplname = $this->name; @@ -441,7 +441,7 @@ class Parser * @return VariableNode * @throws ParseCompileError */ - private function parse_var($openedat, $curly) + private function parse_var(int $openedat, bool $curly): VariableNode { $varnode = new VariableNode($this->name, $openedat); $varnode->name = $this->get_name(); @@ -491,7 +491,7 @@ class Parser * @return TagNode * @throws ParseCompileError */ - private function parse_tag($openedat) + private function parse_tag(int $openedat) { $tplname = $this->name; diff --git a/src/ste/STECore.php b/src/ste/STECore.php index 9274587..897a77f 100644 --- a/src/ste/STECore.php +++ b/src/ste/STECore.php @@ -90,7 +90,7 @@ class STECore * * @throws Exception If the tag could not be registered (if $callback is not callable or if $name is empty) */ - public function register_tag($name, $callback) + public function register_tag(string $name, callable $callback): void { if (!is_callable($callback)) { throw new Exception("Can not register tag \"$name\", not callable."); @@ -113,7 +113,7 @@ class STECore * @return string The output of the tag or, if a {@see RuntimeError} was thrown, the appropriate result * (see {@see STECore::$mute_runtime_errors}). */ - public function call_tag($name, $params, $sub) + public function call_tag(string $name, array $params, callable $sub): string { try { if (!isset($this->tags[$name])) { @@ -140,7 +140,7 @@ class STECore * @return float|int * @throws RuntimeError */ - public function calc($expression) + public function calc(string $expression) { return Calc::calc($expression); } @@ -153,6 +153,7 @@ class STECore * @param string $tpl The name of the template to execute. * * @throws CantLoadTemplate If the template could not be loaded. + * @throws CantSaveTemplate If the template could not be saved (after compilation). * @throws ParseCompileError If the template could not be parsed or compiled. * @throws FatalRuntimeError If a tag threw it or if a tag was not found and <$fatal_error_on_missing_tag> is true. * @@ -161,7 +162,7 @@ class STECore * * @return string The output of the template. */ - public function exectemplate($tpl) + public function exectemplate(string $tpl): string { $output = ""; $lastblock = $this->load($tpl); @@ -183,7 +184,7 @@ class STECore * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets). * @return mixed A Reference to the variable. */ - public function &get_var_reference($name, $create_if_not_exist) + public function &get_var_reference(string $name, bool $create_if_not_exist) { $ref = &$this->scope->get_var_reference($name, $create_if_not_exist); return $ref; @@ -198,7 +199,7 @@ class STECore * * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets). */ - public function set_var_by_name($name, $val) + public function set_var_by_name(string $name, $val): void { $this->scope->set_var_by_name($name, $val); } @@ -212,7 +213,7 @@ class STECore * * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets). */ - public function set_local_var($name, $val) + public function set_local_var(string $name, $val): void { $this->scope->set_local_var($name, $val); } @@ -223,10 +224,10 @@ class STECore * * @param string $name The variables name. * - * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets). * @return mixed The variables value + * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets). */ - public function get_var_by_name($name) + public function get_var_by_name(string $name) { return $this->scope->get_var_by_name($name); } @@ -247,7 +248,7 @@ class STECore * * @return string|null The result of the template (if $quiet == false). */ - public function load($tpl, $quiet = false) + public function load(string $tpl, bool $quiet = false): ?string { $tpldir_b4 = $this->cur_tpl_dir; @@ -303,14 +304,14 @@ class STECore } } - /* + /** * Test, if a text represents false (an empty / only whitespace text) or true (everything else). * * @param string $txt The text to test. * * @return bool */ - public function evalbool($txt) + public function evalbool(string $txt): bool { return trim(@(string)$txt) != ""; } @@ -321,7 +322,7 @@ class STECore * @param callable $fx * @return \Closure */ - public function make_closure($fx) + public function make_closure(callable $fx): \Closure { $bound_scope = $this->scope; return function () use ($bound_scope, $fx) { diff --git a/src/ste/STEStandardLibrary.php b/src/ste/STEStandardLibrary.php index 7191718..cd23fdf 100644 --- a/src/ste/STEStandardLibrary.php +++ b/src/ste/STEStandardLibrary.php @@ -4,7 +4,7 @@ namespace kch42\ste; class STEStandardLibrary { - public static function _register_lib($ste) + public static function _register_lib(STECore $ste): void { foreach (get_class_methods(__CLASS__) as $method) { if ($method[0] != "_") { @@ -19,7 +19,7 @@ class STEStandardLibrary * @param callable $sub * @return string */ - public static function escape($ste, $params, $sub) + public static function escape(STECore $ste, array $params, callable $sub) { if ($ste->evalbool(@$params["lines"])) { return nl2br(htmlspecialchars(str_replace("\r\n", "\n", $sub($ste)))); @@ -34,7 +34,7 @@ class STEStandardLibrary * @param callable $sub * @return string */ - public static function strlen($ste, $params, $sub) + public static function strlen(STECore $ste, array $params, callable $sub) { return strlen($sub($ste)); } @@ -46,7 +46,7 @@ class STEStandardLibrary * @return string * @throws RuntimeError */ - public static function arraylen($ste, $params, $sub) + public static function arraylen(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:arraylen>."); @@ -61,7 +61,7 @@ class STEStandardLibrary * @param callable $sub * @throws RuntimeError */ - public static function inc($ste, $params, $sub) + public static function inc(STECore $ste, array $params, callable $sub) { if (empty($params["var"])) { throw new RuntimeError("Missing var parameter in <ste:inc>."); @@ -76,7 +76,7 @@ class STEStandardLibrary * @param callable $sub * @throws RuntimeError */ - public static function dec($ste, $params, $sub) + public static function dec(STECore $ste, array $params, callable $sub) { if (empty($params["var"])) { throw new RuntimeError("Missing var parameter in <ste:dec>."); @@ -91,7 +91,7 @@ class STEStandardLibrary * @param callable $sub * @return string */ - public static function date($ste, $params, $sub) + public static function date(STECore $ste, array $params, callable $sub) { return @strftime($sub($ste), empty($params["timestamp"]) ? @time() : (int) $params["timestamp"]); } @@ -103,7 +103,7 @@ class STEStandardLibrary * @return string * @throws RuntimeError */ - public static function in_array($ste, $params, $sub) + public static function in_array(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:in_array>."); @@ -122,7 +122,7 @@ class STEStandardLibrary * @return string * @throws RuntimeError */ - public static function join($ste, $params, $sub) + public static function join(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:join>."); @@ -136,7 +136,7 @@ class STEStandardLibrary * @param callable $sub * @throws RuntimeError */ - public static function split($ste, $params, $sub) + public static function split(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:split>."); @@ -153,7 +153,7 @@ class STEStandardLibrary * @param callable $sub * @throws RuntimeError */ - public static function array_add($ste, $params, $sub) + public static function array_add(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:array_add>."); @@ -173,7 +173,7 @@ class STEStandardLibrary * @param callable $sub * @throws RuntimeError */ - public static function array_filter($ste, $params, $sub) + public static function array_filter(STECore $ste, array $params, callable $sub) { if (empty($params["array"])) { throw new RuntimeError("Missing array parameter in <ste:array_filter>."); diff --git a/src/ste/Scope.php b/src/ste/Scope.php index ea71634..2ab4e7f 100644 --- a/src/ste/Scope.php +++ b/src/ste/Scope.php @@ -15,7 +15,7 @@ class Scope implements \ArrayAccess * @return string[] * @throws RuntimeError */ - private static function parse_name($name) + private static function parse_name(string $name): array { $remain = $name; $fields = []; @@ -58,7 +58,7 @@ class Scope implements \ArrayAccess * @return mixed A reference to the resolved variable * @throws VarNotInScope */ - private function &get_topvar_reference($name, $localonly) + private function &get_topvar_reference(string $name, bool $localonly) { if (array_key_exists($name, $this->vars)) { $ref = &$this->vars[$name]; @@ -80,7 +80,7 @@ class Scope implements \ArrayAccess * @return mixed A reference to the resolved variable * @throws RuntimeError */ - public function &get_var_reference($name, $create_if_not_exist, $localonly=false) + public function &get_var_reference(string $name, bool $create_if_not_exist, $localonly=false) { $nullref = null; @@ -133,7 +133,7 @@ class Scope implements \ArrayAccess * @param mixed $val * @throws RuntimeError */ - public function set_var_by_name($name, $val) + public function set_var_by_name(string $name, $val): void { $ref = &$this->get_var_reference($name, true); $ref = $val; @@ -144,7 +144,7 @@ class Scope implements \ArrayAccess * @param mixed $val * @throws RuntimeError */ - public function set_local_var($name, $val) + public function set_local_var(string $name, $val): void { $ref = &$this->get_var_reference($name, true, true); $ref = $val; @@ -155,7 +155,7 @@ class Scope implements \ArrayAccess * @return mixed Returns an empty string, if not found or unset * @throws RuntimeError */ - public function get_var_by_name($name) + public function get_var_by_name(string $name) { $ref = $this->get_var_reference($name, false); return $ref === null ? "" : $ref; @@ -164,7 +164,7 @@ class Scope implements \ArrayAccess /** * @return self */ - public function new_subscope() + public function new_subscope(): self { $o = new self(); $o->parent = $this; diff --git a/src/ste/StorageAccess.php b/src/ste/StorageAccess.php index 38dea7e..ec7f6bc 100644 --- a/src/ste/StorageAccess.php +++ b/src/ste/StorageAccess.php @@ -9,10 +9,10 @@ namespace kch42\ste; interface StorageAccess { /** @var int The template's source */ - const MODE_SOURCE = 0; + public const MODE_SOURCE = 0; /** @var int The compiled template */ - const MODE_TRANSCOMPILED = 1; + public const MODE_TRANSCOMPILED = 1; /** * Loading a template. @@ -30,7 +30,7 @@ interface StorageAccess * * @return string|callable Either the sourcecode or a callable function (first, and only parameter: an {@see STECore} instance). */ - public function load($tpl, &$mode); + public function load(string $tpl, string &$mode); /** * Saves a template. @@ -41,5 +41,5 @@ interface StorageAccess * * @throws CantSaveTemplate If the template could not be saved. */ - public function save($tpl, $data, $mode); + public function save(string $tpl, string $data, int $mode): void; } diff --git a/src/ste/TagNode.php b/src/ste/TagNode.php index cef4220..4adcad8 100644 --- a/src/ste/TagNode.php +++ b/src/ste/TagNode.php @@ -18,7 +18,7 @@ class TagNode extends ASTNode * @param int $off * @param string $name */ - public function __construct($tpl, $off, $name = "") + public function __construct(string $tpl, int $off, string $name = "") { parent::__construct($tpl, $off); $this->name = $name; diff --git a/src/ste/TextNode.php b/src/ste/TextNode.php index 199c203..c46d71f 100644 --- a/src/ste/TextNode.php +++ b/src/ste/TextNode.php @@ -12,7 +12,7 @@ class TextNode extends ASTNode * @param int $off * @param string $text */ - public function __construct($tpl, $off, $text = "") + public function __construct(string $tpl, int $off, string $text = "") { parent::__construct($tpl, $off); $this->text = $text; diff --git a/src/ste/Transcompiler.php b/src/ste/Transcompiler.php index 629a023..3955754 100644 --- a/src/ste/Transcompiler.php +++ b/src/ste/Transcompiler.php @@ -9,12 +9,12 @@ class Transcompiler { private static $builtins = null; - public static function tempvar($typ) + public static function tempvar($typ): string { return $typ . '_' . str_replace('.', '_', uniqid('', true)); } - private static function mark_builtin_callable(callable $c) + private static function mark_builtin_callable(callable $c): callable { return $c; } @@ -52,7 +52,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_if($ast) + private static function builtin_if(TagNode $ast): string { $output = ""; $condition = []; @@ -91,7 +91,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_cmp($ast) + private static function builtin_cmp(TagNode $ast): string { $operators = [ ['eq', '=='], @@ -166,7 +166,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_not($ast) + private static function builtin_not(TagNode $ast): string { $code = "\$outputstack[] = '';\n\$outputstack_i++;\n"; $code .= self::_transcompile($ast->sub); @@ -179,7 +179,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_even($ast) + private static function builtin_even(TagNode $ast): string { $code = "\$outputstack[] = '';\n\$outputstack_i++;\n"; $code .= self::_transcompile($ast->sub); @@ -192,7 +192,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_for($ast) + private static function builtin_for(TagNode $ast): string { $code = ""; $loopname = self::tempvar("forloop"); @@ -259,7 +259,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_foreach($ast) + private static function builtin_foreach(TagNode $ast): string { $loopname = self::tempvar("foreachloop"); $code = ""; @@ -331,7 +331,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_infloop($ast) + private static function builtin_infloop(TagNode $ast): string { return "while(true)\n{\n" . self::indent_code(self::loopbody(self::indent_code(self::_transcompile($ast->sub)))) . "\n}\n"; } @@ -340,7 +340,7 @@ class Transcompiler * @param TagNode $ast * @return string */ - private static function builtin_break($ast) + private static function builtin_break(TagNode $ast): string { return "throw new \\kch42\\ste\\BreakException();\n"; } @@ -349,7 +349,7 @@ class Transcompiler * @param TagNode $ast * @return string */ - private static function builtin_continue($ast) + private static function builtin_continue(TagNode $ast): string { return "throw new \\kch42\\ste\\ContinueException();\n"; } @@ -359,7 +359,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_block($ast) + private static function builtin_block(TagNode $ast): string { if (empty($ast->params["name"])) { throw new ParseCompileError("self::Transcompile Error: name missing in <ste:block>.", $ast->tpl, $ast->offset); @@ -386,7 +386,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_load($ast) + private static function builtin_load(TagNode $ast): string { if (empty($ast->params["name"])) { throw new ParseCompileError("self::Transcompile Error: name missing in <ste:load>.", $ast->tpl, $ast->offset); @@ -402,7 +402,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_mktag($ast) + private static function builtin_mktag(TagNode $ast): string { $code = ""; @@ -438,7 +438,7 @@ class Transcompiler * @param TagNode $ast * @return string */ - private static function builtin_tagcontent($ast) + private static function builtin_tagcontent(TagNode $ast): string { return "\$outputstack[\$outputstack_i] .= \$sub(\$ste);"; } @@ -448,7 +448,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_set($ast) + private static function builtin_set(TagNode $ast): string { if (empty($ast->params["var"])) { throw new ParseCompileError("self::Transcompile Error: var missing in <ste:set>.", $ast->tpl, $ast->offset); @@ -470,7 +470,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_setlocal($ast) + private static function builtin_setlocal(TagNode $ast): string { if (empty($ast->params["var"])) { throw new ParseCompileError("self::Transcompile Error: var missing in <ste:set>.", $ast->tpl, $ast->offset); @@ -492,7 +492,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_get($ast) + private static function builtin_get(TagNode $ast): string { if (empty($ast->params["var"])) { throw new ParseCompileError("self::Transcompile Error: var missing in <ste:get>.", $ast->tpl, $ast->offset); @@ -507,7 +507,7 @@ class Transcompiler * @return string * @throws ParseCompileError */ - private static function builtin_calc($ast) + private static function builtin_calc(TagNode $ast): string { $code = "\$outputstack[] = '';\n\$outputstack_i++;\n"; $code .= self::_transcompile($ast->sub); @@ -516,7 +516,7 @@ class Transcompiler return $code; } - private static function indent_code($code) + private static function indent_code(string $code): string { return implode( "\n", @@ -526,7 +526,7 @@ class Transcompiler ); } - private static function loopbody($code) + private static function loopbody(string $code): string { return "try\n{\n" . self::indent_code($code) . "\n}\ncatch(\\kch42\\ste\\BreakException \$e) { break; }\ncatch(\\kch42\\ste\\ContinueException \$e) { continue; }\n"; } @@ -537,7 +537,7 @@ class Transcompiler * @return array|string * @throws ParseCompileError */ - private static function _transcompile($ast, $avoid_outputstack = false) + private static function _transcompile(array $ast, bool $avoid_outputstack = false) { /* The real self::transcompile function, does not add boilerplate code. */ $code = ""; @@ -599,7 +599,7 @@ class Transcompiler * parameter and returns a string (everything that was not packed into a section). * @throws ParseCompileError */ - public static function transcompile($ast) + public static function transcompile(array $ast): string { /* self::Transcompile and add some boilerplate code. */ $boilerplate = "\$outputstack = array('');\n\$outputstack_i = 0;\n"; return "function(\$ste)\n{\n" . self::indent_code($boilerplate . self::_transcompile($ast) . "return array_pop(\$outputstack);") . "\n}"; diff --git a/src/ste/VariableNode.php b/src/ste/VariableNode.php index fbe1efc..f5a0b26 100644 --- a/src/ste/VariableNode.php +++ b/src/ste/VariableNode.php @@ -13,7 +13,7 @@ class VariableNode extends ASTNode /** * @return string */ - public function transcompile() + public function transcompile(): string { $varaccess = '@$ste->scope[' . (is_numeric($this->name) ? $this->name : '"' . Misc::escape_text($this->name) . '"'). ']'; foreach ($this->arrayfields as $af) { |