Skip to content

Commit

Permalink
#2440 - Remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Nov 24, 2024
1 parent a849380 commit a11cdd5
Show file tree
Hide file tree
Showing 33 changed files with 108 additions and 478 deletions.
2 changes: 0 additions & 2 deletions src/Statements/BreakStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
class BreakStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws CompilerException
*/
public function compile(CompilationContext $compilationContext): void
Expand Down
15 changes: 7 additions & 8 deletions src/Statements/DeclareStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Zephir\Statements;

use ReflectionException;
use Zephir\CompilationContext;
use Zephir\Exception;
use Zephir\Exception\CompilerException;
use Zephir\Expression\Builder\BuilderFactory;

Expand All @@ -25,28 +27,25 @@
class DeclareStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws CompilerException
* @throws ReflectionException
* @throws Exception
*/
public function compile(CompilationContext $compilationContext): void
{
$statement = $this->statement;

if (!isset($statement['data-type'])) {
if (!isset($this->statement['data-type'])) {
throw new CompilerException('Data type is required', $this->statement);
}

$typeInference = $compilationContext->typeInference;
$symbolTable = $compilationContext->symbolTable;

foreach ($statement['variables'] as $variable) {
foreach ($this->statement['variables'] as $variable) {
$varName = $variable['variable'];
if ($symbolTable->hasVariableInBranch($varName, $compilationContext->branchManager->getCurrentBranch())) {
throw new CompilerException("Variable '" . $varName . "' is already defined", $variable);
}

$currentType = $statement['data-type'];
$currentType = $this->statement['data-type'];

/**
* Replace original data type by the pre-processed infered type
Expand Down
2 changes: 0 additions & 2 deletions src/Statements/DoWhileStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
class DoWhileStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws ReflectionException
* @throws Exception
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Statements/EchoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
class EchoStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws ReflectionException
* @throws Exception
*/
Expand Down
26 changes: 2 additions & 24 deletions src/Statements/ForStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
class ForStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws Exception
* @throws ReflectionException
*/
Expand Down Expand Up @@ -105,9 +103,6 @@ public function compile(CompilationContext $compilationContext): void
* - Evaluated expression must be a zval
* - A key must be a zval
* - A value must be a zval
*
* @param CompilationContext $compilationContext
* @param Variable $exprVariable
*/
public function compileHashTraverse(
CompilationContext $compilationContext,
Expand Down Expand Up @@ -231,11 +226,6 @@ public function compileHashTraverse(
/**
* Compiles a 'for' statement that use an 'iterator' as expression.
*
* @param array $exprRaw
* @param CompilationContext $compilationContext
*
* @return void
*
* @throws Exception
* @throws ReflectionException
*/
Expand Down Expand Up @@ -399,11 +389,6 @@ public function compileIterator(array $exprRaw, CompilationContext $compilationC
/**
* Compiles a for statement that use a 'range' as expression.
*
* @param array $exprRaw
* @param CompilationContext $compilationContext
*
* @return bool
*
* @throws ReflectionException
* @throws Exception
*/
Expand Down Expand Up @@ -908,10 +893,6 @@ public function compileStringTraverse(
}

/**
* @param CompilationContext $compilationContext
* @param Printer|null $codePrinter
*
* @return void
* @throws Exception
* @throws ReflectionException
*/
Expand All @@ -938,11 +919,8 @@ private function compileStatementsForBlock(
}

/**
* @param mixed $keyVariableName
* @param Variable $keyVariable
* @param CompilationContext $compilationContext
*
* @return void
* @throws Exception
* @throws ReflectionException
*/
private function getLetStatement(
mixed $keyVariableName,
Expand Down
2 changes: 0 additions & 2 deletions src/Statements/IfStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
class IfStatement extends StatementAbstract
{
/**
* @param CompilationContext $compilationContext
*
* @throws ReflectionException
* @throws Exception
*/
Expand Down
46 changes: 14 additions & 32 deletions src/Statements/Let/ArrayIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Zephir\Statements\Let;

use ReflectionException;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Exception;
Expand All @@ -26,8 +27,6 @@
use function sprintf;

/**
* ArrayIndex.
*
* Adds/Updates an array index
*/
class ArrayIndex
Expand All @@ -37,13 +36,8 @@ class ArrayIndex
/**
* Compiles foo[y] = {expr}.
*
* @param string $variable
* @param ZephirVariable $symbolVariable
* @param CompiledExpression $resolvedExpr
* @param CompilationContext $compilationContext
* @param array $statement
*
* @throws CompilerException
* @throws Exception
* @throws ReflectionException
*/
public function assign(
$variable,
Expand All @@ -52,14 +46,14 @@ public function assign(
CompilationContext $compilationContext,
$statement
): void {
/*
/**
* Arrays must be stored in the HEAP
*/
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
$this->checkVariableReadOnly($variable, $symbolVariable, $statement);
$this->checkVariableLocalOnly($variable, $symbolVariable, $statement);

/*
/**
* Only dynamic variables can be used as arrays
*/
if ($symbolVariable->isNotVariableAndArray()) {
Expand All @@ -74,7 +68,7 @@ public function assign(
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}

/*
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) {
Expand All @@ -85,15 +79,14 @@ public function assign(
}
}

/*
/**
* Choose one-offset or multiple-offset functions
*/
if (1 == count($statement['index-expr'])) {
if (1 === count($statement['index-expr'])) {
$this->_assignArrayIndexSingle($symbolVariable, $resolvedExpr, $compilationContext, $statement);
} else {
$this->_assignArrayIndexMultiple(
$this->assignArrayIndexMultiple(
$variable,
$symbolVariable,
$resolvedExpr,
$compilationContext,
$statement
Expand All @@ -104,21 +97,14 @@ public function assign(
/**
* Compiles foo[y][x] = {expr} (multiple offset).
*
* @param string $variable
* @param ZephirVariable $symbolVariable
* @param CompiledExpression $resolvedExpr
* @param CompilationContext $compilationContext
* @param array $statement
*
* @throws CompilerException
* @throws Exception
* @throws ReflectionException
*/
protected function _assignArrayIndexMultiple(
protected function assignArrayIndexMultiple(
$variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
$statement,
): void {
$offsetExprs = [];

Expand Down Expand Up @@ -174,12 +160,8 @@ protected function _assignArrayIndexMultiple(
/**
* Compiles foo[y] = {expr} (one offset).
*
* @param ZephirVariable $symbolVariable
* @param CompiledExpression $resolvedExpr
* @param CompilationContext $compilationContext
* @param array $statement
*
* @throws CompilerException
* @throws Exception
* @throws ReflectionException
*/
protected function _assignArrayIndexSingle(
ZephirVariable $symbolVariable,
Expand Down
54 changes: 19 additions & 35 deletions src/Statements/Let/ArrayIndexAppend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,24 @@

namespace Zephir\Statements\Let;

use ReflectionException;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Exception;
use Zephir\Exception\CompilerException;
use Zephir\Expression;
use Zephir\Variable\Variable as ZephirVariable;

/**
* ArrayIndexAppend.
*
* Adds/Updates an array index
*/
class ArrayIndexAppend extends ArrayIndex
{
/**
* Compiles foo[y][] = {expr}.
*
* @param string $variable
* @param ZephirVariable $symbolVariable
* @param CompiledExpression $resolvedExpr
* @param CompilationContext $compilationContext
* @param array $statement
*
* @throws CompilerException
* @throws Exception
* @throws ReflectionException
*/
public function assign(
$variable,
Expand All @@ -44,14 +39,14 @@ public function assign(
CompilationContext $compilationContext,
$statement
): void {
/*
/**
* Arrays must be stored in the HEAP
*/
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
$this->checkVariableReadOnly($variable, $symbolVariable, $statement);
$this->checkVariableLocalOnly($variable, $symbolVariable, $statement);

/*
/**
* Only dynamic variables and arrays can be used as arrays
*/
if ($symbolVariable->isNotVariableAndArray()) {
Expand All @@ -65,7 +60,7 @@ public function assign(
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}

/*
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) {
Expand All @@ -75,46 +70,35 @@ public function assign(
);
}

$this->_assignArrayIndexMultiple($variable, $symbolVariable, $resolvedExpr, $compilationContext, $statement);
$this->assignArrayIndexMultiple($variable, $resolvedExpr, $compilationContext, $statement);
}

/**
* Compiles foo[y][x][] = {expr} (multiple offset).
*
* @param string $variable
* @param ZephirVariable $symbolVariable
* @param CompiledExpression $resolvedExpr
* @param CompilationContext $compilationContext
* @param array $statement
* @throws Exception
* @throws ReflectionException
*/
protected function _assignArrayIndexMultiple(
protected function assignArrayIndexMultiple(
$variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
$offsetExprs = [];
$types = ['int', 'uint', 'long', 'ulong', 'string', 'variable'];
foreach ($statement['index-expr'] as $indexExpr) {
$expression = new Expression($indexExpr);
$expression->setReadOnly(true);
$exprIndex = $expression->compile($compilationContext);

switch ($exprIndex->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'string':
case 'variable':
break;
default:
throw new CompilerException(
'Index: '
. $exprIndex->getType()
. ' cannot be used as array index in assignment without cast',
$indexExpr
);
if (!in_array($exprIndex->getType(), $types)) {
throw new CompilerException(
'Index: '
. $exprIndex->getType()
. ' cannot be used as array index in assignment without cast',
$indexExpr
);
}

$offsetExprs[] = $exprIndex;
Expand Down
Loading

0 comments on commit a11cdd5

Please sign in to comment.