diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/NXP/Classes/Calculator.php | 10 | ||||
-rw-r--r-- | src/NXP/Classes/CustomFunction.php | 5 | ||||
-rw-r--r-- | src/NXP/Classes/Operator.php | 11 | ||||
-rw-r--r-- | src/NXP/Classes/Token.php | 11 | ||||
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 13 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 11 |
6 files changed, 11 insertions, 50 deletions
diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 6b63503..80dee36 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -20,22 +20,14 @@ use NXP\Exception\UnknownVariableException; */ class Calculator { - /** @var array<string, CustomFunction> */ - private array $functions = []; - - /** @var array<Operator> */ - private array $operators = []; - /** * @todo PHP8: Use constructor property promotion -> public function __construct(private array $functions, private array $operators) * * @param array<string, CustomFunction> $functions * @param array<Operator> $operators */ - public function __construct(array $functions, array $operators) + public function __construct(private array $functions, private array $operators) { - $this->functions = $functions; - $this->operators = $operators; } /** diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 85db379..e4cdd76 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -8,8 +8,6 @@ use ReflectionFunction; class CustomFunction { - public string $name = ''; - /** * @var callable $function */ @@ -26,9 +24,8 @@ class CustomFunction * * @throws ReflectionException */ - public function __construct(string $name, callable $function) + public function __construct(public string $name, callable $function) { - $this->name = $name; $this->function = $function; $reflection = (new ReflectionFunction($function)); $this->isVariadic = $reflection->isVariadic(); diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index 7dee06d..231b1d9 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -7,12 +7,6 @@ use ReflectionFunction; class Operator { - public string $operator = ''; - - public bool $isRightAssoc = false; - - public int $priority = 0; - /** * @var callable(\SplStack) */ @@ -23,11 +17,8 @@ class Operator /** * Operator constructor. */ - public function __construct(string $operator, bool $isRightAssoc, int $priority, callable $function) + public function __construct(public string $operator, public bool $isRightAssoc, public int $priority, callable $function) { - $this->operator = $operator; - $this->isRightAssoc = $isRightAssoc; - $this->priority = $priority; $this->function = $function; $reflection = new ReflectionFunction($function); $this->places = $reflection->getNumberOfParameters(); diff --git a/src/NXP/Classes/Token.php b/src/NXP/Classes/Token.php index 7532e77..ae460a9 100644 --- a/src/NXP/Classes/Token.php +++ b/src/NXP/Classes/Token.php @@ -22,22 +22,13 @@ class Token public const Space = 'space'; - public string $type = self::Literal; - - public $value; - - public ?string $name; - public ?int $paramCount = null;//to store function parameter count in stack /** * Token constructor. * */ - public function __construct(string $type, $value, ?string $name = null) + public function __construct(public string $type, public mixed $value, public ?string $name = null) { - $this->type = $type; - $this->value = $value; - $this->name = $name; } } diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index a74bff6..656961a 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -23,17 +23,12 @@ class Tokenizer /** @var array<Token> */ public array $tokens = []; - private string $input = ''; - private string $numberBuffer = ''; private string $stringBuffer = ''; private bool $allowNegative = true; - /** @var array<Operator> */ - private array $operators = []; - private bool $inSingleQuotedString = false; private bool $inDoubleQuotedString = false; @@ -42,10 +37,8 @@ class Tokenizer * Tokenizer constructor. * @param Operator[] $operators */ - public function __construct(string $input, array $operators) + public function __construct(private string $input, private array $operators) { - $this->input = $input; - $this->operators = $operators; } public function tokenize() : self @@ -142,7 +135,7 @@ class Tokenizer break; /** @noinspection PhpMissingBreakStatementInspection */ case 'e' === \strtolower($ch): - if (\strlen($this->numberBuffer) && false !== \strpos($this->numberBuffer, '.')) { + if (\strlen($this->numberBuffer) && \str_contains($this->numberBuffer, '.')) { $this->numberBuffer .= 'e'; $this->allowNegative = false; @@ -330,7 +323,7 @@ class Tokenizer break; } $tokens[] = $ctoken; - } catch (RuntimeException $e) { + } catch (RuntimeException) { throw new IncorrectBracketsException(); } } diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index e991586..c857e99 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -140,9 +140,8 @@ class MathExecutor * Get a specific var * * @throws UnknownVariableException if VarNotFoundHandler is not set - * @return int|float */ - public function getVar(string $variable) + public function getVar(string $variable) : mixed { if (! \array_key_exists($variable, $this->variables)) { if ($this->onVarNotFound) { @@ -160,7 +159,7 @@ class MathExecutor * * @throws MathExecutorException if the value is invalid based on the default or custom validator */ - public function setVar(string $variable, $value) : self + public function setVar(string $variable, mixed $value) : self { if ($this->onVarValidation) { \call_user_func($this->onVarValidation, $variable, $value); @@ -273,8 +272,6 @@ class MathExecutor /** * Remove a specific operator - * - * @return array<Operator> of operator class names */ public function removeOperator(string $operator) : self { @@ -380,7 +377,7 @@ class MathExecutor 180, false ], - '^' => [static fn ($a, $b) => \pow($a, $b), 220, true], + '^' => [static fn ($a, $b) => $a ** $b, 220, true], '%' => [static fn ($a, $b) => $a % $b, 180, false], '&&' => [static fn ($a, $b) => $a && $b, 100, false], '||' => [static fn ($a, $b) => $a || $b, 90, false], @@ -521,7 +518,7 @@ class MathExecutor * Default variable validation, ensures that the value is a scalar or array. * @throws MathExecutorException if the value is not a scalar */ - protected function defaultVarValidation(string $variable, $value) : void + protected function defaultVarValidation(string $variable, mixed $value) : void { if (! \is_scalar($value) && ! \is_array($value) && null !== $value) { $type = \gettype($value); |