From ea898d7a7b85aeb677f81f13784232c99b61808a Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Mon, 1 Jun 2020 03:50:39 +0300 Subject: [PATCH] Code style fixes & Github Actions (#65) * Code style fixes Up phpunit to v8 New CI * Remove travis * Fix CI badge * Added php-cs-fixer action --- .github/workflows/tests.yml | 19 +++++++++++++++++ .travis.yml | 11 ---------- README.md | 2 +- composer.json | 2 +- src/NXP/Classes/Calculator.php | 4 ++-- src/NXP/Classes/CustomFunction.php | 5 +---- src/NXP/Classes/Operator.php | 3 +-- src/NXP/Classes/Tokenizer.php | 2 +- src/NXP/MathExecutor.php | 9 ++++---- tests/MathTest.php | 33 ++++++++++++++++++++---------- tests/bootstrap.php | 4 ++-- 11 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..557134d --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,19 @@ +name: Tests + +on: [push] + +jobs: + build-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - uses: php-actions/composer@v1 + - name: PHPUnit + uses: php-actions/phpunit@v1 + with: + config: ./phpunit.xml.dist + - name: PHP CS Fixer + uses: StephaneBour/actions-php-cs-fixer@1.0 + with: + dir: './src' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a471c3e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - - 7.4 - -before_script: - - wget http://getcomposer.org/composer.phar - - php composer.phar install diff --git a/README.md b/README.md index 0e13d9b..98740d6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MathExecutor [![Build Status](https://travis-ci.org/NeonXP/MathExecutor.png?branch=master)](https://travis-ci.org/NeonXP/MathExecutor) [![Latest Packagist release](https://img.shields.io/packagist/v/nxp/math-executor.svg)](https://packagist.org/packages/nxp/math-executor) +# MathExecutor [![Tests](https://github.com/neonxp/MathExecutor/workflows/Tests/badge.svg)](https://github.com/neonxp/MathExecutor/actions?query=workflow%3ATests) [![Latest Packagist release](https://img.shields.io/packagist/v/nxp/math-executor.svg)](https://packagist.org/packages/nxp/math-executor) # A simple and extensible math expressions calculator diff --git a/composer.json b/composer.json index 3098443..3ba5a22 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "php": ">=7.1" }, "require-dev": { - "phpunit/phpunit": "~7.0" + "phpunit/phpunit": "~8.0" }, "autoload": { "psr-0": { diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index c6ccff1..94c6933 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -56,14 +56,14 @@ public function calculate(array $tokens, array $variables) foreach ($tokens as $token) { if ($token->type === Token::Literal || $token->type === Token::String) { $stack[] = $token; - } else if ($token->type === Token::Variable) { + } elseif ($token->type === Token::Variable) { $variable = $token->value; if (!array_key_exists($variable, $variables)) { throw new UnknownVariableException($variable); } $value = $variables[$variable]; $stack[] = new Token(Token::Literal, $value); - } else if ($token->type === Token::Function) { + } 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 bf36405..06e21ef 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -3,7 +3,6 @@ namespace NXP\Classes; - use NXP\Exception\IncorrectExpressionException; use ReflectionException; use ReflectionFunction; @@ -58,6 +57,4 @@ public function execute(array &$stack) : Token return new Token(Token::Literal, $result); } - - -} \ No newline at end of file +} diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index c3762ea..86df549 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -3,7 +3,6 @@ namespace NXP\Classes; - use NXP\Exception\IncorrectExpressionException; use ReflectionFunction; @@ -66,4 +65,4 @@ public function execute(array &$stack) : Token return new Token(Token::Literal, $result); } -} \ No newline at end of file +} diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index caf395f..6b14677 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -106,6 +106,7 @@ public function tokenize() : self $this->allowNegative = true; break; } + // no break case $this->isAlpha($ch): if ($this->numberBuffer != "") { $this->emptyNumberBufferAsLiteral(); @@ -299,4 +300,3 @@ public function buildReversePolishNotation() : array return $tokens; } } - diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 9a45589..8f876dc 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -15,8 +15,9 @@ use NXP\Classes\CustomFunction; use NXP\Classes\Operator; use NXP\Classes\Tokenizer; -use NXP\MathExecutorException; +use NXP\Exception\MathExecutorException; use NXP\Exception\DivisionByZeroException; +use NXP\Exception\UnknownVariableException; use ReflectionException; /** @@ -403,7 +404,7 @@ public function getVars() : array public function getVar(string $variable) { if (!isset($this->variables[$variable])) { - throw new UnknownVariableException("Variable ({$variable}) not set"); + throw new UnknownVariableException("Variable ({$variable}) not set"); } return $this->variables[$variable]; } @@ -444,7 +445,7 @@ public function setVars(array $variables, bool $clear = true) : self return $this; } - /** + /** * Remove variable from executor * * @param string $variable @@ -452,7 +453,7 @@ public function setVars(array $variables, bool $clear = true) : self */ public function removeVar(string $variable) : self { - unset ($this->variables[$variable]); + unset($this->variables[$variable]); return $this; } diff --git a/tests/MathTest.php b/tests/MathTest.php index 39ac649..6108bd8 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -12,7 +12,6 @@ namespace NXP\Tests; use Exception; -use NXP\Classes\Operator; use NXP\Exception\DivisionByZeroException; use NXP\Exception\IncorrectExpressionException; use NXP\Exception\UnknownFunctionException; @@ -267,27 +266,37 @@ public function testFunctionIf() { $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))')); + 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))' + )); $this->assertEquals(cos(2), $calculator->execute( - 'if(cos(2), cos(2), 0)')); + 'if(cos(2), cos(2), 0)' + )); } public function testEvaluateFunctionParameters() { $calculator = new MathExecutor(); - $calculator->addFunction('round', function ($value, $decimals) { + $calculator->addFunction( + 'round', + function ($value, $decimals) { return round($value, $decimals); } ); @@ -314,7 +323,9 @@ public function testQuotes() { $calculator = new MathExecutor(); $testString = "some, long. arg; with: different-separators!"; - $calculator->addFunction('test', function ($arg) use ($testString) { + $calculator->addFunction( + 'test', + function ($arg) use ($testString) { $this->assertEquals($testString, $arg); return 0; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8eb3e7a..9c6e09c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,8 +4,8 @@ if (file_exists($file = $vendorDir . '/autoload.php')) { require_once $file; -} else if (file_exists($file = './vendor/autoload.php')) { +} elseif (file_exists($file = './vendor/autoload.php')) { require_once $file; } else { throw new \RuntimeException("Not found composer autoload"); -} \ No newline at end of file +}