diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..032c1bd --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto + diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 748991b..6b6c6d9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,5 +36,4 @@ jobs: - name: PHP CS Fixer if: matrix.os != 'windows-latest' run: | - vendor/bin/php-cs-fixer fix --dry-run -v ./src - vendor/bin/php-cs-fixer fix --dry-run -v ./tests + vendor/bin/php-cs-fixer fix --dry-run -v diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..2ec098c --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,282 @@ +setRiskyAllowed(true) + ->setIndent(" ") + ->setRules([ + // Each line of multi-line DocComments must have an asterisk [PSR-5] and must be aligned with the first one. + 'align_multiline_comment' => ['comment_type'=>'all_multiline'], + // Each element of an array must be indented exactly once. + 'array_indentation' => true, + // Converts simple usages of `array_push($x, $y);` to `$x[] = $y;`. + 'array_push' => true, + // PHP arrays should be declared using the configured syntax. + 'array_syntax' => ['syntax'=>'short'], + // Converts backtick operators to `shell_exec` calls. + 'backtick_to_shell_exec' => true, + // Binary operators should be surrounded by space as configured. + 'binary_operator_spaces' => true, + // There MUST be one blank line after the namespace declaration. + 'blank_line_after_namespace' => true, + // Ensure there is no code on the same line as the PHP open tag and it is followed by a blank line. + 'blank_line_after_opening_tag' => true, + // An empty line feed must precede any configured statement. + 'blank_line_before_statement' => ['statements'=>['break','case','continue','declare','default','exit','do','exit','for','foreach','goto','if','return','switch','throw','try','while','yield']], + // A single space or none should be between cast and variable. + 'cast_spaces' => ['space'=>'none'], + // Class, trait and interface elements must be separated with one or none blank line. + 'class_attributes_separation' => true, + // Whitespace around the keywords of a class, trait or interfaces definition should be one space. + 'class_definition' => true, + // Namespace must not contain spacing, comments or PHPDoc. + 'clean_namespace' => true, + // Using `isset($var) &&` multiple times should be done in one call. + 'combine_consecutive_issets' => true, + // Calling `unset` on multiple items should be done in one call. + 'combine_consecutive_unsets' => true, + // Replace multiple nested calls of `dirname` by only one call with second `$level` parameter. Requires PHP >= 7.0. + 'combine_nested_dirname' => true, + // Comments with annotation should be docblock when used on structural elements. + 'comment_to_phpdoc' => true, + // Remove extra spaces in a nullable typehint. + 'compact_nullable_typehint' => true, + // Concatenation should be spaced according configuration. + 'concat_space' => ['spacing'=>'one'], + // The PHP constants `true`, `false`, and `null` MUST be written using the correct casing. + 'constant_case' => true, + // Equal sign in declare statement should be surrounded by spaces or not following configuration. + 'declare_equal_normalize' => ['space'=>'single'], + // Replaces `dirname(__FILE__)` expression with equivalent `__DIR__` constant. + 'dir_constant' => true, + // The keyword `elseif` should be used instead of `else if` so that all control keywords look like single words. + 'elseif' => true, + // PHP code MUST use only UTF-8 without BOM (remove BOM). + 'encoding' => true, + // Replace deprecated `ereg` regular expression functions with `preg`. + 'ereg_to_preg' => true, + // Add curly braces to indirect variables to make them clear to understand. Requires PHP >= 7.0. + 'explicit_indirect_variable' => true, + // Converts implicit variables into explicit ones in double-quoted strings or heredoc syntax. + 'explicit_string_variable' => true, + // Order the flags in `fopen` calls, `b` and `t` must be last. + 'fopen_flag_order' => true, + // PHP code must use the long ` true, + // Spaces should be properly placed in a function declaration. + 'function_declaration' => ['closure_function_spacing'=>'none'], + // Replace core functions calls returning constants with the constants. + 'function_to_constant' => true, + // Ensure single space between function's argument and its typehint. + 'function_typehint_space' => true, + // Renames PHPDoc tags. + 'general_phpdoc_tag_rename' => true, + // Function `implode` must be called with 2 arguments in the documented order. + 'implode_call' => true, + // Include/Require and file path should be divided with a single space. File path should not be placed under brackets. + 'include' => true, + // Code MUST use configured indentation type. + 'indentation_type' => true, + // Replaces `is_null($var)` expression with `null === $var`. + 'is_null' => true, + // All PHP files must use same line ending. + 'line_ending' => true, + // Ensure there is no code on the same line as the PHP open tag. + 'linebreak_after_opening_tag' => true, + // List (`array` destructuring) assignment should be declared using the configured syntax. Requires PHP >= 7.1. + 'list_syntax' => ['syntax'=>'short'], + // Use `&&` and `||` logical operators instead of `and` and `or`. + 'logical_operators' => true, + // Cast should be written in lower case. + 'lowercase_cast' => true, + // PHP keywords MUST be in lower case. + 'lowercase_keywords' => true, + // Class static references `self`, `static` and `parent` MUST be in lower case. + 'lowercase_static_reference' => true, + // Magic constants should be referred to using the correct casing. + 'magic_constant_casing' => true, + // Magic method definitions and calls must be using the correct casing. + 'magic_method_casing' => true, + // In method arguments and method call, there MUST NOT be a space before each comma and there MUST be one space after each comma. Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. + 'method_argument_space' => true, + // Method chaining MUST be properly indented. Method chaining with different levels of indentation is not supported. + 'method_chaining_indentation' => true, + // Replaces `intval`, `floatval`, `doubleval`, `strval` and `boolval` function calls with according type casting operator. + 'modernize_types_casting' => true, + // Forbid multi-line whitespace before the closing semicolon or move the semicolon to the new line for chained calls. + 'multiline_whitespace_before_semicolons' => true, + // Function defined by PHP should be called using the correct casing. + 'native_function_casing' => true, + // Add leading `\` before function invocation to speed up resolving. + 'native_function_invocation' => ['include'=>['@all','trans']], + // Native type hints for functions should use the correct case. + 'native_function_type_declaration_casing' => true, + // All instances created with new keyword must be followed by braces. + 'new_with_braces' => true, + // Master functions shall be used instead of aliases. + 'no_alias_functions' => true, + // Master language constructs shall be used instead of aliases. + 'no_alias_language_construct_call' => true, + // Replace control structure alternative syntax to use braces. + 'no_alternative_syntax' => true, + // There should not be blank lines between docblock and the documented element. + 'no_blank_lines_after_phpdoc' => true, + // There must be a comment when fall-through is intentional in a non-empty case body. + 'no_break_comment' => ['comment_text'=>'Intentionally fall through'], + // The closing `? >` tag MUST be omitted from files containing only PHP. + 'no_closing_tag' => true, + // There should not be any empty comments. + 'no_empty_comment' => true, + // There should not be empty PHPDoc blocks. + 'no_empty_phpdoc' => true, + // Remove useless (semicolon) statements. + 'no_empty_statement' => true, + // Replace accidental usage of homoglyphs (non ascii characters) in names. + 'no_homoglyph_names' => true, + // Remove leading slashes in `use` clauses. + 'no_leading_import_slash' => true, + // The namespace declaration line shouldn't contain leading whitespace. + 'no_leading_namespace_whitespace' => true, + // Either language construct `print` or `echo` should be used. + 'no_mixed_echo_print' => true, + // Operator `=>` should not be surrounded by multi-line whitespaces. + 'no_multiline_whitespace_around_double_arrow' => true, + // Convert PHP4-style constructors to `__construct`. + 'no_php4_constructor' => true, + // Short cast `bool` using double exclamation mark should not be used. + 'no_short_bool_cast' => true, + // When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis. + 'no_spaces_after_function_name' => true, + // There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis. + 'no_spaces_inside_parenthesis' => true, + // Removes `@param`, `@return` and `@var` tags that don't provide any useful information. + 'no_superfluous_phpdoc_tags' => true, + // Remove trailing whitespace at the end of non-blank lines. + 'no_trailing_whitespace' => true, + // There MUST be no trailing spaces inside comment or PHPDoc. + 'no_trailing_whitespace_in_comment' => true, + // Removes unneeded parentheses around control statements. + 'no_unneeded_control_parentheses' => true, + // Removes unneeded curly braces that are superfluous and aren't part of a control structure's body. + 'no_unneeded_curly_braces' => true, + // A `final` class must not have `final` methods and `private` methods must not be `final`. + 'no_unneeded_final_method' => true, + // In function arguments there must not be arguments with default values before non-default ones. + 'no_unreachable_default_argument_value' => true, + // Variables must be set `null` instead of using `(unset)` casting. + 'no_unset_cast' => true, + // Properties should be set to `null` instead of using `unset`. + 'no_unset_on_property' => true, + // Unused `use` statements must be removed. + 'no_unused_imports' => true, + // There should not be useless `else` cases. + 'no_useless_else' => true, + // There should not be an empty `return` statement at the end of a function. + 'no_useless_return' => true, + // There must be no `sprintf` calls with only the first argument. + 'no_useless_sprintf' => true, + // In array declaration, there MUST NOT be a whitespace before each comma. + 'no_whitespace_before_comma_in_array' => true, + // Remove trailing whitespace at the end of blank lines. + 'no_whitespace_in_blank_line' => true, + // Remove Zero-width space (ZWSP), Non-breaking space (NBSP) and other invisible unicode symbols. + 'non_printable_character' => ['use_escape_sequences_in_strings'=>true], + // Array index should always be written by using square braces. + 'normalize_index_brace' => true, + // Logical NOT operators (`!`) should have one trailing whitespace. + 'not_operator_with_successor_space' => true, + // Adds or removes `?` before type declarations for parameters with a default `null` value. + 'nullable_type_declaration_for_default_null_value' => true, + // There should not be space before or after object operators `->` and `?->`. + 'object_operator_without_whitespace' => true, + // Orders the elements of classes/interfaces/traits. + 'ordered_class_elements' => ['order'=>['use_trait','constant_public','constant_protected','constant_private','property_public','property_protected','property_private','construct','destruct','magic','phpunit','method_public','method_protected','method_private']], + // Ordering `use` statements. + 'ordered_imports' => true, + // Orders the interfaces in an `implements` or `interface extends` clause. + 'ordered_interfaces' => true, + // Trait `use` statements must be sorted alphabetically. + 'ordered_traits' => true, + // Classy that does not inherit must not have `@inheritdoc` tags. + 'phpdoc_no_useless_inheritdoc' => true, + // Annotations in PHPDoc should be ordered so that `@param` annotations come first, then `@throws` annotations, then `@return` annotations. + 'phpdoc_order' => true, + // The type of `@return` annotations of methods returning a reference to itself must the configured one. + 'phpdoc_return_self_reference' => true, + // Scalar types should always be written in the same form. `int` not `integer`, `bool` not `boolean`, `float` not `real` or `double`. + 'phpdoc_scalar' => true, + // Fixes casing of PHPDoc tags. + 'phpdoc_tag_casing' => true, + // Converts `protected` variables and methods to `private` where possible. + 'protected_to_private' => true, + // Classes must be in a path that matches their namespace, be at least one namespace deep and the class name should match the file name. + 'psr_autoloading' => true, + // There should be one or no space before colon, and one space after it in return type declarations, according to configuration. + 'return_type_declaration' => ['space_before'=>'one'], + // Instructions must be terminated with a semicolon. + 'semicolon_after_instruction' => true, + // Cast shall be used, not `settype`. + 'set_type_to_cast' => true, + // Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`. + 'short_scalar_cast' => true, + // Converts explicit variables in double-quoted strings and heredoc syntax from simple to complex format (`${` to `{$`). + 'simple_to_complex_string_variable' => true, + // Simplify `if` control structures that return the boolean result of their condition. + 'simplified_if_return' => true, + // A return statement wishing to return `void` should not return `null`. + 'simplified_null_return' => true, + // A PHP file without end tag must always end with a single empty line feed. + 'single_blank_line_at_eof' => true, + // There should be exactly one blank line before a namespace declaration. + 'single_blank_line_before_namespace' => true, + // There MUST NOT be more than one property or constant declared per statement. + 'single_class_element_per_statement' => true, + // There MUST be one use keyword per declaration. + 'single_import_per_statement' => true, + // Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block. + 'single_line_after_imports' => true, + // Single-line comments and multi-line comments with only one line of actual content should use the `//` syntax. + 'single_line_comment_style' => true, + // Convert double quotes to single quotes for simple strings. + 'single_quote' => true, + // Each trait `use` must be done as single statement. + 'single_trait_insert_per_statement' => true, + // Replace all `<>` with `!=`. + 'standardize_not_equals' => true, + // Lambdas not (indirect) referencing `$this` must be declared `static`. + 'static_lambda' => true, + // All multi-line strings must use correct line ending. + 'string_line_ending' => true, + // A case should be followed by a colon and not a semicolon. + 'switch_case_semicolon_to_colon' => true, + // Removes extra spaces between colon and case value. + 'switch_case_space' => true, + // Switch case must not be ended with `continue` but with `break`. + 'switch_continue_to_break' => true, + // Standardize spaces around ternary operator. + 'ternary_operator_spaces' => true, + // Use the Elvis operator `?:` where possible. + 'ternary_to_elvis_operator' => true, + // Use `null` coalescing operator `??` where possible. Requires PHP >= 7.0. + 'ternary_to_null_coalescing' => true, + // Arrays should be formatted like function/method arguments, without leading or trailing single line space. + 'trim_array_spaces' => true, + // Unary operators should be placed adjacent to their operands. + 'unary_operator_spaces' => true, + // Visibility MUST be declared on all properties and methods; `abstract` and `final` MUST be declared before the visibility; `static` MUST be declared after the visibility. + 'visibility_required' => true, + // Add `void` return type to functions with missing or empty return statements, but priority is given to `@return` annotations. Requires PHP >= 7.1. + 'void_return' => true, + // In array declaration, there MUST be a whitespace after each comma. + 'whitespace_after_comma_in_array' => true, + // Write conditions in Yoda style (`true`), non-Yoda style (`['equal' => false, 'identical' => false, 'less_and_greater' => false]`) or ignore those conditions (`null`) based on configuration. + 'yoda_style' => true, + ]); + +return $config->setFinder(PhpCsFixer\Finder::create() + ->exclude('vendor') + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ); + diff --git a/composer.json b/composer.json index 61d59eb..6d3759d 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "require": { - "php": ">=7.3" + "php": ">=7.4" }, "require-dev": { "phpunit/phpunit": ">=9.0", diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index a8f16af..2bb5b4d 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -20,21 +20,10 @@ use NXP\Exception\UnknownVariableException; */ class Calculator { - /** - * @var CustomFunction[] - */ - private $functions; + private array $functions = []; - /** - * @var Operator[] - */ - private $operators; + private array $operators = []; - /** - * Calculator constructor. - * @param CustomFunction[] $functions - * @param Operator[] $operators - */ public function __construct(array $functions, array $operators) { $this->functions = $functions; @@ -45,46 +34,49 @@ class Calculator * Calculate array of tokens in reverse polish notation * @param Token[] $tokens * @param array $variables - * @return mixed * @throws IncorrectExpressionException * @throws UnknownVariableException */ - public function calculate(array $tokens, array $variables, callable $onVarNotFound = null) + public function calculate(array $tokens, array $variables, ?callable $onVarNotFound = null) { /** @var Token[] $stack */ $stack = []; + foreach ($tokens as $token) { - if ($token->type === Token::Literal || $token->type === Token::String) { + if (Token::Literal === $token->type || Token::String === $token->type) { $stack[] = $token; - } elseif ($token->type === Token::Variable) { + } elseif (Token::Variable === $token->type) { $variable = $token->value; $value = null; - if (array_key_exists($variable, $variables)) { + + if (\array_key_exists($variable, $variables)) { $value = $variables[$variable]; } elseif ($onVarNotFound) { - $value = call_user_func($onVarNotFound, $variable); + $value = \call_user_func($onVarNotFound, $variable); } else { throw new UnknownVariableException($variable); } $stack[] = new Token(Token::Literal, $value, $variable); - } elseif ($token->type === Token::Function) { - if (!array_key_exists($token->value, $this->functions)) { + } elseif (Token::Function === $token->type) { + if (! \array_key_exists($token->value, $this->functions)) { throw new UnknownFunctionException($token->value); } $stack[] = $this->functions[$token->value]->execute($stack); - } elseif ($token->type === Token::Operator) { - if (!array_key_exists($token->value, $this->operators)) { + } elseif (Token::Operator === $token->type) { + if (! \array_key_exists($token->value, $this->operators)) { throw new UnknownOperatorException($token->value); } $stack[] = $this->operators[$token->value]->execute($stack); } } - $result = array_pop($stack); - if ($result === null || !empty($stack)) { + $result = \array_pop($stack); + + if (null === $result || ! empty($stack)) { throw new IncorrectExpressionException('Stack must be empty'); } + return $result->value; } } diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 225495f..bc0f4e7 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -8,25 +8,17 @@ use ReflectionFunction; class CustomFunction { - /** - * @var string - */ - public $name; + public string $name = ''; /** * @var callable $function */ public $function; - /** - * @var int - */ - public $places; + public int $places = 0; /** * CustomFunction constructor. - * @param string $name - * @param callable $function * @param int $places * @throws ReflectionException * @throws IncorrectNumberOfFunctionParametersException @@ -35,7 +27,8 @@ class CustomFunction { $this->name = $name; $this->function = $function; - if ($places === null) { + + if (null === $places) { $reflection = new ReflectionFunction($function); $this->places = $reflection->getNumberOfParameters(); } else { @@ -48,17 +41,18 @@ class CustomFunction * * @throws IncorrectNumberOfFunctionParametersException */ - public function execute(array &$stack): Token + public function execute(array &$stack) : Token { - if (count($stack) < $this->places) { + if (\count($stack) < $this->places) { throw new IncorrectNumberOfFunctionParametersException($this->name); } $args = []; + for ($i = 0; $i < $this->places; $i++) { - array_unshift($args, array_pop($stack)->value); + \array_unshift($args, \array_pop($stack)->value); } - $result = call_user_func_array($this->function, $args); + $result = \call_user_func_array($this->function, $args); return new Token(Token::Literal, $result); } diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index 1b1acc4..7dee06d 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -7,37 +7,21 @@ use ReflectionFunction; class Operator { - /** - * @var string - */ - public $operator; + public string $operator = ''; - /** - * @var bool - */ - public $isRightAssoc; + public bool $isRightAssoc = false; - /** - * @var int - */ - public $priority; + public int $priority = 0; /** * @var callable(\SplStack) */ public $function; - /** - * @var int - */ - public $places; + public int $places = 0; /** * Operator constructor. - * @param string $operator - * @param bool $isRightAssoc - * @param int $priority - * @param callable $function */ public function __construct(string $operator, bool $isRightAssoc, int $priority, callable $function) { @@ -54,17 +38,18 @@ class Operator * * @throws IncorrectExpressionException */ - public function execute(array &$stack): Token + public function execute(array &$stack) : Token { - if (count($stack) < $this->places) { + if (\count($stack) < $this->places) { throw new IncorrectExpressionException(); } $args = []; + for ($i = 0; $i < $this->places; $i++) { - array_unshift($args, array_pop($stack)->value); + \array_unshift($args, \array_pop($stack)->value); } - $result = call_user_func_array($this->function, $args); + $result = \call_user_func_array($this->function, $args); return new Token(Token::Literal, $result); } diff --git a/src/NXP/Classes/Token.php b/src/NXP/Classes/Token.php index 924771e..b2a0a64 100644 --- a/src/NXP/Classes/Token.php +++ b/src/NXP/Classes/Token.php @@ -4,31 +4,34 @@ namespace NXP\Classes; class Token { - 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"; + public const Literal = 'literal'; - /** @var self::* */ - public $type = self::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'; + + public string $type = self::Literal; - /** @var float|string */ public $value; - /** @var string */ - public $name; + public ?string $name; /** * Token constructor. - * @param self::* $type - * @param float|string $value */ - public function __construct(string $type, $value, string $name = null) + public function __construct(string $type, $value, ?string $name = null) { $this->type = $type; $this->value = $value; diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index 087a78d..1bd6d6d 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -20,44 +20,24 @@ use SplStack; */ class Tokenizer { - /** - * @var Token[] - */ - public $tokens = []; - /** - * @var string - */ - private $input = ''; - /** - * @var string - */ - private $numberBuffer = ''; - /** - * @var string - */ - private $stringBuffer = ''; - /** - * @var bool - */ - private $allowNegative = true; - /** - * @var Operator[] - */ - private $operators = []; + public array $tokens = []; - /** - * @var bool - */ - private $inSingleQuotedString = false; + private string $input = ''; - /** - * @var bool - */ - private $inDoubleQuotedString = false; + private string $numberBuffer = ''; + + private string $stringBuffer = ''; + + private bool $allowNegative = true; + + private array $operators = []; + + private bool $inSingleQuotedString = false; + + private bool $inDoubleQuotedString = false; /** * Tokenizer constructor. - * @param string $input * @param Operator[] $operators */ public function __construct(string $input, array $operators) @@ -66,109 +46,137 @@ class Tokenizer $this->operators = $operators; } - public function tokenize(): self + public function tokenize() : self { - foreach (str_split($this->input, 1) as $ch) { + foreach (\str_split($this->input, 1) as $ch) { switch (true) { case $this->inSingleQuotedString: - if ($ch === "'") { + if ("'" === $ch) { $this->tokens[] = new Token(Token::String, $this->stringBuffer); $this->inSingleQuotedString = false; $this->stringBuffer = ''; + continue 2; } $this->stringBuffer .= $ch; + continue 2; + case $this->inDoubleQuotedString: - if ($ch === '"') { + if ('"' === $ch) { $this->tokens[] = new Token(Token::String, $this->stringBuffer); $this->inDoubleQuotedString = false; $this->stringBuffer = ''; + continue 2; } $this->stringBuffer .= $ch; + continue 2; - case $ch == ' ' || $ch == "\n" || $ch == "\r" || $ch == "\t": + + case ' ' == $ch || "\n" == $ch || "\r" == $ch || "\t" == $ch: $this->tokens[] = new Token(Token::Space, ''); + continue 2; + case $this->isNumber($ch): - if ($this->stringBuffer != '') { + if ('' != $this->stringBuffer) { $this->stringBuffer .= $ch; + continue 2; } $this->numberBuffer .= $ch; $this->allowNegative = false; + break; /** @noinspection PhpMissingBreakStatementInspection */ - case strtolower($ch) === 'e': - if (strlen($this->numberBuffer) && strpos($this->numberBuffer, '.') !== false) { + case 'e' === \strtolower($ch): + if (\strlen($this->numberBuffer) && false !== \strpos($this->numberBuffer, '.')) { $this->numberBuffer .= 'e'; $this->allowNegative = false; + break; } // no break + // Intentionally fall through case $this->isAlpha($ch): - if (strlen($this->numberBuffer)) { + if (\strlen($this->numberBuffer)) { $this->emptyNumberBufferAsLiteral(); $this->tokens[] = new Token(Token::Operator, '*'); } $this->allowNegative = false; $this->stringBuffer .= $ch; + break; - case $ch == '"': + + case '"' == $ch: $this->inDoubleQuotedString = true; + continue 2; - case $ch == "'": + + case "'" == $ch: $this->inSingleQuotedString = true; + continue 2; case $this->isDot($ch): $this->numberBuffer .= $ch; $this->allowNegative = false; + break; + case $this->isLP($ch): - if ($this->stringBuffer != '') { + if ('' != $this->stringBuffer) { $this->tokens[] = new Token(Token::Function, $this->stringBuffer); $this->stringBuffer = ''; - } elseif (strlen($this->numberBuffer)) { + } elseif (\strlen($this->numberBuffer)) { $this->emptyNumberBufferAsLiteral(); $this->tokens[] = new Token(Token::Operator, '*'); } $this->allowNegative = true; $this->tokens[] = new Token(Token::LeftParenthesis, ''); + break; + case $this->isRP($ch): $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); $this->allowNegative = false; $this->tokens[] = new Token(Token::RightParenthesis, ''); + break; + case $this->isComma($ch): $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); $this->allowNegative = true; $this->tokens[] = new Token(Token::ParamSeparator, ''); + break; + default: // special case for unary operations - if ($ch == '-' || $ch == '+') { + if ('-' == $ch || '+' == $ch) { if ($this->allowNegative) { $this->allowNegative = false; - $this->tokens[] = new Token(Token::Operator, $ch == '-' ? 'uNeg' : 'uPos'); + $this->tokens[] = new Token(Token::Operator, '-' == $ch ? 'uNeg' : 'uPos'); + continue 2; } // could be in exponent, in which case negative should be added to the numberBuffer - if ($this->numberBuffer && $this->numberBuffer[strlen($this->numberBuffer) - 1] == 'e') { + if ($this->numberBuffer && 'e' == $this->numberBuffer[\strlen($this->numberBuffer) - 1]) { $this->numberBuffer .= $ch; + continue 2; } } $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); - if ($ch != '$') { - if (count($this->tokens) > 0) { - if ($this->tokens[count($this->tokens) - 1]->type === Token::Operator) { - $this->tokens[count($this->tokens) - 1]->value .= $ch; + + if ('$' != $ch) { + if (\count($this->tokens) > 0) { + if (Token::Operator === $this->tokens[\count($this->tokens) - 1]->type) { + $this->tokens[\count($this->tokens) - 1]->value .= $ch; } else { $this->tokens[] = new Token(Token::Operator, $ch); } @@ -181,107 +189,76 @@ class Tokenizer } $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); + return $this; } - private function isNumber(string $ch): bool - { - return $ch >= '0' && $ch <= '9'; - } - - private function isAlpha(string $ch): bool - { - return $ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch == '_'; - } - - private function emptyNumberBufferAsLiteral(): void - { - if (strlen($this->numberBuffer)) { - $this->tokens[] = new Token(Token::Literal, $this->numberBuffer); - $this->numberBuffer = ''; - } - } - - private function isDot(string $ch): bool - { - return $ch == '.'; - } - - private function isLP(string $ch): bool - { - return $ch == '('; - } - - private function isRP(string $ch): bool - { - return $ch == ')'; - } - - private function emptyStrBufferAsVariable(): void - { - if ($this->stringBuffer != '') { - $this->tokens[] = new Token(Token::Variable, $this->stringBuffer); - $this->stringBuffer = ''; - } - } - - private function isComma(string $ch): bool - { - return $ch == ','; - } - /** - * @return Token[] Array of tokens in revers polish notation * @throws IncorrectBracketsException * @throws UnknownOperatorException + * @return Token[] Array of tokens in revers polish notation */ - public function buildReversePolishNotation(): array + public function buildReversePolishNotation() : array { $tokens = []; /** @var SplStack $stack */ $stack = new SplStack(); + foreach ($this->tokens as $token) { switch ($token->type) { case Token::Literal: case Token::Variable: case Token::String: $tokens[] = $token; + break; + case Token::Function: case Token::LeftParenthesis: $stack->push($token); + break; + case Token::ParamSeparator: - while ($stack->top()->type !== Token::LeftParenthesis) { - if ($stack->count() === 0) { + while (Token::LeftParenthesis !== $stack->top()->type) { + if (0 === $stack->count()) { throw new IncorrectBracketsException(); } $tokens[] = $stack->pop(); } + break; + case Token::Operator: - if (!array_key_exists($token->value, $this->operators)) { + if (! \array_key_exists($token->value, $this->operators)) { throw new UnknownOperatorException($token->value); } $op1 = $this->operators[$token->value]; - while ($stack->count() > 0 && $stack->top()->type === Token::Operator) { - if (!array_key_exists($stack->top()->value, $this->operators)) { + + while ($stack->count() > 0 && Token::Operator === $stack->top()->type) { + if (! \array_key_exists($stack->top()->value, $this->operators)) { throw new UnknownOperatorException($stack->top()->value); } $op2 = $this->operators[$stack->top()->value]; + if ($op2->priority >= $op1->priority) { $tokens[] = $stack->pop(); + continue; } + break; } $stack->push($token); + break; + case Token::RightParenthesis: while (true) { try { $ctoken = $stack->pop(); - if ($ctoken->type === Token::LeftParenthesis) { + + if (Token::LeftParenthesis === $ctoken->type) { break; } $tokens[] = $ctoken; @@ -289,24 +266,77 @@ class Tokenizer throw new IncorrectBracketsException(); } } - if ($stack->count() > 0 && $stack->top()->type == Token::Function) { + + if ($stack->count() > 0 && Token::Function == $stack->top()->type) { $tokens[] = $stack->pop(); } + break; + case Token::Space: //do nothing } } - while ($stack->count() !== 0) { - if ($stack->top()->type === Token::LeftParenthesis || $stack->top()->type === Token::RightParenthesis) { + + while (0 !== $stack->count()) { + if (Token::LeftParenthesis === $stack->top()->type || Token::RightParenthesis === $stack->top()->type) { throw new IncorrectBracketsException(); } - if ($stack->top()->type === Token::Space) { + + if (Token::Space === $stack->top()->type) { $stack->pop(); + continue; } $tokens[] = $stack->pop(); } + return $tokens; } + + private function isNumber(string $ch) : bool + { + return $ch >= '0' && $ch <= '9'; + } + + private function isAlpha(string $ch) : bool + { + return $ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || '_' == $ch; + } + + private function emptyNumberBufferAsLiteral() : void + { + if (\strlen($this->numberBuffer)) { + $this->tokens[] = new Token(Token::Literal, $this->numberBuffer); + $this->numberBuffer = ''; + } + } + + private function isDot(string $ch) : bool + { + return '.' == $ch; + } + + private function isLP(string $ch) : bool + { + return '(' == $ch; + } + + private function isRP(string $ch) : bool + { + return ')' == $ch; + } + + private function emptyStrBufferAsVariable() : void + { + if ('' != $this->stringBuffer) { + $this->tokens[] = new Token(Token::Variable, $this->stringBuffer); + $this->stringBuffer = ''; + } + } + + private function isComma(string $ch) : bool + { + return ',' == $ch; + } } diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index ab550fa..f980270 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -31,7 +31,7 @@ class MathExecutor * * @var array */ - protected $variables = []; + protected array $variables = []; /** * @var callable|null @@ -46,17 +46,17 @@ class MathExecutor /** * @var Operator[] */ - protected $operators = []; + protected array $operators = []; /** * @var array */ - protected $functions = []; + protected array $functions = []; /** * @var array */ - protected $cache = []; + protected array $cache = []; /** * Base math operators @@ -66,386 +66,39 @@ class MathExecutor $this->addDefaults(); } - /** - * Set default operands and functions - * @throws ReflectionException - */ - protected function addDefaults(): void + public function __clone() { - foreach ($this->defaultOperators() as $name => $operator) { - [$callable, $priority, $isRightAssoc] = $operator; - $this->addOperator(new Operator($name, $isRightAssoc, $priority, $callable)); - } - foreach ($this->defaultFunctions() as $name => $callable) { - $this->addFunction($name, $callable); - } - - $this->onVarValidation = [$this, 'defaultVarValidation']; - $this->variables = $this->defaultVars(); - } - - /** - * Get the default operators - * - * @return array - */ - protected function defaultOperators(): array - { - return [ - '+' => [ - function ($a, $b) { - return $a + $b; - }, - 170, - false - ], - '-' => [ - function ($a, $b) { - return $a - $b; - }, - 170, - false - ], - 'uPos' => [ // unary positive token - function ($a) { - return $a; - }, - 200, - false - ], - 'uNeg' => [ // unary minus token - function ($a) { - return 0 - $a; - }, - 200, - false - ], - '*' => [ - function ($a, $b) { - return $a * $b; - }, - 180, - false - ], - '/' => [ - function ($a, $b) { - if ($b == 0) { - throw new DivisionByZeroException(); - } - return $a / $b; - }, - 180, - false - ], - '^' => [ - function ($a, $b) { - return pow($a, $b); - }, - 220, - true - ], - '&&' => [ - function ($a, $b) { - return $a && $b; - }, - 100, - false - ], - '||' => [ - function ($a, $b) { - return $a || $b; - }, - 90, - false - ], - '==' => [ - function ($a, $b) { - if (is_string($a) || is_string($b)) { - return strcmp($a, $b) == 0; - } else { - return $a == $b; - } - }, - 140, - false - ], - '!=' => [ - function ($a, $b) { - if (is_string($a) || is_string($b)) { - return strcmp($a, $b) != 0; - } else { - return $a != $b; - } - }, - 140, - false - ], - '>=' => [ - function ($a, $b) { - return $a >= $b; - }, - 150, - false - ], - '>' => [ - function ($a, $b) { - return $a > $b; - }, - 150, - false - ], - '<=' => [ - function ($a, $b) { - return $a <= $b; - }, - 150, - false - ], - '<' => [ - function ($a, $b) { - return $a < $b; - }, - 150, - false - ], - ]; + $this->addDefaults(); } /** * Add operator to executor * - * @param Operator $operator * @return MathExecutor */ - public function addOperator(Operator $operator): self + public function addOperator(Operator $operator) : self { $this->operators[$operator->operator] = $operator; - return $this; - } - /** - * Gets the default functions as an array. Key is function name - * and value is the function as a closure. - * - * @return array - */ - protected function defaultFunctions(): array - { - return [ - 'abs' => function ($arg) { - return abs($arg); - }, - 'acos' => function ($arg) { - return acos($arg); - }, - 'acosh' => function ($arg) { - return acosh($arg); - }, - 'arcsin' => function ($arg) { - return asin($arg); - }, - 'arcctg' => function ($arg) { - return M_PI/2 - atan($arg); - }, - 'arccot' => function ($arg) { - return M_PI/2 - atan($arg); - }, - 'arccotan' => function ($arg) { - return M_PI/2 - atan($arg); - }, - 'arcsec' => function ($arg) { - return acos(1/$arg); - }, - 'arccosec' => function ($arg) { - return asin(1/$arg); - }, - 'arccsc' => function ($arg) { - return asin(1/$arg); - }, - 'arccos' => function ($arg) { - return acos($arg); - }, - 'arctan' => function ($arg) { - return atan($arg); - }, - 'arctg' => function ($arg) { - return atan($arg); - }, - 'asin' => function ($arg) { - return asin($arg); - }, - 'atan' => function ($arg) { - return atan($arg); - }, - 'atan2' => function ($arg1, $arg2) { - return atan2($arg1, $arg2); - }, - 'atanh' => function ($arg) { - return atanh($arg); - }, - 'atn' => function ($arg) { - return atan($arg); - }, - 'avg' => function ($arg1, $arg2) { - return ($arg1 + $arg2) / 2; - }, - 'bindec' => function ($arg) { - return bindec($arg); - }, - 'ceil' => function ($arg) { - return ceil($arg); - }, - 'cos' => function ($arg) { - return cos($arg); - }, - 'cosec' => function ($arg) { - return 1 / sin($arg); - }, - 'csc' => function ($arg) { - return 1 / sin($arg); - }, - 'cosh' => function ($arg) { - return cosh($arg); - }, - 'ctg' => function ($arg) { - return cos($arg) / sin($arg); - }, - 'cot' => function ($arg) { - return cos($arg) / sin($arg); - }, - 'cotan' => function ($arg) { - return cos($arg) / sin($arg); - }, - 'cotg' => function ($arg) { - return cos($arg) / sin($arg); - }, - 'ctn' => function ($arg) { - return cos($arg) / sin($arg); - }, - 'decbin' => function ($arg) { - return decbin($arg); - }, - 'dechex' => function ($arg) { - return dechex($arg); - }, - 'decoct' => function ($arg) { - return decoct($arg); - }, - 'deg2rad' => function ($arg) { - return deg2rad($arg); - }, - 'exp' => function ($arg) { - return exp($arg); - }, - 'expm1' => function ($arg) { - return expm1($arg); - }, - 'floor' => function ($arg) { - return floor($arg); - }, - 'fmod' => function ($arg1, $arg2) { - return fmod($arg1, $arg2); - }, - 'hexdec' => function ($arg) { - return hexdec($arg); - }, - 'hypot' => function ($arg1, $arg2) { - return hypot($arg1, $arg2); - }, - 'if' => function ($expr, $trueval, $falseval) { - if ($expr === true || $expr === false) { - $exres = $expr; - } else { - $exres = $this->execute($expr); - } - if ($exres) { - return $this->execute($trueval); - } else { - return $this->execute($falseval); - } - }, - 'intdiv' => function ($arg1, $arg2) { - return intdiv($arg1, $arg2); - }, - 'ln' => function ($arg) { - return log($arg); - }, - 'lg' => function ($arg) { - return log10($arg); - }, - 'log' => function ($arg) { - return log($arg); - }, - 'log10' => function ($arg) { - return log10($arg); - }, - 'log1p' => function ($arg) { - return log1p($arg); - }, - 'max' => function ($arg1, $arg2) { - return max($arg1, $arg2); - }, - 'min' => function ($arg1, $arg2) { - return min($arg1, $arg2); - }, - 'octdec' => function ($arg) { - return octdec($arg); - }, - 'pi' => function () { - return pi(); - }, - 'pow' => function ($arg1, $arg2) { - return $arg1 ** $arg2; - }, - 'rad2deg' => function ($arg) { - return rad2deg($arg); - }, - 'round' => function ($arg) { - return round($arg); - }, - 'sin' => function ($arg) { - return sin($arg); - }, - 'sinh' => function ($arg) { - return sinh($arg); - }, - 'sec' => function ($arg) { - return 1 / cos($arg); - }, - 'sqrt' => function ($arg) { - return sqrt($arg); - }, - 'tan' => function ($arg) { - return tan($arg); - }, - 'tanh' => function ($arg) { - return tanh($arg); - }, - 'tn' => function ($arg) { - return tan($arg); - }, - 'tg' => function ($arg) { - return tan($arg); - } - ]; + return $this; } /** * Execute expression * - * @param string $expression - * @param bool $cache - * @return number * @throws Exception\IncorrectBracketsException * @throws Exception\IncorrectExpressionException * @throws Exception\UnknownOperatorException * @throws UnknownVariableException + * @return number */ public function execute(string $expression, bool $cache = true) { $cacheKey = $expression; - if (!array_key_exists($cacheKey, $this->cache)) { + + if (! \array_key_exists($cacheKey, $this->cache)) { $tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation(); + if ($cache) { $this->cache[$cacheKey] = $tokens; } @@ -454,6 +107,7 @@ class MathExecutor } $calculator = new Calculator($this->functions, $this->operators); + return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound); } @@ -463,26 +117,14 @@ class MathExecutor * @param string $name Name of function * @param callable $function Function * @param int $places Count of arguments - * @return MathExecutor * @throws ReflectionException + * @return MathExecutor */ - public function addFunction(string $name, ?callable $function = null, ?int $places = null): self + public function addFunction(string $name, ?callable $function = null, ?int $places = null) : self { $this->functions[$name] = new CustomFunction($name, $function, $places); - return $this; - } - /** - * Returns the default variables names as key/value pairs - * - * @return array - */ - protected function defaultVars(): array - { - return [ - 'pi' => 3.14159265359, - 'e' => 2.71828182846 - ]; + return $this; } /** @@ -490,7 +132,7 @@ class MathExecutor * * @return array */ - public function getVars(): array + public function getVars() : array { return $this->variables; } @@ -498,61 +140,47 @@ class MathExecutor /** * Get a specific var * - * @param string $variable - * @return integer|float * @throws UnknownVariableException if VarNotFoundHandler is not set + * @return int|float */ public function getVar(string $variable) { - if (!array_key_exists($variable, $this->variables)) { + if (! \array_key_exists($variable, $this->variables)) { if ($this->onVarNotFound) { - return call_user_func($this->onVarNotFound, $variable); + return \call_user_func($this->onVarNotFound, $variable); } + throw new UnknownVariableException("Variable ({$variable}) not set"); } + return $this->variables[$variable]; } /** * Add variable to executor. To set a custom validator use setVarValidationHandler. * - * @param string $variable * @param $value - * @return MathExecutor * @throws MathExecutorException if the value is invalid based on the default or custom validator + * @return MathExecutor */ - public function setVar(string $variable, $value): self + public function setVar(string $variable, $value) : self { if ($this->onVarValidation) { - call_user_func($this->onVarValidation, $variable, $value); + \call_user_func($this->onVarValidation, $variable, $value); } $this->variables[$variable] = $value; - return $this; - } - /** - * Default variable validation, ensures that the value is a scalar. - * @param string $variable - * @param $value - * @throws MathExecutorException if the value is not a scalar - */ - protected function defaultVarValidation(string $variable, $value): void - { - if (!is_scalar($value) && $value !== null) { - $type = gettype($value); - throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar"); - } + return $this; } /** * Test to see if a variable exists * - * @param string $variable */ - public function varExists(string $variable): bool + public function varExists(string $variable) : bool { - return array_key_exists($variable, $this->variables); + return \array_key_exists($variable, $this->variables); } /** @@ -560,17 +188,19 @@ class MathExecutor * * @param array $variables * @param bool $clear Clear previous variables - * @return MathExecutor * @throws \Exception + * @return MathExecutor */ - public function setVars(array $variables, bool $clear = true): self + public function setVars(array $variables, bool $clear = true) : self { if ($clear) { $this->removeVars(); } + foreach ($variables as $name => $value) { $this->setVar($name, $value); } + return $this; } @@ -578,13 +208,13 @@ class MathExecutor * Define a method that will be invoked when a variable is not found. * The first parameter will be the variable name, and the returned value will be used as the variable value. * - * @param callable $handler * * @return MathExecutor */ - public function setVarNotFoundHandler(callable $handler): self + public function setVarNotFoundHandler(callable $handler) : self { $this->onVarNotFound = $handler; + return $this; } @@ -597,21 +227,22 @@ class MathExecutor * * @return MathExecutor */ - public function setVarValidationHandler(?callable $handler): self + public function setVarValidationHandler(?callable $handler) : self { $this->onVarValidation = $handler; + return $this; } /** * Remove variable from executor * - * @param string $variable * @return MathExecutor */ - public function removeVar(string $variable): self + public function removeVar(string $variable) : self { unset($this->variables[$variable]); + return $this; } @@ -619,10 +250,11 @@ class MathExecutor * Remove all variables and the variable not found handler * @return MathExecutor */ - public function removeVars(): self + public function removeVars() : self { $this->variables = []; $this->onVarNotFound = null; + return $this; } @@ -642,7 +274,7 @@ class MathExecutor * @return array containing callback and places indexed by * function name */ - public function getFunctions(): array + public function getFunctions() : array { return $this->functions; } @@ -652,14 +284,16 @@ class MathExecutor * * @return MathExecutor */ - public function setDivisionByZeroIsZero(): self + public function setDivisionByZeroIsZero() : self { - $this->addOperator(new Operator("/", false, 180, function ($a, $b) { - if ($b == 0) { + $this->addOperator(new Operator('/', false, 180, static function($a, $b) { + if (0 == $b) { return 0; } + return $a / $b; })); + return $this; } @@ -667,7 +301,7 @@ class MathExecutor * Get cache array with tokens * @return array */ - public function getCache(): array + public function getCache() : array { return $this->cache; } @@ -675,13 +309,393 @@ class MathExecutor /** * Clear token's cache */ - public function clearCache(): void + public function clearCache() : void { $this->cache = []; } - public function __clone() + /** + * Set default operands and functions + * @throws ReflectionException + */ + protected function addDefaults() : void { - $this->addDefaults(); + foreach ($this->defaultOperators() as $name => $operator) { + [$callable, $priority, $isRightAssoc] = $operator; + $this->addOperator(new Operator($name, $isRightAssoc, $priority, $callable)); + } + + foreach ($this->defaultFunctions() as $name => $callable) { + $this->addFunction($name, $callable); + } + + $this->onVarValidation = [$this, 'defaultVarValidation']; + $this->variables = $this->defaultVars(); + } + + /** + * Get the default operators + * + * @return array + */ + protected function defaultOperators() : array + { + return [ + '+' => [ + static function($a, $b) { + return $a + $b; + }, + 170, + false + ], + '-' => [ + static function($a, $b) { + return $a - $b; + }, + 170, + false + ], + 'uPos' => [ // unary positive token + static function($a) { + return $a; + }, + 200, + false + ], + 'uNeg' => [ // unary minus token + static function($a) { + return 0 - $a; + }, + 200, + false + ], + '*' => [ + static function($a, $b) { + return $a * $b; + }, + 180, + false + ], + '/' => [ + static function($a, $b) { + if (0 == $b) { + throw new DivisionByZeroException(); + } + + return $a / $b; + }, + 180, + false + ], + '^' => [ + static function($a, $b) { + return \pow($a, $b); + }, + 220, + true + ], + '&&' => [ + static function($a, $b) { + return $a && $b; + }, + 100, + false + ], + '||' => [ + static function($a, $b) { + return $a || $b; + }, + 90, + false + ], + '==' => [ + static function($a, $b) { + if (\is_string($a) || \is_string($b)) { + return 0 == \strcmp($a, $b); + } + + return $a == $b; + + }, + 140, + false + ], + '!=' => [ + static function($a, $b) { + if (\is_string($a) || \is_string($b)) { + return 0 != \strcmp($a, $b); + } + + return $a != $b; + + }, + 140, + false + ], + '>=' => [ + static function($a, $b) { + return $a >= $b; + }, + 150, + false + ], + '>' => [ + static function($a, $b) { + return $a > $b; + }, + 150, + false + ], + '<=' => [ + static function($a, $b) { + return $a <= $b; + }, + 150, + false + ], + '<' => [ + static function($a, $b) { + return $a < $b; + }, + 150, + false + ], + ]; + } + + /** + * Gets the default functions as an array. Key is function name + * and value is the function as a closure. + * + * @return array + */ + protected function defaultFunctions() : array + { + return [ + 'abs' => static function($arg) { + return \abs($arg); + }, + 'acos' => static function($arg) { + return \acos($arg); + }, + 'acosh' => static function($arg) { + return \acosh($arg); + }, + 'arcsin' => static function($arg) { + return \asin($arg); + }, + 'arcctg' => static function($arg) { + return M_PI / 2 - \atan($arg); + }, + 'arccot' => static function($arg) { + return M_PI / 2 - \atan($arg); + }, + 'arccotan' => static function($arg) { + return M_PI / 2 - \atan($arg); + }, + 'arcsec' => static function($arg) { + return \acos(1 / $arg); + }, + 'arccosec' => static function($arg) { + return \asin(1 / $arg); + }, + 'arccsc' => static function($arg) { + return \asin(1 / $arg); + }, + 'arccos' => static function($arg) { + return \acos($arg); + }, + 'arctan' => static function($arg) { + return \atan($arg); + }, + 'arctg' => static function($arg) { + return \atan($arg); + }, + 'asin' => static function($arg) { + return \asin($arg); + }, + 'atan' => static function($arg) { + return \atan($arg); + }, + 'atan2' => static function($arg1, $arg2) { + return \atan2($arg1, $arg2); + }, + 'atanh' => static function($arg) { + return \atanh($arg); + }, + 'atn' => static function($arg) { + return \atan($arg); + }, + 'avg' => static function($arg1, $arg2) { + return ($arg1 + $arg2) / 2; + }, + 'bindec' => static function($arg) { + return \bindec($arg); + }, + 'ceil' => static function($arg) { + return \ceil($arg); + }, + 'cos' => static function($arg) { + return \cos($arg); + }, + 'cosec' => static function($arg) { + return 1 / \sin($arg); + }, + 'csc' => static function($arg) { + return 1 / \sin($arg); + }, + 'cosh' => static function($arg) { + return \cosh($arg); + }, + 'ctg' => static function($arg) { + return \cos($arg) / \sin($arg); + }, + 'cot' => static function($arg) { + return \cos($arg) / \sin($arg); + }, + 'cotan' => static function($arg) { + return \cos($arg) / \sin($arg); + }, + 'cotg' => static function($arg) { + return \cos($arg) / \sin($arg); + }, + 'ctn' => static function($arg) { + return \cos($arg) / \sin($arg); + }, + 'decbin' => static function($arg) { + return \decbin($arg); + }, + 'dechex' => static function($arg) { + return \dechex($arg); + }, + 'decoct' => static function($arg) { + return \decoct($arg); + }, + 'deg2rad' => static function($arg) { + return \deg2rad($arg); + }, + 'exp' => static function($arg) { + return \exp($arg); + }, + 'expm1' => static function($arg) { + return \expm1($arg); + }, + 'floor' => static function($arg) { + return \floor($arg); + }, + 'fmod' => static function($arg1, $arg2) { + return \fmod($arg1, $arg2); + }, + 'hexdec' => static function($arg) { + return \hexdec($arg); + }, + 'hypot' => static function($arg1, $arg2) { + return \hypot($arg1, $arg2); + }, + 'if' => function($expr, $trueval, $falseval) { + if (true === $expr || false === $expr) { + $exres = $expr; + } else { + $exres = $this->execute($expr); + } + + if ($exres) { + return $this->execute($trueval); + } + + return $this->execute($falseval); + + }, + 'intdiv' => static function($arg1, $arg2) { + return \intdiv($arg1, $arg2); + }, + 'ln' => static function($arg) { + return \log($arg); + }, + 'lg' => static function($arg) { + return \log10($arg); + }, + 'log' => static function($arg) { + return \log($arg); + }, + 'log10' => static function($arg) { + return \log10($arg); + }, + 'log1p' => static function($arg) { + return \log1p($arg); + }, + 'max' => static function($arg1, $arg2) { + return \max($arg1, $arg2); + }, + 'min' => static function($arg1, $arg2) { + return \min($arg1, $arg2); + }, + 'octdec' => static function($arg) { + return \octdec($arg); + }, + 'pi' => static function() { + return M_PI; + }, + 'pow' => static function($arg1, $arg2) { + return $arg1 ** $arg2; + }, + 'rad2deg' => static function($arg) { + return \rad2deg($arg); + }, + 'round' => static function($arg) { + return \round($arg); + }, + 'sin' => static function($arg) { + return \sin($arg); + }, + 'sinh' => static function($arg) { + return \sinh($arg); + }, + 'sec' => static function($arg) { + return 1 / \cos($arg); + }, + 'sqrt' => static function($arg) { + return \sqrt($arg); + }, + 'tan' => static function($arg) { + return \tan($arg); + }, + 'tanh' => static function($arg) { + return \tanh($arg); + }, + 'tn' => static function($arg) { + return \tan($arg); + }, + 'tg' => static function($arg) { + return \tan($arg); + } + ]; + } + + /** + * Returns the default variables names as key/value pairs + * + * @return array + */ + protected function defaultVars() : array + { + return [ + 'pi' => 3.14159265359, + 'e' => 2.71828182846 + ]; + } + + /** + * Default variable validation, ensures that the value is a scalar. + * @param $value + * @throws MathExecutorException if the value is not a scalar + */ + protected function defaultVarValidation(string $variable, $value) : void + { + if (! \is_scalar($value) && null !== $value) { + $type = \gettype($value); + + throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar"); + } } } diff --git a/tests/MathTest.php b/tests/MathTest.php index 07b94fa..9567dd2 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -26,18 +26,19 @@ class MathTest extends TestCase /** * @dataProvider providerExpressions */ - public function testCalculating($expression) + public function testCalculating($expression) : void { $calculator = new MathExecutor(); /** @var float $phpResult */ eval('$phpResult = ' . $expression . ';'); + try { $result = $calculator->execute($expression); } catch (Exception $e) { - $this->fail(sprintf("Exception: %s (%s:%d), expression was: %s", get_class($e), $e->getFile(), $e->getLine(), $expression)); + $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression)); } - $this->assertEquals($phpResult, $result, "Expression was: ${expression}"); + $this->assertEquals($phpResult, $result, "Expression was: {$expression}"); } /** @@ -50,223 +51,223 @@ class MathTest extends TestCase public function providerExpressions() { return [ - ['-5'], - ['-5+10'], - ['4-5'], - ['4 -5'], - ['(4*2)-5'], - ['(4*2) - 5'], - ['4*-5'], - ['4 * -5'], - ['+5'], - ['+(3+2)'], - ['+(+3+2)'], - ['+(-3+2)'], - ['-5'], - ['-(-5)'], - ['-(+5)'], - ['+(-5)'], - ['+(+5)'], - ['-(3+2)'], - ['-(-3+-2)'], + ['-5'], + ['-5+10'], + ['4-5'], + ['4 -5'], + ['(4*2)-5'], + ['(4*2) - 5'], + ['4*-5'], + ['4 * -5'], + ['+5'], + ['+(3+2)'], + ['+(+3+2)'], + ['+(-3+2)'], + ['-5'], + ['-(-5)'], + ['-(+5)'], + ['+(-5)'], + ['+(+5)'], + ['-(3+2)'], + ['-(-3+-2)'], - ['abs(1.5)'], - ['acos(0.15)'], - ['acosh(1.5)'], - ['asin(0.15)'], - ['atan(0.15)'], - ['atan2(1.5, 3.5)'], - ['atanh(0.15)'], - ['bindec("10101")'], - ['ceil(1.5)'], - ['cos(1.5)'], - ['cosh(1.5)'], - ['decbin("15")'], - ['dechex("15")'], - ['decoct("15")'], - ['deg2rad(1.5)'], - ['exp(1.5)'], - ['expm1(1.5)'], - ['floor(1.5)'], - ['fmod(1.5, 3.5)'], - ['hexdec("abcdef")'], - ['hypot(1.5, 3.5)'], - ['intdiv(10, 2)'], - ['log(1.5)'], - ['log10(1.5)'], - ['log1p(1.5)'], - ['max(1.5, 3.5)'], - ['min(1.5, 3.5)'], - ['octdec("15")'], - ['pi()'], - ['pow(1.5, 3.5)'], - ['rad2deg(1.5)'], - ['round(1.5)'], - ['sin(1.5)'], - ['sin(12)'], - ['+sin(12)'], - ['-sin(12)'], - ['sinh(1.5)'], - ['sqrt(1.5)'], - ['tan(1.5)'], - ['tanh(1.5)'], + ['abs(1.5)'], + ['acos(0.15)'], + ['acosh(1.5)'], + ['asin(0.15)'], + ['atan(0.15)'], + ['atan2(1.5, 3.5)'], + ['atanh(0.15)'], + ['bindec("10101")'], + ['ceil(1.5)'], + ['cos(1.5)'], + ['cosh(1.5)'], + ['decbin("15")'], + ['dechex("15")'], + ['decoct("15")'], + ['deg2rad(1.5)'], + ['exp(1.5)'], + ['expm1(1.5)'], + ['floor(1.5)'], + ['fmod(1.5, 3.5)'], + ['hexdec("abcdef")'], + ['hypot(1.5, 3.5)'], + ['intdiv(10, 2)'], + ['log(1.5)'], + ['log10(1.5)'], + ['log1p(1.5)'], + ['max(1.5, 3.5)'], + ['min(1.5, 3.5)'], + ['octdec("15")'], + ['pi()'], + ['pow(1.5, 3.5)'], + ['rad2deg(1.5)'], + ['round(1.5)'], + ['sin(1.5)'], + ['sin(12)'], + ['+sin(12)'], + ['-sin(12)'], + ['sinh(1.5)'], + ['sqrt(1.5)'], + ['tan(1.5)'], + ['tanh(1.5)'], - ['0.1 + 0.2'], - ['1 + 2'], + ['0.1 + 0.2'], + ['1 + 2'], - ['0.1 - 0.2'], - ['1 - 2'], + ['0.1 - 0.2'], + ['1 - 2'], - ['0.1 * 2'], - ['1 * 2'], + ['0.1 * 2'], + ['1 * 2'], - ['0.1 / 0.2'], - ['1 / 2'], + ['0.1 / 0.2'], + ['1 / 2'], - ['2 * 2 + 3 * 3'], - ['2 * 2 / 3 * 3'], - ['2 / 2 / 3 / 3'], - ['2 / 2 * 3 / 3'], - ['2 / 2 * 3 * 3'], + ['2 * 2 + 3 * 3'], + ['2 * 2 / 3 * 3'], + ['2 / 2 / 3 / 3'], + ['2 / 2 * 3 / 3'], + ['2 / 2 * 3 * 3'], - ['1 + 0.6 - 3 * 2 / 50'], + ['1 + 0.6 - 3 * 2 / 50'], - ['(5 + 3) * -1'], + ['(5 + 3) * -1'], - ['-2- 2*2'], - ['2- 2*2'], - ['2-(2*2)'], - ['(2- 2)*2'], - ['2 + 2*2'], - ['2+ 2*2'], - ['2+2*2'], - ['(2+2)*2'], - ['(2 + 2)*-2'], - ['(2+-2)*2'], + ['-2- 2*2'], + ['2- 2*2'], + ['2-(2*2)'], + ['(2- 2)*2'], + ['2 + 2*2'], + ['2+ 2*2'], + ['2+2*2'], + ['(2+2)*2'], + ['(2 + 2)*-2'], + ['(2+-2)*2'], - ['1 + 2 * 3 / (min(1, 5) + 2 + 1)'], - ['1 + 2 * 3 / (min(1, 5) - 2 + 5)'], - ['1 + 2 * 3 / (min(1, 5) * 2 + 1)'], - ['1 + 2 * 3 / (min(1, 5) / 2 + 1)'], - ['1 + 2 * 3 / (min(1, 5) / 2 * 1)'], - ['1 + 2 * 3 / (min(1, 5) / 2 / 1)'], - ['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1)'], - ['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1)'], - ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)'], - ['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) + 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) - 2 + 5)'], + ['1 + 2 * 3 / (min(1, 5) * 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 * 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 / 1)'], + ['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1)'], + ['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1)'], + ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)'], + ['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1)'], - ['(1 + 2) * 3 / (3 / min(1, 5) / 2 + 1)'], + ['(1 + 2) * 3 / (3 / min(1, 5) / 2 + 1)'], - ['sin(10) * cos(50) / min(10, 20/2)'], - ['sin(10) * cos(50) / min(10, (20/2))'], - ['sin(10) * cos(50) / min(10, (max(10,20)/2))'], + ['sin(10) * cos(50) / min(10, 20/2)'], + ['sin(10) * cos(50) / min(10, (20/2))'], + ['sin(10) * cos(50) / min(10, (max(10,20)/2))'], - ['100500 * 3.5e5'], - ['100500 * 3.5e-5'], - ['100500 * 3.5E5'], - ['100500 * 3.5E-5'], + ['100500 * 3.5e5'], + ['100500 * 3.5e-5'], + ['100500 * 3.5E5'], + ['100500 * 3.5E-5'], - ['1 + "2" / 3'], - ["1.5 + '2.5' / 4"], - ['1.5 + "2.5" * ".5"'], + ['1 + "2" / 3'], + ["1.5 + '2.5' / 4"], + ['1.5 + "2.5" * ".5"'], - ['-1 + -2'], - ['-1+-2'], - ['-1- -2'], - ['-1/-2'], - ['-1*-2'], + ['-1 + -2'], + ['-1+-2'], + ['-1- -2'], + ['-1/-2'], + ['-1*-2'], - ['(1+2+3+4-5)*7/100'], - ['(-1+2+3+4- 5)*7/100'], - ['(1+2+3+4- 5)*7/100'], - ['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100'], + ['(1+2+3+4-5)*7/100'], + ['(-1+2+3+4- 5)*7/100'], + ['(1+2+3+4- 5)*7/100'], + ['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100'], - ['1 && 0'], - ['1 && 0 && 1'], - ['1 || 0'], - ['1 && 0 || 1'], + ['1 && 0'], + ['1 && 0 && 1'], + ['1 || 0'], + ['1 && 0 || 1'], - ['5 == 3'], - ['5 == 5'], - ['5 != 3'], - ['5 != 5'], - ['5 > 3'], - ['3 > 5'], - ['3 >= 5'], - ['3 >= 3'], - ['3 < 5'], - ['5 < 3'], - ['3 <= 5'], - ['5 <= 5'], - ['10 < 9 || 4 > (2+1)'], - ['10 < 9 || 4 > (-2+1)'], - ['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7'], + ['5 == 3'], + ['5 == 5'], + ['5 != 3'], + ['5 != 5'], + ['5 > 3'], + ['3 > 5'], + ['3 >= 5'], + ['3 >= 3'], + ['3 < 5'], + ['5 < 3'], + ['3 <= 5'], + ['5 <= 5'], + ['10 < 9 || 4 > (2+1)'], + ['10 < 9 || 4 > (-2+1)'], + ['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7'], - ['1 + 5 == 3 + 1'], - ['1 + 5 == 5 + 1'], - ['1 + 5 != 3 + 1'], - ['1 + 5 != 5 + 1'], - ['1 + 5 > 3 + 1'], - ['1 + 3 > 5 + 1'], - ['1 + 3 >= 5 + 1'], - ['1 + 3 >= 3 + 1'], - ['1 + 3 < 5 + 1'], - ['1 + 5 < 3 + 1'], - ['1 + 3 <= 5 + 1'], - ['1 + 5 <= 5 + 1'], + ['1 + 5 == 3 + 1'], + ['1 + 5 == 5 + 1'], + ['1 + 5 != 3 + 1'], + ['1 + 5 != 5 + 1'], + ['1 + 5 > 3 + 1'], + ['1 + 3 > 5 + 1'], + ['1 + 3 >= 5 + 1'], + ['1 + 3 >= 3 + 1'], + ['1 + 3 < 5 + 1'], + ['1 + 5 < 3 + 1'], + ['1 + 3 <= 5 + 1'], + ['1 + 5 <= 5 + 1'], - ['(-4)'], - ['(-4 + 5)'], - ['(3 * 1)'], - ['(-3 * -1)'], - ['1 + (-3 * -1)'], - ['1 + ( -3 * 1)'], - ['1 + (3 *-1)'], - ['1 - 0'], - ['1-0'], + ['(-4)'], + ['(-4 + 5)'], + ['(3 * 1)'], + ['(-3 * -1)'], + ['1 + (-3 * -1)'], + ['1 + ( -3 * 1)'], + ['1 + (3 *-1)'], + ['1 - 0'], + ['1-0'], - ['-(1.5)'], - ['-log(4)'], - ['0-acosh(1.5)'], - ['-acosh(1.5)'], - ['-(-4)'], - ['-(-4 + 5)'], - ['-(3 * 1)'], - ['-(-3 * -1)'], - ['-1 + (-3 * -1)'], - ['-1 + ( -3 * 1)'], - ['-1 + (3 *-1)'], - ['-1 - 0'], - ['-1-0'], - ['-(4*2)-5'], - ['-(4*-2)-5'], - ['-(-4*2) - 5'], - ['-4*-5'], + ['-(1.5)'], + ['-log(4)'], + ['0-acosh(1.5)'], + ['-acosh(1.5)'], + ['-(-4)'], + ['-(-4 + 5)'], + ['-(3 * 1)'], + ['-(-3 * -1)'], + ['-1 + (-3 * -1)'], + ['-1 + ( -3 * 1)'], + ['-1 + (3 *-1)'], + ['-1 - 0'], + ['-1-0'], + ['-(4*2)-5'], + ['-(4*-2)-5'], + ['-(-4*2) - 5'], + ['-4*-5'], ]; } - public function testUnknownFunctionException() + public function testUnknownFunctionException() : void { $calculator = new MathExecutor(); $this->expectException(UnknownFunctionException::class); $calculator->execute('1 * fred("wilma") + 3'); } - public function testIncorrectExpressionException() + public function testIncorrectExpressionException() : void { $calculator = new MathExecutor(); $this->expectException(IncorrectExpressionException::class); $calculator->execute('1 * + '); } - public function testZeroDivision() + public function testZeroDivision() : void { $calculator = new MathExecutor(); $calculator->setDivisionByZeroIsZero(); $this->assertEquals(0, $calculator->execute('10 / 0')); } - public function testUnaryOperators() + public function testUnaryOperators() : void { $calculator = new MathExecutor(); $this->assertEquals(5, $calculator->execute('+5')); @@ -277,7 +278,7 @@ class MathTest extends TestCase $this->assertEquals(-5, $calculator->execute('-(3+2)')); } - public function testZeroDivisionException() + public function testZeroDivisionException() : void { $calculator = new MathExecutor(); $this->expectException(DivisionByZeroException::class); @@ -286,7 +287,7 @@ class MathTest extends TestCase $this->assertEquals(0.0, $calculator->execute('$one / $zero')); } - public function testVariableIncorrectExpressionException() + public function testVariableIncorrectExpressionException() : void { $calculator = new MathExecutor(); $calculator->setVar('four', 4); @@ -296,85 +297,85 @@ class MathTest extends TestCase $this->assertEquals(0.0, $calculator->execute('$ + $four')); } - public function testExponentiation() + public function testExponentiation() : void { $calculator = new MathExecutor(); $this->assertEquals(100, $calculator->execute('10 ^ 2')); } - public function testFunctionParameterOrder() + public function testFunctionParameterOrder() : void { $calculator = new MathExecutor(); - $calculator->addFunction('concat', function ($arg1, $arg2) { + $calculator->addFunction('concat', static function($arg1, $arg2) { return $arg1 . $arg2; }); $this->assertEquals('testing', $calculator->execute('concat("test","ing")')); $this->assertEquals('testing', $calculator->execute("concat('test','ing')")); } - public function testFunction() + public function testFunction() : void { $calculator = new MathExecutor(); - $calculator->addFunction('round', function ($arg) { - return round($arg); + $calculator->addFunction('round', static function($arg) { + return \round($arg); }); - $this->assertEquals(round(100 / 30), $calculator->execute('round(100/30)')); + $this->assertEquals(\round(100 / 30), $calculator->execute('round(100/30)')); } - public function testFunctionIf() + public function testFunctionIf() : void { $calculator = new MathExecutor(); $this->assertEquals(30, $calculator->execute( - 'if(100 > 99, 30, 0)' + 'if(100 > 99, 30, 0)' )); $this->assertEquals(0, $calculator->execute( - 'if(100 < 99, 30, 0)' + 'if(100 < 99, 30, 0)' )); $this->assertEquals(30, $calculator->execute( - 'if(98 < 99 && sin(1) < 1, 30, 0)' + 'if(98 < 99 && sin(1) < 1, 30, 0)' )); $this->assertEquals(40, $calculator->execute( - 'if(98 < 99 && sin(1) < 1, max(30, 40), 0)' + 'if(98 < 99 && sin(1) < 1, max(30, 40), 0)' )); $this->assertEquals(40, $calculator->execute( - 'if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)' + 'if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)' )); $this->assertEquals(20, $calculator->execute( - 'if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))' + 'if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))' )); - $this->assertEquals(cos(2), $calculator->execute( - 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))' + $this->assertEquals(\cos(2), $calculator->execute( + 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))' )); - $this->assertEquals(cos(2), $calculator->execute( - 'if(cos(2), cos(2), 0)' + $this->assertEquals(\cos(2), $calculator->execute( + 'if(cos(2), cos(2), 0)' )); $trx_amount = 100000; $calculator->setVar('trx_amount', $trx_amount); $this->assertEquals($trx_amount, $calculator->execute('$trx_amount')); $this->assertEquals($trx_amount * 0.03, $calculator->execute( - 'if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)' + 'if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)' )); $this->assertEquals($trx_amount * 0.03, $calculator->execute( - 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' + 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' )); $trx_amount = 39000; $calculator->setVar('trx_amount', $trx_amount); $this->assertEquals($trx_amount * 0.06, $calculator->execute( - 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' + 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' )); $trx_amount = 59000; $calculator->setVar('trx_amount', $trx_amount); $this->assertEquals($trx_amount * 0.05, $calculator->execute( - 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' + 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))' )); $this->expectException(IncorrectNumberOfFunctionParametersException::class); $this->assertEquals(0.0, $calculator->execute( - 'if($trx_amount < 40000, $trx_amount * 0.06)' + 'if($trx_amount < 40000, $trx_amount * 0.06)' )); } - public function testVariables() + public function testVariables() : void { $calculator = new MathExecutor(); $this->assertEquals(3.14159265359, $calculator->execute('$pi')); @@ -382,19 +383,19 @@ class MathTest extends TestCase $this->assertEquals(2.71828182846, $calculator->execute('$e')); $this->assertEquals(2.71828182846, $calculator->execute('e')); $calculator->setVars([ - 'trx_amount' => 100000.01, - 'ten' => 10, - 'nine' => 9, - 'eight' => 8, - 'seven' => 7, - 'six' => 6, - 'five' => 5, - 'four' => 4, - 'three' => 3, - 'two' => 2, - 'one' => 1, - 'zero' => 0, - ]); + 'trx_amount' => 100000.01, + 'ten' => 10, + 'nine' => 9, + 'eight' => 8, + 'seven' => 7, + 'six' => 6, + 'five' => 5, + 'four' => 4, + 'three' => 3, + 'two' => 2, + 'one' => 1, + 'zero' => 0, + ]); $this->assertEquals(100000.01, $calculator->execute('$trx_amount')); $this->assertEquals(10 - 9, $calculator->execute('$ten - $nine')); $this->assertEquals(9 - 10, $calculator->execute('$nine - $ten')); @@ -413,13 +414,13 @@ class MathTest extends TestCase $this->assertEquals(10 / (9 / 5), $calculator->execute('ten / (nine / five)')); } - public function testEvaluateFunctionParameters() + public function testEvaluateFunctionParameters() : void { $calculator = new MathExecutor(); $calculator->addFunction( - 'round', - function ($value, $decimals) { - return round($value, $decimals); + 'round', + static function($value, $decimals) { + return \round($value, $decimals); } ); $expression = 'round(100 * 1.111111, 2)'; @@ -431,24 +432,25 @@ class MathTest extends TestCase $this->assertEquals($phpResult, $calculator->execute($expression)); } - public function testFunctionsWithQuotes() + public function testFunctionsWithQuotes() : void { $calculator = new MathExecutor(); - $calculator->addFunction('concat', function ($first, $second) { + $calculator->addFunction('concat', static function($first, $second) { return $first . $second; }); $this->assertEquals('testing', $calculator->execute('concat("test", "ing")')); $this->assertEquals('testing', $calculator->execute("concat('test', 'ing')")); } - public function testQuotes() + public function testQuotes() : void { $calculator = new MathExecutor(); - $testString = "some, long. arg; with: different-separators!"; + $testString = 'some, long. arg; with: different-separators!'; $calculator->addFunction( - 'test', - function ($arg) use ($testString) { + 'test', + function($arg) use ($testString) { $this->assertEquals($testString, $arg); + return 0; } ); @@ -456,14 +458,14 @@ class MathTest extends TestCase $calculator->execute("test('" . $testString . "')"); // double quotes } - public function testBeginWithBracketAndMinus() + public function testBeginWithBracketAndMinus() : void { $calculator = new MathExecutor(); $this->assertEquals(-4, $calculator->execute('(-4)')); $this->assertEquals(1, $calculator->execute('(-4 + 5)')); } - public function testStringComparison() + public function testStringComparison() : void { $calculator = new MathExecutor(); $this->assertEquals(true, $calculator->execute('"a" == \'a\'')); @@ -477,7 +479,7 @@ class MathTest extends TestCase $this->assertEquals(true, $calculator->execute('"A" != "a"')); } - public function testVarStringComparison() + public function testVarStringComparison() : void { $calculator = new MathExecutor(); $calculator->setVar('var', 97); @@ -487,15 +489,15 @@ class MathTest extends TestCase $this->assertEquals(true, $calculator->execute('$var == "a"')); } - public function testOnVarNotFound() + public function testOnVarNotFound() : void { $calculator = new MathExecutor(); $calculator->setVarNotFoundHandler( - function ($varName) { - if ($varName == 'undefined') { + static function($varName) { + if ('undefined' == $varName) { return 3; } - return null; + } ); $this->assertEquals(15, $calculator->execute('5 * undefined')); @@ -503,21 +505,21 @@ class MathTest extends TestCase $this->assertNull($calculator->getVar('Lucy')); } - public function testGetVarException() + public function testGetVarException() : void { $calculator = new MathExecutor(); $this->expectException(UnknownVariableException::class); $this->assertNull($calculator->getVar('Lucy')); } - public function testMinusZero() + public function testMinusZero() : void { $calculator = new MathExecutor(); $this->assertEquals(1, $calculator->execute('1 - 0')); $this->assertEquals(1, $calculator->execute('1-0')); } - public function testScientificNotation() + public function testScientificNotation() : void { $calculator = new MathExecutor(); $this->assertEquals(1.5e9, $calculator->execute('1.5e9')); @@ -525,39 +527,39 @@ class MathTest extends TestCase $this->assertEquals(1.5e+9, $calculator->execute('1.5e+9')); } - public function testGetFunctionsReturnsArray() + public function testGetFunctionsReturnsArray() : void { $calculator = new MathExecutor(); $this->assertIsArray($calculator->getFunctions()); } - public function testGetFunctionsReturnsFunctions() + public function testGetFunctionsReturnsFunctions() : void { $calculator = new MathExecutor(); - $this->assertGreaterThan(40, count($calculator->getFunctions())); + $this->assertGreaterThan(40, \count($calculator->getFunctions())); } - public function testGetVarsReturnsArray() + public function testGetVarsReturnsArray() : void { $calculator = new MathExecutor(); $this->assertIsArray($calculator->getVars()); } - public function testGetVarsReturnsCount() + public function testGetVarsReturnsCount() : void { $calculator = new MathExecutor(); - $this->assertGreaterThan(1, count($calculator->getVars())); + $this->assertGreaterThan(1, \count($calculator->getVars())); } - public function testUndefinedVarThrowsExecption() + public function testUndefinedVarThrowsExecption() : void { $calculator = new MathExecutor(); - $this->assertGreaterThan(1, count($calculator->getVars())); + $this->assertGreaterThan(1, \count($calculator->getVars())); $this->expectException(UnknownVariableException::class); $calculator->execute('5 * undefined'); } - public function testSetVarsAcceptsAllScalars() + public function testSetVarsAcceptsAllScalars() : void { $calculator = new MathExecutor(); $calculator->setVar('boolTrue', true); @@ -566,7 +568,7 @@ class MathTest extends TestCase $calculator->setVar('null', null); $calculator->setVar('float', 1.1); $calculator->setVar('string', 'string'); - $this->assertEquals(8, count($calculator->getVars())); + $this->assertEquals(8, \count($calculator->getVars())); $this->assertEquals(true, $calculator->getVar('boolTrue')); $this->assertEquals(false, $calculator->getVar('boolFalse')); $this->assertEquals(1, $calculator->getVar('int')); @@ -578,31 +580,31 @@ class MathTest extends TestCase $calculator->setVar('validVar', new \DateTime()); } - public function testSetVarsDoesNotAcceptObject() + public function testSetVarsDoesNotAcceptObject() : void { $calculator = new MathExecutor(); $this->expectException(MathExecutorException::class); $calculator->setVar('object', $this); } - public function testSetVarsDoesNotAcceptResource() + public function testSetVarsDoesNotAcceptResource() : void { $calculator = new MathExecutor(); $this->expectException(MathExecutorException::class); - $calculator->setVar('resource', tmpfile()); + $calculator->setVar('resource', \tmpfile()); } - public function testSetCustomVarValidator() + public function testSetCustomVarValidator() : void { $calculator = new MathExecutor(); - $calculator->setVarValidationHandler(function (string $name, $variable) { + $calculator->setVarValidationHandler(static function(string $name, $variable) : void { // allow all scalars and null - if (is_scalar($variable) || $variable === null) { + if (\is_scalar($variable) || null === $variable) { return; } // Allow variables of type DateTime, but not others if (! $variable instanceof \DateTime) { - throw new MathExecutorException("Invalid variable type"); + throw new MathExecutorException('Invalid variable type'); } }); @@ -618,13 +620,13 @@ class MathTest extends TestCase $calculator->setVar('validVar', $this); } - public function testSetCustomVarNameValidator() + public function testSetCustomVarNameValidator() : void { $calculator = new MathExecutor(); - $calculator->setVarValidationHandler(function (string $name, $variable) { + $calculator->setVarValidationHandler(static function(string $name, $variable) : void { // don't allow variable names with the word invalid in them - if (str_contains($name, 'invalid')) { - throw new MathExecutorException("Invalid variable name"); + if (\str_contains($name, 'invalid')) { + throw new MathExecutorException('Invalid variable name'); } }); @@ -640,7 +642,7 @@ class MathTest extends TestCase $calculator->setVar('invalidVar', 12); } - public function testVarExists() + public function testVarExists() : void { $calculator = new MathExecutor(); $varName = 'Eythel'; @@ -652,16 +654,16 @@ class MathTest extends TestCase /** * @dataProvider providerExpressionValues */ - public function testCalculatingValues($expression, $value) + public function testCalculatingValues($expression, $value) : void { $calculator = new MathExecutor(); try { $result = $calculator->execute($expression); } catch (Exception $e) { - $this->fail(sprintf("Exception: %s (%s:%d), expression was: %s", get_class($e), $e->getFile(), $e->getLine(), $expression)); + $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression)); } - $this->assertEquals($value, $result, "${expression} did not evaluate to {$value}"); + $this->assertEquals($value, $result, "{$expression} did not evaluate to {$value}"); } /** @@ -674,62 +676,62 @@ class MathTest extends TestCase public function providerExpressionValues() { return [ - ['arccos(0.5)', 1.0471975511966], - ['arccos(0.5)', acos(0.5)], - ['arccosec(4)', 0.2526802551421], - ['arccosec(4)', asin(1/4)], - ['arccot(3)', M_PI/2 - atan(3)], - ['arccotan(4)', 0.2449786631269], - ['arccotan(4)', M_PI/2 - atan(4)], - ['arccsc(4)', 0.2526802551421], - ['arccsc(4)', asin(1/4)], - ['arcctg(3)', M_PI/2 - atan(3)], - ['arcsec(4)', 1.3181160716528], - ['arcsec(4)', acos(1/4)], - ['arcsin(0.5)', 0.5235987755983], - ['arcsin(0.5)', asin(0.5)], - ['arctan(0.5)', atan(0.5)], - ['arctan(4)', 1.3258176636680], - ['arctg(0.5)', atan(0.5)], - ['cosec(12)', 1 / sin(12)], - ['cosec(4)', -1.3213487088109], - ['cosh(12)', cosh(12)], - ['cot(12)', cos(12) / sin(12)], - ['cotan(12)', cos(12) / sin(12)], - ['cotan(4)', 0.8636911544506], - ['cotg(3)', cos(3) / sin(3)], - ['csc(4)', 1 / sin(4)], - ['ctg(4)', cos(4) / sin(4)], - ['ctn(4)', cos(4) / sin(4)], - ['decbin(10)', decbin(10)], - ['lg(2)', 0.3010299956639], - ['lg(2)', log10(2)], - ['ln(2)', 0.6931471805599], - ['ln(2)', log(2)], - ['sec(4)', -1.5298856564664], - ['tg(4)', 1.1578212823496], + ['arccos(0.5)', 1.0471975511966], + ['arccos(0.5)', \acos(0.5)], + ['arccosec(4)', 0.2526802551421], + ['arccosec(4)', \asin(1 / 4)], + ['arccot(3)', M_PI / 2 - \atan(3)], + ['arccotan(4)', 0.2449786631269], + ['arccotan(4)', M_PI / 2 - \atan(4)], + ['arccsc(4)', 0.2526802551421], + ['arccsc(4)', \asin(1 / 4)], + ['arcctg(3)', M_PI / 2 - \atan(3)], + ['arcsec(4)', 1.3181160716528], + ['arcsec(4)', \acos(1 / 4)], + ['arcsin(0.5)', 0.5235987755983], + ['arcsin(0.5)', \asin(0.5)], + ['arctan(0.5)', \atan(0.5)], + ['arctan(4)', 1.3258176636680], + ['arctg(0.5)', \atan(0.5)], + ['cosec(12)', 1 / \sin(12)], + ['cosec(4)', -1.3213487088109], + ['cosh(12)', \cosh(12)], + ['cot(12)', \cos(12) / \sin(12)], + ['cotan(12)', \cos(12) / \sin(12)], + ['cotan(4)', 0.8636911544506], + ['cotg(3)', \cos(3) / \sin(3)], + ['csc(4)', 1 / \sin(4)], + ['ctg(4)', \cos(4) / \sin(4)], + ['ctn(4)', \cos(4) / \sin(4)], + ['decbin(10)', \decbin(10)], + ['lg(2)', 0.3010299956639], + ['lg(2)', \log10(2)], + ['ln(2)', 0.6931471805599], + ['ln(2)', \log(2)], + ['sec(4)', -1.5298856564664], + ['tg(4)', 1.1578212823496], ]; } - public function testCache() + public function testCache() : void { $calculator = new MathExecutor(); $this->assertEquals(256, $calculator->execute('2 ^ 8')); // second arg $cache is true by default $this->assertIsArray($calculator->getCache()); - $this->assertEquals(1, count($calculator->getCache())); + $this->assertEquals(1, \count($calculator->getCache())); $this->assertEquals(512, $calculator->execute('2 ^ 9', true)); - $this->assertEquals(2, count($calculator->getCache())); + $this->assertEquals(2, \count($calculator->getCache())); $this->assertEquals(1024, $calculator->execute('2 ^ 10', false)); - $this->assertEquals(2, count($calculator->getCache())); + $this->assertEquals(2, \count($calculator->getCache())); $calculator->clearCache(); $this->assertIsArray($calculator->getCache()); - $this->assertEquals(0, count($calculator->getCache())); + $this->assertEquals(0, \count($calculator->getCache())); $this->assertEquals(2048, $calculator->execute('2 ^ 11', false)); - $this->assertEquals(0, count($calculator->getCache())); + $this->assertEquals(0, \count($calculator->getCache())); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9c6e09c..85cee64 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,10 +2,10 @@ $vendorDir = __DIR__ . '/../../..'; -if (file_exists($file = $vendorDir . '/autoload.php')) { +if (\file_exists($file = $vendorDir . '/autoload.php')) { require_once $file; -} elseif (file_exists($file = './vendor/autoload.php')) { +} elseif (\file_exists($file = './vendor/autoload.php')) { require_once $file; } else { - throw new \RuntimeException("Not found composer autoload"); + throw new \RuntimeException('Not found composer autoload'); }