Skip to content

Commit

Permalink
#2440 - Simplify conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Nov 24, 2024
1 parent d0fc62e commit a849380
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 41 deletions.
4 changes: 0 additions & 4 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ public function __construct(protected array $expression)
/**
* Resolves an expression.
*
* @param CompilationContext $compilationContext
*
* @return CompiledExpression
*
* @throws CompilerException|Exception
* @throws Exception
* @throws ReflectionException
Expand Down
50 changes: 13 additions & 37 deletions src/Statements/LetStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
use Zephir\Statements\Let\Variable as LetVariable;
use Zephir\Statements\Let\VariableAppend as LetVariableAppend;

use function is_object;

/**
* Let statement is used to assign variables
*/
Expand All @@ -59,9 +57,7 @@ class LetStatement extends StatementAbstract
public function compile(CompilationContext $compilationContext): void
{
$readDetector = new ReadDetector();

$statement = $this->statement;
foreach ($statement['assignments'] as $assignment) {
foreach ($this->statement['assignments'] as $assignment) {
$variable = $assignment['variable'];

/**
Expand Down Expand Up @@ -93,6 +89,7 @@ public function compile(CompilationContext $compilationContext): void
/**
* Incr/Decr assignments don't require an expression
*/
$resolvedExpr = null;
if (isset($assignment['expr'])) {
/**
* Replace on direct-assignment if this bitwise-assignment
Expand All @@ -102,44 +99,23 @@ public function compile(CompilationContext $compilationContext): void

$expr = new Expression($assignment['expr']);

switch ($assignment['assign-type']) {
case 'variable':
if (!$readDetector->detect($variable, $assignment['expr'])) {
if (isset($assignment['operator'])) {
if ('assign' == $assignment['operator']) {
$expr->setExpectReturn(true, $symbolVariable);
}
} else {
$expr->setExpectReturn(true, $symbolVariable);
}
} else {
if (isset($assignment['operator'])) {
if ('assign' == $assignment['operator']) {
$expr->setExpectReturn(true);
}
} else {
$expr->setExpectReturn(true);
}
if ($assignment['assign-type'] === 'variable') {
if (!$readDetector->detect($variable, $assignment['expr'])) {
if (!isset($assignment['operator']) || 'assign' === $assignment['operator']) {
$expr->setExpectReturn(true, $symbolVariable);
}
break;
} else {
if (!isset($assignment['operator']) || 'assign' === $assignment['operator']) {
$expr->setExpectReturn(true);
}
}
}

switch ($assignment['expr']['type']) {
case 'property-access':
case 'array-access':
case 'type-hint':
$expr->setReadOnly(true);
break;
if (in_array($assignment['expr']['type'], ['property-access', 'array-access', 'type-hint'])) {
$expr->setReadOnly(true);
}

$resolvedExpr = $expr->compile($compilationContext);

/**
* Bad implemented operators could return values different from objects
*/
if (!is_object($resolvedExpr)) {
throw new CompilerException('Resolved expression is not valid', $assignment['expr']);
}
}

if ($symbolVariable) {
Expand Down

0 comments on commit a849380

Please sign in to comment.