Skip to content

Commit

Permalink
#1094 - Fix nullable array declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Apr 15, 2021
1 parent 603682f commit 0c8523a
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 26 deletions.
16 changes: 9 additions & 7 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public function forStatement(Variable $exprVariable, $keyVariable, $variable, $d
$codePrinter->decreaseLevel();
}

/*
/**
* Compile statements in the 'for' block
*/
if (isset($statement['statements'])) {
Expand Down Expand Up @@ -1176,29 +1176,31 @@ public function fetchClassEntry($str)
* {@inheritdoc}
*
* @param string $type
* @param CompilationContext $context
* @param CompilationContext $compilationContext
* @param bool $isLocal
*
* @return Variable
*/
public function getScalarTempVariable(
string $type,
CompilationContext $context,
CompilationContext $compilationContext,
$isLocal = true
): Variable {
return $context->symbolTable->getTempNonTrackedVariable($type, $context);
return $compilationContext->symbolTable->getTempNonTrackedVariable($type, $compilationContext);
}

/**
* {@inheritdoc}
* Initialize array
*
* Init empty array or specific size array.
*
* @param Variable $variable
* @param CompilationContext $context
* @param int $size
* @param int|null $size
*
* @return void
*/
public function initArray(Variable $variable, CompilationContext $context, int $size = null)
public function initArray(Variable $variable, CompilationContext $context, ?int $size = null): void
{
$code = $this->getVariableCode($variable);

Expand Down
1 change: 0 additions & 1 deletion Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,6 @@ public function assignDefaultValue(array $parameter, CompilationContext $compila
switch ($parameter['default']['type']) {
case 'null':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
$compilationContext->backend->initArray($paramVariable, $compilationContext, null);
break;

case 'empty-array':
Expand Down
12 changes: 6 additions & 6 deletions Library/Operators/Comparison/ComparisonBaseOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,14 @@ public function compile($expression, CompilationContext $compilationContext)
true
);
switch ($right->getType()) {
case 'string':
case 'null':
$rightStr = 'null' == $right->getType() ? '' : $right->getCode();
return new CompiledExpression('bool', $this->zvalNullOperator.'('.$variableLeftCode.')', $expression['left']);
break;

case 'string':
$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableLeftCode.', "'.$rightStr.'")', $expression['left']);
return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableLeftCode.', "'.$right->getCode().'")', $expression['left']);
break;

case 'variable':
Expand Down Expand Up @@ -523,9 +525,7 @@ public function compile($expression, CompilationContext $compilationContext)
case 'array':
switch ($right->getType()) {
case 'null':
$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableCode.', "")', $expression['left']);
return new CompiledExpression('bool', $this->zvalNullOperator.'('.$variableCode.')', $expression['left']);

case 'variable':
$variableRight = $compilationContext->symbolTable->getVariableForRead($right->getCode(), $compilationContext, $expression['left']);
Expand Down
93 changes: 93 additions & 0 deletions ext/stub/arrayaccesstest.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions ext/stub/arrayaccesstest.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions ext/stub/flow/switchflow.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions ext/stub/globals/serverrequestfactory.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ext/stub/mcall.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/Extension/MCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testArrayParamWithDefaultNullValue(): void
$this->assertNumberOfRequiredParameters(0);

$this->assertSame('array', $this->getMethodFirstParameter()->getType()->getName());
$this->assertSame($this->test->testArrayParamWithDefaultNullValue(), []);
$this->assertNull($this->test->testArrayParamWithDefaultNullValue());
$this->assertSame($this->test->testArrayParamWithDefaultNullValue([1]), [1]);
}

Expand Down

0 comments on commit 0c8523a

Please sign in to comment.