Skip to content

Commit c59f4cd

Browse files
authored
Php8.2 (#119)
* PHP 8.2 Support * Code style fixes * Specify precision of 16 for tests * Remove PHPStan testing due to PHP version differences * Update actions to latest versions * Updated tests to avoid hard coded values * Enhanced error reporting for tests
1 parent a041bb5 commit c59f4cd

3 files changed

Lines changed: 173 additions & 141 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
php: [8.1, 8.0, 7.4]
11+
php: [8.2, 8.1, 8.0, 7.4]
1212
dependency-version: [prefer-lowest, prefer-stable]
1313
os: [ubuntu-latest, windows-latest]
1414

1515
name: ${{ matrix.os }} - PHP${{ matrix.php }} - ${{ matrix.dependency-version }}
1616

1717
steps:
1818
- name: Checkout code
19-
uses: actions/checkout@v1
19+
uses: actions/checkout@v3
2020

2121
- name: Setup PHP
2222
uses: shivammathur/setup-php@v2
2323
with:
2424
php-version: ${{ matrix.php }}
2525
extensions: dom, curl, libxml, mbstring, zip, bcmath, intl
26+
ini-values: precision=16
2627
coverage: none
2728

2829
- name: Install dependencies
@@ -32,8 +33,3 @@ jobs:
3233
3334
- name: Execute tests
3435
run: vendor/bin/phpunit
35-
36-
- name: PHP CS Fixer
37-
if: matrix.os != 'windows-latest'
38-
run: |
39-
vendor/bin/php-cs-fixer fix --dry-run -v

src/NXP/MathExecutor.php

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public function __clone()
7474
/**
7575
* Add operator to executor
7676
*
77-
* @return MathExecutor
7877
*/
7978
public function addOperator(Operator $operator) : self
8079
{
@@ -119,7 +118,6 @@ public function execute(string $expression, bool $cache = true)
119118
*
120119
* @throws ReflectionException
121120
* @throws Exception\IncorrectNumberOfFunctionParametersException
122-
* @return MathExecutor
123121
*/
124122
public function addFunction(string $name, ?callable $function = null) : self
125123
{
@@ -161,7 +159,6 @@ public function getVar(string $variable)
161159
* Add variable to executor. To set a custom validator use setVarValidationHandler.
162160
*
163161
* @throws MathExecutorException if the value is invalid based on the default or custom validator
164-
* @return MathExecutor
165162
*/
166163
public function setVar(string $variable, $value) : self
167164
{
@@ -189,7 +186,6 @@ public function varExists(string $variable) : bool
189186
* @param array<string, float|int|string> $variables
190187
* @param bool $clear Clear previous variables
191188
* @throws \Exception
192-
* @return MathExecutor
193189
*/
194190
public function setVars(array $variables, bool $clear = true) : self
195191
{
@@ -209,7 +205,6 @@ public function setVars(array $variables, bool $clear = true) : self
209205
* The first parameter will be the variable name, and the returned value will be used as the variable value.
210206
*
211207
*
212-
* @return MathExecutor
213208
*/
214209
public function setVarNotFoundHandler(callable $handler) : self
215210
{
@@ -225,7 +220,6 @@ public function setVarNotFoundHandler(callable $handler) : self
225220
*
226221
* @param ?callable $handler throws a MathExecutorException in case of an invalid variable
227222
*
228-
* @return MathExecutor
229223
*/
230224
public function setVarValidationHandler(?callable $handler) : self
231225
{
@@ -237,7 +231,6 @@ public function setVarValidationHandler(?callable $handler) : self
237231
/**
238232
* Remove variable from executor
239233
*
240-
* @return MathExecutor
241234
*/
242235
public function removeVar(string $variable) : self
243236
{
@@ -248,7 +241,6 @@ public function removeVar(string $variable) : self
248241

249242
/**
250243
* Remove all variables and the variable not found handler
251-
* @return MathExecutor
252244
*/
253245
public function removeVars() : self
254246
{
@@ -296,7 +288,7 @@ public function removeOperator(string $operator) : self
296288
*/
297289
public function setDivisionByZeroIsZero() : self
298290
{
299-
$this->addOperator(new Operator('/', false, 180, static fn($a, $b) => 0 == $b ? 0 : $a / $b));
291+
$this->addOperator(new Operator('/', false, 180, static fn ($a, $b) => 0 == $b ? 0 : $a / $b));
300292

301293
return $this;
302294
}
@@ -323,10 +315,10 @@ public function clearCache() : self
323315
public function useBCMath(int $scale = 2) : self
324316
{
325317
\bcscale($scale);
326-
$this->addOperator(new Operator('+', false, 170, static fn($a, $b) => \bcadd("{$a}", "{$b}")));
327-
$this->addOperator(new Operator('-', false, 170, static fn($a, $b) => \bcsub("{$a}", "{$b}")));
328-
$this->addOperator(new Operator('uNeg', false, 200, static fn($a) => \bcsub('0.0', "{$a}")));
329-
$this->addOperator(new Operator('*', false, 180, static fn($a, $b) => \bcmul("{$a}", "{$b}")));
318+
$this->addOperator(new Operator('+', false, 170, static fn ($a, $b) => \bcadd("{$a}", "{$b}")));
319+
$this->addOperator(new Operator('-', false, 170, static fn ($a, $b) => \bcsub("{$a}", "{$b}")));
320+
$this->addOperator(new Operator('uNeg', false, 200, static fn ($a) => \bcsub('0.0', "{$a}")));
321+
$this->addOperator(new Operator('*', false, 180, static fn ($a, $b) => \bcmul("{$a}", "{$b}")));
330322
$this->addOperator(new Operator('/', false, 180, static function($a, $b) {
331323
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
332324
if (0 == $b) {
@@ -335,8 +327,8 @@ public function useBCMath(int $scale = 2) : self
335327

336328
return \bcdiv("{$a}", "{$b}");
337329
}));
338-
$this->addOperator(new Operator('^', true, 220, static fn($a, $b) => \bcpow("{$a}", "{$b}")));
339-
$this->addOperator(new Operator('%', false, 180, static fn($a, $b) => \bcmod("{$a}", "{$b}")));
330+
$this->addOperator(new Operator('^', true, 220, static fn ($a, $b) => \bcpow("{$a}", "{$b}")));
331+
$this->addOperator(new Operator('%', false, 180, static fn ($a, $b) => \bcmod("{$a}", "{$b}")));
340332

341333
return $this;
342334
}
@@ -370,13 +362,13 @@ protected function addDefaults() : self
370362
protected function defaultOperators() : array
371363
{
372364
return [
373-
'+' => [static fn($a, $b) => $a + $b, 170, false],
374-
'-' => [static fn($a, $b) => $a - $b, 170, false],
365+
'+' => [static fn ($a, $b) => $a + $b, 170, false],
366+
'-' => [static fn ($a, $b) => $a - $b, 170, false],
375367
// unary positive token
376-
'uPos' => [static fn($a) => $a, 200, false],
368+
'uPos' => [static fn ($a) => $a, 200, false],
377369
// unary minus token
378-
'uNeg' => [static fn($a) => 0 - $a, 200, false],
379-
'*' => [static fn($a, $b) => $a * $b, 180, false],
370+
'uNeg' => [static fn ($a) => 0 - $a, 200, false],
371+
'*' => [static fn ($a, $b) => $a * $b, 180, false],
380372
'/' => [
381373
static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
382374
if (0 == $b) {
@@ -388,16 +380,16 @@ static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($
388380
180,
389381
false
390382
],
391-
'^' => [static fn($a, $b) => \pow($a, $b), 220, true],
392-
'%' => [static fn($a, $b) => $a % $b, 180, false],
393-
'&&' => [static fn($a, $b) => $a && $b, 100, false],
394-
'||' => [static fn($a, $b) => $a || $b, 90, false],
395-
'==' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],
396-
'!=' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false],
397-
'>=' => [static fn($a, $b) => $a >= $b, 150, false],
398-
'>' => [static fn($a, $b) => $a > $b, 150, false],
399-
'<=' => [static fn($a, $b) => $a <= $b, 150, false],
400-
'<' => [static fn($a, $b) => $a < $b, 150, false],
383+
'^' => [static fn ($a, $b) => \pow($a, $b), 220, true],
384+
'%' => [static fn ($a, $b) => $a % $b, 180, false],
385+
'&&' => [static fn ($a, $b) => $a && $b, 100, false],
386+
'||' => [static fn ($a, $b) => $a || $b, 90, false],
387+
'==' => [static fn ($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],
388+
'!=' => [static fn ($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false],
389+
'>=' => [static fn ($a, $b) => $a >= $b, 150, false],
390+
'>' => [static fn ($a, $b) => $a > $b, 150, false],
391+
'<=' => [static fn ($a, $b) => $a <= $b, 150, false],
392+
'<' => [static fn ($a, $b) => $a < $b, 150, false],
401393
];
402394
}
403395

@@ -410,29 +402,29 @@ static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($
410402
protected function defaultFunctions() : array
411403
{
412404
return [
413-
'abs' => static fn($arg) => \abs($arg),
414-
'acos' => static fn($arg) => \acos($arg),
415-
'acosh' => static fn($arg) => \acosh($arg),
416-
'arcsin' => static fn($arg) => \asin($arg),
417-
'arcctg' => static fn($arg) => M_PI / 2 - \atan($arg),
418-
'arccot' => static fn($arg) => M_PI / 2 - \atan($arg),
419-
'arccotan' => static fn($arg) => M_PI / 2 - \atan($arg),
420-
'arcsec' => static fn($arg) => \acos(1 / $arg),
421-
'arccosec' => static fn($arg) => \asin(1 / $arg),
422-
'arccsc' => static fn($arg) => \asin(1 / $arg),
423-
'arccos' => static fn($arg) => \acos($arg),
424-
'arctan' => static fn($arg) => \atan($arg),
425-
'arctg' => static fn($arg) => \atan($arg),
426-
'array' => static fn(...$args) => $args,
427-
'asin' => static fn($arg) => \asin($arg),
428-
'atan' => static fn($arg) => \atan($arg),
429-
'atan2' => static fn($arg1, $arg2) => \atan2($arg1, $arg2),
430-
'atanh' => static fn($arg) => \atanh($arg),
431-
'atn' => static fn($arg) => \atan($arg),
405+
'abs' => static fn ($arg) => \abs($arg),
406+
'acos' => static fn ($arg) => \acos($arg),
407+
'acosh' => static fn ($arg) => \acosh($arg),
408+
'arcsin' => static fn ($arg) => \asin($arg),
409+
'arcctg' => static fn ($arg) => M_PI / 2 - \atan($arg),
410+
'arccot' => static fn ($arg) => M_PI / 2 - \atan($arg),
411+
'arccotan' => static fn ($arg) => M_PI / 2 - \atan($arg),
412+
'arcsec' => static fn ($arg) => \acos(1 / $arg),
413+
'arccosec' => static fn ($arg) => \asin(1 / $arg),
414+
'arccsc' => static fn ($arg) => \asin(1 / $arg),
415+
'arccos' => static fn ($arg) => \acos($arg),
416+
'arctan' => static fn ($arg) => \atan($arg),
417+
'arctg' => static fn ($arg) => \atan($arg),
418+
'array' => static fn (...$args) => $args,
419+
'asin' => static fn ($arg) => \asin($arg),
420+
'atan' => static fn ($arg) => \atan($arg),
421+
'atan2' => static fn ($arg1, $arg2) => \atan2($arg1, $arg2),
422+
'atanh' => static fn ($arg) => \atanh($arg),
423+
'atn' => static fn ($arg) => \atan($arg),
432424
'avg' => static function($arg1, ...$args) {
433425
if (\is_array($arg1)){
434426
if (0 === \count($arg1)){
435-
throw new \InvalidArgumentException('Array must contain at least one element!');
427+
throw new \InvalidArgumentException('avg() must have at least one argument!');
436428
}
437429

438430
return \array_sum($arg1) / \count($arg1);
@@ -442,27 +434,27 @@ protected function defaultFunctions() : array
442434

443435
return \array_sum($args) / \count($args);
444436
},
445-
'bindec' => static fn($arg) => \bindec($arg),
446-
'ceil' => static fn($arg) => \ceil($arg),
447-
'cos' => static fn($arg) => \cos($arg),
448-
'cosec' => static fn($arg) => 1 / \sin($arg),
449-
'csc' => static fn($arg) => 1 / \sin($arg),
450-
'cosh' => static fn($arg) => \cosh($arg),
451-
'ctg' => static fn($arg) => \cos($arg) / \sin($arg),
452-
'cot' => static fn($arg) => \cos($arg) / \sin($arg),
453-
'cotan' => static fn($arg) => \cos($arg) / \sin($arg),
454-
'cotg' => static fn($arg) => \cos($arg) / \sin($arg),
455-
'ctn' => static fn($arg) => \cos($arg) / \sin($arg),
456-
'decbin' => static fn($arg) => \decbin($arg),
457-
'dechex' => static fn($arg) => \dechex($arg),
458-
'decoct' => static fn($arg) => \decoct($arg),
459-
'deg2rad' => static fn($arg) => \deg2rad($arg),
460-
'exp' => static fn($arg) => \exp($arg),
461-
'expm1' => static fn($arg) => \expm1($arg),
462-
'floor' => static fn($arg) => \floor($arg),
463-
'fmod' => static fn($arg1, $arg2) => \fmod($arg1, $arg2),
464-
'hexdec' => static fn($arg) => \hexdec($arg),
465-
'hypot' => static fn($arg1, $arg2) => \hypot($arg1, $arg2),
437+
'bindec' => static fn ($arg) => \bindec($arg),
438+
'ceil' => static fn ($arg) => \ceil($arg),
439+
'cos' => static fn ($arg) => \cos($arg),
440+
'cosec' => static fn ($arg) => 1 / \sin($arg),
441+
'csc' => static fn ($arg) => 1 / \sin($arg),
442+
'cosh' => static fn ($arg) => \cosh($arg),
443+
'ctg' => static fn ($arg) => \cos($arg) / \sin($arg),
444+
'cot' => static fn ($arg) => \cos($arg) / \sin($arg),
445+
'cotan' => static fn ($arg) => \cos($arg) / \sin($arg),
446+
'cotg' => static fn ($arg) => \cos($arg) / \sin($arg),
447+
'ctn' => static fn ($arg) => \cos($arg) / \sin($arg),
448+
'decbin' => static fn ($arg) => \decbin($arg),
449+
'dechex' => static fn ($arg) => \dechex($arg),
450+
'decoct' => static fn ($arg) => \decoct($arg),
451+
'deg2rad' => static fn ($arg) => \deg2rad($arg),
452+
'exp' => static fn ($arg) => \exp($arg),
453+
'expm1' => static fn ($arg) => \expm1($arg),
454+
'floor' => static fn ($arg) => \floor($arg),
455+
'fmod' => static fn ($arg1, $arg2) => \fmod($arg1, $arg2),
456+
'hexdec' => static fn ($arg) => \hexdec($arg),
457+
'hypot' => static fn ($arg1, $arg2) => \hypot($arg1, $arg2),
466458
'if' => function($expr, $trueval, $falseval) {
467459
if (true === $expr || false === $expr) {
468460
$exres = $expr;
@@ -476,39 +468,39 @@ protected function defaultFunctions() : array
476468

477469
return $this->execute($falseval);
478470
},
479-
'intdiv' => static fn($arg1, $arg2) => \intdiv($arg1, $arg2),
480-
'ln' => static fn($arg) => \log($arg),
481-
'lg' => static fn($arg) => \log10($arg),
482-
'log' => static fn($arg) => \log($arg),
483-
'log10' => static fn($arg) => \log10($arg),
484-
'log1p' => static fn($arg) => \log1p($arg),
471+
'intdiv' => static fn ($arg1, $arg2) => \intdiv($arg1, $arg2),
472+
'ln' => static fn ($arg) => \log($arg),
473+
'lg' => static fn ($arg) => \log10($arg),
474+
'log' => static fn ($arg) => \log($arg),
475+
'log10' => static fn ($arg) => \log10($arg),
476+
'log1p' => static fn ($arg) => \log1p($arg),
485477
'max' => static function($arg1, ...$args) {
486478
if (\is_array($arg1) && 0 === \count($arg1)){
487-
throw new \InvalidArgumentException('Array must contain at least one element!');
479+
throw new \InvalidArgumentException('max() must have at least one argument!');
488480
}
489481

490482
return \max(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
491483
},
492484
'min' => static function($arg1, ...$args) {
493485
if (\is_array($arg1) && 0 === \count($arg1)){
494-
throw new \InvalidArgumentException('Array must contain at least one element!');
486+
throw new \InvalidArgumentException('min() must have at least one argument!');
495487
}
496488

497489
return \min(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
498490
},
499-
'octdec' => static fn($arg) => \octdec($arg),
500-
'pi' => static fn() => M_PI,
501-
'pow' => static fn($arg1, $arg2) => $arg1 ** $arg2,
502-
'rad2deg' => static fn($arg) => \rad2deg($arg),
503-
'round' => static fn($num, int $precision = 0) => \round($num, $precision),
504-
'sin' => static fn($arg) => \sin($arg),
505-
'sinh' => static fn($arg) => \sinh($arg),
506-
'sec' => static fn($arg) => 1 / \cos($arg),
507-
'sqrt' => static fn($arg) => \sqrt($arg),
508-
'tan' => static fn($arg) => \tan($arg),
509-
'tanh' => static fn($arg) => \tanh($arg),
510-
'tn' => static fn($arg) => \tan($arg),
511-
'tg' => static fn($arg) => \tan($arg)
491+
'octdec' => static fn ($arg) => \octdec($arg),
492+
'pi' => static fn () => M_PI,
493+
'pow' => static fn ($arg1, $arg2) => $arg1 ** $arg2,
494+
'rad2deg' => static fn ($arg) => \rad2deg($arg),
495+
'round' => static fn ($num, int $precision = 0) => \round($num, $precision),
496+
'sin' => static fn ($arg) => \sin($arg),
497+
'sinh' => static fn ($arg) => \sinh($arg),
498+
'sec' => static fn ($arg) => 1 / \cos($arg),
499+
'sqrt' => static fn ($arg) => \sqrt($arg),
500+
'tan' => static fn ($arg) => \tan($arg),
501+
'tanh' => static fn ($arg) => \tanh($arg),
502+
'tn' => static fn ($arg) => \tan($arg),
503+
'tg' => static fn ($arg) => \tan($arg)
512504
];
513505
}
514506

0 commit comments

Comments
 (0)