From 05d8c29fa861727d264385da2bce36ec2273ffe9 Mon Sep 17 00:00:00 2001 From: Smuuf Date: Thu, 23 Dec 2021 14:38:46 +0100 Subject: [PATCH] Making phpstan level 6 happy. And fixing various little things that were discovered. --- bin/phpstan.sh | 2 +- phpstan.neon | 10 ++-- src/Cli/Entrypoint.php | 18 ++++--- src/Code/Ast.php | 12 ++++- src/Code/AstProvider.php | 7 +++ src/Config.php | 4 ++ src/Context.php | 14 ++++-- src/Ex/ArgumentCountError.php | 4 +- src/Ex/ErrorException.php | 10 ++-- src/Ex/ReturnException.php | 9 +++- src/Handlers/ChainedHandler.php | 4 ++ src/Handlers/Handler.php | 2 + src/Handlers/HandlerFactory.php | 6 +++ src/Handlers/Kinds/DictDefinition.php | 10 ++-- src/Handlers/Kinds/FunctionDefinition.php | 4 ++ src/Handlers/Kinds/ListDefinition.php | 9 +++- src/Handlers/Kinds/TupleDefinition.php | 5 ++ src/Handlers/SharedLogicalHandler.php | 6 +++ src/Handlers/SimpleHandler.php | 8 +++ src/Helpers/ArithmeticLTR.php | 3 ++ src/Helpers/Colors.php | 7 ++- src/Helpers/ComparisonLTR.php | 3 ++ src/Helpers/Func.php | 49 +++++++++++++------ src/Helpers/Indices.php | 3 +- src/Helpers/Interned.php | 2 +- src/Helpers/Stats.php | 6 +-- src/Helpers/Traits/WatchLifecycle.php | 48 ++++++++++++------ src/Helpers/ValueFriends.php | 7 ++- src/Helpers/Wrappers/AbstractWrapper.php | 9 +++- .../Wrappers/CatchPosixSignalsWrapper.php | 13 +++-- .../Wrappers/ContextPushPopWrapper.php | 4 +- src/Helpers/Wrappers/ImportStackWrapper.php | 5 +- src/Modules/Dotpath.php | 13 ++++- src/Modules/Importer.php | 11 +++++ src/Parser/ParserHandler.php | 24 +++++++-- src/Repl.php | 21 ++++---- src/Scope.php | 13 ++++- src/Stdlib/Modules/std/__builtins__.primi.php | 4 +- src/Stdlib/Modules/std/math.primi.php | 3 ++ .../Modules/std/runtime/__init__.primi.php | 2 +- .../Modules/std/runtime/memory.primi.php | 4 +- src/Structures/AssignmentTargets.php | 3 ++ src/Structures/CallArgs.php | 4 ++ src/Structures/FnContainer.php | 12 ++++- src/Structures/MapContainer.php | 27 +++++----- src/Tasks/Emitters/PosixSignalTaskEmitter.php | 6 ++- src/Tasks/TaskQueue.php | 28 +++++------ src/Tasks/Types/CallbackTask.php | 14 +++--- src/Values/AbstractValue.php | 17 ++++++- src/Values/DictValue.php | 6 ++- src/Values/GeneratorValue.php | 6 +++ src/Values/InstanceValue.php | 3 +- src/Values/ListValue.php | 7 ++- src/Values/RegexValue.php | 2 +- src/Values/StringValue.php | 6 ++- src/Values/TupleValue.php | 2 +- src/Values/TypeValue.php | 3 ++ tests/unit/helper.blockwrapper.phpt | 2 +- tests/unit/scope.parents.phpt | 2 +- 59 files changed, 407 insertions(+), 141 deletions(-) diff --git a/bin/phpstan.sh b/bin/phpstan.sh index f65a649a..e52002dc 100644 --- a/bin/phpstan.sh +++ b/bin/phpstan.sh @@ -2,5 +2,5 @@ cd $(dirname $0) -../vendor/bin/phpstan analyze --level=5 ../src -c ../phpstan.neon $@ +../vendor/bin/phpstan analyze --level=6 ../src -c ../phpstan.neon $@ diff --git a/phpstan.neon b/phpstan.neon index a3408acd..851f39bc 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,7 @@ -parameters: - excludePaths: - - **/Parser/Compiled/PrimiParser.php +parameters: + excludePaths: + - **/Parser/Compiled/PrimiParser.php + typeAliases: + TypeDef_AstNode: 'array' + TypeDef_PrimiObjectCouples: 'iterable' + TypeDef_PrimiObjectCouple: 'array{\Smuuf\Primi\Values\AbstractValue, \Smuuf\Primi\Values\AbstractValue}' diff --git a/src/Cli/Entrypoint.php b/src/Cli/Entrypoint.php index 3ede9670..32fc4772 100644 --- a/src/Cli/Entrypoint.php +++ b/src/Cli/Entrypoint.php @@ -15,14 +15,15 @@ use \Smuuf\Primi\Code\Source; use \Smuuf\Primi\Code\SourceFile; use \Smuuf\Primi\Code\AstProvider; -use \Smuuf\Primi\Helpers\Colors; use \Smuuf\Primi\Helpers\Stats; +use \Smuuf\Primi\Helpers\Colors; class Entrypoint { use StrictObject; - private $config = [ + /** @var array */ + private array $config = [ // Print only parsed AST and then exit. 'only_tree' => false, // Parse the input (build AST), print parser stats and exit. @@ -41,17 +42,17 @@ class Entrypoint { ]; /** - * @param array $args Arguments to the CLI script (without the first $0 - * argument). + * @param array $args Arguments to the CLI script (without the + * first $0 argument). */ - public function __construct(array $args, string $rootDir) { + public function __construct(array $args) { self::globalInit(); $this->config = $this->parseArguments($this->config, $args); } - private static function globalInit() { + private static function globalInit(): void { error_reporting(E_ALL); set_error_handler(function($severity, $message, $file, $line) { @@ -161,6 +162,11 @@ private static function errorExit(string $text): void { die(1); } + /** + * @param array $defaults + * @param array $args + * @return array + */ private function parseArguments(array $defaults, array $args): array { $cfg = $defaults; diff --git a/src/Code/Ast.php b/src/Code/Ast.php index 13279e46..2e9326b0 100644 --- a/src/Code/Ast.php +++ b/src/Code/Ast.php @@ -16,13 +16,23 @@ class Ast { use StrictObject; - /** Abstract syntax tree as (nested) array. */ + /** + * Abstract syntax tree as (nested) array. + * + * @var TypeDef_AstNode + */ private array $tree; + /** + * @param TypeDef_AstNode $ast + */ public function __construct(array $ast) { $this->tree = $ast; } + /** + * @return TypeDef_AstNode + */ public function getTree(): array { return $this->tree; } diff --git a/src/Code/AstProvider.php b/src/Code/AstProvider.php index fb9da4ef..f0a55556 100644 --- a/src/Code/AstProvider.php +++ b/src/Code/AstProvider.php @@ -43,6 +43,9 @@ public function getAst(Source $source, bool $caching = \true): Ast { } + /** + * @return TypeDef_AstNode|null + */ private function loadFromCache(string $key): ?array { if ($this->tempDir === \null) { @@ -58,6 +61,9 @@ private function loadFromCache(string $key): ?array { } + /** + * @param TypeDef_AstNode $ast + */ private function storeIntoCache(string $key, array $ast): void { if ($this->tempDir === \null) { @@ -70,6 +76,7 @@ private function storeIntoCache(string $key, array $ast): void { } /** + * @return TypeDef_AstNode * @throws SyntaxError */ private static function parseSource(Source $source): array { diff --git a/src/Config.php b/src/Config.php index c3f78348..d78a08ce 100644 --- a/src/Config.php +++ b/src/Config.php @@ -57,6 +57,7 @@ public function getTempDir(): ?string { // Paths for finding modules. // + /** @var array */ private array $importPaths = [ __DIR__ . '/Stdlib/Modules', ]; @@ -65,6 +66,9 @@ public function addImportPath(string $path): void { $this->importPaths[] = Func::validate_dirs([$path])[0]; } + /** + * @return array + */ public function getImportPaths(): array { return $this->importPaths; } diff --git a/src/Context.php b/src/Context.php index 16f6ce3b..8282e74a 100644 --- a/src/Context.php +++ b/src/Context.php @@ -137,10 +137,6 @@ public function getCallStack(): array { return $this->callStack; } - public function getTraceback(): array { - return $this->callStack; - } - public function pushCall(StackFrame $call): void { $this->callStack[] = $call; @@ -205,14 +201,24 @@ public function getVariable(string $name): ?AbstractValue { ?? $this->builtins->getVariable($name); } + /** + * @return array + */ public function getVariables(bool $includeParents = \false): array { return $this->currentScope->getVariables($includeParents); } + /** + * @return void + */ public function setVariable(string $name, AbstractValue $value) { $this->currentScope->setVariable($name, $value); } + /** + * @param array $pairs + * @return void + */ public function setVariables(array $pairs) { $this->currentScope->setVariables($pairs); } diff --git a/src/Ex/ArgumentCountError.php b/src/Ex/ArgumentCountError.php index 7b4e2723..09fe09b3 100644 --- a/src/Ex/ArgumentCountError.php +++ b/src/Ex/ArgumentCountError.php @@ -6,8 +6,8 @@ class ArgumentCountError extends RuntimeError { - protected $passed = \null; - protected $expected = \null; + protected int $passed; + protected int $expected; public function __construct( int $passed, diff --git a/src/Ex/ErrorException.php b/src/Ex/ErrorException.php index 275447c5..6beb35cf 100644 --- a/src/Ex/ErrorException.php +++ b/src/Ex/ErrorException.php @@ -20,12 +20,13 @@ class ErrorException extends BaseException { /** * Traceback (which is really just the callstack). - * @var array + * + * @var ?array */ private ?array $traceback = \null; /** - * @param array $traceback + * @param ?array $traceback */ public function __construct( string $msg, @@ -55,8 +56,9 @@ public function getLocation(): Location { } /** - * Traceback, if available. - * @return array + * Traceback, if there's any. + * + * @return ?array */ public function getTraceback(): ?array { return $this->traceback; diff --git a/src/Ex/ReturnException.php b/src/Ex/ReturnException.php index 862bdcf3..40535264 100644 --- a/src/Ex/ReturnException.php +++ b/src/Ex/ReturnException.php @@ -5,19 +5,26 @@ namespace Smuuf\Primi\Ex; use \Smuuf\Primi\Helpers\Interned; +use \Smuuf\Primi\Values\AbstractValue; class ReturnException extends ControlFlowException { public const ID = 'return'; - /** @var mixed */ + /** @var AbstractValue|null */ protected $value; + /** + * @param mixed $value + */ public function __construct($value = \null) { parent::__construct(); $this->value = $value; } + /** + * @return AbstractValue + */ public function getValue() { return $this->value ?? Interned::null(); } diff --git a/src/Handlers/ChainedHandler.php b/src/Handlers/ChainedHandler.php index 874c48b0..1e03cdb6 100644 --- a/src/Handlers/ChainedHandler.php +++ b/src/Handlers/ChainedHandler.php @@ -13,6 +13,10 @@ */ abstract class ChainedHandler extends Handler { + /** + * @param TypeDef_AstNode $node + * @return mixed + */ abstract public static function chain( array $node, Context $context, diff --git a/src/Handlers/Handler.php b/src/Handlers/Handler.php index b94d5c2b..7682288e 100644 --- a/src/Handlers/Handler.php +++ b/src/Handlers/Handler.php @@ -27,6 +27,8 @@ abstract class Handler { /** * Additional node-type-specific post-process of the AST node provided by * parser. AST node array is passed by reference. + * + * @param TypeDef_AstNode $node */ public static function reduce(array &$node): void { diff --git a/src/Handlers/HandlerFactory.php b/src/Handlers/HandlerFactory.php index b0e2143b..811219b8 100644 --- a/src/Handlers/HandlerFactory.php +++ b/src/Handlers/HandlerFactory.php @@ -22,6 +22,9 @@ abstract class HandlerFactory { private const PREFIX = '\Smuuf\Primi\Handlers\Kinds'; + /** + * @return class-string|string + */ private static function getClassName(string $nodeName) { return self::PREFIX . "\\$nodeName"; } @@ -59,6 +62,9 @@ public static function getFor(string $name, ?bool $strict = \true) { /** * Shorthand function for running a AST node passed as array. + * + * @param TypeDef_AstNode $node + * @return mixed */ public static function runNode(array $node, Context $ctx) { return self::getFor($node['name'])::run($node, $ctx); diff --git a/src/Handlers/Kinds/DictDefinition.php b/src/Handlers/Kinds/DictDefinition.php index a46b73e5..15c2d332 100644 --- a/src/Handlers/Kinds/DictDefinition.php +++ b/src/Handlers/Kinds/DictDefinition.php @@ -33,13 +33,17 @@ protected static function handle(array $node, Context $context) { } + /** + * @param array $itemNodes + * @return TypeDef_PrimiObjectCouples + */ private static function buildPairs( - array $nodes, + array $itemNodes, Context $context - ): array { + ): iterable { $result = []; - foreach ($nodes as $node) { + foreach ($itemNodes as $node) { $result[] = [ HandlerFactory::runNode($node['key'], $context), diff --git a/src/Handlers/Kinds/FunctionDefinition.php b/src/Handlers/Kinds/FunctionDefinition.php index 2628dd7b..cc924b9d 100644 --- a/src/Handlers/Kinds/FunctionDefinition.php +++ b/src/Handlers/Kinds/FunctionDefinition.php @@ -54,6 +54,10 @@ public static function reduce(array &$node): void { } + /** + * @param TypeDef_AstNode $paramsNode + * @return array{names: array, defaults: array} + */ public static function prepareParameters(array $paramsNode): array { // Prepare dict array for passing specifics about parameters expected diff --git a/src/Handlers/Kinds/ListDefinition.php b/src/Handlers/Kinds/ListDefinition.php index de6641a9..b74c1e86 100644 --- a/src/Handlers/Kinds/ListDefinition.php +++ b/src/Handlers/Kinds/ListDefinition.php @@ -3,10 +3,11 @@ namespace Smuuf\Primi\Handlers\Kinds; use \Smuuf\Primi\Context; -use \Smuuf\Primi\Handlers\HandlerFactory; use \Smuuf\Primi\Helpers\Func; -use \Smuuf\Primi\Handlers\SimpleHandler; use \Smuuf\Primi\Values\ListValue; +use \Smuuf\Primi\Values\AbstractValue; +use \Smuuf\Primi\Handlers\SimpleHandler; +use \Smuuf\Primi\Handlers\HandlerFactory; class ListDefinition extends SimpleHandler { @@ -20,6 +21,10 @@ protected static function handle(array $node, Context $context) { } + /** + * @param array $itemNodes + * @return array + */ protected static function buildValues( array $itemNodes, Context $context diff --git a/src/Handlers/Kinds/TupleDefinition.php b/src/Handlers/Kinds/TupleDefinition.php index 08609ac6..9f2fdffc 100644 --- a/src/Handlers/Kinds/TupleDefinition.php +++ b/src/Handlers/Kinds/TupleDefinition.php @@ -4,6 +4,7 @@ use \Smuuf\Primi\Context; use \Smuuf\Primi\Values\TupleValue; +use \Smuuf\Primi\Values\AbstractValue; use \Smuuf\Primi\Helpers\Func; use \Smuuf\Primi\Handlers\SimpleHandler; use \Smuuf\Primi\Handlers\HandlerFactory; @@ -20,6 +21,10 @@ protected static function handle(array $node, Context $context) { } + /** + * @param array $itemNodes + * @return array + */ protected static function buildValues( array $itemNodes, Context $context diff --git a/src/Handlers/SharedLogicalHandler.php b/src/Handlers/SharedLogicalHandler.php index 6536852f..586e2091 100644 --- a/src/Handlers/SharedLogicalHandler.php +++ b/src/Handlers/SharedLogicalHandler.php @@ -60,6 +60,9 @@ public static function reduce(array &$node): void { } + /** + * @param TypeDef_AstNode $node + */ private static function handleAnd( array $node, Context $context @@ -80,6 +83,9 @@ private static function handleAnd( } + /** + * @param TypeDef_AstNode $node + */ private static function handleOr( array $node, Context $context diff --git a/src/Handlers/SimpleHandler.php b/src/Handlers/SimpleHandler.php index 3264c5f7..3e9ea9c5 100644 --- a/src/Handlers/SimpleHandler.php +++ b/src/Handlers/SimpleHandler.php @@ -16,6 +16,10 @@ */ abstract class SimpleHandler extends Handler { + /** + * @param TypeDef_AstNode $node + * @return mixed + */ final public static function run( array $node, Context $context @@ -40,6 +44,10 @@ final public static function run( } + /** + * @param TypeDef_AstNode $node + * @return mixed + */ abstract protected static function handle(array $node, Context $context); } diff --git a/src/Helpers/ArithmeticLTR.php b/src/Helpers/ArithmeticLTR.php index 6d536fe0..7372f898 100644 --- a/src/Helpers/ArithmeticLTR.php +++ b/src/Helpers/ArithmeticLTR.php @@ -16,6 +16,9 @@ class ArithmeticLTR { use StrictObject; + /** + * @param TypeDef_AstNode $node + */ public static function handle( array $node, Context $context diff --git a/src/Helpers/Colors.php b/src/Helpers/Colors.php index dfab289e..cf8d2e6c 100644 --- a/src/Helpers/Colors.php +++ b/src/Helpers/Colors.php @@ -92,13 +92,16 @@ private static function apply( } - private static function handler(array $m): string { + /** + * @param array $matches + */ + private static function handler(array $matches): string { if (self::$noColor !== \false) { return ''; } - $color = $m[1]; + $color = $matches[1]; if (isset(self::COLORS[$color])) { return \sprintf(self::COLOR_FORMAT, self::COLORS[$color]); diff --git a/src/Helpers/ComparisonLTR.php b/src/Helpers/ComparisonLTR.php index 9fc835db..74391db9 100644 --- a/src/Helpers/ComparisonLTR.php +++ b/src/Helpers/ComparisonLTR.php @@ -16,6 +16,9 @@ class ComparisonLTR { use StrictObject; + /** + * @param TypeDef_AstNode $node + */ public static function handle( array $node, Context $context diff --git a/src/Helpers/Func.php b/src/Helpers/Func.php index c3094cfa..4cbcd744 100644 --- a/src/Helpers/Func.php +++ b/src/Helpers/Func.php @@ -36,8 +36,11 @@ abstract class Func { * Returns a generator yielding `[primi key, primi value]` tuples from some * PHP array. If the value is not an instance of `AbstractValue` * object, it will be converted automatically to a `AbstractValue` object. + * + * @param array $array + * @return TypeDef_PrimiObjectCouples */ - public static function array_to_couples(array $array): \Generator { + public static function array_to_couples(array $array): iterable { foreach ($array as $key => $value) { yield [ @@ -54,14 +57,14 @@ public static function array_to_couples(array $array): \Generator { * representing a valid Primi variable name)_ to PHP dict array mapping * pairs of `['variable_name' => Some Primi object]`. * - * @param array $couples + * @param TypeDef_PrimiObjectCouples $couples * @return array PHP dict array mapping of variables. * @throws TypeError */ public static function couples_to_variables_array( iterable $couples, string $intendedTarget - ) { + ): array { $attrs = []; foreach ($couples as [$k, $v]) { @@ -90,7 +93,7 @@ public static function couples_to_variables_array( * a iterable Primi object that can be interpreted as a mapping. * Best-effort-style. * - * @return iterable + * @return TypeDef_PrimiObjectCouples * @throws TypeError */ public static function mapping_to_couples(AbstractValue $value) { @@ -190,7 +193,7 @@ public static function is_decimal(string $input): bool { * object if the previous object with the same hash was destroyed during * the PHP runtime. */ - public static function object_hash($o): string { + public static function object_hash(object $o): string { return \substr(\md5(\spl_object_hash($o)), 0, 8); } @@ -301,12 +304,14 @@ public static function scientific_to_decimal(string $number): string { * names) as the rest of the arguments, this function either throws a * TypeError exception with a user-friendly message or doesn't do * anything. + * + * @throws TypeError */ public static function allow_argument_types( int $index, AbstractValue $arg, string ...$allowedTypes - ) { + ): void { // If any of the "instanceof" checks is true, // the type is allowed - return without throwing exception. @@ -326,17 +331,23 @@ public static function allow_argument_types( } /** - * Takes array as reference and ensures its contents are represented in a form - * of indexed sub-arrays. This comes handy if we want to be sure that multiple - * AST sub-nodes (which PHP-PEG parser returns) are universally iterable. + * Takes array representing AST node and makes sure that its contents are + * represented in a form of indexed sub-arrays. This comes handy if we want + * to be sure that multiple AST sub-nodes (which PHP-PEG parser returns) are + * universally iterable. + * + * @param TypeDef_AstNode $node + * @return TypeDef_AstNode */ - public static function ensure_indexed(array $array): array { - return !isset($array[0]) ? [$array] : $array; + public static function ensure_indexed(array $node): array { + return !isset($node[0]) ? [$node] : $node; } /** * Return a `[line, pos]` tuple for given (probably multiline) string and * some offset. + * + * @return array{int, int} */ public static function get_position_estimate(string $string, int $offset): array { @@ -365,6 +376,9 @@ public static function get_position_estimate(string $string, int $offset): array * * In another words: This function returns which Primi types a PHP function * expects. + * + * @return array + * @throws EngineError */ public static function check_allowed_parameter_types_of_function( \ReflectionFunction $rf @@ -453,6 +467,9 @@ public static function check_allowed_parameter_types_of_function( * This way client code can, for example, implement short-circuiting by * using the result so-far and not processing the rest of what the generator * would yield. + * + * @param TypeDef_AstNode $node + * @return iterable */ public static function yield_left_to_right(array $node, Context $ctx) { @@ -473,10 +490,11 @@ public static function yield_left_to_right(array $node, Context $ctx) { * Types can be passed as a single class name or array of PHP class names. * * Throws an exception if any PHP class name doesn't represent a Primi type. + * + * @param array $types */ - public static function php_types_to_primi_types($types): string { + public static function php_types_to_primi_types(array $types): string { - $types = \is_string($types) ? [$types] : $types; $primiTypes = \array_map(function($class) { // Resolve PHP nulls as Primi nulls. @@ -490,7 +508,7 @@ public static function php_types_to_primi_types($types): string { ); } - return $class->getTypeName(); + return $class; }, $types); @@ -541,6 +559,9 @@ public static function get_traceback_as_string( * guaranteed to represent a "realpath" to a directory in filesystem. * * If any of the passed strings is NOT a directory, `EngineError` is thrown. + * + * @param array $paths + * @return array */ public static function validate_dirs(array $paths): array { diff --git a/src/Helpers/Indices.php b/src/Helpers/Indices.php index ae3e31bd..15d28af7 100644 --- a/src/Helpers/Indices.php +++ b/src/Helpers/Indices.php @@ -5,6 +5,7 @@ namespace Smuuf\Primi\Helpers; use \Smuuf\Primi\Ex\IndexError; +use \Smuuf\Primi\Values\AbstractValue; /** * Helpers for handling accessing PHP arrays via possibly negative indices. @@ -49,7 +50,7 @@ public static function resolveNegativeIndex( * represent an existing index in the array passed as the second argument, * an IndexError exception is thrown. * - * @param array|\ArrayAccess $array + * @param array|\ArrayAccess $array * @return mixed */ public static function resolveIndexOrError(int $index, $array) { diff --git a/src/Helpers/Interned.php b/src/Helpers/Interned.php index 54d9cc37..fa786826 100644 --- a/src/Helpers/Interned.php +++ b/src/Helpers/Interned.php @@ -81,7 +81,7 @@ abstract class Interned { * This saves us one null-check when building objects for these * super-primitive values. */ - public static function init() { + public static function init(): void { self::$internedNull = new NullValue; self::$internedBoolFalse = new BoolValue(\false); diff --git a/src/Helpers/Stats.php b/src/Helpers/Stats.php index 358e75de..f20de0f8 100644 --- a/src/Helpers/Stats.php +++ b/src/Helpers/Stats.php @@ -17,10 +17,10 @@ abstract class Stats { private static $enabled = \false; /** @var float Point in time when stats gathering was enabled. */ - private static $startTime = 0; + private static float $startTime = 0; - /** @var array Dictionary for gathered statistics. */ - private static $stats = []; + /** @var array Dictionary for gathered statistics. */ + private static array $stats = []; /** * Enable global stats gathering. diff --git a/src/Helpers/Traits/WatchLifecycle.php b/src/Helpers/Traits/WatchLifecycle.php index 02c790ba..068315a2 100644 --- a/src/Helpers/Traits/WatchLifecycle.php +++ b/src/Helpers/Traits/WatchLifecycle.php @@ -1,17 +1,25 @@ */ + public static array $alreadyVisualized = []; + + /** @var array */ + public static array $stack = []; } @@ -60,18 +68,22 @@ public function __destruct() { /** * Add this currently watched instance to global stack. */ - private function add($hash) { + private function add(string $hash): void { WatchLifecycleScope::$stack[++WatchLifecycleScope::$stackCounter] = $hash; } /** * Remove this currently watched instance from global stack. */ - private function remove($hash) { - unset(WatchLifecycleScope::$stack[array_search($hash, WatchLifecycleScope::$stack, true)]); + private function remove(string $hash): void { + unset( + WatchLifecycleScope::$stack[ + array_search($hash, WatchLifecycleScope::$stack, true) + ] + ); } - private function visualize() { + private function visualize(): string { if (!WatchLifecycleScope::$stack) return "x"; @@ -99,11 +111,17 @@ private function visualize() { * * Now that I'm thinking about it, the "1)" should be enough, but, well... */ - private static function getHash($object): string { - return substr(md5(self::$instanceCounter . get_class($object) . spl_object_hash($object)), 0, 8); + private static function getHash(object $object): string { + + return Func::string_hash( + self::$instanceCounter + . get_class($object) + . spl_object_hash($object) + ); + } - private static function truecolor(string $hex, string $content) { + private static function truecolor(string $hex, string $content): string { $r = self::numpad(hexdec(substr($hex, 0, 2)) + 32); $g = self::numpad(hexdec(substr($hex, 2, 2)) + 32); @@ -118,8 +136,8 @@ private static function truecolor(string $hex, string $content) { /** * Return int number as hex and ensure it's of length of 3, padded with zeroes. */ - private static function numpad(int $n) { - return str_pad($n, 3, "0", STR_PAD_LEFT); + private static function numpad(int $n): string { + return str_pad((string) $n, 3, "0", STR_PAD_LEFT); } } diff --git a/src/Helpers/ValueFriends.php b/src/Helpers/ValueFriends.php index af31a9d8..16270321 100644 --- a/src/Helpers/ValueFriends.php +++ b/src/Helpers/ValueFriends.php @@ -3,6 +3,7 @@ namespace Smuuf\Primi\Helpers; use \Smuuf\StrictObject; +use \Smuuf\Primi\Values\AbstractValue; /** * Emulate friend visibility - extending classes can access internal `$value` @@ -21,7 +22,11 @@ abstract class ValueFriends { /** @var mixed Value itself. */ protected $value = \null; - /** Value object attributes. */ + /** + * Value object attributes. + * + * @var array + */ protected array $attrs = []; } diff --git a/src/Helpers/Wrappers/AbstractWrapper.php b/src/Helpers/Wrappers/AbstractWrapper.php index 360ae13e..c02f0ea7 100644 --- a/src/Helpers/Wrappers/AbstractWrapper.php +++ b/src/Helpers/Wrappers/AbstractWrapper.php @@ -33,6 +33,9 @@ */ abstract class AbstractWrapper { + /** + * @return mixed + */ public function wrap(callable $fn) { $enterRetval = $this->executeBefore(); @@ -46,7 +49,11 @@ public function wrap(callable $fn) { } + /** + * @return mixed + */ abstract public function executeBefore(); - abstract public function executeAfter(); + + abstract public function executeAfter(): void; } diff --git a/src/Helpers/Wrappers/CatchPosixSignalsWrapper.php b/src/Helpers/Wrappers/CatchPosixSignalsWrapper.php index 8391802e..a768e42f 100644 --- a/src/Helpers/Wrappers/CatchPosixSignalsWrapper.php +++ b/src/Helpers/Wrappers/CatchPosixSignalsWrapper.php @@ -25,17 +25,20 @@ class CatchPosixSignalsWrapper extends AbstractWrapper { */ private static $level = []; - /** @var TaskQueue Send Posix signal tasks to this queue. */ - private $tq; + /** Send Posix signal tasks to this queue. */ + private TaskQueue $tq; - /** @var string Unique ID for each TaskQueue instance. */ - private $tqId; + /** Unique ID for each TaskQueue instance. */ + private string $tqId; public function __construct(TaskQueue $tq) { $this->tq = $tq; $this->tqId = $tq->getId(); } + /** + * @return mixed + */ public function executeBefore() { // If this is the first (outermost) context that uses this task queue, @@ -49,7 +52,7 @@ public function executeBefore() { } - public function executeAfter() { + public function executeAfter(): void { self::$level[$this->tqId]--; diff --git a/src/Helpers/Wrappers/ContextPushPopWrapper.php b/src/Helpers/Wrappers/ContextPushPopWrapper.php index 6291cbfe..9e175fae 100644 --- a/src/Helpers/Wrappers/ContextPushPopWrapper.php +++ b/src/Helpers/Wrappers/ContextPushPopWrapper.php @@ -33,6 +33,8 @@ public function __construct( /** * Push the call ID and scope (if present) onto call stack and scope stack. + * + * @return mixed */ public function executeBefore() { @@ -51,7 +53,7 @@ public function executeBefore() { /** * Pop the items from call stack and scope stack. */ - public function executeAfter() { + public function executeAfter(): void { if ($this->call) { $this->ctx->popCall(); diff --git a/src/Helpers/Wrappers/ImportStackWrapper.php b/src/Helpers/Wrappers/ImportStackWrapper.php index 4226ec9c..d775697d 100644 --- a/src/Helpers/Wrappers/ImportStackWrapper.php +++ b/src/Helpers/Wrappers/ImportStackWrapper.php @@ -31,6 +31,9 @@ public function __construct( $this->dotpath = $dotpath; } + /** + * @return mixed + */ public function executeBefore() { // Detect circular imports. @@ -40,7 +43,7 @@ public function executeBefore() { } - public function executeAfter() { + public function executeAfter(): void { $this->importer->popImport(); } diff --git a/src/Modules/Dotpath.php b/src/Modules/Dotpath.php index a3f6d927..2f5f1f9f 100644 --- a/src/Modules/Dotpath.php +++ b/src/Modules/Dotpath.php @@ -17,7 +17,11 @@ class Dotpath { /** Final absolute dotpath (with relativity resolved). */ private string $absolute; - /** Parts of the resolved dotpath. */ + /** + * Parts of the resolved dotpath. + * + * @var array + */ private array $parts; public function __construct( @@ -43,6 +47,8 @@ public function __construct( /** * Resolve the original, possibly relative, dotpath into an absolute * dotpath using origin as the origin dotpath. + * + * @return array{string, array} */ private static function resolve( string $dotpath, @@ -113,7 +119,10 @@ public function getFirstPart(): string { return \reset($this->parts); } - public function iterPaths(string $basepath = ''): \Generator { + /** + * @return iterable + */ + public function iterPaths(string $basepath = ''): iterable { $dotpath = ''; $package = ''; diff --git a/src/Modules/Importer.php b/src/Modules/Importer.php index 0a65efed..34095a66 100644 --- a/src/Modules/Importer.php +++ b/src/Modules/Importer.php @@ -50,6 +50,9 @@ class Importer { */ private array $baseCache = []; + /** + * @param array $importPaths + */ public function __construct( Context $ctx, array $importPaths = [] @@ -58,6 +61,9 @@ public function __construct( $this->importPaths = $importPaths; } + /** + * @return array + */ public function getLoaded(): array { return $this->loaded; } @@ -152,6 +158,8 @@ public function getModule(string $dotpath): ModuleValue { * where 'c' directory is present - the resulting base is determined as * '/yyy/', ignoring the '/zzz/', because '/yyy/' was before it and thus * had a higher priority. + * + * @param array $possiblePaths */ private function determineBase( Dotpath $dp, @@ -356,6 +364,9 @@ private static function isModule(string $filepath): bool { } + /** + * @return array + */ private static function withSupportedExtensions(string $filepath): array { $result = []; diff --git a/src/Parser/ParserHandler.php b/src/Parser/ParserHandler.php index 1e04b46e..06dffb71 100644 --- a/src/Parser/ParserHandler.php +++ b/src/Parser/ParserHandler.php @@ -33,11 +33,16 @@ public function __construct(string $source) { /** * Return parser stats. + * + * @return array */ public function getStats(): array { return $this->stats; } + /** + * @return TypeDef_AstNode + */ public function run(): array { $t = (new Timer)->start(); @@ -48,7 +53,7 @@ public function run(): array { // $this->pos is an internal PEG Parser position counter and // we will use it to determine the line and position in the source. - $this->syntaxError($this->parser->pos, $this->source); + $this->syntaxError($this->parser->pos); } @@ -60,7 +65,10 @@ public function run(): array { } - private function syntaxError(int $position, string $source) { + /** + * @return never + */ + private function syntaxError(int $position) { $line = \false; @@ -75,7 +83,7 @@ private function syntaxError(int $position, string $source) { } - private static function sanitizeSource(string $s) { + private static function sanitizeSource(string $s): string { // Unify new-lines. $s = \str_replace("\r\n", "\n", $s); @@ -86,6 +94,10 @@ private static function sanitizeSource(string $s) { } + /** + * @param TypeDef_AstNode $ast + * @return TypeDef_AstNode $ast + */ private function processAST(array $ast, string $source): array { $t = (new Timer)->start(); @@ -107,6 +119,8 @@ private function processAST(array $ast, string $source): array { /** * Go recursively through each of the nodes and strip unnecessary data * in the abstract syntax tree. + * + * @param TypeDef_AstNode $node */ private static function preprocessNode(array &$node): void { @@ -130,6 +144,8 @@ private static function preprocessNode(array &$node): void { /** * Go recursively through each of the nodes and strip unnecessary data * in the abstract syntax tree. + * + * @param TypeDef_AstNode $node */ private static function reduceNode(array &$node): void { @@ -165,6 +181,8 @@ private static function reduceNode(array &$node): void { /** * Recursively iterate the node and its children and add information about * the node's offset (line & position) for later (e.g. error messages). + * + * @param TypeDef_AstNode $node */ private static function addPositions(array &$node, string $source): void { diff --git a/src/Repl.php b/src/Repl.php index 5e5e40a8..31760c7d 100644 --- a/src/Repl.php +++ b/src/Repl.php @@ -76,7 +76,7 @@ public function __construct( } - protected function printHelp() { + protected function printHelp(): void { $this->driver->output(Colors::get("\n". "{green}Use '{_}exit{green}' to exit REPL or '{_}exit!{green}' " . @@ -265,7 +265,7 @@ private function printTraceback(Context $ctx): void { /** * Pretty-prints out traceback from a PHP exception. */ - private function printPhpTraceback(\Throwable $e) { + private function printPhpTraceback(\Throwable $e): void { $type = get_class($e); $msg = Colors::get(sprintf("\n{white}{-red}%s", self::PHP_ERROR_HEADER)); @@ -330,11 +330,10 @@ private function gatherLines(Context $ctx): string { } - private static function isIncompleteInput(string $input) { - - if (empty(trim($input))) { - return [false, 0]; - } + /** + * @return array{bool, int} + */ + private static function isIncompleteInput(string $input): array { // Lines ending with opening curly brackets are considered incomplete. if ($input[-1] === "{") { @@ -352,9 +351,11 @@ private static function isIncompleteInput(string $input) { return [true, 0]; } + return [false, 0]; + } - private function loadHistory() { + private function loadHistory(): void { if (is_readable(self::$historyFilePath)) { $this->driver->loadHistory(self::$historyFilePath); @@ -362,7 +363,7 @@ private function loadHistory() { } - private function saveHistory() { + private function saveHistory(): void { if (is_writable(dirname(self::$historyFilePath))) { $this->driver->storeHistory(self::$historyFilePath); @@ -395,7 +396,7 @@ private static function getStackInfo( } - private static function formatType(AbstractValue $value) { + private static function formatType(AbstractValue $value): string { return Colors::get(sprintf( "{darkgrey}(%s %s){_}", diff --git a/src/Scope.php b/src/Scope.php index e2638547..ea2a38ca 100644 --- a/src/Scope.php +++ b/src/Scope.php @@ -25,7 +25,9 @@ class Scope { * Flag to distinguish scopes representing a class scope. * * This is used to tell function definitions that they should not set their - * scopes' parent to this scope. + * scopes' parent to this scope. In another words: Methods of a class + * should _not_ have direct access to the scope of the class. Those + * should be accessed only via the "self" special variable. * * @const int */ @@ -40,6 +42,9 @@ class Scope { /** Scope type. */ private int $type = self::TYPE_STANDARD; + /** + * @param array $variables + */ public function __construct( array $variables = [], int $type = self::TYPE_STANDARD @@ -90,6 +95,8 @@ public function getVariable(string $name): ?AbstractValue { * If the `$includeParents` argument is `true`, variables from parent scopes * will be included too (variables in child scopes have priority over those * from parent scopes). + * + * @return array */ public function getVariables(bool $includeParents = \false): array { @@ -102,6 +109,9 @@ public function getVariables(bool $includeParents = \false): array { } + /** + * @return void + */ public function setVariable(string $name, AbstractValue $value) { $this->variables[$name] = $value; } @@ -110,6 +120,7 @@ public function setVariable(string $name, AbstractValue $value) { * Set multiple variables to the scope. * * @param array $pairs + * @return void */ public function setVariables(array $pairs) { // NOTE: array_merge() instead of '+' keeps original and expected order. diff --git a/src/Stdlib/Modules/std/__builtins__.primi.php b/src/Stdlib/Modules/std/__builtins__.primi.php index e8c4886d..8f53ce31 100644 --- a/src/Stdlib/Modules/std/__builtins__.primi.php +++ b/src/Stdlib/Modules/std/__builtins__.primi.php @@ -151,7 +151,7 @@ public static function assert( * * @primi.function(no-stack, call-convention: object) */ - public static function range(CallArgs $callArgs) { + public static function range(CallArgs $callArgs): GeneratorValue { $arg1 = $callArgs->safeGetArg(0); $arg2 = $callArgs->safeGetArg(1); @@ -220,7 +220,7 @@ public static function range(CallArgs $callArgs) { * * @primi.function(no-stack) */ - public static function dir(AbstractValue $value) { + public static function dir(AbstractValue $value): ListValue { return new ListValue( array_map( [Interned::class, 'string'], diff --git a/src/Stdlib/Modules/std/math.primi.php b/src/Stdlib/Modules/std/math.primi.php index 803d06ee..24273af7 100644 --- a/src/Stdlib/Modules/std/math.primi.php +++ b/src/Stdlib/Modules/std/math.primi.php @@ -27,6 +27,9 @@ public function execute(Context $ctx): array { } + /** + * @param array $items + */ private static function minmax(string $op, array $items): AbstractValue { $minmax = $items[0]; diff --git a/src/Stdlib/Modules/std/runtime/__init__.primi.php b/src/Stdlib/Modules/std/runtime/__init__.primi.php index 5b162be9..1ae5f2c9 100644 --- a/src/Stdlib/Modules/std/runtime/__init__.primi.php +++ b/src/Stdlib/Modules/std/runtime/__init__.primi.php @@ -18,7 +18,7 @@ * @primi.function(no-stack, inject-context) */ public static function get_stack(Context $ctx): AbstractValue { - return AbstractValue::buildAuto($ctx->getTraceback()); + return AbstractValue::buildAuto($ctx->getCallStack()); } }; diff --git a/src/Stdlib/Modules/std/runtime/memory.primi.php b/src/Stdlib/Modules/std/runtime/memory.primi.php index 5041545e..bf086de9 100644 --- a/src/Stdlib/Modules/std/runtime/memory.primi.php +++ b/src/Stdlib/Modules/std/runtime/memory.primi.php @@ -4,7 +4,7 @@ use \Smuuf\Primi\Values\DictValue; use \Smuuf\Primi\Values\NumberValue; -use \Smuuf\Primi\Values\AbstractValue; +use \Smuuf\Primi\Helpers\Func; use \Smuuf\Primi\Helpers\Interned; use \Smuuf\Primi\Modules\NativeModule; @@ -58,7 +58,7 @@ public static function gc_run(): NumberValue { * @primi.function(no-stack) */ public static function gc_status(): DictValue { - return AbstractValue::buildAuto(\gc_status()); + return new DictValue(Func::array_to_couples(\gc_status())); } }; diff --git a/src/Structures/AssignmentTargets.php b/src/Structures/AssignmentTargets.php index ed46cc0c..e4eb78c4 100644 --- a/src/Structures/AssignmentTargets.php +++ b/src/Structures/AssignmentTargets.php @@ -29,6 +29,9 @@ class AssignmentTargets { /** Total number of targets. */ private int $targetsCount; + /** + * @param array $targets + */ public function __construct(array $targets = []) { if (!\array_is_list($targets)) { diff --git a/src/Structures/CallArgs.php b/src/Structures/CallArgs.php index e10ddd50..22447d63 100644 --- a/src/Structures/CallArgs.php +++ b/src/Structures/CallArgs.php @@ -37,6 +37,10 @@ class CallArgs { /** Total number of args and kwargs combined. */ private ?int $totalCount = \null; + /** + * @param array $args + * @param array $kwargs + */ public function __construct(array $args = [], array $kwargs = []) { if (!\array_is_list($args)) { diff --git a/src/Structures/FnContainer.php b/src/Structures/FnContainer.php index 4841c4ea..2efa499e 100644 --- a/src/Structures/FnContainer.php +++ b/src/Structures/FnContainer.php @@ -46,12 +46,16 @@ class FnContainer { * by its node tree). * * The closure returns some Primi value object as a result. + * + * @param TypeDef_AstNode $entryNode + * @param ?array{names: array, defaults: array} $defParams + * @return self */ public static function build( array $entryNode, string $definitionName, ModuleValue $definitionModule, - array $defParams = [], + ?array $defParams = \null, ?Scope $defScope = \null ) { @@ -117,6 +121,10 @@ public static function build( } + /** + * @param array $flags + * @return self + */ public static function buildFromClosure(callable $fn, array $flags = []) { $closure = \Closure::fromCallable($fn); @@ -213,7 +221,7 @@ public function isPhpFunction(): bool { * NOTE: Only docblock type-hinting for performance reasons. * * @param Context $ctx - * @param array $defParams + * @param array{names: array, defaults: array} $defParams * @param CallArgs $callArgs * @param Scope $scope */ diff --git a/src/Structures/MapContainer.php b/src/Structures/MapContainer.php index 0812dd63..5cd6962d 100644 --- a/src/Structures/MapContainer.php +++ b/src/Structures/MapContainer.php @@ -35,14 +35,19 @@ class MapContainer implements * Create new instance from iterable list containing `[key, value]` Primi * value tuples. * + * @param TypeDef_PrimiObjectCouples $couples + * @return self * @internal */ - public static function fromTuples(iterable $pairs) { - return new self($pairs); + public static function fromCouples(iterable $couples) { + return new self($couples); } - private function __construct(iterable $pairs = []) { - $this->setAll($pairs); + /** + * @param TypeDef_PrimiObjectCouples $couples + */ + private function __construct(iterable $couples = []) { + $this->setAll($couples); } /** @@ -56,11 +61,11 @@ private static function buildScalarKey(AbstractValue $key): string { } /** - * @param iterable $pairs + * @param TypeDef_PrimiObjectCouples $couples */ - public function setAll(iterable $pairs): void { + public function setAll(iterable $couples): void { - foreach ($pairs as [$key, $value]) { + foreach ($couples as [$key, $value]) { $scalarKey = self::buildScalarKey($key); $this->values[$scalarKey] = $value; $this->keys[$scalarKey] = $key; @@ -136,7 +141,7 @@ public function count(): int { * Returns a generator yielding keys and items from this container (as * expected). * - * @returns \Generator + * @return \Generator */ public function getItemsIterator(): \Generator { @@ -150,7 +155,7 @@ public function getItemsIterator(): \Generator { * Returns a generator yielding keys and items from this container (as * expected). * - * @returns \Generator + * @return \Generator */ public function getKeysIterator(): \Generator { yield from $this->keys; @@ -158,7 +163,7 @@ public function getKeysIterator(): \Generator { /** * Returns a generator yielding values from this container. - * @returns \Generator + * @return \Generator */ public function getValuesIterator(): \Generator { yield from $this->values; @@ -168,7 +173,7 @@ public function getValuesIterator(): \Generator { * Returns a generator yielding keys and items from this container in * reversed order. * - * @returns \Generator + * @return \Generator */ public function getReverseIterator(): \Generator { diff --git a/src/Tasks/Emitters/PosixSignalTaskEmitter.php b/src/Tasks/Emitters/PosixSignalTaskEmitter.php index f53f5ee4..d26d1b10 100644 --- a/src/Tasks/Emitters/PosixSignalTaskEmitter.php +++ b/src/Tasks/Emitters/PosixSignalTaskEmitter.php @@ -42,6 +42,8 @@ public static function catch(int $signum): void { return; } + $signalsToCatch[] = $signum; + // Let's make sure any already registered signal handler is also called. $original = \pcntl_signal_get_handler($signum); \pcntl_signal($signum, function(...$args) use ($original) { @@ -58,7 +60,7 @@ public static function catch(int $signum): void { } - public static function registerTaskQueue(TaskQueue $queue) { + public static function registerTaskQueue(TaskQueue $queue): void { // A specific receiver instance can be added only once. if (\in_array($queue, self::$queues, \true)) { @@ -76,7 +78,7 @@ public static function registerTaskQueue(TaskQueue $queue) { * avoid keeping unnecessary reference to the receiver object - to avoid * leaks caused by blocking proper garbage collection. */ - public static function unregisterTaskQueue(TaskQueue $queue) { + public static function unregisterTaskQueue(TaskQueue $queue): void { $index = \array_search($queue, self::$queues, \true); if ($index === \false) { diff --git a/src/Tasks/TaskQueue.php b/src/Tasks/TaskQueue.php index 678b613f..5a364ceb 100644 --- a/src/Tasks/TaskQueue.php +++ b/src/Tasks/TaskQueue.php @@ -12,28 +12,26 @@ class TaskQueue { use StrictObject; - /** - * Run queued tasks after this time interval passes (in seconds). - * @var int|float - */ - public static $interval = 0.25; + private Context $context; - /** @var Context */ - private $context; + /** Run queued tasks after this time interval passes (in seconds). */ + public static float $interval = 0.25; - /** @var float Measuring time. */ - private $timer; + /** For measuring time. */ + private float $timer; - /** @var array FIFO queue for scheduling tasks. */ - private $queue = []; + /** + * FIFO queue for scheduling tasks. + * + * @var array + */ + private array $queue = []; /** * Random ID for this queue instance (safer than spl_object_id() or similar * for checking uniqueness). - * - * @var string */ - private $id; + private string $id; public function __construct(Context $ctx) { $this->id = Func::unique_id(); @@ -113,7 +111,7 @@ private function executeQueued(bool $force = \false): void { } - private function executeTask(TaskInterface $task) { + private function executeTask(TaskInterface $task): void { $task->execute($this->context); } diff --git a/src/Tasks/Types/CallbackTask.php b/src/Tasks/Types/CallbackTask.php index 2cf79c92..acf63f9b 100644 --- a/src/Tasks/Types/CallbackTask.php +++ b/src/Tasks/Types/CallbackTask.php @@ -14,19 +14,19 @@ class CallbackTask implements TaskInterface { use StrictObject; - /** @var FuncValue Callback function. */ - private $fn = \null; + /** The callback function. */ + private FuncValue $fn; - /** @var array Arguments passed to callback. */ - private $args = []; + /** Arguments passed to callback. */ + private CallArgs $args; - public function __construct(FuncValue $fn, array $args = []) { + public function __construct(FuncValue $fn, ?CallArgs $args = null) { $this->fn = $fn; - $this->args = $args; + $this->args = $args ?? CallArgs::getEmpty(); } public function execute(Context $ctx): void { - $this->fn->invoke($ctx, new CallArgs($this->args)); + $this->fn->invoke($ctx, $this->args); } } diff --git a/src/Values/AbstractValue.php b/src/Values/AbstractValue.php index 65e79894..fe11536b 100644 --- a/src/Values/AbstractValue.php +++ b/src/Values/AbstractValue.php @@ -21,7 +21,11 @@ abstract class AbstractValue extends ValueFriends { /** @const string Name of Primi (object) type. */ protected const TYPE = '__undefined__'; - /** Attributes of Primi object. */ + /** + * Attributes of Primi object. + * + * @var array + */ protected array $attrs = []; /** @@ -31,6 +35,9 @@ abstract class AbstractValue extends ValueFriends { * NOTE: We're not checking \is_callable on bare $value, because for example * string 'time' would be determined to be the PHP's 'time' function and * we do not want that (and it would also be a security issue). + * + * @param mixed $value + * @return AbstractValue */ public static function buildAuto($value) { @@ -64,6 +71,8 @@ public static function buildAuto($value) { /** * Returns the core PHP value of this Primi value object. + * + * @return mixed */ final public function getInternalValue() { return $this->value; @@ -86,6 +95,8 @@ public function getStringValue(): string { /** * Returns dict array with this all attributes of this value. + * + * @return array */ final public function getAttrs(): array { return $this->attrs; @@ -205,6 +216,9 @@ public function invoke( throw new TypeError("'{$this->getTypeName()}' object is not callable"); } + /** + * @return ?\Iterator + */ public function getIterator(): ?\Iterator { return \null; } @@ -295,6 +309,7 @@ public function hash(): string { * This is mainly for the builtin dir() function Primi provides for * easy inspection of contents of an object. * + * @return array * @throws UnhashableTypeException */ public function dirItems(): ?array { diff --git a/src/Values/DictValue.php b/src/Values/DictValue.php index 56a773bd..b9b01610 100644 --- a/src/Values/DictValue.php +++ b/src/Values/DictValue.php @@ -20,9 +20,11 @@ class DictValue extends AbstractNativeValue { /** * Create new instance from iterable list containing `[key, value]` Primi * value tuples. + * + * @param TypeDef_PrimiObjectCouples $items */ public function __construct(iterable $items = []) { - $this->value = MapContainer::fromTuples($items); + $this->value = MapContainer::fromCouples($items); } public function __clone() { @@ -68,7 +70,7 @@ private static function convertToString( } /** - * @returns \Iterator + * @return \Iterator */ public function getIterator(): \Iterator { yield from $this->value->getKeysIterator(); diff --git a/src/Values/GeneratorValue.php b/src/Values/GeneratorValue.php index 85cc97c7..a48d5fb2 100644 --- a/src/Values/GeneratorValue.php +++ b/src/Values/GeneratorValue.php @@ -10,6 +10,9 @@ class GeneratorValue extends AbstractNativeValue { protected const TYPE = "Generator"; + /** + * @param iterable $it + */ public function __construct(iterable $it) { $this->value = $it; } @@ -22,6 +25,9 @@ public function getStringRepr(): string { return ""; } + /** + * @return \Iterator + */ public function getIterator(): \Iterator { if ($this->value->valid()) { yield from $this->value; diff --git a/src/Values/InstanceValue.php b/src/Values/InstanceValue.php index 51b41895..ea2e28e6 100644 --- a/src/Values/InstanceValue.php +++ b/src/Values/InstanceValue.php @@ -7,8 +7,7 @@ use \Smuuf\Primi\Values\TypeValue; /** - * NOTE: You should not instantiate this PHP class directly - use the helper - * `Interned::bool()` factory to get these. + * Class for representing instances of userland classes/types. */ class InstanceValue extends AbstractValue { diff --git a/src/Values/ListValue.php b/src/Values/ListValue.php index ec157543..2014f58f 100644 --- a/src/Values/ListValue.php +++ b/src/Values/ListValue.php @@ -8,12 +8,14 @@ use \Smuuf\Primi\Stdlib\StaticTypes; use \Smuuf\Primi\Helpers\Func; use \Smuuf\Primi\Helpers\Indices; -use \Smuuf\Primi\Helpers\Interned; class ListValue extends AbstractNativeValue { protected const TYPE = "list"; + /** + * @param array $items + */ public function __construct(array $items) { // Ensuring the list is indexed from 0. Keys will be ignored. @@ -67,6 +69,9 @@ private static function convertToString( } + /** + * @return \Iterator + */ public function getIterator(): \Iterator { yield from $this->value; } diff --git a/src/Values/RegexValue.php b/src/Values/RegexValue.php index db95bc25..8610b957 100644 --- a/src/Values/RegexValue.php +++ b/src/Values/RegexValue.php @@ -4,8 +4,8 @@ namespace Smuuf\Primi\Values; -use \Smuuf\Primi\Helpers\Func; use \Smuuf\Primi\Stdlib\StaticTypes; +use \Smuuf\Primi\Helpers\Func; /** * NOTE: You should not instantiate this PHP class directly - use the helper diff --git a/src/Values/StringValue.php b/src/Values/StringValue.php index 0352223d..89702f60 100644 --- a/src/Values/StringValue.php +++ b/src/Values/StringValue.php @@ -9,7 +9,6 @@ use \Smuuf\Primi\Ex\RuntimeError; use \Smuuf\Primi\Stdlib\StaticTypes; use \Smuuf\Primi\Helpers\Func; -use \Smuuf\Primi\Helpers\Interned; use \Smuuf\Primi\Helpers\StringEscaping; /** @@ -158,6 +157,9 @@ public function itemGet(AbstractValue $index): AbstractValue { } + /** + * @return \Iterator + */ public function getIterator(): \Iterator { return self::utfSplit($this->value); } @@ -181,6 +183,8 @@ public function doesContain(AbstractValue $right): ?bool { /** * Return a generator yielding each of this string's characters as * new one-character StringValue objects. + * + * @return \Generator */ private static function utfSplit(string $string): \Generator { diff --git a/src/Values/TupleValue.php b/src/Values/TupleValue.php index 0650e6ae..b53956fa 100644 --- a/src/Values/TupleValue.php +++ b/src/Values/TupleValue.php @@ -95,7 +95,7 @@ public function isTruthy(): bool { } /** - * @returns \Iterator + * @return \Iterator */ public function getIterator(): \Iterator { yield from $this->value; diff --git a/src/Values/TypeValue.php b/src/Values/TypeValue.php index 622a17d9..888d4ef2 100644 --- a/src/Values/TypeValue.php +++ b/src/Values/TypeValue.php @@ -32,6 +32,9 @@ final class TypeValue extends AbstractNativeValue { /** A parent Primi type of this type. */ protected ?TypeValue $parent; + /** + * @param array $attrs + */ public function __construct( string $name, ?TypeValue $parent = \null, diff --git a/tests/unit/helper.blockwrapper.phpt b/tests/unit/helper.blockwrapper.phpt index 50bb2190..7100f320 100644 --- a/tests/unit/helper.blockwrapper.phpt +++ b/tests/unit/helper.blockwrapper.phpt @@ -24,7 +24,7 @@ class YayWrapper extends AbstractWrapper { $gatherer[] = "enter {$this->arg}"; } - public function executeAfter() { + public function executeAfter(): void { global $gatherer; $gatherer[] = "exit {$this->arg}"; } diff --git a/tests/unit/scope.parents.phpt b/tests/unit/scope.parents.phpt index 4e8ff3a8..c8670c12 100644 --- a/tests/unit/scope.parents.phpt +++ b/tests/unit/scope.parents.phpt @@ -1,9 +1,9 @@