aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2020-06-01 03:50:39 +0300
committerGitHub <noreply@github.com>2020-06-01 03:50:39 +0300
commitea898d7a7b85aeb677f81f13784232c99b61808a (patch)
treea1f6985aa44e3af0296989435427d57bc5219add
parent7852fc7d04bcdb3aa60384a1f6dddfd637fedd70 (diff)
Code style fixes & Github Actions (#65)V2.0.2
* Code style fixes Up phpunit to v8 New CI * Remove travis * Fix CI badge * Added php-cs-fixer action
-rw-r--r--.github/workflows/tests.yml19
-rw-r--r--.travis.yml11
-rw-r--r--README.md2
-rw-r--r--composer.json2
-rw-r--r--src/NXP/Classes/Calculator.php4
-rw-r--r--src/NXP/Classes/CustomFunction.php5
-rw-r--r--src/NXP/Classes/Operator.php3
-rw-r--r--src/NXP/Classes/Tokenizer.php2
-rw-r--r--src/NXP/MathExecutor.php9
-rw-r--r--tests/MathTest.php33
-rw-r--r--tests/bootstrap.php4
11 files changed, 55 insertions, 39 deletions
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 @@ class Calculator
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 @@ class CustomFunction
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 @@ class Operator
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 @@ class Tokenizer
$this->allowNegative = true;
break;
}
+ // no break
case $this->isAlpha($ch):
if ($this->numberBuffer != "") {
$this->emptyNumberBufferAsLiteral();
@@ -299,4 +300,3 @@ class Tokenizer
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\Calculator;
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 @@ class MathExecutor
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 @@ class MathExecutor
return $this;
}
- /**
+ /**
* Remove variable from executor
*
* @param string $variable
@@ -452,7 +453,7 @@ class MathExecutor
*/
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 @@ class MathTest extends TestCase
{
$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 @@ class MathTest extends TestCase
{
$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 @@ $vendorDir = __DIR__ . '/../../..';
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
+}