diff options
author | Javier Marín <javier@marinros.com> | 2021-07-14 05:12:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-14 05:12:41 +0300 |
commit | d1d27b494dececc4e96e608d0bb47fd948d7f959 (patch) | |
tree | 02e73bd4eb6772cc3432667fcea7b64e413616d0 | |
parent | aa37abbaeb73ac1cac5798608da51d813e976a25 (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>
-rw-r--r-- | phpstan.neon | 9 | ||||
-rw-r--r-- | src/NXP/Classes/Calculator.php | 4 | ||||
-rw-r--r-- | src/NXP/Classes/CustomFunction.php | 5 | ||||
-rw-r--r-- | src/NXP/Classes/Operator.php | 7 | ||||
-rw-r--r-- | src/NXP/Classes/Token.php | 31 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 36 | ||||
-rw-r--r-- | tests/MathTest.php | 11 |
7 files changed, 68 insertions, 35 deletions
diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..bed17ae --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: 6 + paths: + - ./ + excludes_analyse: + - vendor/* + - tests/* + bootstrapFiles: + - vendor/autoload.php
\ No newline at end of file diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 4785a8f..a8f16af 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -44,7 +44,7 @@ class Calculator /** * Calculate array of tokens in reverse polish notation * @param Token[] $tokens - * @param array $variables + * @param array<string, float|string> $variables * @return mixed * @throws IncorrectExpressionException * @throws UnknownVariableException @@ -68,7 +68,7 @@ class Calculator throw new UnknownVariableException($variable); } - $stack[] = new Token(Token::Literal, $value); + $stack[] = new Token(Token::Literal, $value, $variable); } elseif ($token->type === Token::Function) { if (!array_key_exists($token->value, $this->functions)) { throw new UnknownFunctionException($token->value); diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index f127ad2..6e4fdc5 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -44,6 +44,11 @@ class CustomFunction } } + /** + * @param array<Token> $stack + * + * @throws IncorrectNumberOfFunctionParametersException + */ public function execute(array &$stack) : Token { if (count($stack) < $this->places) { diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index 00485bc..d9a70d7 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -24,7 +24,7 @@ class Operator public $priority; /** - * @var callable<\SplStack> + * @var callable(\SplStack) */ public $function; @@ -50,6 +50,11 @@ class Operator $this->places = $reflection->getNumberOfParameters(); } + /** + * @param array<Token> $stack + * + * @throws IncorrectExpressionException + */ public function execute(array &$stack) : Token { if (count($stack) < $this->places) { diff --git a/src/NXP/Classes/Token.php b/src/NXP/Classes/Token.php index 49bf741..e12315e 100644 --- a/src/NXP/Classes/Token.php +++ b/src/NXP/Classes/Token.php @@ -5,31 +5,34 @@ namespace NXP\Classes; class Token { - const Literal = "literal"; - const Variable = "variable"; - const Operator = "operator"; - const LeftParenthesis = "LP"; - const RightParenthesis = "RP"; - const Function = "function"; - const ParamSeparator = "separator"; - const String = "string"; - const Space = "space"; + public const Literal = "literal"; + public const Variable = "variable"; + public const Operator = "operator"; + public const LeftParenthesis = "LP"; + public const RightParenthesis = "RP"; + public const Function = "function"; + public const ParamSeparator = "separator"; + public const String = "string"; + public const Space = "space"; + /** @var self::* */ public $type = self::Literal; - /** - * @var float|string - */ + /** @var float|string */ public $value; + /** @var string */ + public $name; + /** * Token constructor. - * @param string $type + * @param self::* $type * @param float|string $value */ - public function __construct(string $type, $value) + public function __construct(string $type, $value, string $name = null) { $this->type = $type; $this->value = $value; + $this->name = $name; } } 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 { diff --git a/tests/MathTest.php b/tests/MathTest.php index f37d0ec..d086acb 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -266,6 +266,17 @@ class MathTest extends TestCase $this->assertEquals(0, $calculator->execute('10 / 0')); } + public function testUnaryOperators() + { + $calculator = new MathExecutor(); + $this->assertEquals(5, $calculator->execute('+5')); + $this->assertEquals(5, $calculator->execute('+(3+2)')); + $this->assertEquals(-5, $calculator->execute('-5')); + $this->assertEquals(5, $calculator->execute('-(-5)')); + $this->assertEquals(-5, $calculator->execute('+(-5)')); + $this->assertEquals(-5, $calculator->execute('-(3+2)')); + } + public function testZeroDivisionException() { $calculator = new MathExecutor(); |