diff options
Diffstat (limited to 'src/NXP/Classes')
-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 |
4 files changed, 30 insertions, 17 deletions
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; } } |