aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/MathExecutor.php
diff options
context:
space:
mode:
authorJavier Marín <javier@marinros.com>2021-07-14 05:12:41 +0300
committerGitHub <noreply@github.com>2021-07-14 05:12:41 +0300
commitd1d27b494dececc4e96e608d0bb47fd948d7f959 (patch)
tree02e73bd4eb6772cc3432667fcea7b64e413616d0 /src/NXP/MathExecutor.php
parentaa37abbaeb73ac1cac5798608da51d813e976a25 (diff)
PhpStan support, consts visibilty and name for tokens (#89)
* Added handler to define not found variables Added support for string variables Fixed strings and ints comparison error * Check if variables have scalar types (int, float, string and bool) Better $onVarNotFound logic * Better support for null variables * Better support for null variables * Better support for null variables * Allow null values in `setVar` method * Support for unary positive operator * Add PhpStan config file Fix PhpStan warnings Set consts visibility Add name info to variable tokens for easier debugging Co-authored-by: Javier Marín <contacto@ideatic.net>
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r--src/NXP/MathExecutor.php36
1 files changed, 18 insertions, 18 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index a774561..5584d1a 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -1,5 +1,4 @@
<?php
-
/**
* This file is part of the MathExecutor package
*
@@ -14,6 +13,7 @@ namespace NXP;
use NXP\Classes\Calculator;
use NXP\Classes\CustomFunction;
use NXP\Classes\Operator;
+use NXP\Classes\Token;
use NXP\Classes\Tokenizer;
use NXP\Exception\DivisionByZeroException;
use NXP\Exception\MathExecutorException;
@@ -29,12 +29,12 @@ class MathExecutor
/**
* Available variables
*
- * @var array
+ * @var array<string, float|string>
*/
private $variables = [];
/**
- * @var callable
+ * @var callable|null
*/
private $onVarNotFound = null;
@@ -44,12 +44,12 @@ class MathExecutor
private $operators = [];
/**
- * @var CustomFunction[]
+ * @var array<string, CustomFunction>
*/
private $functions = [];
/**
- * @var array
+ * @var array<string, Token[]>
*/
private $cache = [];
@@ -80,7 +80,7 @@ class MathExecutor
/**
* Get the default operators
*
- * @return array of class names
+ * @return array<string, array{callable, int, bool}>
*/
protected function defaultOperators() : array
{
@@ -220,7 +220,7 @@ class MathExecutor
* Gets the default functions as an array. Key is function name
* and value is the function as a closure.
*
- * @return array
+ * @return array<callable>
*/
protected function defaultFunctions() : array
{
@@ -436,14 +436,14 @@ class MathExecutor
*/
public function execute(string $expression, bool $cache = true)
{
- $cachekey = $expression;
- if (!array_key_exists($cachekey, $this->cache)) {
+ $cacheKey = $expression;
+ if (!array_key_exists($cacheKey, $this->cache)) {
$tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation();
if ($cache) {
- $this->cache[$cachekey] = $tokens;
+ $this->cache[$cacheKey] = $tokens;
}
} else {
- $tokens = $this->cache[$cachekey];
+ $tokens = $this->cache[$cacheKey];
}
$calculator = new Calculator($this->functions, $this->operators);
@@ -468,7 +468,7 @@ class MathExecutor
/**
* Returns the default variables names as key/value pairs
*
- * @return array
+ * @return array<string, float>
*/
protected function defaultVars() : array
{
@@ -481,7 +481,7 @@ class MathExecutor
/**
* Get all vars
*
- * @return array
+ * @return array<string, float|string>
*/
public function getVars() : array
{
@@ -507,7 +507,7 @@ class MathExecutor
* Add variable to executor
*
* @param string $variable
- * @param integer|float $value
+ * @param int|float $value
* @return MathExecutor
*/
public function setVar(string $variable, $value) : self
@@ -524,7 +524,7 @@ class MathExecutor
/**
* Add variables to executor
*
- * @param array $variables
+ * @param array<string, float|int|string> $variables
* @param bool $clear Clear previous variables
* @return MathExecutor
* @throws \Exception
@@ -580,7 +580,7 @@ class MathExecutor
/**
* Get all registered operators to executor
*
- * @return array of operator class names
+ * @return array<Operator> of operator class names
*/
public function getOperators()
{
@@ -590,7 +590,7 @@ class MathExecutor
/**
* Get all registered functions
*
- * @return array containing callback and places indexed by
+ * @return array<string, CustomFunction> containing callback and places indexed by
* function name
*/
public function getFunctions() : array
@@ -616,7 +616,7 @@ class MathExecutor
/**
* Get cache array with tokens
- * @return array
+ * @return array<string, Token[]>
*/
public function getCache() : array
{