array functions accept null
This commit is contained in:
@@ -44,12 +44,13 @@ class AtType extends BaseFunction
|
||||
$this->throwTooFewArguments();
|
||||
}
|
||||
|
||||
$array = $args[0];
|
||||
$array = $args[0] ?? [];
|
||||
$index = $args[1];
|
||||
|
||||
if (!is_array($array)) {
|
||||
$this->throwBadArgumentType(1, 'array');
|
||||
}
|
||||
|
||||
if (!is_int($index)) {
|
||||
$this->throwBadArgumentType(2, 'int');
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class IncludesType extends BaseFunction
|
||||
$this->throwTooFewArguments();
|
||||
}
|
||||
|
||||
$list = $args[0];
|
||||
$list = $args[0] ?? [];
|
||||
$needle = $args[1];
|
||||
|
||||
if (!is_array($list)) {
|
||||
|
||||
@@ -42,7 +42,7 @@ class JoinType extends BaseFunction
|
||||
$this->throwTooFewArguments();
|
||||
}
|
||||
|
||||
$list = $this->evaluate($args[0]);
|
||||
$list = $this->evaluate($args[0]) ?? [];
|
||||
|
||||
$separator = $this->evaluate($args[1]);
|
||||
|
||||
|
||||
@@ -42,15 +42,19 @@ class PushType extends BaseFunction
|
||||
$this->throwTooFewArguments();
|
||||
}
|
||||
|
||||
$list = $this->evaluate($args[0]);
|
||||
$list = $this->evaluate($args[0]) ?? [];
|
||||
|
||||
if (!is_array($list)) {
|
||||
return false;
|
||||
$this->throwError("Argument is non-array.");
|
||||
}
|
||||
|
||||
foreach ($args as $i => $v) {
|
||||
if ($i === 0) continue;
|
||||
if ($i === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$element = $this->evaluate($args[$i]);
|
||||
|
||||
$list[] = $element;
|
||||
}
|
||||
|
||||
|
||||
@@ -199,6 +199,42 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testArrayPush1(): void
|
||||
{
|
||||
$expression = "array\\push(null, 1)";
|
||||
|
||||
$actual = $this->evaluator->process($expression);
|
||||
|
||||
$this->assertEquals([1], $actual);
|
||||
}
|
||||
|
||||
public function testArrayPush2(): void
|
||||
{
|
||||
$expression = "array\\push(list(0), 1)";
|
||||
|
||||
$actual = $this->evaluator->process($expression);
|
||||
|
||||
$this->assertEquals([0, 1], $actual);
|
||||
}
|
||||
|
||||
public function testArrayIncludes1(): void
|
||||
{
|
||||
$expression = "array\\includes(list(1), 1)";
|
||||
|
||||
$actual = $this->evaluator->process($expression);
|
||||
|
||||
$this->assertTrue($actual);
|
||||
}
|
||||
|
||||
public function testArrayIncludes2(): void
|
||||
{
|
||||
$expression = "array\\includes(null, 1)";
|
||||
|
||||
$actual = $this->evaluator->process($expression);
|
||||
|
||||
$this->assertFalse($actual);
|
||||
}
|
||||
|
||||
public function testWhile()
|
||||
{
|
||||
$expression = "
|
||||
|
||||
Reference in New Issue
Block a user