Skip to content

Commit

Permalink
code style / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Ackermann committed Dec 9, 2019
1 parent 4f3232b commit 4a673e3
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/MathParser/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static function factory($value)
return new Parenthesis($value);
} elseif ($value == '^') {
return new Power($value);
}elseif ($value == '%') {
} elseif ($value == '%') {
return new Modulo($value);
}elseif (strlen($value) >= 2 && $value[0] == '$') {
} elseif (strlen($value) >= 2 && $value[0] == '$') {
return new Variable(substr($value, 1));
}
throw new \RuntimeException('Undefined Value ' . $value);
Expand Down
8 changes: 3 additions & 5 deletions lib/MathParser/Expressions/Addition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Addition extends Operator
{
protected $precedence = 4;

public function operate(array &$stack)
{
$left = array_pop($stack)->operate($stack);
$right = array_pop($stack)->operate($stack);

if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}

Expand Down
10 changes: 4 additions & 6 deletions lib/MathParser/Expressions/Division.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Division extends Operator
{
protected $precedence = 5;

public function operate(array &$stack)
{
$left = array_pop($stack)->operate($stack);
$right = array_pop($stack)->operate($stack);

if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}

if($left === 0){
if ($left === 0) {
return null;
}

Expand Down
12 changes: 5 additions & 7 deletions lib/MathParser/Expressions/Modulo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Modulo extends Operator
{
protected $precedence = 5;

public function operate(array &$stack)
{
$left = array_pop($stack)->operate($stack);
$right = array_pop($stack)->operate($stack);

if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}

if($left===0){
if ($left === 0) {
return null;
}else{
} else {
return $right % $left;
}
}
Expand Down
4 changes: 1 addition & 3 deletions lib/MathParser/Expressions/Multiplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Multiplication extends Operator
{
protected $precedence = 5;
Expand All @@ -12,7 +10,7 @@ public function operate(array &$stack)
{
$left = array_pop($stack)->operate($stack);
$right = array_pop($stack)->operate($stack);
if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}
return $left * $right;
Expand Down
1 change: 0 additions & 1 deletion lib/MathParser/Expressions/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MathParser\Expressions;

use MathParser\Stack;
use MathParser\Expression;

class Number extends Expression
Expand Down
1 change: 0 additions & 1 deletion lib/MathParser/Expressions/Parenthesis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MathParser\Expressions;

use MathParser\Stack;
use MathParser\Expression;

class Parenthesis extends Expression
Expand Down
6 changes: 2 additions & 4 deletions lib/MathParser/Expressions/Power.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Power extends Operator
{
protected $precedence = 6;

public function operate(array &$stack)
{
$right = array_pop($stack)->operate($stack);
$left = array_pop($stack)->operate($stack);

if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}
return pow($left, $right);
Expand Down
4 changes: 1 addition & 3 deletions lib/MathParser/Expressions/Subtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Subtraction extends Operator
{
protected $precedence = 4;
Expand All @@ -13,7 +11,7 @@ public function operate(array &$stack)
$left = array_pop($stack)->operate($stack);
$right = array_pop($stack)->operate($stack);

if($left === null || $right === null){
if ($left === null || $right === null) {
return null;
}

Expand Down
10 changes: 4 additions & 6 deletions lib/MathParser/Expressions/Unary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

namespace MathParser\Expressions;

use MathParser\Stack;

class Unary extends Operator
{
protected $precedence = 7;

public function isUnary()
{
return true;
}

public function operate(array &$stack)
{
//the operate here should always be returning a value alone
$next = array_pop($stack)->operate($stack);

if($next === null){
if ($next === null) {
return null;
}

return -$next;
}
}
3 changes: 1 addition & 2 deletions lib/MathParser/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Math
*/
public function evaluate($string)
{
if(!is_string($string)){
if (!is_string($string)) {
throw new \RuntimeException('not a string provided as formula');
}
$stack = $this->parse($string);
Expand Down Expand Up @@ -107,7 +107,6 @@ private function render(array &$stack)
return $output;
}
return null;

}

private function parseParenthesis(Expression $expression, array &$output, array &$operators)
Expand Down
1 change: 0 additions & 1 deletion test/MathParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static function provideValidData()
['1-1/6', 5 / 6],
['(1-7)/6', -1],
['-0 + 0', 0],
// should fail..? https://floating-point-gui.de/
['8 - 6.4', 1.6],
['1 / 2', 0.5],
['10 * 10', 100],
Expand Down

0 comments on commit 4a673e3

Please sign in to comment.