Skip to content

Commit

Permalink
Making phpstan level 6 happy.
Browse files Browse the repository at this point in the history
And fixing various little things that were discovered.
  • Loading branch information
smuuf committed Mar 27, 2022
1 parent 830dee0 commit 05d8c29
Show file tree
Hide file tree
Showing 59 changed files with 407 additions and 141 deletions.
2 changes: 1 addition & 1 deletion bin/phpstan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@

10 changes: 7 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
parameters:
excludePaths:
- **/Parser/Compiled/PrimiParser.php
parameters:
excludePaths:
- **/Parser/Compiled/PrimiParser.php
typeAliases:
TypeDef_AstNode: 'array<mixed, mixed>'
TypeDef_PrimiObjectCouples: 'iterable<int, TypeDef_PrimiObjectCouple>'
TypeDef_PrimiObjectCouple: 'array{\Smuuf\Primi\Values\AbstractValue, \Smuuf\Primi\Values\AbstractValue}'
18 changes: 12 additions & 6 deletions src/Cli/Entrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> */
private array $config = [
// Print only parsed AST and then exit.
'only_tree' => false,
// Parse the input (build AST), print parser stats and exit.
Expand All @@ -41,17 +42,17 @@ class Entrypoint {
];

/**
* @param array $args Arguments to the CLI script (without the first $0
* argument).
* @param array<string> $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) {
Expand Down Expand Up @@ -161,6 +162,11 @@ private static function errorExit(string $text): void {
die(1);
}

/**
* @param array<string, mixed> $defaults
* @param array<string, mixed> $args
* @return array<string, mixed>
*/
private function parseArguments(array $defaults, array $args): array {

$cfg = $defaults;
Expand Down
12 changes: 11 additions & 1 deletion src/Code/Ast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Code/AstProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -70,6 +76,7 @@ private function storeIntoCache(string $key, array $ast): void {
}

/**
* @return TypeDef_AstNode
* @throws SyntaxError
*/
private static function parseSource(Source $source): array {
Expand Down
4 changes: 4 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getTempDir(): ?string {
// Paths for finding modules.
//

/** @var array<string> */
private array $importPaths = [
__DIR__ . '/Stdlib/Modules',
];
Expand All @@ -65,6 +66,9 @@ public function addImportPath(string $path): void {
$this->importPaths[] = Func::validate_dirs([$path])[0];
}

/**
* @return array<string>
*/
public function getImportPaths(): array {
return $this->importPaths;
}
Expand Down
14 changes: 10 additions & 4 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -205,14 +201,24 @@ public function getVariable(string $name): ?AbstractValue {
?? $this->builtins->getVariable($name);
}

/**
* @return array<string, AbstractValue>
*/
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<string, AbstractValue> $pairs
* @return void
*/
public function setVariables(array $pairs) {
$this->currentScope->setVariables($pairs);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Ex/ArgumentCountError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/Ex/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class ErrorException extends BaseException {

/**
* Traceback (which is really just the callstack).
* @var array<StackFrame>
*
* @var ?array<StackFrame>
*/
private ?array $traceback = \null;

/**
* @param array<StackFrame> $traceback
* @param ?array<StackFrame> $traceback
*/
public function __construct(
string $msg,
Expand Down Expand Up @@ -55,8 +56,9 @@ public function getLocation(): Location {
}

/**
* Traceback, if available.
* @return array<string, StackFrame>
* Traceback, if there's any.
*
* @return ?array<StackFrame>
*/
public function getTraceback(): ?array {
return $this->traceback;
Expand Down
9 changes: 8 additions & 1 deletion src/Ex/ReturnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Handlers/ChainedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
abstract class ChainedHandler extends Handler {

/**
* @param TypeDef_AstNode $node
* @return mixed
*/
abstract public static function chain(
array $node,
Context $context,
Expand Down
2 changes: 2 additions & 0 deletions src/Handlers/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
6 changes: 6 additions & 0 deletions src/Handlers/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/Handlers/Kinds/DictDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ protected static function handle(array $node, Context $context) {

}

/**
* @param array<TypeDef_AstNode> $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),
Expand Down
4 changes: 4 additions & 0 deletions src/Handlers/Kinds/FunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static function reduce(array &$node): void {

}

/**
* @param TypeDef_AstNode $paramsNode
* @return array{names: array<string, bool>, defaults: array<string, TypeDef_AstNode>}
*/
public static function prepareParameters(array $paramsNode): array {

// Prepare dict array for passing specifics about parameters expected
Expand Down
9 changes: 7 additions & 2 deletions src/Handlers/Kinds/ListDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -20,6 +21,10 @@ protected static function handle(array $node, Context $context) {

}

/**
* @param array<TypeDef_AstNode> $itemNodes
* @return array<AbstractValue>
*/
protected static function buildValues(
array $itemNodes,
Context $context
Expand Down
5 changes: 5 additions & 0 deletions src/Handlers/Kinds/TupleDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,10 @@ protected static function handle(array $node, Context $context) {

}

/**
* @param array<TypeDef_AstNode> $itemNodes
* @return array<AbstractValue>
*/
protected static function buildValues(
array $itemNodes,
Context $context
Expand Down
6 changes: 6 additions & 0 deletions src/Handlers/SharedLogicalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static function reduce(array &$node): void {

}

/**
* @param TypeDef_AstNode $node
*/
private static function handleAnd(
array $node,
Context $context
Expand All @@ -80,6 +83,9 @@ private static function handleAnd(

}

/**
* @param TypeDef_AstNode $node
*/
private static function handleOr(
array $node,
Context $context
Expand Down
Loading

0 comments on commit 05d8c29

Please sign in to comment.